1JSON::WebToken(3) User Contributed Perl Documentation JSON::WebToken(3)
2
3
4
6 JSON::WebToken - JSON Web Token (JWT) implementation
7
9 use Test::More;
10 use JSON;
11 use JSON::WebToken;
12
13 my $claims = {
14 iss => 'joe',
15 exp => 1300819380,
16 'http://example.com/is_root' => JSON::true,
17 };
18 my $secret = 'secret';
19
20 my $jwt = encode_jwt $claims, $secret;
21 my $got = decode_jwt $jwt, $secret;
22 is_deeply $got, $claims;
23
24 done_testing;
25
27 JSON::WebToken is JSON Web Token (JWT) implementation for Perl
28
29 THIS MODULE IS ALPHA LEVEL INTERFACE.
30
32 encode($claims [, $secret, $algorithm, $extra_headers ]) : String
33 This method is encoding JWT from hash reference.
34
35 my $jwt = JSON::WebToken->encode({
36 iss => 'joe',
37 exp => 1300819380,
38 'http://example.com/is_root' => JSON::true,
39 }, 'secret');
40 # $jwt = join '.',
41 # 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
42 # 'eyJleHAiOjEzMDA4MTkzODAsImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlLCJpc3MiOiJqb2UifQ'
43 # '4ldFxjibgJGz_uaIRCIq89b5ipR-sbI2Uq7B2WNEDs0'
44
45 Default encryption algorithm is "HS256". You can change algorithm as
46 following:
47
48 my $pricate_key_string = '...';
49 my $public_key_string = '...';
50
51 my $jwt = JSON::WebToken->encode({
52 iss => 'joe',
53 exp => 1300819380,
54 'http://example.com/is_root' => JSON::true,
55 }, $pricate_key_string, 'RS256');
56
57 my $claims = JSON::WebToken->decode($jwt, $public_key_string);
58
59 When you use RS256, RS384 or RS512 algorithm then, We need
60 Crypt::OpenSSL::RSA.
61
62 If you want to create a "Plaintext JWT", should be specify "none" for
63 the algorithm.
64
65 my $jwt = JSON::WebToken->encode({
66 iss => 'joe',
67 exp => 1300819380,
68 'http://example.com/is_root' => JSON::true,
69 }, '', 'none');
70 # $jwt = join '.',
71 # 'eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0',
72 # 'eyJleHAiOjEzMDA4MTkzODAsImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlLCJpc3MiOiJqb2UifQ',
73 # ''
74
75 decode($jwt [, $secret, $verify_signature, $accepted_algorithms ]) : HASH
76 This method is decoding hash reference from JWT string.
77
78 my $claims = JSON::WebToken->decode($jwt, $secret, 1, ["RS256"]);
79
80 Any signing algorithm (except "none") is acceptable by default, so you
81 should check it with $accepted_algorithms parameter.
82
83 add_signing_algorithm($algorithm, $class)
84 This method is adding signing algorithm.
85
86 # resolve JSON::WebToken::Crypt::MYALG
87 JSON::WebToken->add_signing_algorithm('MYALGXXX' => 'MYALG');
88
89 # resolve Some::Class::Algorithm
90 JSON::WebToken->add_signing_algorithm('SOMEALGXXX' => '+Some::Class::Algorithm');
91
92 SEE ALSO JSON::WebToken::Crypt::HMAC or JSON::WebToken::Crypt::RAS.
93
95 encode_jwt($claims [, $secret, $algorithm, $extra_headers ]) : String
96 Same as "encode()" method.
97
98 decode_jwt($jwt [, $secret, $verify_signature, $accepted_algorithms ]) :
99 Hash
100 Same as "decode()" method.
101
103 JSON::WebToken::Exception will be thrown with following code.
104
105 ERROR_JWT_INVALID_PARAMETER
106 When some method arguments are not valid.
107
108 ERROR_JWT_MISSING_SECRET
109 When secret is required. ("alg != "none"")
110
111 ERROR_JWT_INVALID_SEGMENT_COUNT
112 When JWT segment count is not between 2 and 4.
113
114 ERROR_JWT_INVALID_SEGMENT_ENCODING
115 When each JWT segment is not encoded by base64url.
116
117 ERROR_JWT_UNWANTED_SIGNATURE
118 When "alg == "none"" but signature segment found.
119
120 ERROR_JWT_INVALID_SIGNATURE
121 When JWT signature is invalid.
122
123 ERROR_JWT_NOT_SUPPORTED_SIGNING_ALGORITHM
124 When given signing algorithm is not supported.
125
126 ERROR_JWT_UNACCEPTABLE_ALGORITHM
127 When given signing algorithm is not included in acceptable_algorithms.
128
130 xaicron <xaicron@cpan.org>
131
132 zentooo
133
135 Copyright 2012 - xaicron
136
138 This library is free software; you can redistribute it and/or modify it
139 under the same terms as Perl itself.
140
142 <http://tools.ietf.org/html/draft-ietf-oauth-json-web-token>
143
144
145
146perl v5.32.0 2020-07-28 JSON::WebToken(3)