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
6 changes: 6 additions & 0 deletions changes/fix-basic-auth-of.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Fixed

- **The `api` group tells a Basic scheme by its type, so a newer cli-base cannot change who signs.** cli-base #643
makes `signInPlace` sign Basic, and cli-core told Basic apart by `signInPlace` answering `undefined`. With that
cli-base, a call that offers Basic and a token would be signed with Basic, and a token without a user name would be
sent in base64, not refused with "run session start --login <name>". Both rules now hold with either cli-base.
50 changes: 50 additions & 0 deletions src/cli/service-api-basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect, it, vi } from "vitest"
import type { ServiceApiManifest } from "./service-api-manifest.js"
import { signRequest } from "./service-api-manifest.js"

// A cli-base that signs Basic itself, as cli-base #643 makes it: cli-core's own rules must still hold over it.
vi.mock("@wirecat/cli-base/codegen", async (original) => {
const real = await original<typeof import("@wirecat/cli-base/codegen")>()
return {
...real,
signInPlace: (scheme: Parameters<typeof real.signInPlace>[0]) =>
scheme.type === "http" && /^basic$/i.test(scheme.scheme ?? "")
? {
in: "header",
name: "authorization",
value: (token: string) => `Basic ${Buffer.from(token).toString("base64")}`,
basic: true,
}
: real.signInPlace(scheme),
}
})

const manifest = (security: ServiceApiManifest["security"]) =>
({
servers: [{ url: "https://blog.example/wp-json" }],
securitySchemes: { basic: { type: "http", scheme: "basic" }, bearer: { type: "http", scheme: "bearer" } },
security,
operations: [
{
id: "listPosts",
command: "list-posts",
effect: "read",
binding: { kind: "http", method: "GET", path: "/posts" },
parameters: [],
},
],
}) as unknown as ServiceApiManifest

const signed = async (one: ServiceApiManifest, token: string) => {
const request = { url: new URL("https://blog.example/wp-json/posts"), headers: new Headers() }
await signRequest(one, one.operations[0] as never, request, () => token)
return request.headers.get("authorization")
}

it("over a cli-base that signs Basic, a token scheme still goes first and a token without a user name is refused", async () => {
expect(await signed(manifest([{ basic: [] }, { bearer: [] }]), "token-a")).toBe("Bearer token-a")
await expect(signed(manifest([{ basic: [] }]), "token-a")).rejects.toThrow("session start --login <name>")
expect(await signed(manifest([{ basic: [] }]), "editor:pw")).toBe(
`Basic ${Buffer.from("editor:pw").toString("base64")}`,
)
})
39 changes: 21 additions & 18 deletions src/cli/service-api-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,34 @@ const serverValue = (
* none: its security, its own or else the document's, is absent, as in a spec that says nothing about sign-in, or
* empty (`[]`, `[{}]`). An optional sign-in (`[{}, { oauth: [] }]`) still sends the token.
*/
// cli-base reads no basic scheme yet; the token of one is `<user>:<password>`, as `session start --login` keeps it.
const basicPlace = (scheme: SecurityScheme): SignInPlace | undefined =>
scheme.type === "http" && /^basic$/i.test(scheme.scheme ?? "")
? {
in: "header",
name: "authorization",
value: (token) => {
if (!token.includes(":"))
throw new CliError(
"authentication_error",
"this service signs in with a user name and a password: run session start --login <name>",
)
return `Basic ${Buffer.from(token).toString("base64")}`
},
}
: undefined
const isBasic = (scheme: SecurityScheme) => scheme.type === "http" && /^basic$/i.test(scheme.scheme ?? "")

// Told apart by the scheme, not by signInPlace: a newer cli-base signs Basic and an older one does not, and
// either may be installed. Neither refuses a token without a user name. The token is `<user>:<password>`, as
// `session start --login` keeps it.
const BASIC: SignInPlace = {
in: "header",
name: "authorization",
value: (token) => {
if (!token.includes(":"))
throw new CliError(
"authentication_error",
"this service signs in with a user name and a password: run session start --login <name>",
)
return `Basic ${Buffer.from(token).toString("base64")}`
},
}

const authOf = (manifest: ServiceApiManifest, operation: ManifestOperation): SignInPlace | undefined => {
const requirements = signInRequirements(operation, manifest.security)
if (!requirements) return undefined
const names = requirements.flatMap((one) => Object.keys(one))
const sendable = names.flatMap((name) => {
const scheme = manifest.securitySchemes?.[name]
const place = scheme && (signInPlace(scheme) ?? basicPlace(scheme))
return place ? [{ place, basic: !signInPlace(scheme) }] : []
if (!scheme) return []
const basic = isBasic(scheme)
const place = basic ? BASIC : signInPlace(scheme)
return place ? [{ place, basic }] : []
})
// Basic only when nothing else is offered: it needs a user name a token sign-in does not keep.
const auth = (
Expand Down
Loading