Class: Intermodal::Rack::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/intermodal/rack/auth.rb

Constant Summary collapse

UNAUTHORIZED =
[401, {}, []]
IDENTITY =
'HTTP_X_AUTH_IDENTITY'
KEY =
'HTTP_X_AUTH_KEY'

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object

USAGE:

(1) Define ::AccessCredential.authenticate!(identity, key) and returns an account object (2) Define ::AccessToken.generate!(account) and inserts token for authentication

You can inherit from Intermodal::Auth::AccessToken

(3) Add to routes:

match '/auth', :to => Intermodal::Rack::Auth


21
22
23
24
25
# File 'lib/intermodal/rack/auth.rb', line 21

def call(env)
  return UNAUTHORIZED unless valid?(env) && (  = ::AccessCredential.authenticate!(env[IDENTITY], env[KEY]))
  access_token = ::AccessToken.generate!()
  [ 204, { 'X-Auth-Token' => access_token.to_s }, []]
end

.valid?(env) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/intermodal/rack/auth.rb', line 9

def valid?(env)
  env[IDENTITY] && env[KEY]
end