Module: ApiWarden::Helpers::Accessable

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

Instance Method Summary collapse

Instance Method Details

#current_authentication_for(scope) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/api_warden/helpers/accessable.rb', line 33

def current_authentication_for(scope)
  scope = validate_scope(scope)

  ivar_authentication = "@current_#{scope.name}_authentication"
  unless authentication = instance_variable_get(ivar_authentication)
    authentication = Authentication.new(scope, request)
    instance_variable_set(ivar_authentication, authentication)
  else
    authentication
  end
end

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/api_warden/helpers/accessable.rb', line 45

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

  access_token = ApiWarden.friendly_token(20)

  ApiWarden.redis do |conn|
    conn.set(scope.key_for_access_token(id, access_token), 
      scope.value_for_access_token(access_token, *args), 
      ex: scope.expire_time_for_access_token
    )
  end

  access_token
end

#ward_by(scope) ⇒ Boolean



7
8
9
# File 'lib/api_warden/helpers/accessable.rb', line 7

def ward_by(scope)
  current_authentication_for(scope).authenticated?
end

#ward_by!(scope) ⇒ Boolean

If not authenticated, an unauthorized response is rendered.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/api_warden/helpers/accessable.rb', line 14

def ward_by!(scope)
  scope = validate_scope(scope)
  
  authentication = current_authentication_for(scope)
  unless authentication.authenticated?
    if (block = scope.on_authenticate_failed) && block.respond_to?(:call)
      instance_exec(authentication, &block)
    else
      render json: { err_msg: 'Unauthorized' }, status: 401
    end
    false
  else
    if (block = scope.on_authenticate_success) && block.respond_to?(:call)
      instance_exec(authentication, &block)
    end
    true
  end
end