@typespec/http-client-js 0.4.0-dev.0 → 0.4.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/client-context/client-context-factory.d.ts.map +1 -1
- package/dist/src/components/client-context/client-context-factory.js +104 -37
- package/dist/src/components/client-context/client-context-factory.js.map +1 -1
- package/dist/src/components/external-packages/ts-http-runtime.d.ts +8 -3
- package/dist/src/components/external-packages/ts-http-runtime.d.ts.map +1 -1
- package/dist/src/components/external-packages/ts-http-runtime.js +2 -2
- package/dist/src/components/external-packages/ts-http-runtime.js.map +1 -1
- package/dist/src/utils/parameters.d.ts.map +1 -1
- package/dist/src/utils/parameters.js +16 -2
- package/dist/src/utils/parameters.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/client-context/client-context-factory.tsx +86 -37
- package/src/components/external-packages/ts-http-runtime.ts +9 -4
- package/src/utils/parameters.tsx +20 -3
- package/test/e2e/http/authentication/oauth2/main.test.ts +6 -16
- package/test/e2e/http/authentication/union/main.test.ts +2 -5
- package/test/scenarios/auth/basic_auth.md +11 -7
- package/test/scenarios/auth/bearer.md +11 -7
- package/test/scenarios/auth/client_parameters.md +19 -6
- package/test/scenarios/auth/client_structure.md +1 -1
- package/test/scenarios/auth/key_credential.md +11 -6
- package/test/scenarios/auth/oauth2.md +91 -0
- package/test/scenarios/auth/sub_client_override.md +11 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/http-client-js",
|
|
3
|
-
"version": "0.4.0-dev.
|
|
3
|
+
"version": "0.4.0-dev.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"homepage": "https://typespec.io",
|
|
6
6
|
"readme": "https://github.com/microsoft/typespec/blob/main/packages/http-client-js/README.md",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"@types/yargs": "~17.0.33",
|
|
46
46
|
"@typespec/http": "^1.0.0-rc.0",
|
|
47
47
|
"@typespec/http-specs": "^0.1.0-alpha.17 || >=0.1.0-alpha.18-dev <0.1.0-alpha.18",
|
|
48
|
-
"@typespec/spector": "^0.1.0-alpha.
|
|
49
|
-
"@typespec/ts-http-runtime": "0.1
|
|
48
|
+
"@typespec/spector": "^0.1.0-alpha.11 || >=0.1.0-alpha.12-dev <0.1.0-alpha.12",
|
|
49
|
+
"@typespec/ts-http-runtime": "0.2.1",
|
|
50
50
|
"@typespec/versioning": "^0.68.0 || >=0.69.0-dev <0.69.0",
|
|
51
51
|
"@vitest/ui": "^3.0.9",
|
|
52
52
|
"change-case": "~5.4.4",
|
|
@@ -2,6 +2,7 @@ import * as ay from "@alloy-js/core";
|
|
|
2
2
|
import * as ts from "@alloy-js/typescript";
|
|
3
3
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
4
4
|
import { FunctionDeclaration } from "@typespec/emitter-framework/typescript";
|
|
5
|
+
import { HttpAuth, type OAuth2Flow } from "@typespec/http";
|
|
5
6
|
import * as cl from "@typespec/http-client";
|
|
6
7
|
import { reportDiagnostic } from "../../lib.js";
|
|
7
8
|
import { buildClientParameters } from "../../utils/parameters.jsx";
|
|
@@ -37,7 +38,7 @@ export function ClientContextFactoryDeclaration(props: ClientContextFactoryProps
|
|
|
37
38
|
);
|
|
38
39
|
|
|
39
40
|
let credentialsRef: string | undefined;
|
|
40
|
-
if (
|
|
41
|
+
if ("credential" in parameters) {
|
|
41
42
|
credentialsRef = "credential";
|
|
42
43
|
}
|
|
43
44
|
|
|
@@ -75,66 +76,114 @@ interface ClientFactoryArgumentsProps {
|
|
|
75
76
|
function ClientFactoryArguments(props: ClientFactoryArgumentsProps) {
|
|
76
77
|
const params = [<>{props.endpointRef},</>];
|
|
77
78
|
|
|
78
|
-
if (props.credentialsRef) {
|
|
79
|
-
params.push(<>{props.credentialsRef},</>);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
79
|
return [
|
|
83
80
|
...params,
|
|
84
|
-
|
|
85
|
-
<
|
|
86
|
-
|
|
81
|
+
props.credentialsRef ? (
|
|
82
|
+
<ClientOptionsExpression>
|
|
83
|
+
{props.credentialsRef}
|
|
84
|
+
<AuthSchemeOptions client={props.client} />
|
|
85
|
+
</ClientOptionsExpression>
|
|
86
|
+
) : (
|
|
87
|
+
<ClientOptionsExpression />
|
|
88
|
+
),
|
|
87
89
|
];
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
interface
|
|
92
|
+
interface AuthSchemeOptionsProps {
|
|
91
93
|
client: cl.Client;
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (clientCredential.schemes.length !== 1) {
|
|
102
|
-
reportDiagnostic($.program, {
|
|
103
|
-
code: "multiple-auth-schemes-not-yet-supported",
|
|
104
|
-
target: props.client.type,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const scheme = clientCredential.schemes[0];
|
|
96
|
+
interface AuthSchemeProps {
|
|
97
|
+
scheme: HttpAuth;
|
|
98
|
+
client: cl.Client;
|
|
99
|
+
}
|
|
109
100
|
|
|
110
|
-
|
|
101
|
+
function AuthScheme(props: AuthSchemeProps) {
|
|
102
|
+
switch (props.scheme.type) {
|
|
111
103
|
case "http":
|
|
112
|
-
// Todo: handle scopes?
|
|
113
104
|
return (
|
|
114
|
-
<ts.
|
|
115
|
-
<
|
|
116
|
-
<ts.ObjectProperty name="
|
|
117
|
-
|
|
118
|
-
|
|
105
|
+
<ts.ObjectExpression>
|
|
106
|
+
<ay.List comma>
|
|
107
|
+
<ts.ObjectProperty name="kind" jsValue="http" />
|
|
108
|
+
<ts.ObjectProperty name="scheme" jsValue={props.scheme.scheme.toLowerCase()} />
|
|
109
|
+
</ay.List>
|
|
110
|
+
</ts.ObjectExpression>
|
|
119
111
|
);
|
|
120
112
|
case "apiKey":
|
|
121
|
-
if (scheme.in !== "header") {
|
|
113
|
+
if (props.scheme.in !== "header") {
|
|
122
114
|
reportDiagnostic($.program, { code: "non-model-parts", target: props.client.service });
|
|
123
115
|
return null;
|
|
124
116
|
}
|
|
125
117
|
|
|
126
118
|
return (
|
|
127
|
-
<ts.
|
|
128
|
-
<
|
|
129
|
-
<ts.ObjectProperty name="
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
<ts.ObjectExpression>
|
|
120
|
+
<ay.List comma>
|
|
121
|
+
<ts.ObjectProperty name="kind" jsValue="apiKey" />
|
|
122
|
+
<ts.ObjectProperty name="apiKeyLocation" jsValue={props.scheme.in} />
|
|
123
|
+
<ts.ObjectProperty name="name" jsValue={props.scheme.name} />
|
|
124
|
+
</ay.List>
|
|
125
|
+
</ts.ObjectExpression>
|
|
126
|
+
);
|
|
127
|
+
case "oauth2":
|
|
128
|
+
return (
|
|
129
|
+
<ts.ObjectExpression>
|
|
130
|
+
<ay.List comma>
|
|
131
|
+
<ts.ObjectProperty name="kind" jsValue="oauth2" />
|
|
132
|
+
<ts.ObjectProperty name="flows">
|
|
133
|
+
[
|
|
134
|
+
<ay.For each={props.scheme.flows} comma line>
|
|
135
|
+
{(flow) => <OAuth2Flow flow={flow} />}
|
|
136
|
+
</ay.For>
|
|
137
|
+
]
|
|
138
|
+
</ts.ObjectProperty>
|
|
139
|
+
</ay.List>
|
|
140
|
+
</ts.ObjectExpression>
|
|
132
141
|
);
|
|
133
142
|
default:
|
|
134
143
|
return null;
|
|
135
144
|
}
|
|
136
145
|
}
|
|
137
146
|
|
|
147
|
+
interface OAuth2FlowProps {
|
|
148
|
+
flow: OAuth2Flow;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function OAuth2Flow(props: OAuth2FlowProps) {
|
|
152
|
+
// rename type to kind and clean up the scopes
|
|
153
|
+
const { type, ...rewrittenFlow } = {
|
|
154
|
+
kind: props.flow.type,
|
|
155
|
+
...props.flow,
|
|
156
|
+
// scopes are sometimes duplicated so converting to set and back to dedupe
|
|
157
|
+
scopes: [...new Set(props.flow.scopes.map((s) => s.value))],
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return <ts.ObjectExpression jsValue={rewrittenFlow} />;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function AuthSchemeOptions(props: AuthSchemeOptionsProps) {
|
|
164
|
+
const clientCredential = $.client.getAuth(props.client);
|
|
165
|
+
|
|
166
|
+
if (clientCredential.schemes.length === 0) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Filtering out custom http schemes for "http/custom" spector scenario
|
|
171
|
+
// TODO: Should typekit allow for arbitrary strings on scheme with http auth type?
|
|
172
|
+
const supportedSchemes = clientCredential.schemes.filter(
|
|
173
|
+
(s) => s.type !== "http" || ["Basic", "Bearer"].includes(s.scheme),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<ts.ObjectProperty name="authSchemes">
|
|
178
|
+
[
|
|
179
|
+
<ay.For each={supportedSchemes} comma line>
|
|
180
|
+
{(scheme) => <AuthScheme scheme={scheme} client={props.client} />}
|
|
181
|
+
</ay.For>
|
|
182
|
+
]
|
|
183
|
+
</ts.ObjectProperty>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
138
187
|
interface ClientOptionsExpressionProps {
|
|
139
188
|
children?: ay.Children;
|
|
140
189
|
}
|
|
@@ -2,16 +2,21 @@ import { createPackage } from "@alloy-js/typescript";
|
|
|
2
2
|
|
|
3
3
|
export const httpRuntimeTemplateLib = createPackage({
|
|
4
4
|
name: "@typespec/ts-http-runtime",
|
|
5
|
-
version: "0.1
|
|
5
|
+
version: "0.2.1",
|
|
6
6
|
descriptor: {
|
|
7
7
|
".": {
|
|
8
8
|
named: [
|
|
9
9
|
"Client",
|
|
10
10
|
"ClientOptions",
|
|
11
11
|
"getClient",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
12
|
+
"ApiKeyCredential",
|
|
13
|
+
"BasicCredential",
|
|
14
|
+
"BearerTokenCredential",
|
|
15
|
+
"OAuth2TokenCredential",
|
|
16
|
+
"AuthorizationCodeFlow",
|
|
17
|
+
"ClientCredentialsFlow",
|
|
18
|
+
"ImplicitFlow",
|
|
19
|
+
"PasswordFlow",
|
|
15
20
|
"PathUncheckedResponse",
|
|
16
21
|
"PipelineRequest",
|
|
17
22
|
"HttpResponse",
|
package/src/utils/parameters.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import * as ts from "@alloy-js/typescript";
|
|
|
3
3
|
import { ModelProperty, Value } from "@typespec/compiler";
|
|
4
4
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
5
5
|
import { buildParameterDescriptor } from "@typespec/emitter-framework/typescript";
|
|
6
|
-
import { HttpAuth, HttpProperty } from "@typespec/http";
|
|
6
|
+
import { HttpAuth, HttpProperty, OAuth2FlowType } from "@typespec/http";
|
|
7
7
|
import * as cl from "@typespec/http-client";
|
|
8
8
|
import { getClientContextOptionsRef } from "../components/client-context/client-context-options.jsx";
|
|
9
9
|
import { httpRuntimeTemplateLib } from "../components/external-packages/ts-http-runtime.js";
|
|
@@ -69,13 +69,30 @@ function buildClientParameterDescriptor(
|
|
|
69
69
|
return buildParameterDescriptor(modelProperty);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
const oauth2FlowRefs: Record<OAuth2FlowType, ay.Refkey> = {
|
|
73
|
+
authorizationCode: httpRuntimeTemplateLib.AuthorizationCodeFlow,
|
|
74
|
+
clientCredentials: httpRuntimeTemplateLib.ClientCredentialsFlow,
|
|
75
|
+
password: httpRuntimeTemplateLib.PasswordFlow,
|
|
76
|
+
implicit: httpRuntimeTemplateLib.ImplicitFlow,
|
|
77
|
+
};
|
|
78
|
+
|
|
72
79
|
function getCredentialType(scheme: HttpAuth) {
|
|
73
80
|
switch (scheme.type) {
|
|
74
81
|
case "apiKey":
|
|
82
|
+
return httpRuntimeTemplateLib.ApiKeyCredential;
|
|
75
83
|
case "http":
|
|
76
|
-
|
|
84
|
+
if (scheme.scheme === "Basic") {
|
|
85
|
+
return httpRuntimeTemplateLib.BasicCredential;
|
|
86
|
+
} else {
|
|
87
|
+
return httpRuntimeTemplateLib.BearerTokenCredential;
|
|
88
|
+
}
|
|
77
89
|
case "oauth2":
|
|
78
|
-
|
|
90
|
+
const flowType = ay.mapJoin(
|
|
91
|
+
() => scheme.flows,
|
|
92
|
+
(x) => oauth2FlowRefs[x.type],
|
|
93
|
+
{ joiner: " | " },
|
|
94
|
+
);
|
|
95
|
+
return ay.code`${httpRuntimeTemplateLib.OAuth2TokenCredential}<${flowType}>`;
|
|
79
96
|
default:
|
|
80
97
|
return null;
|
|
81
98
|
}
|
|
@@ -4,38 +4,28 @@ import { OAuth2Client } from "../../../generated/authentication/oauth2/src/index
|
|
|
4
4
|
describe("Authentication.OAuth2", () => {
|
|
5
5
|
const client = new OAuth2Client(
|
|
6
6
|
{
|
|
7
|
-
|
|
8
|
-
token: "Bearer https://security.microsoft.com/.default",
|
|
9
|
-
expiresOnTimestamp: Date.now() + 3600 * 1000,
|
|
10
|
-
}),
|
|
7
|
+
getOAuth2Token: async () => "https://security.microsoft.com/.default",
|
|
11
8
|
},
|
|
12
9
|
{ allowInsecureConnection: true },
|
|
13
10
|
);
|
|
14
11
|
|
|
15
|
-
it
|
|
12
|
+
it("should validate the client is authenticated", async () => {
|
|
16
13
|
const response = await client.valid();
|
|
17
14
|
expect(response).toBe(undefined); // No content response
|
|
18
15
|
});
|
|
19
16
|
|
|
20
|
-
it
|
|
17
|
+
it("should handle invalid authentication and return error", async () => {
|
|
21
18
|
try {
|
|
22
19
|
const client = new OAuth2Client(
|
|
23
20
|
{
|
|
24
|
-
|
|
25
|
-
token: "invalid",
|
|
26
|
-
expiresOnTimestamp: Date.now() + 3600 * 1000,
|
|
27
|
-
}),
|
|
21
|
+
getOAuth2Token: async () => "invalid",
|
|
28
22
|
},
|
|
29
23
|
{ allowInsecureConnection: true },
|
|
30
24
|
);
|
|
31
25
|
await client.invalid();
|
|
32
26
|
} catch (error: any) {
|
|
33
|
-
expect(error.
|
|
34
|
-
expect(error.error).
|
|
35
|
-
message: "Expected Bearer x but got Bearer y",
|
|
36
|
-
expected: "Bearer x",
|
|
37
|
-
actual: "Bearer y",
|
|
38
|
-
});
|
|
27
|
+
expect(error.status).toBe("403");
|
|
28
|
+
expect(error.body.error).toBe("invalid-grant");
|
|
39
29
|
}
|
|
40
30
|
});
|
|
41
31
|
});
|
|
@@ -13,13 +13,10 @@ describe("Authentication.Union", () => {
|
|
|
13
13
|
// Assert successful request
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
-
it
|
|
16
|
+
it("should authenticate using the valid OAuth token", async () => {
|
|
17
17
|
const client = new UnionClient(
|
|
18
18
|
{
|
|
19
|
-
|
|
20
|
-
token: "Bearer https://security.microsoft.com/.default",
|
|
21
|
-
expiresOnTimestamp: Date.now() + 3600 * 1000,
|
|
22
|
-
}),
|
|
19
|
+
getOAuth2Token: async () => "https://security.microsoft.com/.default",
|
|
23
20
|
},
|
|
24
21
|
{ allowInsecureConnection: true },
|
|
25
22
|
);
|
|
@@ -20,13 +20,13 @@ op valid(): NoContentResponse;
|
|
|
20
20
|
|
|
21
21
|
### Client
|
|
22
22
|
|
|
23
|
-
The client signature should include a positional parameter for credential of type
|
|
23
|
+
The client signature should include a positional parameter for credential of type BasicCredential. A basic auth token consists of a username and password which are concatenated with a colon and base64 encoded. The resulting string is then put into the Authorization header.
|
|
24
24
|
|
|
25
25
|
```ts src/testClient.ts class TestClient
|
|
26
26
|
export class TestClient {
|
|
27
27
|
#context: TestClientContext;
|
|
28
28
|
|
|
29
|
-
constructor(endpoint: string, credential:
|
|
29
|
+
constructor(endpoint: string, credential: BasicCredential, options?: TestClientOptions) {
|
|
30
30
|
this.#context = createTestClientContext(endpoint, credential, options);
|
|
31
31
|
}
|
|
32
32
|
async valid(options?: ValidOptions) {
|
|
@@ -42,7 +42,7 @@ The client context should setup the pipeline to use the credential in the Author
|
|
|
42
42
|
```ts src/api/testClientContext.ts function createTestClientContext
|
|
43
43
|
export function createTestClientContext(
|
|
44
44
|
endpoint: string,
|
|
45
|
-
credential:
|
|
45
|
+
credential: BasicCredential,
|
|
46
46
|
options?: TestClientOptions,
|
|
47
47
|
): TestClientContext {
|
|
48
48
|
const params: Record<string, any> = {
|
|
@@ -55,11 +55,15 @@ export function createTestClientContext(
|
|
|
55
55
|
throw new Error(`Missing parameter: ${key}`);
|
|
56
56
|
})(),
|
|
57
57
|
);
|
|
58
|
-
return getClient(resolvedEndpoint,
|
|
58
|
+
return getClient(resolvedEndpoint, {
|
|
59
59
|
...options,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
credential,
|
|
61
|
+
authSchemes: [
|
|
62
|
+
{
|
|
63
|
+
kind: "http",
|
|
64
|
+
scheme: "basic",
|
|
65
|
+
},
|
|
66
|
+
],
|
|
63
67
|
});
|
|
64
68
|
}
|
|
65
69
|
```
|
|
@@ -20,13 +20,13 @@ op valid(): NoContentResponse;
|
|
|
20
20
|
|
|
21
21
|
### Client
|
|
22
22
|
|
|
23
|
-
The client signature should include a positional parameter for credential of type
|
|
23
|
+
The client signature should include a positional parameter for credential of type BearerTokenCredential. A Bearer token is a token credential that gets put into the Authorization header
|
|
24
24
|
|
|
25
25
|
```ts src/testClient.ts class TestClient
|
|
26
26
|
export class TestClient {
|
|
27
27
|
#context: TestClientContext;
|
|
28
28
|
|
|
29
|
-
constructor(endpoint: string, credential:
|
|
29
|
+
constructor(endpoint: string, credential: BasicCredential, options?: TestClientOptions) {
|
|
30
30
|
this.#context = createTestClientContext(endpoint, credential, options);
|
|
31
31
|
}
|
|
32
32
|
async valid(options?: ValidOptions) {
|
|
@@ -42,7 +42,7 @@ The client context should setup the pipeline to use the credential in the Author
|
|
|
42
42
|
```ts src/api/testClientContext.ts function createTestClientContext
|
|
43
43
|
export function createTestClientContext(
|
|
44
44
|
endpoint: string,
|
|
45
|
-
credential:
|
|
45
|
+
credential: BasicCredential,
|
|
46
46
|
options?: TestClientOptions,
|
|
47
47
|
): TestClientContext {
|
|
48
48
|
const params: Record<string, any> = {
|
|
@@ -55,11 +55,15 @@ export function createTestClientContext(
|
|
|
55
55
|
throw new Error(`Missing parameter: ${key}`);
|
|
56
56
|
})(),
|
|
57
57
|
);
|
|
58
|
-
return getClient(resolvedEndpoint,
|
|
58
|
+
return getClient(resolvedEndpoint, {
|
|
59
59
|
...options,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
credential,
|
|
61
|
+
authSchemes: [
|
|
62
|
+
{
|
|
63
|
+
kind: "http",
|
|
64
|
+
scheme: "bearer",
|
|
65
|
+
},
|
|
66
|
+
],
|
|
63
67
|
});
|
|
64
68
|
}
|
|
65
69
|
```
|
|
@@ -27,7 +27,11 @@ TODO: Revisit if we need additional types since it will be difficult at runtime
|
|
|
27
27
|
export class TestClient {
|
|
28
28
|
#context: TestClientContext;
|
|
29
29
|
|
|
30
|
-
constructor(
|
|
30
|
+
constructor(
|
|
31
|
+
endpoint: string,
|
|
32
|
+
credential: BearerTokenCredential | ApiKeyCredential,
|
|
33
|
+
options?: TestClientOptions,
|
|
34
|
+
) {
|
|
31
35
|
this.#context = createTestClientContext(endpoint, credential, options);
|
|
32
36
|
}
|
|
33
37
|
async valid(options?: ValidOptions) {
|
|
@@ -43,7 +47,7 @@ The client context should setup the pipeline to use the credential in the Author
|
|
|
43
47
|
```ts src/api/testClientContext.ts function createTestClientContext
|
|
44
48
|
export function createTestClientContext(
|
|
45
49
|
endpoint: string,
|
|
46
|
-
credential:
|
|
50
|
+
credential: BearerTokenCredential | ApiKeyCredential,
|
|
47
51
|
options?: TestClientOptions,
|
|
48
52
|
): TestClientContext {
|
|
49
53
|
const params: Record<string, any> = {
|
|
@@ -56,11 +60,20 @@ export function createTestClientContext(
|
|
|
56
60
|
throw new Error(`Missing parameter: ${key}`);
|
|
57
61
|
})(),
|
|
58
62
|
);
|
|
59
|
-
return getClient(resolvedEndpoint,
|
|
63
|
+
return getClient(resolvedEndpoint, {
|
|
60
64
|
...options,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
credential,
|
|
66
|
+
authSchemes: [
|
|
67
|
+
{
|
|
68
|
+
kind: "http",
|
|
69
|
+
scheme: "bearer",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
kind: "apiKey",
|
|
73
|
+
apiKeyLocation: "header",
|
|
74
|
+
name: "X-API-KEY",
|
|
75
|
+
},
|
|
76
|
+
],
|
|
64
77
|
});
|
|
65
78
|
}
|
|
66
79
|
```
|
|
@@ -27,7 +27,7 @@ export class MyApiClient {
|
|
|
27
27
|
#context: MyApiClientContext;
|
|
28
28
|
fooClient: FooClient;
|
|
29
29
|
barClient: BarClient;
|
|
30
|
-
constructor(endpoint: string, credential:
|
|
30
|
+
constructor(endpoint: string, credential: BasicCredential, options?: MyApiClientOptions) {
|
|
31
31
|
this.#context = createMyApiClientContext(endpoint, credential, options);
|
|
32
32
|
this.fooClient = new FooClient(endpoint, credential, options);
|
|
33
33
|
this.barClient = new BarClient(endpoint, credential, options);
|
|
@@ -26,7 +26,7 @@ The client signature should include a positional parameter for credential of typ
|
|
|
26
26
|
export class TestClient {
|
|
27
27
|
#context: TestClientContext;
|
|
28
28
|
|
|
29
|
-
constructor(endpoint: string, credential:
|
|
29
|
+
constructor(endpoint: string, credential: ApiKeyCredential, options?: TestClientOptions) {
|
|
30
30
|
this.#context = createTestClientContext(endpoint, credential, options);
|
|
31
31
|
}
|
|
32
32
|
async valid(options?: ValidOptions) {
|
|
@@ -42,7 +42,7 @@ The client context should setup the pipeline to use the credential in the header
|
|
|
42
42
|
```ts src/api/testClientContext.ts function createTestClientContext
|
|
43
43
|
export function createTestClientContext(
|
|
44
44
|
endpoint: string,
|
|
45
|
-
credential:
|
|
45
|
+
credential: ApiKeyCredential,
|
|
46
46
|
options?: TestClientOptions,
|
|
47
47
|
): TestClientContext {
|
|
48
48
|
const params: Record<string, any> = {
|
|
@@ -55,11 +55,16 @@ export function createTestClientContext(
|
|
|
55
55
|
throw new Error(`Missing parameter: ${key}`);
|
|
56
56
|
})(),
|
|
57
57
|
);
|
|
58
|
-
return getClient(resolvedEndpoint,
|
|
58
|
+
return getClient(resolvedEndpoint, {
|
|
59
59
|
...options,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
credential,
|
|
61
|
+
authSchemes: [
|
|
62
|
+
{
|
|
63
|
+
kind: "apiKey",
|
|
64
|
+
apiKeyLocation: "header",
|
|
65
|
+
name: "X-API-KEY",
|
|
66
|
+
},
|
|
67
|
+
],
|
|
63
68
|
});
|
|
64
69
|
}
|
|
65
70
|
```
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Handles oauth2 authentication scheme
|
|
2
|
+
|
|
3
|
+
This test validates that the emitter can handle a basic authentication scheme correctly, including the client signature and its initialization
|
|
4
|
+
|
|
5
|
+
## Typespec
|
|
6
|
+
|
|
7
|
+
The spec contains a simple service with a Bearer authentication scheme
|
|
8
|
+
|
|
9
|
+
```tsp
|
|
10
|
+
@service(#{ title: "Test Service" })
|
|
11
|
+
@useAuth(
|
|
12
|
+
OAuth2Auth<
|
|
13
|
+
[
|
|
14
|
+
{
|
|
15
|
+
type: OAuth2FlowType.clientCredentials,
|
|
16
|
+
tokenUrl: "https://api.example.com/oauth2/authorize",
|
|
17
|
+
refreshUrl: "https://api.example.com/oauth2/refresh",
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
["read"]
|
|
21
|
+
>
|
|
22
|
+
)
|
|
23
|
+
namespace Test;
|
|
24
|
+
|
|
25
|
+
@route("/valid")
|
|
26
|
+
@get
|
|
27
|
+
op valid(): NoContentResponse;
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## TypeScript
|
|
31
|
+
|
|
32
|
+
### Client
|
|
33
|
+
|
|
34
|
+
The client signature should include a positional parameter for credential of type KeyCredential. A basic auth token is a key credential that gets put into the Authorization header
|
|
35
|
+
|
|
36
|
+
```ts src/testClient.ts class TestClient
|
|
37
|
+
export class TestClient {
|
|
38
|
+
#context: TestClientContext;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
endpoint: string,
|
|
42
|
+
credential: OAuth2TokenCredential<ClientCredentialsFlow>,
|
|
43
|
+
options?: TestClientOptions,
|
|
44
|
+
) {
|
|
45
|
+
this.#context = createTestClientContext(endpoint, credential, options);
|
|
46
|
+
}
|
|
47
|
+
async valid(options?: ValidOptions) {
|
|
48
|
+
return valid(this.#context, options);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### ClientContext
|
|
54
|
+
|
|
55
|
+
The client context should setup the pipeline to use the credential in the Authorization header.
|
|
56
|
+
|
|
57
|
+
```ts src/api/testClientContext.ts function createTestClientContext
|
|
58
|
+
export function createTestClientContext(
|
|
59
|
+
endpoint: string,
|
|
60
|
+
credential: OAuth2TokenCredential<ClientCredentialsFlow>,
|
|
61
|
+
options?: TestClientOptions,
|
|
62
|
+
): TestClientContext {
|
|
63
|
+
const params: Record<string, any> = {
|
|
64
|
+
endpoint: endpoint,
|
|
65
|
+
};
|
|
66
|
+
const resolvedEndpoint = "{endpoint}".replace(/{([^}]+)}/g, (_, key) =>
|
|
67
|
+
key in params
|
|
68
|
+
? String(params[key])
|
|
69
|
+
: (() => {
|
|
70
|
+
throw new Error(`Missing parameter: ${key}`);
|
|
71
|
+
})(),
|
|
72
|
+
);
|
|
73
|
+
return getClient(resolvedEndpoint, {
|
|
74
|
+
...options,
|
|
75
|
+
credential,
|
|
76
|
+
authSchemes: [
|
|
77
|
+
{
|
|
78
|
+
kind: "oauth2",
|
|
79
|
+
flows: [
|
|
80
|
+
{
|
|
81
|
+
kind: "clientCredentials",
|
|
82
|
+
tokenUrl: "https://api.example.com/oauth2/authorize",
|
|
83
|
+
refreshUrl: "https://api.example.com/oauth2/refresh",
|
|
84
|
+
scopes: ["read"],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
```
|
|
@@ -35,7 +35,7 @@ The subclient is not a child of the TestClient because they have different param
|
|
|
35
35
|
export class TestClient {
|
|
36
36
|
#context: TestClientContext;
|
|
37
37
|
|
|
38
|
-
constructor(endpoint: string, credential:
|
|
38
|
+
constructor(endpoint: string, credential: BasicCredential, options?: TestClientOptions) {
|
|
39
39
|
this.#context = createTestClientContext(endpoint, credential, options);
|
|
40
40
|
}
|
|
41
41
|
async valid(options?: ValidOptions) {
|
|
@@ -66,7 +66,7 @@ The client context should setup the pipeline to use the credential in the Author
|
|
|
66
66
|
```ts src/api/testClientContext.ts function createTestClientContext
|
|
67
67
|
export function createTestClientContext(
|
|
68
68
|
endpoint: string,
|
|
69
|
-
credential:
|
|
69
|
+
credential: BasicCredential,
|
|
70
70
|
options?: TestClientOptions,
|
|
71
71
|
): TestClientContext {
|
|
72
72
|
const params: Record<string, any> = {
|
|
@@ -79,11 +79,15 @@ export function createTestClientContext(
|
|
|
79
79
|
throw new Error(`Missing parameter: ${key}`);
|
|
80
80
|
})(),
|
|
81
81
|
);
|
|
82
|
-
return getClient(resolvedEndpoint,
|
|
82
|
+
return getClient(resolvedEndpoint, {
|
|
83
83
|
...options,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
credential,
|
|
85
|
+
authSchemes: [
|
|
86
|
+
{
|
|
87
|
+
kind: "http",
|
|
88
|
+
scheme: "basic",
|
|
89
|
+
},
|
|
90
|
+
],
|
|
87
91
|
});
|
|
88
92
|
}
|
|
89
93
|
```
|
|
@@ -105,7 +109,7 @@ export function createSubClientContext(
|
|
|
105
109
|
throw new Error(`Missing parameter: ${key}`);
|
|
106
110
|
})(),
|
|
107
111
|
);
|
|
108
|
-
return getClient(resolvedEndpoint,
|
|
112
|
+
return getClient(resolvedEndpoint, {
|
|
109
113
|
...options,
|
|
110
114
|
});
|
|
111
115
|
}
|