Example #1
0
 /// <summary>
 /// Encodes this instance as Base64UrlEncoded JSON.
 /// </summary>
 /// <returns>Base64UrlEncoded JSON.</returns>
 /// <remarks>use <see cref="JsonExtensions.Serializer"/> to customize JSON serialization.</remarks>
 public virtual string Base64UrlEncode()
 {
     return(Base64UrlEncoder.Encode(SerializeToJson()));
 }
Example #2
0
 /// <summary>
 /// Deserializes Base64UrlEncoded JSON into a <see cref="JwtHeader"/> instance.
 /// </summary>
 /// <param name="base64UrlEncodedJsonString">base64url encoded JSON to deserialize.</param>
 /// <returns>an instance of <see cref="JwtHeader"/>.</returns>
 /// <remarks>use <see cref="JsonExtensions.Deserializer"/> to customize JSON serialization.</remarks>
 public static JwtHeader Base64UrlDeserialize(string base64UrlEncodedJsonString)
 {
     return(JsonExtensions.DeserializeJwtHeader(Base64UrlEncoder.Decode(base64UrlEncodedJsonString)));
 }
Example #3
0
        /// <summary>
        /// Decodes the string into the header, payload and signature
        /// </summary>
        /// <param name="jwtEncodedString">Base64Url encoded string.</param>
        internal void Decode(string jwtEncodedString)
        {
            string[] tokenParts = jwtEncodedString.Split(new char[] { '.' }, 4);
            if (tokenParts.Length != 3)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10709, "jwtEncodedString", jwtEncodedString));
            }

            try
            {
                this.header = JwtHeader.Base64UrlDeserialize(tokenParts[0]);

                // if present, "typ" should be set to "JWT" or "http://openid.net/specs/jwt/1.0"
                string type = this.header.Typ;
                if (type != null)
                {
                    if (!(StringComparer.Ordinal.Equals(type, JwtConstants.HeaderType) || StringComparer.Ordinal.Equals(type, JwtConstants.HeaderTypeAlt)))
                    {
                        throw new SecurityTokenException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10702, JwtConstants.HeaderType, JwtConstants.HeaderTypeAlt, type));
                    }
                }
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "header", tokenParts[0], jwtEncodedString), ex);
            }

            try
            {
                this.payload = JwtPayload.Base64UrlDeserialize(tokenParts[1]);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "payload", tokenParts[1], jwtEncodedString), ex);
            }

            // ensure signature is well-formed, GitIssue 103
            if (!string.IsNullOrEmpty(tokenParts[2]))
            {
                try
                {
                    Base64UrlEncoder.Decode(tokenParts[2]);
                }
                catch (Exception ex)
                {
                    if (DiagnosticUtility.IsFatal(ex))
                    {
                        throw;
                    }

                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "signature", tokenParts[1], jwtEncodedString), ex);
                }
            }

            this.rawData      = jwtEncodedString;
            this.rawHeader    = tokenParts[0];
            this.rawPayload   = tokenParts[1];
            this.rawSignature = tokenParts[2];
        }