class KeycloakRack::ReadToken

Read the bearer token from the `Authorization` token.

@api private

Constants

BEARER_TOKEN

The pattern to match bearer tokens with.

Public Instance Methods

call(env) click to toggle source

@param [Hash, []] env @return [Dry::Monads::Success(String)] when a token is found @return [Dry::Monads::Success(nil)] when a token is not found, but unauthenticated requests are allowed @return [Dry::Monads::Failure(:no_token, String)]

# File lib/keycloak_rack/read_token.rb, line 19
def call(env)
  found_token = read_from env

  return Success(found_token) if found_token.present?

  return Success(nil) if config.allow_anonymous?

  Failure[:no_token, "No JWT provided"]
end

Private Instance Methods

read_from(env) click to toggle source

@param [Hash] env the rack environment @option env [String] “HTTP_AUTHORIZATION” the Authorization header @return [String, nil]

# File lib/keycloak_rack/read_token.rb, line 34
def read_from(env)
  match = BEARER_TOKEN.match env["HTTP_AUTHORIZATION"]

  match&.[](:token)
end