Skip to content

Commit 991e9fc

Browse files
committed
jwt_io: parse header for key id
The key id should not be in the body, but in the header. This is also the default case for auth0.
1 parent e0a3bd0 commit 991e9fc

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

prolog/jwt_io.pl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29-
:- module(jwt_io, [jwt_encode/3, jwt_decode/3]).
29+
:- module(jwt_io, [jwt_encode/3, jwt_decode/3, jwt_decode_head/2]).
3030
/** <module> Json Web Tokens implementation
3131
3232
Generates and verifies Json Web Tokens.
@@ -126,18 +126,22 @@
126126
% @arg Options options
127127
%
128128
jwt_decode(Data, Payload, Options) :-
129-
jwt_decode_from_string(Data, PayloadFirst, _, ''),
130-
atom_json_dict(PayloadFirst, PayloadDict, [as(string)]),
131-
atom_string(Kid, PayloadDict.kid),
129+
jwt_decode_head(Data, PayloadFirst),
130+
atom_json_dict(PayloadFirst, PayloadHeader, [as(string)]),
131+
atom_string(Kid, PayloadHeader.kid),
132132
get_key_from_settings(Kid, KeyDict),
133133
jwt_decode_from_string(Data, Payload, KeyDict),
134+
atom_json_dict(Payload, PayloadDict, [as(string)]),
134135
jwt_jti_valid(PayloadDict),
135136
jwt_exp_valid(PayloadDict),
136137
jwt_nbf_valid(PayloadDict),
137138
jwt_iss_valid(PayloadDict, Options),
138139
jwt_aud_valid(PayloadDict, Options),
139140
jwt_iat_valid(PayloadDict).
140141

142+
jwt_decode_head(Data, Payload) :-
143+
jwt_parse_head(Data, Payload).
144+
141145
get_jti(Jti) :-
142146
setting(jti_generator, Generator),
143147
call(Generator, Jti).

src/jwt_io.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ pl_jwt_encode(term_t in, term_t out, term_t key_term, term_t algorithm)
3838
return rval;
3939
}
4040

41+
static foreign_t
42+
pl_jwt_parse_head(term_t in, term_t head_term) {
43+
char *input, *head_payload;
44+
jwt_t *jwt;
45+
int jwt_result;
46+
get_pl_arg_str("jwt_encode_from_string/4", "in", in, &input);
47+
48+
jwt_result = jwt_decode(&jwt, input, NULL, 0);
49+
if (!jwt_result) {
50+
head_payload = jwt_get_headers_json(jwt, NULL);
51+
(void) PL_unify_atom_chars(head_term, head_payload);
52+
jwt_free(jwt);
53+
PL_succeed;
54+
}
55+
else {
56+
PL_fail;
57+
}
58+
}
59+
4160
static foreign_t
4261
pl_jwt_decode(term_t in, term_t out_payload, term_t out_algorithm, term_t in_key)
4362
{
@@ -69,6 +88,7 @@ pl_jwt_decode(term_t in, term_t out_payload, term_t out_algorithm, term_t in_key
6988
install_t
7089
install(void)
7190
{
91+
PL_register_foreign("jwt_parse_head", 2, pl_jwt_parse_head, 0);
7292
PL_register_foreign("jwt_encode_from_string", 4, pl_jwt_encode, 0);
7393
PL_register_foreign("jwt_decode_from_string", 4, pl_jwt_decode, 0);
7494
}

0 commit comments

Comments
 (0)