Class: AgentAdmit::TokensClient
- Inherits:
-
Object
- Object
- AgentAdmit::TokensClient
- Defined in:
- lib/agentadmit/tokens_client.rb
Overview
TokensClient — issue, exchange, and revoke connection tokens via the AgentAdmit hosted service.
Constant Summary collapse
- UNSET =
Sentinel for issue_token’s duration_seconds: leave the field out of the request entirely, so AgentAdmit applies its default (30 days). Pass nil instead for an until-revoked connection (explicit JSON null).
Object.new.freeze
Instance Method Summary collapse
-
#exchange(connection_token, agent_label: nil, agent_id: nil) ⇒ Hash
Exchange a single-use connection token for an access token.
-
#initialize(config = nil) ⇒ TokensClient
constructor
A new instance of TokensClient.
-
#issue_token(user_id:, scopes:, role: nil, duration_seconds: UNSET) ⇒ Hash
Issue a connection token for one of your users.
-
#revoke(connection_id, reason: nil) ⇒ Hash
Revoke a connection (and its access tokens).
Constructor Details
#initialize(config = nil) ⇒ TokensClient
Returns a new instance of TokensClient.
18 19 20 21 |
# File 'lib/agentadmit/tokens_client.rb', line 18 def initialize(config = nil) @config = config || AgentAdmit.configuration || Config.new @config.validate_api_key! end |
Instance Method Details
#exchange(connection_token, agent_label: nil, agent_id: nil) ⇒ Hash
Exchange a single-use connection token for an access token. Calls POST /api/v1/exchange — unauthenticated by design: the connection token itself is the credential, so the operator API key is NOT sent.
62 63 64 65 66 67 68 |
# File 'lib/agentadmit/tokens_client.rb', line 62 def exchange(connection_token, agent_label: nil, agent_id: nil) body = { "token" => connection_token } body["agent_label"] = agent_label if agent_label body["agent_id"] = agent_id if agent_id post("/api/v1/exchange", body, authenticated: false, op: "exchange") end |
#issue_token(user_id:, scopes:, role: nil, duration_seconds: UNSET) ⇒ Hash
Issue a connection token for one of your users. Calls POST /api/v1/apps/app_id/token.
The duration is tri-state:
-
omit the argument — field omitted; AgentAdmit applies its default (30 days)
-
nil — explicit JSON null; the connection lasts until revoked
-
Integer — explicit duration in seconds (60–31536000)
41 42 43 44 45 46 47 48 49 |
# File 'lib/agentadmit/tokens_client.rb', line 41 def issue_token(user_id:, scopes:, role: nil, duration_seconds: UNSET) body = { "user_id" => user_id, "scopes" => scopes } body["role"] = role if role # Tri-state: the UNSET sentinel omits the key entirely; nil survives # JSON.generate as explicit JSON null (no compact, no nil-guard). body["duration_seconds"] = duration_seconds unless duration_seconds.equal?(UNSET) post("/api/v1/apps/#{@config.app_id}/token", body, authenticated: true, op: "issue_token") end |
#revoke(connection_id, reason: nil) ⇒ Hash
Revoke a connection (and its access tokens). Calls POST /api/v1/revoke.
79 80 81 82 83 84 |
# File 'lib/agentadmit/tokens_client.rb', line 79 def revoke(connection_id, reason: nil) body = { "connection_id" => connection_id } body["reason"] = reason if reason post("/api/v1/revoke", body, authenticated: true, op: "revoke") end |