module Shaf::Authentication

Public Instance Methods

authenticate(realm: Settings.default_authentication_realm) click to toggle source
# File lib/shaf/helpers/authentication.rb, line 34
def authenticate(realm: Settings.default_authentication_realm)
  if defined?(@current_realm) && @current_realm&.to_s != realm&.to_s
    raise RealmChangedError.new(from: @current_realm , to: realm)
  else
    @current_realm = realm
  end

  current_user.tap do |user|
    www_authenticate(realm: realm) unless user
  end
end
authenticate!(realm: Settings.default_authentication_realm) click to toggle source
# File lib/shaf/helpers/authentication.rb, line 46
def authenticate!(realm: Settings.default_authentication_realm)
  user = authenticate(realm: realm)
  return user if user

  msg = +"Unauthorized action"
  msg << " (Realm: #{realm})" if realm
  raise Shaf::Errors::UnauthorizedError, msg
end
Also aliased as: current_user!
authenticated?() click to toggle source
# File lib/shaf/helpers/authentication.rb, line 56
def authenticated?
  !current_user.nil?
end
current_user() click to toggle source
# File lib/shaf/helpers/authentication.rb, line 60
    def current_user
      unless defined? @current_realm
        if Settings.key? :default_authentication_realm
          @current_realm = Settings.default_authentication_realm
        else
          Shaf.logger.info <<~MSG
            No realm has been provided!
            Authentication/authorization cannot be performed. Did you perhaps
            forget to configure a realm in
            `Settings.default_authentication_realm` or provide it when calling
            `#authenticate!` (or `#authenticate!`)
          MSG
          return
        end
      end

      @current_user ||= Authenticator.user(request.env, realm: @current_realm)
    end
current_user!(realm: Settings.default_authentication_realm)
Alias for: authenticate!
www_authenticate(realm: Settings.default_authentication_realm) click to toggle source
# File lib/shaf/helpers/authentication.rb, line 27
def www_authenticate(realm: Settings.default_authentication_realm)
  challenges = Authenticator.challenges_for(realm: realm)
  raise NoChallengesError.new(realm) if challenges.empty?

  headers 'WWW-Authenticate' => challenges.map(&:to_s)
end