Module: Desviar::Auth

Defined in:
lib/auth.rb

Class Method Summary collapse

Class Method Details

.access_granted?(username, password) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/auth.rb', line 46

def self.access_granted?(username, password)
  authenticated? || authenticate(username, password)
end

.authenticate(username, password) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/auth.rb', line 17

def self.authenticate(username, password)
  checked   = [ username, password ] == authentication.credentials
  validated = authentication.provided? && authentication.basic?
  granted   = htpasswd.authenticated? username, password
  if checked and validated and granted
    request.env["desviar.authenticated"] = true
    request.env["REMOTE_USER"] = authentication.username
  else
    nil
  end
end

.authenticate!Object



38
39
40
41
42
43
44
# File 'lib/auth.rb', line 38

def self.authenticate!
  return if authenticated?
  unauthorized! unless authentication.provided?
  bad_request!  unless authentication.basic?
  unauthorized! unless authenticate(*authentication.credentials)
  request.env["REMOTE_USER"] = authentication.username
end

.authenticated?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/auth.rb', line 13

def self.authenticated?
  request.env["REMOTE_USER"] && request.env["desviar.authenticated"]
end

.authenticationObject



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

def self.authentication
  @authentication ||= Rack::Auth::Basic::Request.new request.env
end

.bad_request!Object



34
35
36
# File 'lib/auth.rb', line 34

def self.bad_request!
  throw :halt, [ 400, "Bad Request" ]
end

.htpasswdObject



5
6
7
# File 'lib/auth.rb', line 5

def self.htpasswd
  @htpasswd ||= Htpasswd.new(git.path_to("htpasswd"))
end

.unauthorized!(realm = Desviar::info) ⇒ Object



29
30
31
32
# File 'lib/auth.rb', line 29

def self.unauthorized!(realm = Desviar::info)
  headers "WWW-Authenticate" => %(Basic realm="#{realm}")
  throw :halt, [ 401, "Authorization Required" ]
end