Class: Actn::Api::Mw::Auth

Inherits:
Object
  • Object
show all
Includes:
Goliath::Rack::BarrierAroundware, Goliath::Validation
Defined in:
lib/actn/api/mw/auth.rb

Defined Under Namespace

Classes: InvalidCredentialsError, MissingApikeyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, opts = {}) ⇒ Auth

Returns a new instance of Auth.



22
23
24
25
# File 'lib/actn/api/mw/auth.rb', line 22

def initialize(env, opts = {})
  self.opts = opts
  super(env)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



20
21
22
# File 'lib/actn/api/mw/auth.rb', line 20

def client
  @client
end

#optsObject

Returns the value of attribute opts.



20
21
22
# File 'lib/actn/api/mw/auth.rb', line 20

def opts
  @opts
end

Instance Method Details

#apikeyObject



81
82
83
# File 'lib/actn/api/mw/auth.rb', line 81

def apikey
  env['HTTP_X_APIKEY']
end

#authorize_client!Object



73
74
75
76
77
78
79
# File 'lib/actn/api/mw/auth.rb', line 73

def authorize_client!
  return true if with_session? && current_user_uuid          
  unless client_valid? && client_authorized?
    raise InvalidCredentialsError.new("Invalid Credentials")
  end
  env['rack.session'][:user_uuid] = self.client.uuid
end

#client_authorized?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
# File 'lib/actn/api/mw/auth.rb', line 93

def client_authorized?
  return unless self.client
  (
  self.secret.nil? ? 
  self.client.auth_by_session(env['rack.session'].id) : 
  self.client.auth_by_secret(self.secret)
  ) && self.client.can?("#{env['REQUEST_METHOD']}:#{env['REQUEST_PATH']}")
end

#client_valid?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/actn/api/mw/auth.rb', line 89

def client_valid?
  self.client = Client.find_for_auth(host, apikey)
end

#current_user_uuidObject



114
115
116
# File 'lib/actn/api/mw/auth.rb', line 114

def current_user_uuid
  env['rack.session'][:user_uuid]
end

#excluded?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/actn/api/mw/auth.rb', line 106

def excluded?
  opts[:exclude].nil? ? false : (env['REQUEST_PATH'] =~ opts[:exclude])
end

#hostObject



102
103
104
# File 'lib/actn/api/mw/auth.rb', line 102

def host
  (env['HTTP_ORIGIN'] || env['HTTP_HOST']).to_domain
end

#lazy_authorization?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/actn/api/mw/auth.rb', line 59

def lazy_authorization?
  (env['REQUEST_METHOD'] == 'GET') || (env['REQUEST_METHOD'] == 'HEAD')
end

#post_processObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/actn/api/mw/auth.rb', line 45

def post_process
  
  unless excluded?
      
    # We have to check auth now, we skipped it before
    if lazy_authorization?
      validate_client!
    end

  end

  [status, headers, body]
end

#pre_processObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/actn/api/mw/auth.rb', line 28

def pre_process
  
  unless excluded?
      
    validate_apikey! 

    # On non-GET non-HEAD requests, we have to check auth now.
    unless lazy_authorization?
      perform     # yield execution until user_info has arrived
      authorize_client!
    end
  
  end
  
  return Goliath::Connection::AsyncResponse
end

#secretObject



85
86
87
# File 'lib/actn/api/mw/auth.rb', line 85

def secret
  env['HTTP_X_SECRET']
end

#validate_apikey!Object

Raises:



63
64
65
66
# File 'lib/actn/api/mw/auth.rb', line 63

def validate_apikey!
  return true if with_session? && current_user_uuid
  raise MissingApikeyError.new("Missing Api Key") if apikey.to_s.empty?
end

#validate_client!Object

Raises:

  • (Goliath::Validation::UnauthorizedError)


68
69
70
71
# File 'lib/actn/api/mw/auth.rb', line 68

def validate_client!          
  return true if with_session? && current_user_uuid          
  raise Goliath::Validation::UnauthorizedError unless client_valid?
end

#with_session?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/actn/api/mw/auth.rb', line 110

def with_session?
  opts[:with_session]
end