Class: PlatformSdk::Identity::AuthClient
- Inherits:
-
Object
- Object
- PlatformSdk::Identity::AuthClient
- Defined in:
- lib/platform_sdk/identity/clients.rb
Overview
Client for getting auth tokens from identity server
Instance Attribute Summary collapse
-
#conn ⇒ Object
Returns the value of attribute conn.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #auth_token ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(base_url, client_id, client_secret) ⇒ AuthClient
constructor
A new instance of AuthClient.
- #refresh_session(session: {}) ⇒ Object
- #refresh_token(jwt:, refresh_token:) ⇒ Object
- #token_expired?(jwt) ⇒ Boolean
Constructor Details
#initialize(base_url, client_id, client_secret) ⇒ AuthClient
Returns a new instance of AuthClient.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/platform_sdk/identity/clients.rb', line 29 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
#conn ⇒ Object
Returns the value of attribute conn.
27 28 29 |
# File 'lib/platform_sdk/identity/clients.rb', line 27 def conn @conn end |
#token ⇒ Object
Returns the value of attribute token.
27 28 29 |
# File 'lib/platform_sdk/identity/clients.rb', line 27 def token @token end |
Instance Method Details
#auth_token ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/platform_sdk/identity/clients.rb', line 40 def auth_token if expired? response = @conn.post("/connect/token", request_body) @token = response.body.transform_keys(&:to_sym) end @token[:access_token] end |
#expired? ⇒ Boolean
49 50 51 52 53 |
# File 'lib/platform_sdk/identity/clients.rb', line 49 def expired? return true if @token.nil? token_expired?(@token[:access_token]) end |
#refresh_session(session: {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/platform_sdk/identity/clients.rb', line 73 def refresh_session(session: {}) raise ArgumentError if session[:id_token].nil? || session[:refresh_token].nil? refreshed_tokens = refresh_token(jwt: session[:id_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(jwt:, refresh_token:) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/platform_sdk/identity/clients.rb', line 64 def refresh_token(jwt:, refresh_token:) raise ArgumentError if refresh_token.nil? || jwt.nil? return unless token_expired?(jwt) response = @conn.post("/connect/token", request_body( grant_type: "refresh_token", refresh_token:)) response.body.transform_keys!(&:to_sym) end |
#token_expired?(jwt) ⇒ Boolean
55 56 57 58 59 60 61 62 |
# File 'lib/platform_sdk/identity/clients.rb', line 55 def token_expired?(jwt) begin expiry_time = Time.at(JWT.decode(jwt, nil, false)[0]["exp"]) rescue JWT::ExpiredSignature return true end expiry_time <= Time.now.utc + 45 end |