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

FUTR-67: Setup route handler body for oauth connect. #44

Merged
merged 1 commit into from
Aug 14, 2024
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
33 changes: 14 additions & 19 deletions packages/admin/src/app/integrations/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
'use server'
'use server';

import { DataIntegrationCredential } from 'core'
import { future } from "../../../../example.future.config";
import { DataIntegrationCredential } from 'core';

export async function connectIntegration({ name, credential, connectionId }: {
name: string,
connectionId: string
credential: DataIntegrationCredential
}) {
const authenticator = future.authenticator(name);

await future.connectPlugin({ name, connectionId, authenticator, credential });
}
import { future } from '../../../../example.future.config';

export async function buildRedirectUri({ name, connectionId, clientRedirectPath }: {
name: string,
connectionId: string
clientRedirectPath: string
export async function connectIntegration({
name,
credential,
connectionId,
}: {
name: string;
connectionId: string;
credential: DataIntegrationCredential;
}) {
const authenticator = future.authenticator(name);
const authenticator = future.authenticator(name);

return await authenticator.getRedirectUri(clientRedirectPath);
}
await future.connectPlugin({ name, connectionId, authenticator, credential });
}
11 changes: 8 additions & 3 deletions packages/admin/src/app/integrations/client-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { useCallback, useState } from 'react';

import { Button } from '@/components/ui/button';

import { buildRedirectUri } from './actions';

type IntegrationRowProps = {
name: string;
};
Expand All @@ -17,7 +15,14 @@ export function IntegrationRow({ name }: IntegrationRowProps) {
const handleConnect = useCallback(async () => {
setIsConnecting(true);
try {
window.location.assign(await buildRedirectUri({ name, connectionId: '1', clientRedirectPath: 'import-data' }));
const path = '/api/integrations/connect';
const params = new URLSearchParams({
name,
connectionId: '1',
clientRedirectPath: 'import-data',
});

window.location.assign(`${path}?${params.toString()}`);
} catch (err) {
console.error(err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"analyze": "size-limit --why",
"build": "dts build && mkdir ./dist/prisma && cp ./src/prisma ./dist",
"build": "dts build && mkdir ./dist/prisma && cp ./src/prisma/* ./dist/prisma",
"build:dev": "dts watch",
"lint": "dts lint",
"prepare": "dts build",
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/authenticator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { z } from 'zod';
import { OAuth2Client } from '@badgateway/oauth2-client';
import { DataIntegration } from '@prisma/client';
import { DataLayer } from './data-access';
Expand All @@ -10,6 +11,8 @@ import {
AuthToken,
} from './types';

type OAuthState = z.infer<typeof oauthState>;

type OAuthConfig = {
SERVER: string;
CLIENT_ID: string;
Expand Down Expand Up @@ -101,7 +104,9 @@ export class IntegrationAuth {
return this.config.SCOPES;
}

async getRedirectUri(clientRedirectPath?: string) {
async getRedirectUri(
state: Pick<OAuthState, 'connectionId' | 'clientRedirectPath'>
) {
if (this.config.AUTH_TYPE === IntegrationCredentialType.API_KEY) {
throw new Error(
'Plugins using API Key authentication do not use redirect URIs'
Expand All @@ -116,9 +121,7 @@ export class IntegrationAuth {
return await client.authorizationCode.getAuthorizeUri({
redirectUri,
scope: scopes,
state: Buffer.from(JSON.stringify({ name, clientRedirectPath })).toString(
'base64'
),
state: Buffer.from(JSON.stringify({ name, ...state })).toString('base64'),
extraParams: this.config.EXTRA_AUTH_PARAMS ?? {},
});
}
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ export class IntegrationFramework {
actions: IntegrationAction[];
pluginName?: string;
}) {
console.log('registering actions', { actions, pluginName });

const pluginActions = this.globalActions.get(pluginName) || {};

this.globalActions.set(pluginName, {
Expand All @@ -128,8 +126,6 @@ export class IntegrationFramework {
{}
),
});

console.log('registered actions', { actt: this.globalActions });
}

registerRoutes() {}
Expand Down
26 changes: 24 additions & 2 deletions packages/core/src/next/connect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { NextRequest, NextResponse } from 'next/server';
import { IntegrationFramework } from '../index';
import { connectParams } from '../schemas';

export const makeConnect = (framework: IntegrationFramework) => {
return (req: NextRequest) => {
return NextResponse.json({ hello: 'from connect' });
return async (req: NextRequest) => {
const { data, success, error } = connectParams.safeParse(
Array.from(new URLSearchParams(req.nextUrl.search).entries()).reduce(
(acc, [key, value]) => ({
...acc,
[key]: value,
}),
{} as Record<string, string>
)
);

if (!success) {
return NextResponse.json({ error, status: 400 });
}

const { name, connectionId } = data;
const plugin = framework.getPlugin(name)!;
const authenticator = plugin.getAuthenticator();
const redirectUri = await authenticator.getRedirectUri({
connectionId,
});

return NextResponse.redirect(redirectUri);
};
};
7 changes: 5 additions & 2 deletions packages/core/src/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { z } from 'zod';

export const oauthState = z.object({
export const connectParams = z.object({
name: z.string(),
connectionId: z.string(),
clientRedirectPath: z.string(),
clientRedirectPath: z.string().optional(),
});

export const oauthState = connectParams.extend({
previewRedirect: z.string().optional(),
});