Class: ApiWarden::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/api_warden/scope.rb

Constant Summary collapse

EXPIRE_TIME_FOR_ACCESS_TOKEN =
7.days.seconds
EXPIRE_TIME_FOR_REFRESH_TOKEN =
14.days.seconds

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Scope

Options

* params_class: [ApiWarden::Authentication::Params]
    the class from which to retrieve authentication related params. Default is 
    ApiWarden::Authentication::HeaderParams.

* load_owner: [Proc]
    the block to be called to load the owner for the scope, so that you can call current_#{scope}
    to access the owner. Id, value for the access token and the authentication will be passed as arguments.

      ApiWarden.ward_by(:users, load_owner: proc { |id, value, auth| User.find(id) })

* disable_refresh_token: [Boolean]
    whether or not to disable using refresh token to refresh access token. Default is false.

* expire_time_for_access_token: [Fixnum]
    the expire time for access token in seconds. Default is EXPIRE_TIME_FOR_ACCESS_TOKEN.

* value_for_access_token: [Proc]
    the block will be called to obtain the value for the access token key. The block will be
    passed with access_token, and other args you specified when calling generate_tokens_for.
    By default the access token will be used as the value.

* on_authenticate_failed: [Proc]
    the block to be called when authentication failed. An authentication will be passed as an argument.

* on_authenticate_success: [Proc]
    the block to be called when authentication succeeds. An authentication will be passed as an argument.

* expire_time_for_refresh_token: [Fixnum]
    the expire time for refresh token in seconds, default is EXPIRE_TIME_FOR_REFRESH_TOKEN.

* value_for_refresh_token: [Proc]
    the block will be called to obtain the value for the refresh token key. The block will be
    passed with refresh_token, and other args you specified when calling generate_tokens_for.
    By default the refresh token will be used as the value.

* on_refresh_failed: [Proc]
    the block to be called when refreshing failed. An authentication will be passed as an argument.


49
50
51
52
53
54
55
56
57
58
# File 'lib/api_warden/scope.rb', line 49

def initialize(name, options = {})
  @name = name

  options[:params_class] ||= ApiWarden::Authentication::HeaderParams
  options[:disable_refresh_token] ||= false
  options[:expire_time_for_access_token] ||= EXPIRE_TIME_FOR_ACCESS_TOKEN
  options[:expire_time_for_refresh_token] ||= EXPIRE_TIME_FOR_REFRESH_TOKEN

  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



85
86
87
88
# File 'lib/api_warden/scope.rb', line 85

def method_missing(method_name, *args)
  key = (method_name[-1] == "?" ? method_name[0..-2] : method_name).to_sym
  options[key]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/api_warden/scope.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/api_warden/scope.rb', line 8

def options
  @options
end

Instance Method Details

#key_for_access_token(id, access_token) ⇒ Object



60
61
62
# File 'lib/api_warden/scope.rb', line 60

def key_for_access_token(id, access_token)
  "#{@name}_#{id}_access_token_#{access_token}"
end

#key_for_refresh_token(id, refresh_token) ⇒ Object



72
73
74
# File 'lib/api_warden/scope.rb', line 72

def key_for_refresh_token(id, refresh_token)
  "#{@name}_#{id}_refresh_token_#{refresh_token}"
end

#value_for_access_token(access_token, *args) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/api_warden/scope.rb', line 64

def value_for_access_token(access_token, *args)
  if options[:value_for_access_token].respond_to?(:call)
    options[:value_for_access_token].call(access_token, *args)
  else
    access_token
  end
end

#value_for_refresh_token(refresh_token, *args) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/api_warden/scope.rb', line 76

def value_for_refresh_token(refresh_token, *args)
  if options[:value_for_refresh_token].respond_to?(:call)
    options[:value_for_refresh_token].call(refresh_token, *args)
  else
    refresh_token
  end
end