* A JSON Web Token (JWT) implementation. * @see RFC 7519 * * This implementation supports the 'none', 'RS256' and 'HS256' algorithms. * Header parameters other than 'alg' and 'typ' aren't parsed. To comply * with RFC 7515, headers with 'crit' parameter are invalid. * Currently, only the 'exp' standard claim is validated. Applications * that
| 55 | * validation logic. |
| 56 | */ |
| 57 | class JWT |
| 58 | { |
| 59 | public: |
| 60 | enum class Alg |
| 61 | { |
| 62 | None, |
| 63 | HS256, |
| 64 | RS256 |
| 65 | }; |
| 66 | |
| 67 | struct Header |
| 68 | { |
| 69 | Alg alg; |
| 70 | Option<std::string> typ; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * Parse an unsecured JWT. |
| 75 | * |
| 76 | * @param token The JWT to parse. |
| 77 | * |
| 78 | * @return The JWT representation if successful otherwise an Error. |
| 79 | */ |
| 80 | static Try<JWT, JWTError> parse(const std::string& token); |
| 81 | |
| 82 | /** |
| 83 | * Parse a JWT and validate its HS256 signature. |
| 84 | * |
| 85 | * @param token The JWT to parse. |
| 86 | * @param secret The secret to validate the signature with. |
| 87 | * |
| 88 | * @return The validated JWT representation if successful otherwise an |
| 89 | * Error. |
| 90 | */ |
| 91 | static Try<JWT, JWTError> parse( |
| 92 | const std::string& token, |
| 93 | const std::string& secret); |
| 94 | |
| 95 | /** |
| 96 | * Parse a JWT and validate its RS256 signature. |
| 97 | * |
| 98 | * @param token The JWT to parse. |
| 99 | * @param publicKey The public key to validate the signature with. |
| 100 | * |
| 101 | * @return The validated JWT representation if successful otherwise an |
| 102 | * Error. |
| 103 | */ |
| 104 | static Try<JWT, JWTError> parse( |
| 105 | const std::string& token, |
| 106 | std::shared_ptr<RSA> publicKey); |
| 107 | |
| 108 | /** |
| 109 | * Create an unsecured JWT. |
| 110 | * |
| 111 | * @param payload The payload of the JWT. |
| 112 | * |
| 113 | * @return The unsecured JWT representation if successful otherwise an |
| 114 | * Error. |