Class: Aha::Auth::Client

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • code (String)

    The authorization code

Returns:

  • (Hash)

    Token response with :session_token, :refresh_token, :expires_at



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

Parameters:

  • refresh_token (String)

    The refresh token

Returns:

  • (Hash)

    Token response with :session_token, :refresh_token, :expires_at



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_jwksHash

Fetch JWKS from the server

Returns:

  • (Hash)

    The JWKS response



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

Parameters:

  • session_token (String)

    The session token to revoke

Returns:

  • (Boolean)

    true if successful



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

Parameters:

  • session_token (String)

    The JWT session token

  • refresh_token (String, nil) (defaults to: nil)

    Optional refresh token for automatic refresh

Returns:

  • (Session)

    Session validation result



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