1
- " use strict" ;
1
+ ' use strict' ;
2
2
3
- const express = require ( "express" ) ;
4
- const jwt = require ( "express-jwt" ) ;
5
- const jwksRsa = require ( "jwks-rsa" ) ;
6
- const getUserOrgs = require ( "./library/githubUser/getUserOrgs" ) ;
7
- const getUser = require ( "./library/githubUser/getUser" ) ;
8
- const getRepos = require ( "./library/githubUser/getRepos" ) ;
9
- const managementApiToken = require ( "./library/auth0/getManagementApiToken" ) ;
10
- const session = require ( "express-session" ) ;
11
- const redis = require ( "redis" ) ;
12
- let RedisStore = require ( "connect-redis" ) ( session ) ;
3
+ const express = require ( 'express' ) ;
4
+ const getUserOrgs = require ( './library/githubUser/getUserOrgs' ) ;
5
+ const getUser = require ( './library/githubUser/getUser' ) ;
6
+ const getRepos = require ( './library/githubUser/getRepos' ) ;
7
+ const managementApiToken = require ( './library/auth0/getManagementApiToken' ) ;
8
+ const session = require ( 'express-session' ) ;
9
+ const redis = require ( 'redis' ) ;
10
+ let RedisStore = require ( 'connect-redis' ) ( session ) ;
13
11
let redisClient = redis . createClient ( ) ;
14
- require ( " dotenv" ) . config ( ) ;
12
+ require ( ' dotenv' ) . config ( ) ;
15
13
16
14
// Create a new Express app
17
15
const app = express ( ) ;
18
16
19
17
// Logging
20
- const pino = require ( " pino" ) ;
21
- const expressPino = require ( " express-pino-logger" ) ;
22
- const logger = pino ( { level : process . env . LOG_LEVEL || " info" } ) ;
18
+ const pino = require ( ' pino' ) ;
19
+ const expressPino = require ( ' express-pino-logger' ) ;
20
+ const logger = pino ( { level : process . env . LOG_LEVEL || ' info' } ) ;
23
21
const expressLogger = expressPino ( { logger } ) ;
24
22
app . use ( expressLogger ) ;
25
23
26
- // Set up Auth0 configuration
27
- const authConfig = {
28
- domain : "devopslibrary.auth0.com" ,
29
- audience : "kondo-backend"
30
- } ;
31
-
32
24
// Define middleware that validates incoming bearer tokens
33
- // using JWKS from devopslibrary.auth0.com
34
- const checkJwt = jwt ( {
35
- secret : jwksRsa . expressJwtSecret ( {
36
- cache : true ,
37
- rateLimit : true ,
38
- jwksRequestsPerMinute : 5 ,
39
- jwksUri : `https://${ authConfig . domain } /.well-known/jwks.json`
40
- } ) ,
41
-
42
- audience : authConfig . audience ,
43
- issuer : `https://${ authConfig . domain } /` ,
44
- algorithm : [ "RS256" ]
45
- } ) ;
25
+ const checkJwt = require ( './src/middleware/authGuard' ) ;
46
26
47
27
// Use session https://github.com/expressjs/session
48
28
var sess = {
49
29
store : new RedisStore ( { client : redisClient } ) ,
50
- secret : " I like Santa" ,
30
+ secret : ' I like Santa' ,
51
31
cookie : { } ,
52
32
resave : false ,
53
- saveUninitialized : false
33
+ saveUninitialized : false ,
54
34
} ;
55
- if ( app . get ( " env" ) === " production" ) {
56
- app . set ( " trust proxy" , 1 ) ; // trust first proxy
35
+ if ( app . get ( ' env' ) === ' production' ) {
36
+ app . set ( ' trust proxy' , 1 ) ; // trust first proxy
57
37
sess . cookie . secure = true ; // serve secure cookies
58
38
}
59
39
app . use ( session ( sess ) ) ;
@@ -62,36 +42,36 @@ app.use(session(sess));
62
42
// Get management token from Auth0
63
43
let authToken ;
64
44
app . use ( express . json ( ) ) ;
65
- app . listen ( 3001 , async function ( ) {
66
- logger . info ( " App is ready" ) ;
45
+ app . listen ( 3000 , async function ( ) {
46
+ logger . info ( ' App is ready' ) ;
67
47
await managementApiToken . token . then ( function ( token ) {
68
48
authToken = token ;
69
49
} ) ;
70
50
} ) ;
71
51
72
52
// Retrieve Org Information
73
- app . get ( "/api/get- orgs" , checkJwt , async ( req , res ) => {
53
+ app . get ( '/ orgs' , checkJwt , async ( req , res ) => {
74
54
if ( ! req . session . orgs ) {
75
- const userId = req . user . sub . split ( "|" ) [ 1 ] ;
55
+ const userId = req . user . sub . split ( '|' ) [ 1 ] ;
76
56
const githubUser = await getUser ( authToken , userId ) ;
77
57
const githubToken = githubUser . identities [ 0 ] . access_token ;
78
58
const orgs = await getUserOrgs ( githubToken ) ;
79
59
req . session . orgs = orgs ;
80
60
}
81
- req . log . info ( " Returning cached Github Orgs" ) ;
61
+ req . log . info ( ' Returning cached Github Orgs' ) ;
82
62
res . send ( req . session . orgs ) ;
83
63
} ) ;
84
64
85
65
// Receive Repo Information
86
- app . get ( "/api/get- repos" , checkJwt , async ( req , res ) => {
87
- const userId = req . user . sub . split ( "|" ) [ 1 ] ;
66
+ app . get ( '/orgs/ repos' , checkJwt , async ( req , res ) => {
67
+ const userId = req . user . sub . split ( '|' ) [ 1 ] ;
88
68
const githubUser = await getUser ( authToken , userId ) ;
89
69
const githubToken = githubUser . identities [ 0 ] . access_token ;
90
70
const repos = await getRepos ( req . query . org , githubToken ) ;
91
71
res . send ( repos ) ;
92
72
} ) ;
93
73
94
- process . on ( " SIGINT" , function ( ) {
74
+ process . on ( ' SIGINT' , function ( ) {
95
75
redisClient . quit ( ) ;
96
- console . log ( " redis client quit" ) ;
76
+ console . log ( ' redis client quit' ) ;
97
77
} ) ;
0 commit comments