module Hanami::Authentication::Token

Private Class Methods

included(base) click to toggle source
# File lib/hanami/authentication/token.rb, line 38
def self.included(base)
  base.class_eval do
    _expose :current_user
    extend  ClassMethods
  end
end

Private Instance Methods

authenticate(_params) click to toggle source
# File lib/hanami/authentication/token.rb, line 18
def authenticate(_params)
  bearer_token = token_from_header
  halt 401 unless bearer_token
  token = self.class.find_token_block.call(bearer_token)
  halt 401 unless token
  @current_user = self.class.find_user_block.call(token)
  halt 401 unless @current_user
end
authenticated?() click to toggle source
# File lib/hanami/authentication/token.rb, line 27
def authenticated?
  !!@current_user
end
create_token() click to toggle source
# File lib/hanami/authentication/token.rb, line 10
def create_token
  SecureRandom.uuid
end
current_user() click to toggle source
# File lib/hanami/authentication/token.rb, line 14
def current_user
  @current_user
end
token_from_header() click to toggle source
# File lib/hanami/authentication/token.rb, line 31
def token_from_header
  header = request.get_header('HTTP_AUTHORIZATION')
  return unless header
  matched = header.match(/Bearer (.+)$/)
  matched && matched[1]
end