public static string Encode(string reference1,string reference2,string reference3,string reference4,string reference5, string reference6,string reference7,string reference8,string reference9,string reference10, string reference11,string reference12,string reference13,string reference14,string reference15){ var utc0 = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc); var issueTime = DateTime.Now; var iat = (int)issueTime.Subtract(utc0).TotalSeconds; var exp = (int)issueTime.AddMinutes(60*10).Subtract(utc0).TotalSeconds; // Expiration time is up to 10 hours string titan_AUDIENCE =ConfigurationManager.AppSettings["titan_AUDIENCE"]; string titan_SCOPE =ConfigurationManager.AppSettings["titan_SCOPE"]; titan_token token= new titan_token(); token.r1 = reference1; token.r2 = reference2; token.r3 = reference3; token.r4 = reference4; token.r5 = reference5; token.r6 = reference6; token.r7 = reference7; token.r8 = reference8; token.r9 = reference9; token.r10 = reference10; token.r11 = reference11; token.r12 = reference12; token.r13 = reference13; token.r14 = reference14; token.r15 = reference15; token.expiration = exp; token.issued_at = iat; byte[] privateKey=get_certificate_private_key(); return JsonWebToken.Encode(token, privateKey, JwtHashAlgorithm.RS256); }
public static titan_token Decode(string JWT) { byte[] privateKey=get_certificate_private_key(); if(null==privateKey) { titan_token t=new titan_token(); t.valid=false; } titan_token token= JsonWebToken.Decode(JWT,privateKey ,true); return token; }
public static models.json_results wrapper(string JWT,models.lambda i,Func<titan_token,object>func){ models.json_results res=new models.json_results(); if(JWT!=null) { byte[] privateKey=get_certificate_private_key(); titan_token token= JsonWebToken.Decode(JWT,privateKey ,true); if(token.valid) { res.results=func(token); } else { res.results=new error("Failed to validate token"); res.success=false; } } else { res.results=new error("Failed to validate token"); res.success=false; } res.request_for=i.group+"/"+i.method+"/"+i.owner; return res; }
//exmple //"eyJhbGciOiJSUzI1NiIsInR5cGUiOiJKV1QifQ. //eyJyZWZlcmVuY2UxIjoiNiIsInJlZmVyZW5jZTIiOiI0IiwicmVmZXJlbmNlMyI6bnVsbCwicmVmZXJlbmNlNCI6IjYiLCJzY29wZSI6Imh0dHA6Ly9wZXJjZW50LmNvbXBsZXRlL21hbmdvL2FwaS90aXRhbi8iLCJhdWRpZW5jZSI6Imh0dHA6Ly9wZXJjZW50LmNvbXBsZXRlLyIsImV4cGlyYXRpb24iOjE0OTQ4MDU5ODYsImlzc3VlZF9hdCI6MTQ5NDc2OTk4NiwidmFsaWQiOmZhbHNlfQ //._3aBo6Y2xZ4darI9CR9Eq07jhJrEnj-KjsJfYBiszM4" public static titan_token Decode(string token, byte[] keyBytes, bool verify){ if(String.IsNullOrWhiteSpace(token) || null==keyBytes) { titan_token t=new titan_token(); t.valid=false; return t; } System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); var parts = token.Split('.'); var header = parts[0]; var payload = parts[1]; byte[] crypto = Base64UrlDecode(parts[2]); var headerJSON = Encoding.UTF8.GetString(Base64UrlDecode(header)); var payloadJSON=Encoding.UTF8.GetString(Base64UrlDecode(payload)); jwt_header headerData = jss.Deserialize<jwt_header>(headerJSON); titan_token t_token=jss.Deserialize<titan_token>(payloadJSON); if (verify){ var bytesToSign = Encoding.UTF8.GetBytes(string.Concat(header, ".", payload)); //var keyBytes = Encoding.UTF8.GetBytes(key); var algorithm = headerData.alg; var signature = HashAlgorithms[GetHashAlgorithm(algorithm)](keyBytes, bytesToSign); var decodedCrypto = Convert.ToBase64String(crypto); var decodedSignature = Convert.ToBase64String(signature); var utc0 = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc); var now = DateTime.Now; var time = (int)now.Subtract(utc0).TotalSeconds; if (decodedCrypto != decodedSignature || time<t_token.issued_at || time>t_token.expiration ){ //invalid signatures or expirations... blow up! throw new ApplicationException(string.Format("Invalid signature. Expected {0} got {1}", decodedCrypto, decodedSignature)); } t_token.valid=true; } return t_token; }
public static string Encode(titan_token payload, byte[] keyBytes, JwtHashAlgorithm algorithm){ System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); var segments = new List<string>(); jwt_header header= new jwt_header(); header.alg=algorithm.ToString(); header.type="JWT"; byte[] headerBytes = Encoding.UTF8.GetBytes(jss.Serialize(header)); byte[] payloadBytes = Encoding.UTF8.GetBytes(jss.Serialize(payload)); segments.Add(Base64UrlEncode(headerBytes)); segments.Add(Base64UrlEncode(payloadBytes)); var stringToSign = string.Join(".", segments.ToArray()); var bytesToSign = Encoding.UTF8.GetBytes(stringToSign); byte[] signature = HashAlgorithms[algorithm](keyBytes, bytesToSign); segments.Add(Base64UrlEncode(signature)); return string.Join(".", segments.ToArray()); }
public static string Encode(titan_token payload, string key, JwtHashAlgorithm algorithm){ return Encode(payload, Encoding.UTF8.GetBytes(key), algorithm); }