Module: School21::Authenticator

Defined in:
lib/school21/auth/authenticator.rb

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/school21/auth/authenticator.rb', line 9

def self.call
  return unless School21.config.access_token.expired?

  MUTEX.synchronize do
    return unless School21.config.access_token.expired?

    auth_api_response = School21.auth_api.token(
      login: School21.config.credentials[:login],
      password: School21.config.credentials[:password]
    )

    raise AccessTokenError unless auth_api_response.success?

    access_token = AccessToken.new(*auth_api_response.data.values_at(:access_token, :expires_in))
    bearer_auth_credentials = BearerAuthCredentials.new(access_token:)
    auth_header = AuthorizationHeader.new(bearer_auth_credentials)

    School21.config.access_token = access_token
    School21.config.auth_managers[BaseApi::PLATFORM_AUTH_PARTICIPANT] = auth_header
  end
end