MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / mint_token

Function mint_token

atomic-remote/src/auth/token.rs:123–199  ·  view source on GitHub ↗
(server: &str, identity: &Identity)

Source from the content-addressed store, hash-verified

121// ---------------------------------------------------------------------------
122
123fn mint_token(server: &str, identity: &Identity) -> AuthResult<String> {
124 // The token is keyed by the signer's own public key — no server-assigned
125 // identifier to look up. For an agent the signer is the agent itself.
126 let public_key_b32 = identity.public_key_base32();
127
128 // Load the keypair (needs the secret key to sign).
129 let store = IdentityStore::open_default()
130 .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to open identity store: {e}")))?;
131 let keypair = store.load_keypair(&identity.id, None).map_err(|e| {
132 AuthError::Internal(anyhow::anyhow!(
133 "Failed to load keypair for '{}': {e}",
134 identity.name
135 ))
136 })?;
137
138 // An agent acts on behalf of the human who delegated to it, so the subject
139 // becomes the human and the agent moves into `act`. Resolving the
140 // certificate here means an unusable delegation is reported before any
141 // request is made, with the command that fixes it.
142 let (sub, act, dlg) = if identity.identity_type.is_delegated() {
143 let resolved = crate::auth::delegation::active_for(&store, identity, Some(server))?;
144 let delegator_key = atomic_canonical::delegation::delegator_public_key(
145 &resolved.delegation,
146 )
147 .map_err(|e| AuthError::DelegationError {
148 message: format!("Delegation for '{}' is malformed: {e}", identity.name),
149 })?;
150 (
151 delegator_key.to_base32(),
152 Some(Actor {
153 sub: public_key_b32.clone(),
154 }),
155 Some(resolved.delegation.id.to_urn()),
156 )
157 } else {
158 (public_key_b32.clone(), None, None)
159 };
160
161 let now = Utc::now();
162 let claims = Claims {
163 sub,
164 aud: audience(server),
165 iat: now.timestamp(),
166 exp: (now + TOKEN_TTL).timestamp(),
167 jti: Uuid::new_v4().to_string(),
168 act,
169 dlg,
170 };
171
172 let header = JwtHeader {
173 alg: "EdDSA",
174 typ: "JWT",
175 kid: public_key_b32,
176 };
177
178 let header_json = serde_json::to_vec(&header)
179 .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to encode JWT header: {e}")))?;
180 let claims_json = serde_json::to_vec(&claims)

Callers 1

get_tokenFunction · 0.85

Calls 13

active_forFunction · 0.85
delegator_public_keyFunction · 0.85
audienceFunction · 0.85
public_key_base32Method · 0.80
load_keypairMethod · 0.80
is_delegatedMethod · 0.80
to_base32Method · 0.45
cloneMethod · 0.45
to_urnMethod · 0.45
timestampMethod · 0.45
encodeMethod · 0.45
signMethod · 0.45

Tested by

no test coverage detected