diff --git a/packages/codearts/index.mjs b/packages/codearts/index.mjs index e328bae..bf5367b 100644 --- a/packages/codearts/index.mjs +++ b/packages/codearts/index.mjs @@ -520,16 +520,47 @@ function profileOf(cred) { * `iam:::user:`, an ID nobody can tell apart from another * account's. `user_phone` (read from IAM after sign-in, see * `fetchAccountPhone`) stands in front of them when it is there. + * + * `id` is the identity (the URN) and keeps every IAM shape; `name` is only + * what a person can read. When the claim carries no such value, `name` is + * empty and the host falls back to the provider's name — an IAM id is never + * the name (the `accountId` / `uid` rule in magpie-plugin-scaffold, #27). */ function accountIdentityOf(cred) { const legacy = { id: firstOf(cred?.user_name, cred?.user_id), name: firstOf(cred?.user_name) } const profile = profileOf(cred) - if (!profile) return { ...legacy, name: firstOf(cred?.user_phone, legacy.name) } + if (!profile) return { ...legacy, name: displayNameOf(cred?.user_phone, legacy.name) } const id = firstOf(profile.principal_urn, profile.account_name, profile.account_id, profile.user_id, legacy.id) - const name = firstOf(cred?.user_phone, profile.user_name, profile.account_name, profile.domain_name, legacy.name) + const name = displayNameOf(cred?.user_phone, profile.user_name, profile.account_name, profile.domain_name, legacy.name) return { id, name } } +// What IAM writes in place of a name: `iam:::user:`, a `urn:…`, +// the bare 32-character domain id, the account's `hw…` name, or a uuid. The +// shapes are read as IDs wherever they appear; none of them is a name. +const IDENTIFIER_PATTERNS = [ + /::/, // iam:::user: / arn:… + /^urn:/i, // urn:… + /^hw[0-9a-f]{6,}$/i, // Huawei IAM account name, e.g. hw123456789012345 + /^[0-9a-f]{8}-[0-9a-f]{4}-/i, // uuid + /^[0-9a-f]{16,}$/i, // bare hash, incl. the 32-character domain id +] + +/** Does this value look like an internal ID rather than a display name? */ +function looksLikeIdentifier(value) { + const s = String(value ?? "").trim() + return !!s && IDENTIFIER_PATTERNS.some((re) => re.test(s)) +} + +/** The first candidate that a person can tell apart from another account. */ +function displayNameOf(...candidates) { + for (const candidate of candidates) { + const name = firstOf(candidate) + if (name && !looksLikeIdentifier(name)) return name + } + return "" +} + /** * A phone number as the cards show it: `0086-13800138000` → `138****8000`. * The area code Huawei answers with is dropped, and any number too short to @@ -2806,7 +2837,7 @@ export const _internal = { generatePkcePair, generateDpopKeyPair, keyPairFromStoredJwk, signDpopJws, buildCanonicalRequest, signRequestHuawei, credentialFromTokenResponse, credentialOf, expiresFromCredential, accountIdentityOf, accountSessionIds, - profileOf, maskPhone, fetchAccountPhone, accountPhoneField, IAM_HOST, IAM_USER_URL, + profileOf, looksLikeIdentifier, maskPhone, fetchAccountPhone, accountPhoneField, IAM_HOST, IAM_USER_URL, requestToken, exchangeAuthorizationCode, exchangeRefreshToken, refreshCredential, isDeadRefreshToken, isPermanentRefreshFailure, nearExpiry, persistCredential, buildOAuthLoginUrl, buildPortalLoginResultUrl, diff --git a/packages/codearts/index.test.mjs b/packages/codearts/index.test.mjs index bb79d56..1e162af 100644 --- a/packages/codearts/index.test.mjs +++ b/packages/codearts/index.test.mjs @@ -295,6 +295,39 @@ test("accountIdentityOf names an account by its phone, never by IAM's ids", () = assert.deepEqual(x.accountIdentityOf({ refresh_token: token }), { id: "iam::d1:user:hw12323", name: "hw12323" }) }) +test("accountIdentityOf never makes an IAM id the account's name", () => { + const b64u = (o) => Buffer.from(JSON.stringify(o)).toString("base64url") + const urn = "iam::d1:user:hw123456789012345" + // The claim names the signer only by its IAM ids. With no phone read there is + // no name to show: `id` keeps the URN, `name` is empty (the host falls back + // to the provider's name) — never the URN. + for (const profile of [ + { principal_urn: urn, user_name: urn, account_name: urn, domain_name: urn }, + { principal_urn: urn, user_name: "hw123456789012345" }, + ]) { + const token = `h.${b64u({ user_profile: JSON.stringify(profile) })}.s` + assert.deepEqual(x.accountIdentityOf({ refresh_token: token }), { id: urn, name: "" }) + } + // A credential without a `user_profile` is the same: `id` is the URN, and + // `user_name` is not a name. + assert.deepEqual(x.accountIdentityOf({ refresh_token: "opaque", user_name: urn }), { id: urn, name: "" }) +}) + +test("looksLikeIdentifier spots the shapes IAM writes, and leaves names alone", () => { + for (const id of [ + "iam::d1:user:hw123456789012345", + "urn:ietf:params:oauth:jwt", + "0123456789abcdef0123456789abcdef", + "123e4567-e89b-12d3-a456-426614174000", + "hw123456789012345", + ]) { + assert.equal(x.looksLikeIdentifier(id), true, id) + } + for (const name of ["alice", "138****8000", "a@b.com", "", "hw123"]) { + assert.equal(x.looksLikeIdentifier(name), false, name) + } +}) + test("fetchAccountPhone reads IAM with the account's own credentials, and only once", async () => { const seen = [] const cred = credWithProfile({ principal_id: "uid-1" }) diff --git a/packages/codearts/package.json b/packages/codearts/package.json index 0940bbb..80aae6c 100644 --- a/packages/codearts/package.json +++ b/packages/codearts/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-codearts-auth", - "version": "0.2.5", + "version": "0.2.6", "description": "Huawei Cloud CodeArts (码道) in OpenCode and magpie: its models through Huawei Cloud OAuth sign-in, signed requests, credit usage and the daily check-in (unofficial)", "type": "module", "main": "./index.mjs",