|
23 | 23 | #ifndef SRC_INCLUDE_JWT_JWT_H_
|
24 | 24 | #define SRC_INCLUDE_JWT_JWT_H_
|
25 | 25 |
|
26 |
| -#include "jwt/claimvalidator.h" |
27 |
| -#include "jwt/json.hpp" |
28 |
| -#include "jwt/messagevalidator.h" |
29 |
| -#include <memory> |
30 | 26 | #include <stddef.h>
|
| 27 | +#include <memory> |
31 | 28 | #include <string>
|
32 | 29 | #include <utility>
|
| 30 | +#include <tuple> |
| 31 | +#include "jwt/claimvalidator.h" |
| 32 | +#include "jwt/json.hpp" |
| 33 | +#include "jwt/messagevalidator.h" |
33 | 34 |
|
34 | 35 | // Stack allocated signature.
|
35 | 36 | #define MAX_SIGNATURE_LENGTH 256
|
|
48 | 49 | * more details.
|
49 | 50 | */
|
50 | 51 | class JWT {
|
51 |
| - using json = nlohmann::json; |
52 |
| - |
53 |
| -public: |
54 |
| - ~JWT(); |
55 |
| - |
56 |
| - /** |
57 |
| - * Parses an encoded web token and validates it. |
58 |
| - * |
59 |
| - * @param jwsToken String containing a valid webtoken |
60 |
| - * @param verifier Optional verifier used to validate the signature. If this |
61 |
| - * parameter is null the signature will not be verified. |
62 |
| - * @param validator Optional validator to validate the claims in this token. |
63 |
| - * The payload will not be validated if this parameter is null |
64 |
| - * @throw TokenFormatError in case the token cannot be parsed |
65 |
| - * @throw InvalidSignatureError in case the token is not signed |
66 |
| - * @throw InvalidClaimError in case the payload cannot be validated |
67 |
| - */ |
68 |
| - static JWT *Decode(std::string jwsToken, MessageValidator *verifier = nullptr, |
69 |
| - ClaimValidator *validator = nullptr); |
| 52 | + using json = nlohmann::json; |
70 | 53 |
|
71 |
| - /** |
72 |
| - * Decodes and validates a JSON Web Token. |
73 |
| - * |
74 |
| - * @param jws_token String containing a valid webtoken |
75 |
| - * @param num_jws_token The number of bytes in the jws_token string |
76 |
| - * @param verifier Optional verifier used to validate the JOSE header. No |
77 |
| - * verification will be done if this parameter is null . |
78 |
| - * @param validator Optional validator to validate the claims in this token. |
79 |
| - * The payload will not be validated if this parameter is null |
80 |
| - * @throw TokenFormatError in case the token cannot be parsed |
81 |
| - * @throw InvalidSignatureError in case the token is not signed |
82 |
| - * @throw InvalidClaimError in case the payload cannot be validated |
83 |
| - */ |
84 |
| - static JWT *Decode(const char *jws_token, size_t num_jws_token, |
85 |
| - MessageValidator *verifier = nullptr, |
86 |
| - ClaimValidator *validator = nullptr); |
| 54 | + public: |
87 | 55 |
|
88 |
| - /** |
89 |
| - * Encodes the given json payload and optional header with the given signer. |
90 |
| - * |
91 |
| - * @param signer The MessageSigner used to sign the resulting token. |
92 |
| - * @param payload The payload for this token. |
93 |
| - * @param header The optional header. Note the "jwt" and "alg" fields will be |
94 |
| - * set |
95 |
| - * @return a char[] with a signed token. To be cleared up with calling |
96 |
| - * delete[] |
97 |
| - */ |
98 |
| - static std::string Encode(MessageSigner *signer, json payload, |
99 |
| - json header = nullptr); |
| 56 | + /** |
| 57 | + * Parses an encoded web token and validates it. |
| 58 | + * |
| 59 | + * @param jwsToken String containing a valid webtoken |
| 60 | + * @param verifier Optional verifier used to validate the signature. If this |
| 61 | + * parameter is null the signature will not be verified. |
| 62 | + * @param validator Optional validator to validate the claims in this token. |
| 63 | + * The payload will not be validated if this parameter is null |
| 64 | + * @throw TokenFormatError in case the token cannot be parsed |
| 65 | + * @throw InvalidSignatureError in case the token is not signed |
| 66 | + * @throw InvalidClaimError in case the payload cannot be validated |
| 67 | + */ |
| 68 | + static std::tuple<json, json> Decode(std::string jwsToken, |
| 69 | + MessageValidator *verifier = nullptr, |
| 70 | + ClaimValidator *validator = nullptr); |
100 | 71 |
|
101 |
| - /** |
102 |
| - * The contents of the JOSE Header describe the cryptographic operations |
103 |
| - * applied to the JWT Claims Set. Callers do not own the reference returned |
104 |
| - * and should not free it. |
105 |
| - */ |
106 |
| - inline const json header() { return header_; } |
| 72 | + /** |
| 73 | + * Decodes and validates a JSON Web Token. |
| 74 | + * |
| 75 | + * @param jws_token String containing a valid webtoken |
| 76 | + * @param num_jws_token The number of bytes in the jws_token string |
| 77 | + * @param verifier Optional verifier used to validate the JOSE header. No |
| 78 | + * verification will be done if this parameter is null . |
| 79 | + * @param validator Optional validator to validate the claims in this token. |
| 80 | + * The payload will not be validated if this parameter is null |
| 81 | + * @return A tuple containing the json header and the payload. |
| 82 | + * @throw TokenFormatError in case the token cannot be parsed |
| 83 | + * @throw InvalidSignatureError in case the token is not signed |
| 84 | + * @throw InvalidClaimError in case the payload cannot be validated |
| 85 | + */ |
| 86 | + static std::tuple<json, json> Decode(const char *jws_token, |
| 87 | + size_t num_jws_token, |
| 88 | + MessageValidator *verifier = nullptr, |
| 89 | + ClaimValidator *validator = nullptr); |
107 | 90 |
|
108 |
| - /** |
109 |
| - * A JSON object that contains the claims conveyed by the JWT. Callers do not |
110 |
| - * own the reference returned and should not free it. |
111 |
| - */ |
112 |
| - inline const json payload() { return payload_; } |
| 91 | + /** |
| 92 | + * Encodes the given json payload and optional header with the given signer. |
| 93 | + * |
| 94 | + * @param signer The MessageSigner used to sign the resulting token. |
| 95 | + * @param payload The payload for this token. |
| 96 | + * @param header The optional header. Note the "jwt" and "alg" fields will |
| 97 | + * be set |
| 98 | + * @return a char[] with a signed token. To be cleared up with calling |
| 99 | + * delete[] |
| 100 | + */ |
| 101 | + static std::string Encode(MessageSigner *signer, json payload, |
| 102 | + json header = nullptr); |
113 | 103 |
|
114 |
| -private: |
115 |
| - JWT(json header, json payload); |
116 |
| - |
117 |
| - static json ExtractPayload(const char *payload, size_t num_payload); |
118 |
| - static bool VerifySignature(json header_claims_, const char *header, |
119 |
| - size_t num_header_and_payload, |
120 |
| - const char *signature, size_t num_signature, |
121 |
| - MessageValidator *verifier); |
122 |
| - |
123 |
| - json header_; |
124 |
| - json payload_; |
| 104 | + private: |
| 105 | + static json ExtractPayload(const char *payload, size_t num_payload); |
| 106 | + static bool VerifySignature(json header_claims_, const char *header, |
| 107 | + size_t num_header_and_payload, |
| 108 | + const char *signature, size_t num_signature, |
| 109 | + MessageValidator *verifier); |
125 | 110 | };
|
126 |
| - |
127 |
| -/** Auto pointer that will release the token when it goes out of scope */ |
128 |
| -typedef std::unique_ptr<JWT> jwt_ptr; |
129 |
| -#endif // SRC_INCLUDE_JWT_JWT_H_ |
| 111 | +#endif // SRC_INCLUDE_JWT_JWT_H_ |
0 commit comments