codearts 0.2.6: an IAM internal id is never the account's display name (#518)

An account whose IAM phone read yields nothing no longer shows
iam::<domain>:user:<name>, the hw... account name, a urn:, a uuid or a bare
hex id as its card name. accountIdentityOf() walks the same candidates in the
same order and skips any that looks like an id; with none left the name is ""
and the host shows the provider's name. id/uid/accountId are unchanged (still
the URN, as #483 settled), so no account is renamed. The only reader of
.name is usage()'s `user` fallback.

Reviewed at d8583da plus the reviewer's version bump (0.2.5 -> 0.2.6) at
f77e5bb. Run on mote (macOS, Bun 1.4.2) at f77e5bb, from the repo root:
- HOME=$(mktemp -d) bun test: 3763 pass / 0 fail (3771 tests, 298 files)
- bun scripts/check.mjs: exit 0
Red on the old code: the PR's tests against main's index.mjs, 125 pass /
2 fail (both new tests).
Broken by hand: displayNameOf without the looksLikeIdentifier skip ->
"accountIdentityOf never makes an IAM id the account's name" red (126/1).
Not tried: a real CodeArts sign-in whose phone read fails.
This commit is contained in:
MYCF711
2026-10-30 17:26:37 +08:00
committed by GitHub
parent 3dd0f44c15
commit 4d8d0310f9
3 changed files with 68 additions and 4 deletions
+34 -3
View File
@@ -520,16 +520,47 @@ function profileOf(cred) {
* `iam::<domain>:user:<name>`, an ID nobody can tell apart from another * `iam::<domain>:user:<name>`, an ID nobody can tell apart from another
* account's. `user_phone` (read from IAM after sign-in, see * account's. `user_phone` (read from IAM after sign-in, see
* `fetchAccountPhone`) stands in front of them when it is there. * `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) { function accountIdentityOf(cred) {
const legacy = { id: firstOf(cred?.user_name, cred?.user_id), name: firstOf(cred?.user_name) } const legacy = { id: firstOf(cred?.user_name, cred?.user_id), name: firstOf(cred?.user_name) }
const profile = profileOf(cred) 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 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 } return { id, name }
} }
// What IAM writes in place of a name: `iam::<domain>:user:<name>`, 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::<domain>:user:<name> / 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`. * 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 * 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, generatePkcePair, generateDpopKeyPair, keyPairFromStoredJwk, signDpopJws,
buildCanonicalRequest, signRequestHuawei, buildCanonicalRequest, signRequestHuawei,
credentialFromTokenResponse, credentialOf, expiresFromCredential, accountIdentityOf, accountSessionIds, 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, requestToken, exchangeAuthorizationCode, exchangeRefreshToken, refreshCredential,
isDeadRefreshToken, isPermanentRefreshFailure, nearExpiry, persistCredential, isDeadRefreshToken, isPermanentRefreshFailure, nearExpiry, persistCredential,
buildOAuthLoginUrl, buildPortalLoginResultUrl, buildOAuthLoginUrl, buildPortalLoginResultUrl,
+33
View File
@@ -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" }) 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 () => { test("fetchAccountPhone reads IAM with the account's own credentials, and only once", async () => {
const seen = [] const seen = []
const cred = credWithProfile({ principal_id: "uid-1" }) const cred = credWithProfile({ principal_id: "uid-1" })
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@magpie-community/opencode-codearts-auth", "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)", "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", "type": "module",
"main": "./index.mjs", "main": "./index.mjs",