Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion lib/__tests__/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ describe("auth", () => {
)
})

it("reads a login an older LPM CLI stored under the trailing-slash registry", async () => {
process.env.LPM_REGISTRY_URL = "https://lpm.dev/"
const keytar = await import("keytar")
keytar.default.getPassword
.mockResolvedValueOnce(null)
.mockResolvedValueOnce("lpm_slashed_token")

const token = await getToken()
expect(token).toBe("lpm_slashed_token")
expect(keytar.default.getPassword).toHaveBeenNthCalledWith(
1,
"lpm-cli",
"auth-token:bd90fc32d95766d5",
)
expect(keytar.default.getPassword).toHaveBeenNthCalledWith(
2,
"lpm-cli",
"auth-token:89f54e26677b97ec",
)
})

it("reads a login an older LPM CLI stored under the trailing-slash spelling of the default registry", async () => {
const keytar = await import("keytar")
keytar.default.getPassword
.mockResolvedValueOnce(null)
.mockResolvedValueOnce("lpm_slashed_token")

const token = await getToken()
expect(token).toBe("lpm_slashed_token")
expect(keytar.default.getPassword).toHaveBeenNthCalledWith(
2,
"lpm-cli",
"auth-token:89f54e26677b97ec",
)
})

it("returns null when keytar has no token", async () => {
const keytar = await import("keytar")
keytar.default.getPassword.mockResolvedValueOnce(null)
Expand Down Expand Up @@ -136,7 +172,7 @@ describe("auth", () => {
describe("registry-scoped keychain accounts", () => {
it.each([
["https://lpm.dev", "auth-token:bd90fc32d95766d5"],
["https://lpm.dev/", "auth-token:89f54e26677b97ec"],
["https://lpm.dev/", "auth-token:bd90fc32d95766d5"],
["http://localhost:3000", "auth-token:f1de9e489ba88cb1"],
["http://127.0.0.1:8787", "auth-token:b1a61bf29a38ff36"],
["https://registry.example.com/custom", "auth-token:1658e811bb24ec08"],
Expand Down
31 changes: 25 additions & 6 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,30 @@ export async function getToken(loadKeytar = () => import("keytar")) {

try {
const keytar = keytarModule.default ?? keytarModule
const token = await keytar.getPassword(
KEYTAR_SERVICE_NAME,
getKeychainAccount(),
)
if (token) return token
for (const account of keychainAccountsToTry(getBaseUrl())) {
const token = await keytar.getPassword(KEYTAR_SERVICE_NAME, account)
if (token) return token
}
} catch {
throw new Error(KEYCHAIN_READ_ERROR)
}

return null
}

/**
* The accounts that may hold the login for `registryUrl`, whichever way it
* ends: the one LPM CLI uses now, then the one CLI releases before 0.86 used
* for a login made with the registry spelled with a trailing slash.
*
* @param {string} registryUrl
* @returns {string[]}
*/
function keychainAccountsToTry(registryUrl) {
const canonical = registryUrl.replace(/\/+$/, "")
return [accountForKey(canonical), accountForKey(`${canonical}/`)]
}

/**
* Apply the LPM CLI's default registry transport contract: HTTPS, or HTTP
* only for loopback. The MCP server has no `--insecure` remote-HTTP escape hatch.
Expand Down Expand Up @@ -85,12 +97,19 @@ export function getBaseUrl() {

/**
* Derive the keychain account used by LPM CLI for one effective registry URL.
* LPM CLI keys logins by the registry without trailing slashes, so both
* spellings name one account.
*
* @param {string} [registryUrl]
* @returns {string}
*/
export function getKeychainAccount(registryUrl = getBaseUrl()) {
const digest = createHash("sha256").update(registryUrl, "utf8").digest("hex")
return accountForKey(registryUrl.replace(/\/+$/, ""))
}

/** @param {string} key */
function accountForKey(key) {
const digest = createHash("sha256").update(key, "utf8").digest("hex")
return `${KEYTAR_ACCOUNT_PREFIX}:${digest.slice(0, 16)}`
}

Expand Down
Loading