Class: Goliath::Rack::AuthBarrier

Inherits:
Object
  • Object
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_tokenObject

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

#dbObject (readonly)

Memcache Client



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

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 102

def 
  return true if access_token[:mode] == Arms::Auth::ADMIN
  [access_token[:hosts]].flatten.join(",") =~ /#{env['HTTP_ORIGIN'] || env['SERVER_NAME']}/
end

#account_valid?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 88

def 
  # puts "VALID? #{Digest::MD5.hexdigest(apikey) == access_token[:token]},#{account_belongs_to_host?},#{Arms::Auth.can?(access_token[:mode],env['REQUEST_METHOD'])}"
  # is token or key altered?
  Digest::MD5.hexdigest(apikey) == access_token[:token] && 
  # is on right host?
   &&
  # mode is able to do HTTP VERB?
  Arms::Auth.can?(access_token[:mode],env['REQUEST_METHOD'])
end

#apikeyObject



80
81
82
# File 'lib/grass/goliath/rack/auth_barrier.rb', line 80

def apikey
  env.params['apikey']
end

#apikey_pathObject



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 && 
    raise InvalidApikeyError.new("Invalid Api Key")
  else
    renew_token
  end
end

#get_access_tokenObject



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
  # puts "GET KEY #{apikey_path.inspect} -> #{@access_token.inspect}"
  @access_token
end

#lazy_authorization?Boolean

Returns:

  • (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_processObject



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')
  # [:access_token, :status, :headers, :body].each{|attr| env.logger.info(("%23s\t%s" % [attr, self.send(attr).inspect[0..200]])) }

  # inject_headers

  # We have to check auth now, we skipped it before
  if lazy_authorization?
    check_authorization!
  end

  env.trace('post_process_end')
  [status, headers, body]
end

#pre_processObject



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!

  # the results of the afirst deferrable will be set right into access_token (and the request into successes)
  get_access_token

  # On non-GET non-HEAD requests, we have to check auth now.
  unless lazy_authorization?
    perform     # yield execution until user_info has arrived
    check_authorization!
  end

  env.trace('pre_process_end')
  return Goliath::Connection::AsyncResponse
end

#renew_tokenObject



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

Raises:



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