Module: ApiKeys::Authentication

Extended by:
ActiveSupport::Concern
Includes:
Logging
Defined in:
lib/api_keys/authentication.rb

Overview

Controller concern for handling API key authentication. Provides authenticate_api_key! method and helper methods.

Constant Summary collapse

FORBIDDEN_ERROR_CODES =

Failures where the credential is valid but the request context is refused. The key itself is fine, so these answer 403 rather than 401 — the same distinction :missing_scope already makes.

%i[origin_not_allowed ip_not_allowed restriction_misconfigured].freeze

Instance Method Summary collapse

Instance Method Details

#current_api_keyApiKeys::ApiKey?

Returns the currently authenticated API key instance if authentication was successful, otherwise returns nil.

Returns:



30
31
32
# File 'lib/api_keys/authentication.rb', line 30

def current_api_key
  @current_api_key
end

#current_api_ownerObject? Also known as: current_api_key_owner

Returns the owner of the currently authenticated ApiKey, if any.

Returns:

  • (Object, nil)

    The polymorphic owner instance (e.g., User).



36
37
38
# File 'lib/api_keys/authentication.rb', line 36

def current_api_owner
  current_api_key&.owner
end

#current_api_userUser?

Convenience helper: returns the owner if it's a User instance.

Returns:

  • (User, nil)


44
45
46
47
# File 'lib/api_keys/authentication.rb', line 44

def current_api_user
  owner = current_api_owner
  owner if defined?(::User) && owner.is_a?(::User)
end