Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply openapi3 extension on Security schemes #3732

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,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/openapi3"
---

Apply openapi3 extension on Security schemes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/http"
---

Expose `model` property on `HttpAuth` to retrieve original type used to define security scheme
18 changes: 9 additions & 9 deletions packages/http/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
HttpService,
HttpServiceAuthentication,
OAuth2Flow,
OAuth2Scope,
Oauth2Auth,
} from "./types.js";

Expand Down Expand Up @@ -141,13 +140,12 @@ function mergeOAuthScopes<Flows extends OAuth2Flow[]>(
};
}

function setOauth2Scopes<Flows extends OAuth2Flow[]>(
scheme: Oauth2Auth<Flows>,
scopes: OAuth2Scope[]
): Oauth2Auth<Flows> {
function ignoreScopes<Flows extends OAuth2Flow[]>(
scheme: Omit<Oauth2Auth<Flows>, "model">
): Omit<Oauth2Auth<Flows>, "model"> {
const flows: Flows = deepClone(scheme.flows);
flows.forEach((flow) => {
flow.scopes = scopes;
flow.scopes = [];
});
return {
...scheme,
Expand All @@ -156,8 +154,10 @@ function setOauth2Scopes<Flows extends OAuth2Flow[]>(
}

function authsAreEqual(scheme1: HttpAuth, scheme2: HttpAuth): boolean {
if (scheme1.type === "oauth2" && scheme2.type === "oauth2") {
return deepEquals(setOauth2Scopes(scheme1, []), setOauth2Scopes(scheme2, []));
const { model: _model1, ...withoutModel1 } = scheme1;
const { model: _model2, ...withoutModel2 } = scheme2;
if (withoutModel1.type === "oauth2" && withoutModel2.type === "oauth2") {
return deepEquals(ignoreScopes(withoutModel1), ignoreScopes(withoutModel2));
}
return deepEquals(scheme1, scheme2);
return deepEquals(withoutModel1, withoutModel2);
}
8 changes: 6 additions & 2 deletions packages/http/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,10 @@ function extractHttpAuthentication(
return [result, diagnostics];
}
const description = getDoc(program, modelType);
const auth = result.type === "oauth2" ? extractOAuth2Auth(result) : result;
const auth =
result.type === "oauth2"
? extractOAuth2Auth(modelType, result)
: { ...result, model: modelType };
return [
{
...auth,
Expand All @@ -595,7 +598,7 @@ function extractHttpAuthentication(
];
}

function extractOAuth2Auth(data: any): HttpAuth {
function extractOAuth2Auth(modelType: Model, data: any): HttpAuth {
// Validation of OAuth2Flow models in this function is minimal because the
// type system already validates whether the model represents a flow
// configuration. This code merely avoids runtime errors.
Expand All @@ -608,6 +611,7 @@ function extractOAuth2Auth(data: any): HttpAuth {
return {
id: data.id,
type: data.type,
model: modelType,
flows: flows.map((flow: any) => {
const scopes: Array<string> = flow.scopes ? flow.scopes : defaultScopes;
return {
Expand Down
3 changes: 3 additions & 0 deletions packages/http/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export interface HttpAuthBase {
* Optional description.
*/
description?: string;

/** Model that defined the authentication */
readonly model: Model;
}

/**
Expand Down
121 changes: 97 additions & 24 deletions packages/http/test/http-decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
expectDiagnostics,
} from "@typespec/compiler/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { beforeEach, describe, expect, it } from "vitest";
import {
getAuthentication,
getHeaderFieldName,
Expand Down Expand Up @@ -726,14 +726,15 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
{
id: "BasicAuth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
Expand All @@ -749,11 +750,17 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
{ id: "MyAuth", description: "My custom basic auth", type: "http", scheme: "basic" },
{
id: "MyAuth",
description: "My custom basic auth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
],
Expand All @@ -766,14 +773,15 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
{
id: "BearerAuth",
type: "http",
scheme: "bearer",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
Expand All @@ -787,7 +795,7 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
Expand All @@ -796,6 +804,7 @@ describe("http: decorators", () => {
type: "apiKey",
in: "header",
name: "x-my-header",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
Expand All @@ -815,7 +824,7 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
Expand All @@ -830,6 +839,7 @@ describe("http: decorators", () => {
scopes: [{ value: "read" }, { value: "write" }],
},
],
model: expect.objectContaining({ kind: "Model" }),
},
],
},
Expand All @@ -849,7 +859,7 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
Expand All @@ -864,6 +874,7 @@ describe("http: decorators", () => {
scopes: [{ value: "read" }, { value: "write" }],
},
],
model: expect.objectContaining({ kind: "Model" }),
},
],
},
Expand All @@ -877,13 +888,14 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
{
id: "NoAuth",
type: "noAuth",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
Expand All @@ -897,13 +909,27 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [{ id: "BasicAuth", type: "http", scheme: "basic" }],
schemes: [
{
id: "BasicAuth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
{
schemes: [{ id: "BearerAuth", type: "http", scheme: "bearer" }],
schemes: [
{
id: "BearerAuth",
type: "http",
scheme: "bearer",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
],
});
Expand All @@ -915,12 +941,22 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [
{ id: "BasicAuth", type: "http", scheme: "basic" },
{ id: "BearerAuth", type: "http", scheme: "bearer" },
{
id: "BasicAuth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
{
id: "BearerAuth",
type: "http",
scheme: "bearer",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
],
Expand All @@ -933,10 +969,17 @@ describe("http: decorators", () => {
@test namespace Foo {}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo), {
expect(getAuthentication(runner.program, Foo)).toEqual({
options: [
{
schemes: [{ id: "BearerAuth", type: "http", scheme: "bearer" }],
schemes: [
{
id: "BearerAuth",
type: "http",
scheme: "bearer",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
{
schemes: [
Expand All @@ -945,8 +988,14 @@ describe("http: decorators", () => {
type: "apiKey",
in: "header",
name: "x-my-header",
model: expect.objectContaining({ kind: "Model" }),
},
{
id: "BasicAuth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
{ id: "BasicAuth", type: "http", scheme: "basic" },
],
},
],
Expand All @@ -963,13 +1012,27 @@ describe("http: decorators", () => {
}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo.interfaces.get("Bar")!), {
expect(getAuthentication(runner.program, Foo.interfaces.get("Bar")!)).toEqual({
options: [
{
schemes: [{ id: "BasicAuth", type: "http", scheme: "basic" }],
schemes: [
{
id: "BasicAuth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
{
schemes: [{ id: "BearerAuth", type: "http", scheme: "bearer" }],
schemes: [
{
id: "BearerAuth",
type: "http",
scheme: "bearer",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
],
});
Expand All @@ -985,12 +1048,22 @@ describe("http: decorators", () => {
}
`)) as { Foo: Namespace };

deepStrictEqual(getAuthentication(runner.program, Foo.operations.get("bar")!), {
expect(getAuthentication(runner.program, Foo.operations.get("bar")!)).toEqual({
options: [
{
schemes: [
{ id: "BasicAuth", type: "http", scheme: "basic" },
{ id: "BearerAuth", type: "http", scheme: "bearer" },
{
id: "BasicAuth",
type: "http",
scheme: "basic",
model: expect.objectContaining({ kind: "Model" }),
},
{
id: "BearerAuth",
type: "http",
scheme: "bearer",
model: expect.objectContaining({ kind: "Model" }),
},
],
},
],
Expand Down
8 changes: 8 additions & 0 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,14 @@ function createOAPIEmitter(
}

function getOpenAPI3Scheme(auth: HttpAuth): OpenAPI3SecurityScheme | undefined {
const scheme = getOpenAPI3SchemeInternal(auth);

if (scheme) {
attachExtensions(program, auth.model, scheme);
}
return scheme;
}
function getOpenAPI3SchemeInternal(auth: HttpAuth): OpenAPI3SecurityScheme | undefined {
switch (auth.type) {
case "http":
return { type: "http", scheme: auth.scheme, description: auth.description };
Expand Down
Loading
Loading