Class: Rack::Request

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

Defined Under Namespace

Classes: AuthHandlers

Instance Method Summary collapse

Instance Method Details

#authObject

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/auth.rb', line 39

def auth
   return @auth if @auth
   return nil if @env["HTTP_AUTHORIZATION"].to_s.empty?

   type, token = @env["HTTP_AUTHORIZATION"].split(" ", 2).map(&:strip)
   type = type.strip.downcase.to_sym
   
   raise TokenValidityError, "Invalid token type." unless AuthHandlers::TOKENS.include? type
   raise TokenValidityError, "Authorization data cannot have a zero length." if token.empty?
   
   args = case type
   when :bearer, :mac then { "token" => token }
   when :basic then Hash[["username","password"].zip(Base64.decode64(token).split(":"))]
   end
   
   @auth ||= AuthHandlers[type].call(*args.values) || args
end

#authenticated!Object

Raises:



61
62
63
64
# File 'lib/rack/auth.rb', line 61

def authenticated!
raise TokenPresenceError, "No valid token provided." if auth.nil?
  auth
end

#authenticated?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rack/auth.rb', line 57

def authenticated?
  !auth.nil?
end