/// <summary> /// Decodes the string into the header, payload and signature /// </summary> /// <param name="jwtEncodedString">Base64Url encoded string.</param> internal void Decode(string jwtEncodedString) { IdentityModelEventSource.Logger.WriteInformation(LogMessages.IDX10716, jwtEncodedString); string[] tokenParts = jwtEncodedString.Split(new char[] { '.' }, 4); if (tokenParts.Length != 3) { throw LogHelper.LogException <ArgumentException>(LogMessages.IDX10709, "jwtEncodedString", jwtEncodedString); } try { IdentityModelEventSource.Logger.WriteVerbose(LogMessages.IDX10717, tokenParts[0]); Header = JwtHeader.Base64UrlDeserialize(tokenParts[0]); // if present, "typ" should be set to "JWT" or "http://openid.net/specs/jwt/1.0" string type = Header.Typ; if (type != null) { if (!(StringComparer.Ordinal.Equals(type, JwtConstants.HeaderType) || StringComparer.Ordinal.Equals(type, JwtConstants.HeaderTypeAlt))) { throw LogHelper.LogException <SecurityTokenException>(LogMessages.IDX10702, JwtConstants.HeaderType, JwtConstants.HeaderTypeAlt, type); } } } catch (Exception ex) { throw LogHelper.LogException <ArgumentException>(ex, LogMessages.IDX10703, "header", tokenParts[0], jwtEncodedString); } try { IdentityModelEventSource.Logger.WriteVerbose(LogMessages.IDX10718, tokenParts[1]); Payload = JwtPayload.Base64UrlDeserialize(tokenParts[1]); } catch (Exception ex) { throw LogHelper.LogException <ArgumentException>(ex, LogMessages.IDX10703, "payload", tokenParts[1], jwtEncodedString); } if (!string.IsNullOrEmpty(tokenParts[2])) { try { Base64UrlEncoder.DecodeBytes(tokenParts[2]); } catch (Exception ex) { throw LogHelper.LogException <ArgumentException>(ex, LogMessages.IDX10703, "signature", tokenParts[2], jwtEncodedString); } } RawData = jwtEncodedString; RawHeader = tokenParts[0]; RawPayload = tokenParts[1]; RawSignature = tokenParts[2]; }
/// <summary> /// Decodes the payload and signature from the JWS parts. /// </summary> /// <param name="tokenParts">Parts of the JWS including the header.</param> /// <remarks>Assumes Header has already been set.</remarks> private void DecodeJws(string[] tokenParts) { // Log if CTY is set, assume compact JWS if (Header.Cty != null) { IdentityModelEventSource.Logger.WriteVerbose(string.Format(CultureInfo.InvariantCulture, LogMessages.IDX10738, Header.Cty)); } try { Payload = JwtPayload.Base64UrlDeserialize(tokenParts[1]); } catch (Exception ex) { throw LogHelper.LogExceptionMessage(new ArgumentException(string.Format(CultureInfo.InvariantCulture, LogMessages.IDX10723, tokenParts[1], RawData), ex)); } RawHeader = tokenParts[0]; RawPayload = tokenParts[1]; RawSignature = tokenParts[2]; }
/// <summary> /// Decodes the payload and signature from the JWS parts. /// </summary> /// <param name="tokenParts">Parts of the JWS including the header.</param> /// <remarks>Assumes Header has already been set.</remarks> private void DecodeJws(string[] tokenParts) { // Log if CTY is set, assume compact JWS if (Header.Cty != null) { LogHelper.LogVerbose(LogHelper.FormatInvariant(LogMessages.IDX12738, Header.Cty)); } try { Payload = JwtPayload.Base64UrlDeserialize(tokenParts[1]); } catch (Exception ex) { throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX12723, tokenParts[1], RawData), ex)); } RawHeader = tokenParts[0]; RawPayload = tokenParts[1]; RawSignature = tokenParts[2]; }
/// <summary> /// Decodes the payload and signature from the JWS parts. /// </summary> /// <param name="tokenParts">Parts of the JWS including the header.</param> /// <remarks>Assumes Header has already been set.</remarks> private void DecodeJws(string[] tokenParts) { // We do not support other content types for JWS. if (Header.Cty != null) { throw LogHelper.LogExceptionMessage(new ArgumentException(string.Format(CultureInfo.InvariantCulture, LogMessages.IDX10723, tokenParts[1], RawData))); } try { Payload = JwtPayload.Base64UrlDeserialize(tokenParts[1]); } catch (Exception ex) { throw LogHelper.LogExceptionMessage(new ArgumentException(string.Format(CultureInfo.InvariantCulture, LogMessages.IDX10723, tokenParts[1], RawData), ex)); } RawHeader = tokenParts[0]; RawPayload = tokenParts[1]; RawSignature = tokenParts[2]; }