JsonWebToken constructor

JsonWebToken(
  1. String originalToken
)

Creates a new JsonWebToken.

The originalToken-String should contain header, payload and the signature base64 encoded. Decodes the given input into the decodedPayload.

Throws FormatException if there are not 3 parts (header, payload, signature) or the payload could not be decoded. Could be thrown if there are other parsing errors too.

Implementation

JsonWebToken(this.originalToken) {
  final List<String> tokenParts = originalToken.split('.');
  if (tokenParts.length != _jwtNumberOfParts) {
    throw const FormatException('No valid JWT - missing parts');
  }
  final Map<String, dynamic> decodedHeader = _decodeBase64(tokenParts[0]);
  if (!decodedHeader.containsKey(_headerKeyTokenType)) {
    throw const FormatException('No token type in header');
  }
  if (decodedHeader[_headerKeyTokenType] != _headerValueJwtTokenType) {
    throw const FormatException('Wrong token type in header');
  }
  // TODO(poq): check header for "alg"
  // TODO(poq): validate signature/token -> throw InvalidTokenSignature
  decodedPayload =
      Map<String, dynamic>.unmodifiable(_decodeBase64(tokenParts[1]));
}