@@ -3,29 +3,31 @@ import { env } from "../typeDefinitions/default.types";
33import config from "../../config/config" ;
44import { discordTextResponse } from "./discordResponse" ;
55import { DISCORD_BASE_URL , AWS_IAM_SIGNIN_URL } from "../constants/urls" ;
6+ import { generateDiscordAuthToken } from "./authTokenGenerator" ;
67
78export 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" ,
0 commit comments