Class: Warden::Strategies::Token

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

Constant Summary collapse

VERSION =
"0.1.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
# 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
    @id, @token = params[:user_id], 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



22
23
24
25
26
27
28
29
# File 'lib/warden/strategies/token.rb', line 22

def authenticate!
  user = User.where(id: id).first
  if user && Token.secure_compare(user.auth_token)
    success!(user)
  else
    fail!("Invalid user id or token")
  end
end

#valid?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/warden/strategies/token.rb', line 18

def valid?
  id && token
end