Skip to content

Commit

Permalink
feat(api): unauthenticated standard endpoint (#2735)
Browse files Browse the repository at this point in the history
## Describe your changes

- Move `POST /unauth` to a dedicated file
- Renamed `POST /unauth` to `POST /auth/unauthenticated` following the
first endpoint that was created using the new format
Deprecated old path (but not removed)

- Fixed a lot of discrepancy with the previously created auth endpoint
File naming, type naming, type vs zod, etc.

- Sync type with frontend
It's the first I'm updating this file and it seems most of the types are
incorrect or outdated or copy pasted. I'll probably do a big cleanup
here soon


## 🧪Tests
You can test everything the main dashboard, by creating a new
unauthenticated connection.
  • Loading branch information
bodinsamuel authored Sep 17, 2024
1 parent 4077925 commit a60777b
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 369 deletions.
156 changes: 4 additions & 152 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"path": "../shared"
},
{
"path": "../nango-yaml"
"path": "../types"
},
{
"path": "../nango-yaml"
}
],
"include": [
"lib/**/*"
]
"include": ["lib/**/*"]
}
9 changes: 5 additions & 4 deletions packages/frontend/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2023 Nango, all rights reserved.
*/
import type { PostPublicUnauthenticatedAuthorization } from '@nangohq/types';

const prodHost = 'https://api.nango.dev';
const debugLogPrefix = 'NANGO DEBUG LOG: ';
Expand Down Expand Up @@ -86,7 +87,7 @@ export default class Nango {
* @returns A promise that resolves with the authentication result
*/
public async create(providerConfigKey: string, connectionId: string, connectionConfig?: ConnectionConfig): Promise<AuthResult> {
const url = this.hostBaseUrl + `/unauth/${providerConfigKey}${this.toQueryString(connectionId, connectionConfig)}`;
const url = this.hostBaseUrl + `/auth/unauthenticated/${providerConfigKey}${this.toQueryString(connectionId, connectionConfig)}`;

const res = await fetch(url, {
method: 'POST',
Expand All @@ -96,11 +97,11 @@ export default class Nango {
});

if (!res.ok) {
const errorResponse = await res.json();
throw new AuthError(errorResponse.error.message, errorResponse.error.code);
const errorResponse = (await res.json()) as PostPublicUnauthenticatedAuthorization['Errors'];
throw new AuthError(errorResponse.error.message || errorResponse.error.code, errorResponse.error.code);
}

return res.json();
return (await res.json()) as PostPublicUnauthenticatedAuthorization['Success'];
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"license": "SEE LICENSE IN LICENSE FILE IN GIT REPOSITORY",
"scripts": {},
"dependencies": {},
"devDependencies": {
"@nangohq/types": "^0.42.10"
},
"files": [
"dist/**/*.js",
"dist/**/*.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions packages/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"rootDir": "lib",
"outDir": "dist"
},
"references": [
{
"path": "../types"
}
],
"include": ["lib/**/*"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
ErrorSourceEnum,
LogActionEnum
} from '@nangohq/shared';
import type { TableauAuthorization } from '@nangohq/types';
import type { PostPublicTableauAuthorization } from '@nangohq/types';
import type { LogContext } from '@nangohq/logs';
import { defaultOperationExpiration, logContextGetter } from '@nangohq/logs';
import { hmacCheck } from '../../utils/hmac.js';
import { connectionCreated as connectionCreatedHook, connectionCreationFailed as connectionCreationFailedHook } from '../../hooks/hooks.js';
import { providerConfigKeySchema } from '../../helpers/validation.js';
import { connectionIdSchema, providerConfigKeySchema } from '../../helpers/validation.js';

const bodyValidation = z
.object({
Expand All @@ -29,10 +29,8 @@ const bodyValidation = z

const queryStringValidation = z
.object({
connection_id: z.string().nonempty(),
connection_id: connectionIdSchema,
params: z.record(z.any()).optional(),
authorization_params: z.record(z.any()).optional(),
user_scope: z.string().optional(),
public_key: z.string().uuid(),
hmac: z.string().optional()
})
Expand All @@ -44,7 +42,7 @@ const paramValidation = z
})
.strict();

export const tableauAuthorization = asyncWrapper<TableauAuthorization>(async (req, res, next: NextFunction) => {
export const postPublicTableauAuthorization = asyncWrapper<PostPublicTableauAuthorization>(async (req, res, next: NextFunction) => {
const val = bodyValidation.safeParse(req.body);
if (!val.success) {
res.status(400).send({
Expand All @@ -70,10 +68,10 @@ export const tableauAuthorization = asyncWrapper<TableauAuthorization>(async (re
}

const { account, environment } = res.locals;
const { pat_name: patName, pat_secret: patSecret, content_url: contentUrl } = val.data;
const { connection_id: connectionId } = queryStringVal.data;
const { providerConfigKey } = paramVal.data;
const connectionConfig = req.query['params'] != null ? getConnectionConfig(req.query['params']) : {};
const { pat_name: patName, pat_secret: patSecret, content_url: contentUrl }: PostPublicTableauAuthorization['Body'] = val.data;
const { connection_id: connectionId, params, hmac }: PostPublicTableauAuthorization['Querystring'] = queryStringVal.data;
const { providerConfigKey }: PostPublicTableauAuthorization['Params'] = paramVal.data;
const connectionConfig = params ? getConnectionConfig(params) : {};

let logCtx: LogContext | undefined;

Expand All @@ -93,7 +91,7 @@ export const tableauAuthorization = asyncWrapper<TableauAuthorization>(async (re
logCtx,
providerConfigKey,
connectionId,
hmac: queryStringVal.data.hmac,
hmac,
res
});

Expand Down
Loading

0 comments on commit a60777b

Please sign in to comment.