@@ -3,29 +3,31 @@ import { env } from "../typeDefinitions/default.types";
3
3
import config from "../../config/config" ;
4
4
import { discordTextResponse } from "./discordResponse" ;
5
5
import { DISCORD_BASE_URL , AWS_IAM_SIGNIN_URL } from "../constants/urls" ;
6
+ import { generateDiscordAuthToken } from "./authTokenGenerator" ;
6
7
7
8
export async function processAWSAccessRequest (
8
9
discordUserId : string ,
9
10
awsGroupId : string ,
10
11
env : env ,
11
12
channelId : number
12
13
) : 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 ,
15
17
env . BOT_PRIVATE_KEY ,
16
- { algorithm : "RS256" }
18
+ "RS256"
17
19
) ;
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` ;
18
23
19
24
try {
20
- const base_url = config ( env ) . RDS_BASE_API_URL ;
21
25
const requestData = {
22
26
groupId : awsGroupId ,
23
27
userId : discordUserId ,
24
28
} ;
25
29
26
- const url = `${ base_url } /aws-access` ;
27
-
28
- const response = await fetch ( url , {
30
+ const response = await fetch ( grantAWSAccessAPIUrl , {
29
31
method : "POST" ,
30
32
headers : {
31
33
"Content-Type" : "application/json" ,
@@ -42,7 +44,7 @@ export async function processAWSAccessRequest(
42
44
} else {
43
45
content = `AWS access granted successfully <@${ discordUserId } >! Please head over to AWS - ${ AWS_IAM_SIGNIN_URL } .` ;
44
46
}
45
- await fetch ( ` ${ DISCORD_BASE_URL } /channels/ ${ channelId } /messages` , {
47
+ await fetch ( discordReplyUrl , {
46
48
method : "POST" ,
47
49
headers : {
48
50
"Content-Type" : "application/json" ,
@@ -54,7 +56,7 @@ export async function processAWSAccessRequest(
54
56
} ) ;
55
57
} catch ( err ) {
56
58
const content = `<@${ discordUserId } > Error occurred while granting AWS access.` ;
57
- await fetch ( ` ${ DISCORD_BASE_URL } /channels/ ${ channelId } /messages` , {
59
+ await fetch ( discordReplyUrl , {
58
60
method : "POST" ,
59
61
headers : {
60
62
"Content-Type" : "application/json" ,
0 commit comments