Class: RestCore::AuthBasic

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/auth_basic.rb

Constant Summary

Constant Summary

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

contain_binary?, #error_callback, escape, #fail, #give_promise, #id, included, #log, merge_hash, percent_encode, request_uri, #run, string_keys

Methods included from RestCore

eagerload, id

Class Method Details

.membersObject



5
# File 'lib/rest-core/middleware/auth_basic.rb', line 5

def self.members; [:username, :password]; end

Instance Method Details

#auth_basic_header(env) ⇒ Object



25
26
27
28
# File 'lib/rest-core/middleware/auth_basic.rb', line 25

def auth_basic_header env
  {'Authorization' =>
     "Basic #{["#{username(env)}:#{password(env)}"].pack('m0')}"}
end

#call(env, &k) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rest-core/middleware/auth_basic.rb', line 8

def call env, &k
  if username(env)
    if password(env)
      app.call(env.merge(REQUEST_HEADERS =>
        auth_basic_header(env).merge(env[REQUEST_HEADERS])), &k)
    else
      app.call(log(env, "AuthBasic: username provided: #{username(env)}," \
                        " but password is missing."), &k)
    end
  elsif password(env)
    app.call(log(env, "AuthBasic: password provided: #{password(env)}," \
                      " but username is missing."), &k)
  else
    app.call(env, &k)
  end
end