Class: PlatformSdk::Identity::AuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/platform_sdk/identity/clients.rb

Overview

Client for getting auth tokens from identity server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, client_id, client_secret) ⇒ AuthClient

Returns a new instance of AuthClient.



35
36
37
38
39
40
41
42
43
44
# File 'lib/platform_sdk/identity/clients.rb', line 35

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.



33
34
35
# File 'lib/platform_sdk/identity/clients.rb', line 33

def conn
  @conn
end

#tokenObject

Returns the value of attribute token.



33
34
35
# File 'lib/platform_sdk/identity/clients.rb', line 33

def token
  @token
end

Instance Method Details

#auth_tokenObject



66
67
68
69
70
# File 'lib/platform_sdk/identity/clients.rb', line 66

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

  @token[:access_token]
end

#expired?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/platform_sdk/identity/clients.rb', line 72

def expired?
  return true if @token.nil?

  token_expired?(@token[:access_token])
end

#jwt_expiry_time(jwt) ⇒ Object



87
88
89
# File 'lib/platform_sdk/identity/clients.rb', line 87

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

#new_refresh_token(refresh_token) ⇒ Object

Raises:

  • (ArgumentError)


110
111
112
113
114
# File 'lib/platform_sdk/identity/clients.rb', line 110

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



56
57
58
59
60
61
62
63
64
# File 'lib/platform_sdk/identity/clients.rb', line 56

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

#raise_error_with_payload(exception_class, error) ⇒ Object

Raises:

  • (exception_class)


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

def raise_error_with_payload(exception_class, error)
  json_log = {
    exception: exception_class.new.class.name.demodulize,
    payload: error.response.dig(:request, :body),
    response_body: error.response[:body],
    status: error.response[:status]
  }.compact
  Rails.logger.info json_log.to_json if defined?(Rails)
  raise exception_class, error.response
end

#refresh_session(session: {}) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/platform_sdk/identity/clients.rb', line 91

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_token_if_expired(jwt:, refresh_token:) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
# File 'lib/platform_sdk/identity/clients.rb', line 103

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

#token_expired?(jwt) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'lib/platform_sdk/identity/clients.rb', line 78

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

#with_rescueObject



46
47
48
49
50
51
52
53
54
# File 'lib/platform_sdk/identity/clients.rb', line 46

def with_rescue
  yield
rescue Faraday::TimeoutError => e
  raise TimeoutError, e
rescue Faraday::ServerError => e
  raise_error_with_payload(ServerError, e)
rescue Faraday::ClientError => e
  raise_error_with_payload(ClientError, e)
end