Skip to content

Commit 7134d50

Browse files
committed
Add connectTokenExternal route
1 parent eb7bdc6 commit 7134d50

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { NextRequest } from 'next/server';
2+
import { getCorbadoConnectTokenExternal, verifyAmplifyTokenExternal } from '@/lib/utils';
3+
4+
type Payload = {
5+
idToken: string;
6+
connectTokenType: string;
7+
};
8+
9+
export async function POST(req: NextRequest) {
10+
const body = (await req.json()) as Payload;
11+
12+
const { idToken, connectTokenType } = body;
13+
14+
try {
15+
const { displayName, identifier } = await verifyAmplifyTokenExternal(idToken);
16+
17+
const connectToken = await getCorbadoConnectTokenExternal(connectTokenType, {
18+
displayName: displayName,
19+
identifier: identifier,
20+
});
21+
22+
return new Response(JSON.stringify({ token: connectToken }), {
23+
status: 201,
24+
headers: { 'Content-Type': 'application/json' },
25+
});
26+
} catch (e) {
27+
console.error('Error verifying token or getting connect token', e);
28+
29+
return new Response(JSON.stringify({ error: 'Failed to verify token or get connect token' }), {
30+
status: 500,
31+
headers: { 'Content-Type': 'application/json' },
32+
});
33+
}
34+
}

playground/connect-next/lib/utils.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ export const verifyAmplifyToken = async (idToken: string): Promise<TokenData> =>
2626
return { displayName, identifier };
2727
};
2828

29+
export const verifyAmplifyTokenExternal = async (idToken: string): Promise<TokenData> => {
30+
const verifier = CognitoJwtVerifier.create({
31+
userPoolId: process.env.AWS_COGNITO_USER_POOL_ID_EXTERNAL!,
32+
tokenUse: 'id',
33+
clientId: process.env.AWS_COGNITO_CLIENT_ID_EXTERNAL!,
34+
});
35+
36+
console.log(
37+
'verifying token with external verifier',
38+
idToken,
39+
process.env.AWS_COGNITO_USER_POOL_ID_EXTERNAL,
40+
process.env.AWS_COGNITO_CLIENT_ID_EXTERNAL,
41+
);
42+
43+
const verifiedToken = await verifier.verify(idToken);
44+
const displayName: string = verifiedToken.email as string;
45+
const identifier = verifiedToken['cognito:username'];
46+
47+
return { displayName, identifier };
48+
};
49+
2950
export type CognitoUserInfo = {
3051
username: string;
3152
email: string;
@@ -68,3 +89,30 @@ export const getCorbadoConnectToken = async (connectTokenType: string, connectTo
6889

6990
return out.secret;
7091
};
92+
93+
export const getCorbadoConnectTokenExternal = async (
94+
connectTokenType: string,
95+
connectTokenData: any,
96+
): Promise<string> => {
97+
const payload = {
98+
type: connectTokenType,
99+
data: connectTokenData,
100+
};
101+
102+
const body = JSON.stringify(payload);
103+
104+
const url = `${process.env.CORBADO_BACKEND_API_URL_EXTERNAL}/v2/connectTokens`;
105+
const response = await fetch(url, {
106+
method: 'POST',
107+
headers: {
108+
Authorization: `Basic ${process.env.CORBADO_BACKEND_API_BASIC_AUTH_EXTERNAL}`,
109+
'Content-Type': 'application/json',
110+
},
111+
cache: 'no-cache',
112+
body: body,
113+
});
114+
115+
const out = await response.json();
116+
117+
return out.secret;
118+
};

0 commit comments

Comments
 (0)