Class: Warden::Strategies::Token

Inherits:
Base
  • Object
show all
Defined in:
lib/warden/strategies/token.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, scope = nil) ⇒ Token

Returns a new instance of Token.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/warden/strategies/token.rb', line 8

def initialize(env, scope=nil)
  super

  if request.authorization && request.authorization =~ /^Basic (.*)$/m
    @id, @token = Base64.decode64($1).split(/:/, 2)
  else
    uid = config[:user_id_param] || :user_id
    token = config[:user_token_param] || :token
    @id, @token = params[uid], params[token]
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/warden/strategies/token.rb', line 6

def id
  @id
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/warden/strategies/token.rb', line 6

def token
  @token
end

Instance Method Details

#authenticate!Object



24
25
26
27
28
29
30
31
32
# File 'lib/warden/strategies/token.rb', line 24

def authenticate!
  user = User.where(id: id).first
  token = user.send(config[:token_name])
  if user && secure_compare(token)
    success!(user)
  else
    fail!("Invalid user id or token")
  end
end

#configObject

Returns the configuration data for the default user scope.



35
36
37
# File 'lib/warden/strategies/token.rb', line 35

def config
  env["warden"].config[:scope_defaults][:user][:config]
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/warden/strategies/token.rb', line 20

def valid?
  id && token
end