Skip to content

Commit 6d9b3f2

Browse files
committed
Make private key optional for decoding JWT
1 parent 1c2e3a4 commit 6d9b3f2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

prolog/jwt_io.pl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* =kid=: key id for identifying the key to use
4545
* =type=: type of the key, one of HMAC, RSA or ECDSA.
4646
* =algorithm=: algorithm to use, one of HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384 or ES512.
47-
* =key=: private key to use - string for HMAC, private key file for RSA and private PEM file for ECDSA.
47+
* =key=: private key to use - string for HMAC, private key file for RSA and private PEM file for ECDSA. Optional for decoding, mandatory for encoding.
4848
* =public_key=: public key to use - irrelevant for HMAC, public key file for RSA and public PEM file for ECDSA.
4949
5050
RSA keys can be generated by:
@@ -268,12 +268,21 @@
268268
!.
269269
read_key_file(Key,KeyRead) :-
270270
member(Key.type, ['RSA', 'ECDSA']),
271-
read_file_to_string(Key.key, KeyStr, []),
271+
get_dict('key', Key, PrivateKeyFile),
272+
!,
273+
read_file_to_string(PrivateKeyFile, KeyStr, []),
272274
read_file_to_string(Key.public_key, PublicKeyStr, []),
273275
atom_string(KeyAtom, KeyStr),
274276
atom_string(PublicKeyAtom, PublicKeyStr),
275277
KeyRead = Key.put(_{key: KeyAtom, public_key: PublicKeyAtom}),
276278
!.
279+
read_key_file(Key, KeyRead) :-
280+
member(Key.type, ['RSA', 'ECDSA']),
281+
read_file_to_string(Key.public_key, PublicKeyStr, []),
282+
atom_string(PublicKeyAtom, PublicKeyStr),
283+
KeyRead = Key.put(_{public_key: PublicKeyAtom}),
284+
!.
285+
277286

278287
get_key_file(File, Key) :-
279288
read_file_to_string(File, KeyStr, []),

0 commit comments

Comments
 (0)