Class: Moov::SDKHooks::ClientCredentialsHook

Inherits:
AbstractSDKHook show all
Extended by:
T::Sig
Defined in:
lib/moov/sdk_hooks/clientcredentials.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractAfterSuccessHook

#after_success

Class Attribute Details

.client_locksObject

Returns the value of attribute client_locks.



92
93
94
# File 'lib/moov/sdk_hooks/clientcredentials.rb', line 92

def client_locks
  @client_locks
end

.global_lockObject (readonly)

Returns the value of attribute global_lock.



91
92
93
# File 'lib/moov/sdk_hooks/clientcredentials.rb', line 91

def global_lock
  @global_lock
end

.sessionsObject

Returns the value of attribute sessions.



92
93
94
# File 'lib/moov/sdk_hooks/clientcredentials.rb', line 92

def sessions
  @sessions
end

Instance Method Details

#after_error(error:, hook_ctx:, response:) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/moov/sdk_hooks/clientcredentials.rb', line 136

def after_error(error:, hook_ctx:, response:)
  return response if hook_disabled?(hook_ctx)

  # We don't want to refresh the token if the error is not related to the token
  return response unless error.nil?

  credentials = get_credentials(hook_ctx)
  return response if credentials.nil?

  if !response.nil? && response.status == 401
    session_key = get_session_key(credentials.client_id, credentials.client_secret)
    scopes = get_required_scopes(credentials, hook_ctx)
    scope_key = get_scope_key(scopes)
    remove_session(session_key, scope_key)
  end

  response
end

#before_request(hook_ctx:, request:) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/moov/sdk_hooks/clientcredentials.rb', line 109

def before_request(hook_ctx:, request:)
  return request if hook_disabled?(hook_ctx)

  credentials = get_credentials(hook_ctx)
  return request if credentials.nil?

  session_key = get_session_key(credentials.client_id, credentials.client_secret)
  scopes = get_required_scopes(credentials, hook_ctx)
  session = get_existing_session(session_key, scopes)

  if session.nil?
    token_url = resolve_token_endpoint(hook_ctx.base_url, credentials.token_url)
    session = do_token_request(token_url, credentials, scopes)
    store_session(session_key, scopes, session)
  end

  request.headers['Authorization'] = "Bearer #{session.token}"
  request
end

#sdk_init(config:) ⇒ Object



96
97
98
99
100
101
# File 'lib/moov/sdk_hooks/clientcredentials.rb', line 96

def sdk_init(config:)
  raise 'Client is required' if config.client.nil?

  @client = config.client
  config
end