Module: ApiGuard::JwtAuth::RefreshJwtToken

Included in:
Test::ControllerHelper
Defined in:
lib/api_guard/jwt_auth/refresh_jwt_token.rb

Overview

Common module for refresh token functionality

Instance Method Summary collapse

Instance Method Details

#destroy_all_refresh_tokens(resource) ⇒ Object



37
38
39
40
41
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 37

def destroy_all_refresh_tokens(resource)
  return unless refresh_token_enabled?(resource)

  refresh_tokens_for(resource).destroy_all
end

#find_refresh_token_of(resource, refresh_token) ⇒ Object



18
19
20
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 18

def find_refresh_token_of(resource, refresh_token)
  refresh_tokens_for(resource).find_by_token(refresh_token)
end

#new_refresh_token(resource) ⇒ Object

Create a new refresh_token for the current resource



31
32
33
34
35
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 31

def new_refresh_token(resource)
  return unless refresh_token_enabled?(resource)

  refresh_tokens_for(resource).create(token: uniq_refresh_token(resource)).token
end

#refresh_token_association(resource) ⇒ Object



5
6
7
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 5

def refresh_token_association(resource)
  resource.class.refresh_token_association
end

#refresh_token_enabled?(resource) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 9

def refresh_token_enabled?(resource)
  refresh_token_association(resource).present?
end

#refresh_tokens_for(resource) ⇒ Object



13
14
15
16
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 13

def refresh_tokens_for(resource)
  refresh_token_association = refresh_token_association(resource)
  resource.send(refresh_token_association)
end

#uniq_refresh_token(resource) ⇒ Object

Generate and return unique refresh token for the resource



23
24
25
26
27
28
# File 'lib/api_guard/jwt_auth/refresh_jwt_token.rb', line 23

def uniq_refresh_token(resource)
  loop do
    random_token = SecureRandom.urlsafe_base64
    return random_token unless refresh_tokens_for(resource).exists?(token: random_token)
  end
end