Class: Bouncer::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/bouncer-client/token.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Token

Returns a new instance of Token.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/bouncer-client/token.rb', line 4

def initialize token
  @token = token.split(' ')[1]
  return get_from_bouncer unless redis.exists "TOKEN:#{@token}"

  data = redis.get("TOKEN:#{@token}")
  begin
    @payload = JSON.parse data
  rescue JSON::ParserError
    get_from_bouncer
  end
end

Class Method Details

.present?(request) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/bouncer-client/token.rb', line 44

def self.present? request
  return false unless auth_header = request.headers['authorization']
  split = auth_header.split(' ')
  split[0] == 'Bearer' && split.length > 1 && !split[1].empty?
end

Instance Method Details

#activationObject



30
31
32
33
# File 'lib/bouncer-client/token.rb', line 30

def activation
  return nil unless activation?
  @activation ||= build_model
end

#activation?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/bouncer-client/token.rb', line 26

def activation?
  @payload['activations'] && !@payload['activations'].empty?
end

#userObject



39
40
41
42
# File 'lib/bouncer-client/token.rb', line 39

def user
  return nil unless user?
  @user ||= build_model
end

#user?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/bouncer-client/token.rb', line 35

def user?
  @payload['users'] && !@payload['users'].empty?
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/bouncer-client/token.rb', line 21

def valid?
  return false unless @payload
  @payload["users"] != nil || @payload["activations"] != nil
end

#validate!Object



16
17
18
19
# File 'lib/bouncer-client/token.rb', line 16

def validate!
  puts "DEPRECATED: valid? should be used instead of validate!"
  valid?
end