Module: Roy::BasicAuth::InstanceMethods

Defined in:
lib/roy/basic_auth.rb

Instance Method Summary collapse

Instance Method Details

#authorized?(data = nil) ⇒ Boolean

Runs the authentication logic against the user and passord given in the request, using custom additional data.

Parameters:

  • data (defaults to: nil)

    user data to pass to the authentication logic

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/roy/basic_auth.rb', line 66

def authorized?(data=nil)
  auth = Rack::Auth::Basic::Request.new(request.env)

  auth.provided? && auth.basic? && auth.credentials &&
    (conf.auth[:logic] || ->(data, u, p) {
      %w(admin password) == [u, p]
     }).(data, *auth.credentials)
end

#protected!(data = nil) ⇒ Object

Protect all subsequent code using HTTP Basic Authentication.

Parameters:

  • data (defaults to: nil)

    user data to pass to #authorized?



54
55
56
57
58
59
60
# File 'lib/roy/basic_auth.rb', line 54

def protected!(data=nil)
  unless authorized?(data)
    realm = conf.auth && conf.auth[:realm] || 'Realm'
    response['WWW-Authenticate'] = %(Basic realm="#{realm}")
    halt 401
  end
end