forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
163 lines (151 loc) · 5.02 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Operation, Program } from "@typespec/compiler";
import { deepClone, deepEquals } from "@typespec/compiler/utils";
import { getAuthentication } from "./decorators.js";
import {
Authentication,
AuthenticationOptionReference,
AuthenticationReference,
HttpAuth,
HttpAuthRef,
HttpService,
HttpServiceAuthentication,
OAuth2Flow,
OAuth2Scope,
Oauth2Auth,
} from "./types.js";
/**
* Resolve the authentication for a given operation.
* @param program Program
* @param operation Operation
* @returns Authentication provided on the operation or containing interface or namespace.
*/
export function getAuthenticationForOperation(
program: Program,
operation: Operation
): Authentication | undefined {
const operationAuth = getAuthentication(program, operation);
if (operationAuth) {
return operationAuth;
}
if (operation.interface !== undefined) {
const interfaceAuth = getAuthentication(program, operation.interface);
if (interfaceAuth) {
return interfaceAuth;
}
}
let namespace = operation.namespace;
while (namespace) {
const namespaceAuth = getAuthentication(program, namespace);
if (namespaceAuth) {
return namespaceAuth;
}
namespace = namespace.namespace;
}
return undefined;
}
/**
* Compute the authentication for a given service.
* @param service Http Service
* @returns The normalized authentication for a service.
*/
export function resolveAuthentication(service: HttpService): HttpServiceAuthentication {
let schemes: Record<string, HttpAuth> = {};
let defaultAuth: AuthenticationReference = { options: [] };
const operationsAuth: Map<Operation, AuthenticationReference> = new Map();
if (service.authentication) {
const { newServiceSchemes, authOptions } = gatherAuth(service.authentication, {});
schemes = newServiceSchemes;
defaultAuth = authOptions;
}
for (const op of service.operations) {
if (op.authentication) {
const { newServiceSchemes, authOptions } = gatherAuth(op.authentication, schemes);
schemes = newServiceSchemes;
operationsAuth.set(op.operation, authOptions);
}
}
return { schemes: Object.values(schemes), defaultAuth, operationsAuth };
}
function gatherAuth(
authentication: Authentication,
serviceSchemes: Record<string, HttpAuth>
): {
newServiceSchemes: Record<string, HttpAuth>;
authOptions: AuthenticationReference;
} {
const newServiceSchemes: Record<string, HttpAuth> = serviceSchemes;
const authOptions: AuthenticationReference = { options: [] };
for (const option of authentication.options) {
const authOption: AuthenticationOptionReference = { all: [] };
for (const optionScheme of option.schemes) {
const serviceScheme = serviceSchemes[optionScheme.id];
let newServiceScheme = optionScheme;
if (serviceScheme) {
// If we've seen a different scheme by this id,
// Make sure to not overwrite it
if (!authsAreEqual(serviceScheme, optionScheme)) {
while (serviceSchemes[newServiceScheme.id]) {
newServiceScheme.id = newServiceScheme.id + "_";
}
}
// Merging scopes when encountering the same Oauth2 scheme
else if (serviceScheme.type === "oauth2" && optionScheme.type === "oauth2") {
const x = mergeOAuthScopes(serviceScheme, optionScheme);
newServiceScheme = x;
}
}
const httpAuthRef = makeHttpAuthRef(optionScheme, newServiceScheme);
newServiceSchemes[newServiceScheme.id] = newServiceScheme;
authOption.all.push(httpAuthRef);
}
authOptions.options.push(authOption);
}
return { newServiceSchemes, authOptions };
}
function makeHttpAuthRef(local: HttpAuth, reference: HttpAuth): HttpAuthRef {
if (reference.type === "oauth2" && local.type === "oauth2") {
const scopes: string[] = [];
for (const flow of local.flows) {
scopes.push(...flow.scopes.map((x) => x.value));
}
return { kind: "oauth2", auth: reference, scopes: scopes };
} else if (reference.type === "noAuth") {
return { kind: "noAuth", auth: reference };
} else {
return { kind: "any", auth: reference };
}
}
function mergeOAuthScopes<Flows extends OAuth2Flow[]>(
scheme1: Oauth2Auth<Flows>,
scheme2: Oauth2Auth<Flows>
): Oauth2Auth<Flows> {
const flows = deepClone(scheme1.flows);
flows.forEach((flow1, i) => {
const flow2 = scheme2.flows[i];
const scopes = Array.from(new Set(flow1.scopes.concat(flow2.scopes)));
flows[i].scopes = scopes;
});
return {
...scheme1,
flows,
};
}
function setOauth2Scopes<Flows extends OAuth2Flow[]>(
scheme: Oauth2Auth<Flows>,
scopes: OAuth2Scope[]
): Oauth2Auth<Flows> {
const flows: Flows = deepClone(scheme.flows);
flows.forEach((flow) => {
flow.scopes = scopes;
});
return {
...scheme,
flows,
};
}
function authsAreEqual(scheme1: HttpAuth, scheme2: HttpAuth): boolean {
if (scheme1.type === "oauth2" && scheme2.type === "oauth2") {
return deepEquals(setOauth2Scopes(scheme1, []), setOauth2Scopes(scheme2, []));
}
return deepEquals(scheme1, scheme2);
}