-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathjson_web_token.rb
More file actions
32 lines (30 loc) · 1011 Bytes
/
json_web_token.rb
File metadata and controls
32 lines (30 loc) · 1011 Bytes
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
# frozen_string_literal: true
module Api
module V1
module Auth
module Jwt
# Class to handle encryption/decryption of the JWT
class JsonWebToken
class << self
def encode(payload:, exp: 24.hours.from_now)
payload[:exp] = exp.to_i
JWT.encode(payload, Rails.application.credentials.secret_key_base)
rescue JWT::EncodeError => e
Rails.logger.error "Api::V1::Auth::Jwt::JsonWebToken.encode - #{e.message}"
nil
end
def decode(token:)
body = JWT.decode(token, Rails.application.credentials.secret_key_base)[0]
ActiveSupport::HashWithIndifferentAccess.new body
rescue JWT::ExpiredSignature => e
raise e
rescue JWT::DecodeError => e
Rails.logger.error "Api::V1::Auth::Jwt::JsonWebToken.decode - #{e.message}"
nil
end
end
end
end
end
end
end