-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathjwt.rb
48 lines (43 loc) · 1.52 KB
/
jwt.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require 'jwt/version'
require 'jwt/base64'
require 'jwt/json'
require 'jwt/decode'
require 'jwt/configuration'
require 'jwt/encode'
require 'jwt/error'
require 'jwt/jwk'
require 'jwt/claims'
require 'jwt/encoded_token'
require 'jwt/token'
# JSON Web Token implementation
#
# Should be up to date with the latest spec:
# https://tools.ietf.org/html/rfc7519
module JWT
extend ::JWT::Configuration
module_function
# Encodes a payload into a JWT.
#
# @param payload [Hash] the payload to encode.
# @param key [String] the key used to sign the JWT.
# @param algorithm [String] the algorithm used to sign the JWT.
# @param header_fields [Hash] additional headers to include in the JWT.
# @return [String] the encoded JWT.
def encode(payload, key, algorithm = 'HS256', header_fields = {})
Encode.new(payload: payload,
key: key,
algorithm: algorithm,
headers: header_fields).segments
end
# Decodes a JWT to extract the payload and header
#
# @param jwt [String] the JWT to decode.
# @param key [String] the key used to verify the JWT.
# @param verify [Boolean] whether to verify the JWT signature.
# @param options [Hash] additional options for decoding.
# @return [Array<Hash>] the decoded payload and headers.
def decode(jwt, key = nil, verify = true, options = {}, &keyfinder) # rubocop:disable Style/OptionalBooleanParameter
Decode.new(jwt, key, verify, configuration.decode.to_h.merge(options), &keyfinder).decode_segments
end
end