Class: GraphQL::Auth::JwtManager

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-auth/jwt_manager.rb

Constant Summary collapse

ALGORITHM =
'HS256'
TYPE =
'Bearer'

Class Method Summary collapse

Class Method Details

.decode(token) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/graphql-auth/jwt_manager.rb', line 33

def decode(token)
  token = extract_token token
  decrypted_token = JWT.decode token,
                               auth_secret,
                               true,
                               { algorithm: ALGORITHM }
  decrypted_token.first
end

.issue(payload) ⇒ Object Also known as: issue_without_expiration



21
22
23
24
25
26
# File 'lib/graphql-auth/jwt_manager.rb', line 21

def issue(payload)
  token = JWT.encode payload,
                     auth_secret,
                     ALGORITHM
  set_type token
end

.issue_with_expiration(payload, custom_expiration = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/graphql-auth/jwt_manager.rb', line 11

def issue_with_expiration(payload, custom_expiration = nil)
  if custom_expiration.present? && custom_expiration.is_a?(ActiveSupport::Duration)
    payload[:exp] = custom_expiration
  else
    payload.merge!(expiration)
  end

  issue payload
end

.token_expiration(token) ⇒ Object



28
29
30
31
# File 'lib/graphql-auth/jwt_manager.rb', line 28

def token_expiration(token)
  decrypted_token = decode(token)
  decrypted_token.try(:[], 'exp')
end