Class: Goliath::Rack::AuthBarrier
- Inherits:
-
Object
- Object
- Goliath::Rack::AuthBarrier
show all
- Includes:
- BarrierAroundware, Validation
- Defined in:
- lib/grass/goliath/rack/auth_barrier.rb
Defined Under Namespace
Classes: InvalidApikeyError, MissingApikeyError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(env, db_name) ⇒ AuthBarrier
Returns a new instance of AuthBarrier.
14
15
16
17
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 14
def initialize(env, db_name)
@db = env.config[db_name]
super(env)
end
|
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
9
10
11
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 9
def access_token
@access_token
end
|
#db ⇒ Object
8
9
10
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 8
def db
@db
end
|
Instance Method Details
#accept_response(handle, *args) ⇒ Object
61
62
63
64
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 61
def accept_response(handle, *args)
env.trace("received_#{handle}")
super(handle, *args)
end
|
#account_belongs_to_host? ⇒ Boolean
102
103
104
105
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 102
def account_belongs_to_host?
return true if access_token[:mode] == Arms::Auth::ADMIN
[access_token[:hosts]].flatten.join(",") =~ /#{env['HTTP_ORIGIN'] || env['SERVER_NAME']}/
end
|
#account_valid? ⇒ Boolean
88
89
90
91
92
93
94
95
96
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 88
def account_valid?
Digest::MD5.hexdigest(apikey) == access_token[:token] &&
account_belongs_to_host? &&
Arms::Auth.can?(access_token[:mode],env['REQUEST_METHOD'])
end
|
#apikey ⇒ Object
80
81
82
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 80
def apikey
env.params['apikey']
end
|
#apikey_path ⇒ Object
84
85
86
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 84
def apikey_path
Arms::Auth.keypath(apikey)
end
|
#check_authorization! ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 72
def check_authorization!
unless access_token && account_valid?
raise InvalidApikeyError.new("Invalid Api Key")
else
renew_token
end
end
|
#get_access_token ⇒ Object
55
56
57
58
59
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 55
def get_access_token
@access_token = db.get(apikey_path) rescue nil
@access_token
end
|
#lazy_authorization? ⇒ Boolean
51
52
53
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 51
def lazy_authorization?
(env['REQUEST_METHOD'] == 'GET') || (env['REQUEST_METHOD'] == 'HEAD')
end
|
#post_process ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 36
def post_process
env.trace('post_process_beg')
if lazy_authorization?
check_authorization!
end
env.trace('post_process_end')
[status, , body]
end
|
#pre_process ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 19
def pre_process
env.trace('pre_process_beg')
validate_apikey!
get_access_token
unless lazy_authorization?
perform check_authorization!
end
env.trace('pre_process_end')
return Goliath::Connection::AsyncResponse
end
|
#renew_token ⇒ Object
98
99
100
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 98
def renew_token
db.touch apikey_path, Arms::Auth::TTLS[access_token[:mode]] unless access_token[:ttl].nil?
end
|
#validate_apikey! ⇒ Object
68
69
70
|
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 68
def validate_apikey!
raise MissingApikeyError.new("Missing Api Key") if apikey.to_s.empty?
end
|