Class: TentD::API::AuthenticationLookup

Inherits:
Middleware
  • Object
show all
Defined in:
lib/tentd/api/authentication_lookup.rb

Instance Method Summary collapse

Methods inherited from Middleware

#call, #initialize

Methods included from Authorizable

#authorize_env!, #authorize_env?

Constructor Details

This class inherits a constructor from TentD::API::Middleware

Instance Method Details

#action(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tentd/api/authentication_lookup.rb', line 4

def action(env)
  return env unless env['HTTP_AUTHORIZATION']
  env['hmac'] = Hash[env['HTTP_AUTHORIZATION'].scan(/([a-z]+)="([^"]+)"/i)]
  mac_key_id = env['hmac']['id']
  env.potential_auth = case mac_key_id.to_s[0,1]
  when 's'
    TentD::Model::Follower.first(:mac_key_id => mac_key_id)
  when 'a'
    TentD::Model::App.first(:mac_key_id => mac_key_id)
  when 'u'
    TentD::Model::AppAuthorization.first(:mac_key_id => mac_key_id)
  end
  env.potential_auth = Model::Following.first(:mac_key_id => mac_key_id) unless env.potential_auth
  if env.potential_auth
    env.hmac.secret = env.potential_auth.mac_key
    env.hmac.algorithm = env.potential_auth.mac_algorithm
  else
    env.hmac = nil
  end
  env
end