# typed: ignore # frozen_string_literal: true module Setsuzoku # The API Authentication Interface definition. # Any AuthStrategy that implements this interface must implement all abstract methods defined by AuthStrategy. # # Defines all necessary methods for handling authentication for any authentication strategy. module AuthStrategy extend Forwardable extend T::Sig extend T::Helpers include HasConfigContext abstract! attr_accessor :service attr_accessor :credential def_delegators :@service, :plugin, :api_strategy, :external_api_handler # # initialize sig(:final) do params( service: T.any( Setsuzoku::Service::WebService::Service, T.untyped ), args: T.untyped ).returns(T.any( Setsuzoku::Service::WebService::AuthStrategies::BasicAuthStrategy, Setsuzoku::Service::WebService::AuthStrategies::CustomAuthStrategy, Setsuzoku::Service::WebService::AuthStrategies::OAuthStrategy, T.untyped )) end # Initialize the auth_strategy and provide reference to service. # # @param service [Service] the new instance of service with its correct strategies. # # @return [AuthStrategy] the new instance of auth_strategy def initialize(service:, **args) self.service = service self.set_credential!(self.class.credential_class.stub_credential) unless self.plugin.registered_instance self.config_context = args self end # The getter method for a credential should retrieve dynamically if it's a proc. # We cannot assign this at initialize time, as it may not yet exist on the instance. # # @return [Credential] the credential to use for the current requests. def credential self.plugin.get_registered_instance_val(@credential) end # # set_credential! sig { params(credential: T.untyped).void } # # Assign the credential to the auth_strategy. # # @return [void] def set_credential!(credential) if credential self.credential = credential credential.auth_strategy = self end end # Check if a credential is valid. # Additionally it should revalidate if invalid. # # @return [Boolean] true if the credential is valid. sig { returns(T::Boolean) } def auth_credential_valid?; end # Authorize a credential for a specific auth_strategy. # It should also set the credential attributes and save if appropriate. # # @return [void] sig { abstract.params(args: T.untyped).void } def new_credential!(**args); end end end