Class: ApiWarden::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/api_warden/authentication.rb,
lib/api_warden/authentication/params.rb,
lib/api_warden/authentication/header_params.rb

Defined Under Namespace

Classes: AuthenticationError, HeaderParams, Params

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, request) ⇒ Authentication

Returns a new instance of Authentication.



10
11
12
13
14
# File 'lib/api_warden/authentication.rb', line 10

def initialize(scope, request)
  @scope = scope
  @request = request
  @params = scope.params_class.new(self)
end

Instance Attribute Details

#key_for_access_tokenObject (readonly)

Returns the value of attribute key_for_access_token.



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

def key_for_access_token
  @key_for_access_token
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#authenticateObject

Returns self.

Returns:

  • self



42
43
44
45
46
# File 'lib/api_warden/authentication.rb', line 42

def authenticate
  authenticate!
rescue AuthenticationError => e
  self
end

#authenticate!Object

This method will only authenticate once, and cache the result.

Returns:

  • self



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/api_warden/authentication.rb', line 51

def authenticate!
  return unless @authenticated.nil?

  id, access_token = @params.retrieve_id, @params.retrieve_access_token
  @key_for_access_token = @scope.key_for_access_token(id, access_token)

  if access_token && !access_token.empty?
    ApiWarden.redis { |conn| @value_for_access_token = conn.get(@key_for_access_token) }
  end

  unless @value_for_access_token
    @authenticated = false
    raise AuthenticationError
  end

  @authenticated = true
  @id = id
  @access_token = access_token
  self
end

#authenticated?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/api_warden/authentication.rb', line 16

def authenticated?
  ensure_authenticated
  @authenticated
end

#idObject



26
27
28
29
# File 'lib/api_warden/authentication.rb', line 26

def id
  ensure_authenticated_or_refreshable
  @id
end

#refreshable?Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/api_warden/authentication.rb', line 21

def refreshable?
  ensure_refreshable
  @refreshable
end

#sign_outObject

TODO remove refresh token as well



101
102
103
104
105
# File 'lib/api_warden/authentication.rb', line 101

def sign_out
  key = @scope.key_for_access_token(@id, @access_token)

  ApiWarden.redis { |conn| conn.del(key) }
end

#ttl_for_access_tokenFixnum

Returns the time to live for access token in seconds.

Returns:

  • (Fixnum)

    the time to live for access token in seconds



108
109
110
111
112
# File 'lib/api_warden/authentication.rb', line 108

def ttl_for_access_token
  raise_if_authentication_failed!

  ttl_for_key(@key_for_access_token)
end

#ttl_for_access_token=(seconds) ⇒ Object

Set the ttl for access token.



115
116
117
118
119
120
121
# File 'lib/api_warden/authentication.rb', line 115

def ttl_for_access_token=(seconds)
  raise_if_authentication_failed!

  key = @key_for_access_token
  value = @value_for_access_token
  ApiWarden.redis { |conn| conn.set(key, value, ex: seconds) }
end

#validate_refresh_tokenObject



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

def validate_refresh_token
  validate_refresh_token!
rescue AuthenticationError => e
end

#validate_refresh_token!Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/api_warden/authentication.rb', line 77

def validate_refresh_token!
  return unless @refreshable.nil?

  id, refresh_token = @params.retrieve_id, @params.retrieve_refresh_token
  key = @scope.key_for_refresh_token(id, refresh_token)

  if refresh_token && !refresh_token.empty?
    ApiWarden.redis do |conn|
      @value_for_refresh_token = conn.get(key)
      conn.del(key)
    end
  end

  unless @value_for_refresh_token
    @refreshable = false
    raise AuthenticationError
  end

  @refreshable = true
  @id = id
  self
end

#value_for_access_tokenObject



31
32
33
34
# File 'lib/api_warden/authentication.rb', line 31

def value_for_access_token
  ensure_authenticated
  @value_for_access_token
end

#value_for_refresh_tokenObject



36
37
38
39
# File 'lib/api_warden/authentication.rb', line 36

def value_for_refresh_token
  ensure_refreshable
  @value_for_refresh_token
end