Class: Aha::Auth::Client
- Inherits:
-
Object
- Object
- Aha::Auth::Client
- Defined in:
- lib/aha/auth/client.rb
Overview
HTTP client for communicating with the BuilderCore auth server
Constant Summary collapse
- ALGORITHM =
"RS256"
Instance Method Summary collapse
-
#authenticate_with_code(code:) ⇒ Hash
Exchange an authorization code for tokens.
-
#authenticate_with_refresh_token(refresh_token:) ⇒ Hash
Refresh tokens using a refresh token.
-
#fetch_jwks ⇒ Hash
Fetch JWKS from the server.
-
#initialize(configuration) ⇒ Client
constructor
A new instance of Client.
-
#logout(session_token:) ⇒ Boolean
Logout and revoke the session.
-
#validate_session(session_token, refresh_token: nil) ⇒ Session
Validate a session token locally using cached JWKS.
Constructor Details
#initialize(configuration) ⇒ Client
Returns a new instance of Client.
11 12 13 14 |
# File 'lib/aha/auth/client.rb', line 11 def initialize(configuration) @configuration = configuration @token_cache = TokenCache.new(ttl: configuration.jwks_cache_ttl) end |
Instance Method Details
#authenticate_with_code(code:) ⇒ Hash
Exchange an authorization code for tokens
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/aha/auth/client.rb', line 20 def authenticate_with_code(code:) response = post( "/api/core/auth/authenticate", { grant_type: "code", code: code } ) parse_token_response(response) end |
#authenticate_with_refresh_token(refresh_token:) ⇒ Hash
Refresh tokens using a refresh token
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/aha/auth/client.rb', line 35 def authenticate_with_refresh_token(refresh_token:) response = post( "/api/core/auth/authenticate", { grant_type: "refresh_token", refresh_token: refresh_token } ) parse_token_response(response) end |
#fetch_jwks ⇒ Hash
Fetch JWKS from the server
94 95 96 |
# File 'lib/aha/auth/client.rb', line 94 def fetch_jwks get("/api/core/auth/jwks/#{@configuration.client_id}") end |
#logout(session_token:) ⇒ Boolean
Logout and revoke the session
84 85 86 87 88 89 |
# File 'lib/aha/auth/client.rb', line 84 def logout(session_token:) get("/api/core/auth_ui/logout", headers: { "Authorization" => "Bearer #{session_token}" }) true rescue ApiError false end |
#validate_session(session_token, refresh_token: nil) ⇒ Session
Validate a session token locally using cached JWKS
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/aha/auth/client.rb', line 51 def validate_session(session_token, refresh_token: nil) begin claims = decode_and_verify_token(session_token) return Session.from_claims(claims) if claims rescue ExpiredTokenError # Token has expired, attempt refresh end if refresh_token begin tokens = authenticate_with_refresh_token(refresh_token: refresh_token) new_claims = decode_and_verify_token(tokens[:session_token]) return Session.from_claims( new_claims || claims, refreshed: true, new_session_token: tokens[:session_token], new_refresh_token: tokens[:refresh_token] ) rescue Error return Session.invalid end end Session.invalid rescue InvalidTokenError, ExpiredTokenError Session.invalid end |