Module: ApiWarden::Helpers::Refreshable

Defined in:
lib/api_warden/helpers/refreshable.rb

Instance Method Summary collapse

Instance Method Details

#generate_refresh_token_for(scope, id, *args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/api_warden/helpers/refreshable.rb', line 6

def generate_refresh_token_for(scope, id, *args)
  scope = validate_scope(scope)

  refresh_token = ApiWarden.friendly_token(30)

  ApiWarden.redis do |conn|
    conn.set(scope.key_for_refresh_token(id, refresh_token), 
      scope.value_for_refresh_token(refresh_token, *args), 
      ex: scope.expire_time_for_refresh_token
    )
  end

  refresh_token
end

#validate_refresh_token_for!(scope) ⇒ Boolean

If not refreshable, a forbidden response is rendered.

Returns:

  • (Boolean)

    whether or not refreshable



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_warden/helpers/refreshable.rb', line 24

def validate_refresh_token_for!(scope)
  scope = validate_scope(scope)

  authentication = current_authentication_for(scope)
  unless authentication.refreshable?
    if (block = scope.on_refresh_failed) && block.respond_to?(:call)
      instance_exec(authentication, &block)
    else
      render json: { err_msg: 'Forbidden' }, status: 403
    end
    false
  else
    true
  end
end