Module: Capcode::Helpers::Authorization

Included in:
Capcode::Helpers
Defined in:
lib/capcode/helpers/auth.rb

Overview

Because this helper was trully inspired by this post : www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/ and because the code in this post was extracted out of Wink, this file follow the Wink license :

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instance Method Summary collapse

Instance Method Details

#A1(auth, password) ⇒ Object

:nodoc:



52
53
54
# File 'lib/capcode/helpers/auth.rb', line 52

def A1(auth, password) #:nodoc:
  [ auth.username, auth.realm, password ] * ':'
end

#A2(auth) ⇒ Object

:nodoc:



56
57
58
# File 'lib/capcode/helpers/auth.rb', line 56

def A2(auth) #:nodoc:
  [ auth.method, auth.uri ] * ':'
end

#authObject

:nodoc:



26
27
28
29
30
31
32
# File 'lib/capcode/helpers/auth.rb', line 26

def auth #:nodoc:
  if @auth_type == :basic
    @auth ||= Rack::Auth::Basic::Request.new(env)
  else
    @auth ||= Rack::Auth::Digest::Request.new(env)
  end
end

#authorized?Boolean

:nodoc:

Returns:

  • (Boolean)


88
89
90
# File 'lib/capcode/helpers/auth.rb', line 88

def authorized? #:nodoc:
  request.env['REMOTE_USER']
end

#bad_request!Object

:nodoc:



84
85
86
# File 'lib/capcode/helpers/auth.rb', line 84

def bad_request! #:nodoc:
  throw :halt, [ 400, {}, 'Bad Request' ]
end

#basic_authorize(username, password) ⇒ Object

:nodoc:



75
76
77
78
79
80
81
82
# File 'lib/capcode/helpers/auth.rb', line 75

def basic_authorize(username, password) #:nodoc:
  h = @authorizations.call( )
  
  user = username
  pass = h[user]||false
  
  (pass == password)
end

#basic_unauthorized!(realm) ⇒ Object

:nodoc:



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

def basic_unauthorized!(realm) #:nodoc:
  response['WWW-Authenticate'] = %(Basic realm="#{realm}")
  throw :halt, [ 401, {}, 'Authorization Required' ]
end

#digest(auth, password) ⇒ Object

:nodoc:



60
61
62
63
64
# File 'lib/capcode/helpers/auth.rb', line 60

def digest(auth, password) #:nodoc:
  password_hash = H(A1(auth, password))

  KD(password_hash, [ auth.nonce, auth.nc, auth.cnonce, "auth", H(A2(auth)) ] * ':')
end

#digest_authorizeObject

:nodoc:



66
67
68
69
70
71
72
73
# File 'lib/capcode/helpers/auth.rb', line 66

def digest_authorize( ) #:nodoc:
  h = @authorizations.call( )
  
  user = auth.username
  pass = h[user]||false

  (pass && (digest(auth, pass) == auth.response))
end

#digest_unauthorized!(realm, opaque) ⇒ Object

:nodoc:



39
40
41
42
# File 'lib/capcode/helpers/auth.rb', line 39

def digest_unauthorized!(realm, opaque) #:nodoc:
  response['WWW-Authenticate'] = %(Digest realm="#{realm}", qop="auth", nonce="#{Rack::Auth::Digest::Nonce.new.to_s}", opaque="#{H(opaque)}") 
  throw :halt, [ 401, {}, 'Authorization Required' ]
end

#H(data) ⇒ Object

:nodoc:



44
45
46
# File 'lib/capcode/helpers/auth.rb', line 44

def H(data) #:nodoc:
  ::Digest::MD5.hexdigest(data)
end

#http_authentication(opts = {}, &b) ⇒ Object

Allow you to add and HTTP Authentication (Basic or Digest) to a controller

Options :

  • :type : Authentication type (:basic or :digest) - default : :basic

  • :realm : realm ;) - default : “Capcode.app”

  • :opaque : Your secret passphrase. You MUST set it if you use Digest Auth - default : “opaque”

The block must return a Hash of username => password like that :

{
  "user1" => "pass1",
  "user2" => "pass2",
  # ...
}


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/capcode/helpers/auth.rb', line 105

def http_authentication( opts = {}, &b )
  @auth = nil
  
  @auth_type = opts[:type]||:basic
  realm = opts[:realm]||"Capcode.app"
  opaque = opts[:opaque]||"opaque"
  @authorizations = b
  
  return if authorized?
  
  if @auth_type == :basic
    basic_unauthorized!(realm) unless auth.provided?
    bad_request! unless auth.basic?
    basic_unauthorized!(realm) unless basic_authorize(*auth.credentials)
  else
    digest_unauthorized!(realm, opaque) unless auth.provided?
    bad_request! unless auth.digest?
    digest_unauthorized!(realm, opaque) unless digest_authorize
  end        
  
  request.env['REMOTE_USER'] = auth.username
end

#KD(secret, data) ⇒ Object

:nodoc:



48
49
50
# File 'lib/capcode/helpers/auth.rb', line 48

def KD(secret, data) #:nodoc:
  H([secret, data] * ':')
end