Class: PlatformSdk::Identity::AuthClient

Inherits:
Object
  • Object
show all
Includes:
ErrorHandleable
Defined in:
lib/platform_sdk/identity/clients.rb,
sig/platform_sdk/identity/auth_client.rbs

Overview

Client for getting auth tokens from identity server

Constant Summary

Constants included from ErrorHandleable

ErrorHandleable::FORM_SECRET_PATTERN, ErrorHandleable::JSON_SECRET_PATTERN, ErrorHandleable::SENSITIVE_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorHandleable

#error_log_payload, #raise_error_with_payload, #scrub_headers, #scrub_request, #scrub_response, #scrub_secrets, #with_rescue

Constructor Details

#initialize(base_url, client_id, client_secret) ⇒ AuthClient

Returns a new instance of AuthClient.



117
118
119
120
121
122
123
124
125
126
# File 'lib/platform_sdk/identity/clients.rb', line 117

def initialize(base_url, client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
  @conn = Faraday.new(base_url) do |conn|
    conn.request :url_encoded
    conn.response :raise_error
    conn.response :json
    conn.adapter :net_http
  end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.

Returns:

  • (Object)


115
116
117
# File 'lib/platform_sdk/identity/clients.rb', line 115

def conn
  @conn
end

#tokenString

Returns the value of attribute token.

Returns:

  • (String)


115
116
117
# File 'lib/platform_sdk/identity/clients.rb', line 115

def token
  @token
end

Instance Method Details

#auth_tokenString

Returns:

  • (String)


138
139
140
141
142
# File 'lib/platform_sdk/identity/clients.rb', line 138

def auth_token
  @token = post_payload('/connect/token', request_body) if expired?

  @token[:access_token]
end

#expired?Boolean

Returns:

  • (Boolean)


144
145
146
147
148
# File 'lib/platform_sdk/identity/clients.rb', line 144

def expired?
  return true if @token.nil?

  token_expired?(@token[:access_token])
end

#jwt_expiry_time(jwt) ⇒ Time

Returns:

  • (Time)


159
160
161
# File 'lib/platform_sdk/identity/clients.rb', line 159

def jwt_expiry_time(jwt)
  Time.at(JWT.decode(jwt, nil, false)[0]['exp'])
end

#new_refresh_token(refresh_token) ⇒ Object

Raises:

  • (ArgumentError)


182
183
184
185
186
# File 'lib/platform_sdk/identity/clients.rb', line 182

def new_refresh_token(refresh_token)
  raise ArgumentError if refresh_token.nil?

  post_payload('/connect/token', request_body(grant_type: 'refresh_token', refresh_token:))
end

#post_payload(path, body) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/platform_sdk/identity/clients.rb', line 128

def post_payload(path, body)
  with_rescue do
    response = @conn.post(path, body)
    response_body = response.body
    return if response_body == ''

    response_body.transform_keys!(&:to_sym)
  end
end

#refresh_session(session: {}) ⇒ nil

Returns:

  • (nil)

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
172
173
# File 'lib/platform_sdk/identity/clients.rb', line 163

def refresh_session(session: {})
  raise ArgumentError if session[:access_token].nil? || session[:refresh_token].nil?

  refreshed_tokens = refresh_token_if_expired(jwt: session[:access_token], refresh_token: session[:refresh_token])

  return if refreshed_tokens.nil?

  session[:id_token] = refreshed_tokens[:id_token]
  session[:access_token] = refreshed_tokens[:access_token]
  session[:refresh_token] = refreshed_tokens[:refresh_token]
end

#refresh_tokenHash[Symbol, String]

Returns:

  • (Hash[Symbol, String])


20
# File 'sig/platform_sdk/identity/auth_client.rbs', line 20

def refresh_token: -> Hash[Symbol, String]

#refresh_token_if_expired(jwt:, refresh_token:) ⇒ Object

Raises:

  • (ArgumentError)


175
176
177
178
179
180
# File 'lib/platform_sdk/identity/clients.rb', line 175

def refresh_token_if_expired(jwt:, refresh_token:)
  raise ArgumentError if refresh_token.nil? || jwt.nil?
  return unless token_expired?(jwt)

  new_refresh_token(refresh_token)
end

#request_body(grant_type: 'client_credentials', refresh_token: nil) ⇒ Hash[Symbol, String]

Returns:

  • (Hash[Symbol, String])


190
191
192
193
194
# File 'lib/platform_sdk/identity/clients.rb', line 190

def request_body(grant_type: 'client_credentials', refresh_token: nil)
  return { grant_type:, client_id: @client_id, client_secret: @client_secret } if refresh_token.nil?

  { grant_type:, client_id: @client_id, client_secret: @client_secret, refresh_token: }
end

#token_expired?(jwt) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
# File 'lib/platform_sdk/identity/clients.rb', line 150

def token_expired?(jwt)
  begin
    expiry_time = jwt_expiry_time(jwt)
  rescue JWT::ExpiredSignature
    return true
  end
  expiry_time <= Time.now.utc + 45
end