Skip to content

Commit

Permalink
fix: api resolver should respect authed=false (with tests) (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored May 29, 2024
1 parent a2968d6 commit 1d2715f
Show file tree
Hide file tree
Showing 4 changed files with 587 additions and 6 deletions.
9 changes: 3 additions & 6 deletions packages/ui/app/src/resolver/ApiDefinitionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,24 +429,21 @@ export class ApiDefinitionResolver {
return { auth, headers };
}

const filteredHeaders: ResolvedObjectProperty[] = [];

for (const header of headers) {
if (
header.key.toLowerCase() === "authorization" ||
header.key.toLowerCase().includes("api-key") ||
header.key.toLowerCase().includes("apikey")
) {
auth = {
const auth: APIV1Read.ApiAuth = {
type: "header",
headerWireValue: header.key,
};
continue;
return { auth, headers: headers.filter((h) => h.key !== header.key) };
}
filteredHeaders.push(header);
}

return { auth, headers: filteredHeaders };
return { auth: undefined, headers };
}

async resolveWebsocketChannel(websocket: FlattenedWebSocketChannel): Promise<ResolvedWebSocketChannel> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "fs";
import path from "path";
import { DEFAULT_FEATURE_FLAGS } from "../../contexts/FeatureFlagContext";
import { ApiDefinitionResolver } from "../ApiDefinitionResolver";
import { ResolvedEndpointDefinition } from "../types";

describe("resolveApiDefinition", () => {
it("should finish resolving", async () => {
Expand All @@ -22,4 +23,27 @@ describe("resolveApiDefinition", () => {
);
expect(resolved).toMatchSnapshot();
});

it("should resolve authed and unauthed endpoints", async () => {
const fixturePath = path.join(__dirname, "fixtures/authed.json");
const content = fs.readFileSync(fixturePath, "utf-8");

const fixture = JSON.parse(content) as APIV1Read.ApiDefinition;
const flattened = flattenApiDefinition(fixture, [], undefined, "documentation.sayari.com");
const resolved = await ApiDefinitionResolver.resolve(
"API Reference",
flattened,
{},
undefined,
DEFAULT_FEATURE_FLAGS,
"documentation.sayari.com",
);
expect(resolved).toMatchSnapshot();
expect((resolved.items[0] as ResolvedEndpointDefinition).auth).toBeUndefined();
expect((resolved.items[1] as ResolvedEndpointDefinition).auth?.type).toBe("bearerAuth");
expect((resolved.items[2] as ResolvedEndpointDefinition).auth).toEqual({
type: "header",
headerWireValue: "test-api-key",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,312 @@ exports[`resolveApiDefinition > should finish resolving 1`] = `
},
}
`;

exports[`resolveApiDefinition > should resolve authed and unauthed endpoints 1`] = `
{
"api": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"auth": {
"tokenName": "token",
"type": "bearerAuth",
},
"items": [
{
"apiPackageId": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"apiSectionId": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"auth": undefined,
"availability": undefined,
"defaultEnvironment": {
"baseUrl": "https://api.sayari.com",
"id": "Production",
},
"description": "Hit the auth endpoint to get a bearer token",
"environments": [
{
"baseUrl": "https://api.sayari.com",
"id": "Production",
},
],
"errors": [],
"examples": [],
"headers": [],
"id": "getToken",
"method": "POST",
"path": [
{
"type": "literal",
"value": "",
},
{
"type": "literal",
"value": "/oauth/token",
},
],
"pathParameters": [],
"queryParameters": [],
"requestBody": {
"availability": undefined,
"contentType": "application/json",
"description": undefined,
"shape": {
"availability": undefined,
"description": undefined,
"extends": [],
"name": undefined,
"properties": [
{
"availability": undefined,
"description": undefined,
"hidden": false,
"key": "client_id",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "primitive",
"value": {
"type": "string",
},
},
},
{
"availability": undefined,
"description": undefined,
"hidden": false,
"key": "client_secret",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "primitive",
"value": {
"type": "string",
},
},
},
{
"availability": undefined,
"description": undefined,
"hidden": false,
"key": "audience",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "literal",
"value": {
"type": "stringLiteral",
"value": "sayari.com",
},
},
},
{
"availability": undefined,
"description": undefined,
"hidden": false,
"key": "grant_type",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "literal",
"value": {
"type": "stringLiteral",
"value": "client_credentials",
},
},
},
],
"type": "object",
},
},
"responseBody": {
"availability": undefined,
"description": undefined,
"shape": {
"type": "reference",
"typeId": "type_auth:AuthResponse",
},
"statusCode": 200,
},
"slug": [
"get-token",
],
"snippetTemplates": undefined,
"stream": undefined,
"title": "Get Token",
"type": "endpoint",
},
{
"apiPackageId": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"apiSectionId": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"auth": {
"tokenName": "token",
"type": "bearerAuth",
},
"availability": undefined,
"defaultEnvironment": {
"baseUrl": "https://api.sayari.com",
"id": "Production",
},
"description": undefined,
"environments": [
{
"baseUrl": "https://api.sayari.com",
"id": "Production",
},
],
"errors": [],
"examples": [],
"headers": [],
"id": "testAuth",
"method": "POST",
"path": [
{
"type": "literal",
"value": "/test/authed",
},
],
"pathParameters": [],
"queryParameters": [],
"requestBody": {
"availability": undefined,
"contentType": "text/plain",
"description": undefined,
"shape": {
"availability": undefined,
"description": undefined,
"type": "unknown",
},
},
"responseBody": {
"availability": undefined,
"description": undefined,
"shape": {
"availability": undefined,
"description": undefined,
"type": "unknown",
},
"statusCode": 200,
},
"slug": [
"test-authed",
],
"snippetTemplates": undefined,
"stream": undefined,
"title": "Get Token",
"type": "endpoint",
},
{
"apiPackageId": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"apiSectionId": "054ca3d6-33a5-4d53-a813-f2a33e4062cc",
"auth": {
"headerWireValue": "test-api-key",
"type": "header",
},
"availability": undefined,
"defaultEnvironment": {
"baseUrl": "https://api.sayari.com",
"id": "Production",
},
"description": undefined,
"environments": [
{
"baseUrl": "https://api.sayari.com",
"id": "Production",
},
],
"errors": [],
"examples": [],
"headers": [],
"id": "testHeaderAuth",
"method": "POST",
"path": [
{
"type": "literal",
"value": "/test/authed",
},
],
"pathParameters": [],
"queryParameters": [],
"requestBody": {
"availability": undefined,
"contentType": "text/plain",
"description": undefined,
"shape": {
"availability": undefined,
"description": undefined,
"type": "unknown",
},
},
"responseBody": {
"availability": undefined,
"description": undefined,
"shape": {
"availability": undefined,
"description": undefined,
"type": "unknown",
},
"statusCode": 200,
},
"slug": [
"test-header-auth",
],
"snippetTemplates": undefined,
"stream": undefined,
"title": "Get Token",
"type": "endpoint",
},
],
"slug": [],
"type": "rootPackage",
"types": {
"type_auth:AuthResponse": {
"availability": undefined,
"description": undefined,
"extends": [],
"name": "AuthResponse",
"properties": [
{
"availability": undefined,
"description": "The bearer token you will pass in to subsequent API calls to authenticate.",
"hidden": false,
"key": "access_token",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "primitive",
"value": {
"type": "string",
},
},
},
{
"availability": undefined,
"description": "Tells you how long (in seconds) until your bearer token expires.",
"hidden": false,
"key": "expires_in",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "primitive",
"value": {
"type": "integer",
},
},
},
{
"availability": undefined,
"description": "Will always be "Bearer"",
"hidden": false,
"key": "token_type",
"valueShape": {
"availability": undefined,
"description": undefined,
"type": "primitive",
"value": {
"type": "string",
},
},
},
],
"type": "object",
},
},
}
`;
Loading

0 comments on commit 1d2715f

Please sign in to comment.