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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/openapi3",
"comment": "Uptake changes to http service authentication oauth2 scopes",
"type": "minor"
}
],
"packageName": "@cadl-lang/openapi3"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/rest",
"comment": "Api: Service Authentication oauth2 flow scopes is now an object with value and description",
"type": "minor"
}
],
"packageName": "@cadl-lang/rest"
}
4 changes: 2 additions & 2 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1360,12 +1360,12 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt
const flows: OpenAPI3OAuthFlows = {};
const scopes: string[] = [];
for (const flow of auth.flows) {
scopes.push(...flow.scopes);
scopes.push(...flow.scopes.map((x) => x.value));
flows[flow.type] = {
authorizationUrl: (flow as any).authorizationUrl,
tokenUrl: (flow as any).tokenUrl,
refreshUrl: flow.refreshUrl,
scopes: Object.fromEntries(flow.scopes.map((x: string) => [x, ""])),
scopes: Object.fromEntries(flow.scopes.map((x) => [x.value, x.description ?? ""])),
};
}
return [{ type: "oauth2", flows, description: auth.description }, scopes];
Expand Down
15 changes: 14 additions & 1 deletion packages/rest/src/http/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,29 @@ function extractHttpAuthentication(
return [result, diagnostics];
}
const description = getDoc(program, modelType);
const auth = result.type === "oauth2" ? extractOAuth2Auth(result) : result;
return [
{
...result,
...auth,
id: modelType.name || result.type,
...(description && { description }),
},
diagnostics,
];
}

function extractOAuth2Auth(data: any): HttpAuth {
return {
...data,
flows: data.flows.map((flow: any) => {
return {
...flow,
scopes: flow.scopes.map((x: string) => ({ value: x })),
};
}),
};
}

export function getAuthentication(
program: Program,
namespace: Namespace
Expand Down
13 changes: 9 additions & 4 deletions packages/rest/src/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface AuthorizationCodeFlow {
authorizationUrl: string;
tokenUrl: string;
refreshUrl?: string;
scopes: string[];
scopes: OAuth2Scope[];
}

/**
Expand All @@ -122,7 +122,7 @@ export interface ImplicitFlow {
type: "implicit";
authorizationUrl: string;
refreshUrl?: string;
scopes: string[];
scopes: OAuth2Scope[];
}

/**
Expand All @@ -132,7 +132,7 @@ export interface PasswordFlow {
type: "password";
authorizationUrl: string;
refreshUrl?: string;
scopes: string[];
scopes: OAuth2Scope[];
}

/**
Expand All @@ -142,5 +142,10 @@ export interface ClientCredentialsFlow {
type: "clientCredentials";
tokenUrl: string;
refreshUrl?: string;
scopes: string[];
scopes: OAuth2Scope[];
}

export interface OAuth2Scope {
value: string;
description?: string;
}
2 changes: 1 addition & 1 deletion packages/rest/test/http-decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ describe("rest: http decorators", () => {
type: "implicit",
authorizationUrl: "https://api.example.com/oauth2/authorize",
refreshUrl: "https://api.example.com/oauth2/refresh",
scopes: ["read", "write"],
scopes: [{ value: "read" }, { value: "write" }],
},
],
},
Expand Down