Skip to content

Commit ef03bd0

Browse files
committed
Updated the logic of signing JWT into seperate file
1 parent a9f8156 commit ef03bd0

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/utils/authTokenGenerator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import jwt from "@tsndr/cloudflare-worker-jwt";
2+
3+
export async function generateDiscordAuthToken(
4+
name: string,
5+
expiry: number,
6+
privateKey: string,
7+
algorithm: string
8+
) {
9+
return await jwt.sign({ name: name, exp: expiry }, privateKey, {
10+
algorithm: algorithm,
11+
});
12+
}

src/utils/awsAccess.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ import { env } from "../typeDefinitions/default.types";
33
import config from "../../config/config";
44
import { discordTextResponse } from "./discordResponse";
55
import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls";
6+
import { generateDiscordAuthToken } from "./authTokenGenerator";
67

78
export async function processAWSAccessRequest(
89
discordUserId: string,
910
awsGroupId: string,
1011
env: env,
1112
channelId: number
1213
): Promise<void> {
13-
const authToken = await jwt.sign(
14-
{ name: "Cloudflare Worker", exp: Math.floor(Date.now() / 1000) + 2 },
14+
const authToken = await generateDiscordAuthToken(
15+
"Cloudflare Worker",
16+
Math.floor(Date.now() / 1000) + 2,
1517
env.BOT_PRIVATE_KEY,
16-
{ algorithm: "RS256" }
18+
"RS256"
1719
);
20+
const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${channelId}/messages`;
21+
const base_url = config(env).RDS_BASE_API_URL;
22+
const grantAWSAccessAPIUrl = `${base_url}/aws-access`;
1823

1924
try {
20-
const base_url = config(env).RDS_BASE_API_URL;
2125
const requestData = {
2226
groupId: awsGroupId,
2327
userId: discordUserId,
2428
};
2529

26-
const url = `${base_url}/aws-access`;
27-
28-
const response = await fetch(url, {
30+
const response = await fetch(grantAWSAccessAPIUrl, {
2931
method: "POST",
3032
headers: {
3133
"Content-Type": "application/json",
@@ -42,7 +44,7 @@ export async function processAWSAccessRequest(
4244
} else {
4345
content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`;
4446
}
45-
await fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
47+
await fetch(discordReplyUrl, {
4648
method: "POST",
4749
headers: {
4850
"Content-Type": "application/json",
@@ -54,7 +56,7 @@ export async function processAWSAccessRequest(
5456
});
5557
} catch (err) {
5658
const content = `<@${discordUserId}> Error occurred while granting AWS access.`;
57-
await fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
59+
await fetch(discordReplyUrl, {
5860
method: "POST",
5961
headers: {
6062
"Content-Type": "application/json",

src/utils/sendUserDiscordData.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { env } from "../typeDefinitions/default.types";
22
import jwt from "@tsndr/cloudflare-worker-jwt";
33
import { DISCORD_AVATAR_BASE_URL } from "../constants/urls";
44
import config from "../../config/config";
5+
import { generateDiscordAuthToken } from "./authTokenGenerator";
56

67
export const sendUserDiscordData = async (
78
token: string,
@@ -12,10 +13,11 @@ export const sendUserDiscordData = async (
1213
discordJoinedAt: string,
1314
env: env
1415
) => {
15-
const authToken = await jwt.sign(
16-
{ name: "Cloudflare Worker", exp: Math.floor(Date.now() / 1000) + 2 },
16+
const authToken = await generateDiscordAuthToken(
17+
"Cloudflare Worker",
18+
Math.floor(Date.now() / 1000) + 2,
1719
env.BOT_PRIVATE_KEY,
18-
{ algorithm: "RS256" }
20+
"RS256"
1921
);
2022
const data = {
2123
type: "discord",

0 commit comments

Comments
 (0)