# typed: false # frozen_string_literal: true require 'active_support/core_ext/numeric/time' module Setsuzoku module Service module WebService module Credentials module OAuthCredential extend T::Sig extend T::Helpers include Setsuzoku::Credential include UsesTokenCredential abstract! # # auth_actions sig { override.returns(T::Hash[T.untyped, T.untyped]) } # # All auth actions that are implemented. # # @return [Hash] all auth endpoint definitions for the API. def auth_actions { new_token: { 'POST' => 'token', request_type: :json, response_type: :json }, refresh_token: { 'POST' => 'token', request_type: :json, response_type: :json } } end # The application's client_id to o_auth with. # # @return [String] the client_id for o_auth. sig { abstract.returns(T.nilable(String)) } def client_id; end # The application's client_secret to o_auth with. # # @return [String] the client_secret for o_auth. sig { abstract.returns(T.nilable(String)) } def client_secret; end # The application's redirect url to handle an o_auth redirect # # @return [String] the redirect url. sig { abstract.returns(T.nilable(String)) } def redirect_url; end # The token, refresh_token, and expires_at to assign for an OAuth token request. # # @param resp [Hash] the response hash from the external api call for a token. # # @return [Hash] the attributes to assign to the credential. sig{ overridable.params(resp: Setsuzoku::ApiResponse).returns(T::Hash[Symbol, T.untyped]) } def token_attributes_to_assign(resp) attrs = { token: resp.data[:access_token], refresh_token: resp.data[:refresh_token] } expires_in = resp.data[:expires_in] if expires_in && (expires_in.is_a?(Integer) || expires_in.match(/^\d+$/)) attrs[:expires_on] = expires_in.to_i.seconds.from_now end attrs end # Stub an o_auth_credential-like instance. # # @return [Struct] a stubbed o_auth_credential-like struct. sig { returns(Struct) } def self.stub_credential s = Struct.new(:auth_strategy, :status, :settings, :client_id, :client_secret, :redirect_url, :token, :refresh_token, :expires_on) s.new(nil, 'active', {'extension': 'test'}, 'stubbed_client_id', 'stubbed_client_secret', 'stubbed_redirect_url', 'stubbed_token', 'stubbed_refresh_token', (Time.now + 30.days)) end end end end end end