public EncodedParameters Encode() { var result = new EncodedParameters(AccessToken); result.TimeStamp = TimeStamp.ToEpochTime(); if (Method != null) { Logger.Debug("Encoding method"); result.Method = Method.Method; } if (Host != null) { Logger.Debug("Encoding host"); result.Host = Host; } if (Path != null) { Logger.Debug("Encoding path"); result.Path = Path; } if (QueryParameters != null && QueryParameters.Any()) { Logger.Debug("Encoding query params"); var query = new EncodingQueryParameters(QueryParameters); result.QueryParameters = query.Encode(); } if (RequestHeaders != null && RequestHeaders.Any()) { Logger.Debug("Encoding request headers"); var headers = new EncodingHeaderList(RequestHeaders); result.RequestHeaders = headers.Encode(); } if (Body != null) { Logger.Debug("Encoding body"); result.BodyHash = CalculateBodyHash(); } return(result); }
public EncodedParameters Verify(string token) { if (token == null) { throw new ArgumentNullException("token"); } try { var headers = JWT.Headers(token); if (headers == null || !headers.ContainsKey(HttpSigningConstants.Jwk.AlgorithmProperty)) { Logger.Error("Token does not contain " + HttpSigningConstants.Jwk.AlgorithmProperty + " property in header"); return(null); } var alg = headers[HttpSigningConstants.Jwk.AlgorithmProperty]; if (!Alg.Equals(alg)) { Logger.Error("Signature alg does not match token alg"); return(null); } var json = JWT.Decode(token, _key); if (json == null) { Logger.Error("Failed to decode token"); return(null); } return(EncodedParameters.FromJson(json)); } catch (Exception ex) { Logger.ErrorException("Failed to decode token", ex); } return(null); }
public bool IsSame(EncodedParameters other) { if (other == null) { return(false); } if (AccessToken != other.AccessToken) { Logger.Debug("AccessToken mismatch"); return(false); } if (Method != other.Method) { Logger.Debug("Method mismatch"); return(false); } if (Host != other.Host) { Logger.Debug("Host mismatch"); return(false); } if (Path != other.Path) { Logger.Debug("Path mismatch"); return(false); } if (BodyHash != other.BodyHash) { Logger.Debug("BodyHash mismatch"); return(false); } if (QueryParameters == null && other.QueryParameters != null) { Logger.Debug("One QueryParameters is null, the other is not"); return(false); } if (QueryParameters != null && other.QueryParameters == null) { Logger.Debug("One QueryParameters is null, the other is not"); return(false); } if (QueryParameters != null && !QueryParameters.IsSame(other.QueryParameters)) { Logger.Debug("QueryParameters mismatch"); return(false); } if (RequestHeaders == null && other.RequestHeaders != null) { Logger.Debug("One RequestHeaders is null, the other is not"); return(false); } if (RequestHeaders != null && other.RequestHeaders == null) { Logger.Debug("One RequestHeaders is null, the other is not"); return(false); } if (RequestHeaders != null && !RequestHeaders.IsSame(other.RequestHeaders)) { Logger.Debug("RequestHeaders mismatch"); return(false); } return(true); }