-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
165 lines (133 loc) · 4.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var nunjucks = require("nunjucks");
const passport = require('passport');
const AtlassianOAuth2Strategy = require('passport-atlassian-oauth2');
const session = require('express-session');
const winston = require('winston');
const compression = require('compression');
var fs = require('fs');
if (!fs.existsSync('.env')) {
console.log("No .env file found. Please create one using the .env.example file as a template.");
process.exit(1);
}
const dotenv = require('dotenv').config();
const https = require('https');
var indexRouter = require('./routes/index');
var app = express();
// view engine setup
app.set('view engine', 'njk');
app.set('views', path.join(__dirname, 'views'));
nunjucks.configure('views', {
autoescape: true,
express: app
});
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(compression());
// Create logger
const winstonLogger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
new winston.transports.File({ filename: 'logs/combined.log' })
]
});
// Add console transport if not in production
if (process.env.NODE_ENV !== 'production') {
winstonLogger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
if (process.env.JIRA_AUTH_TYPE == "OAUTH") {
app.use(session({
secret: 'plywoods-amazing-session-secret',
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
}));
app.use(passport.initialize());
app.use(passport.session());
const authStrategy = new AtlassianOAuth2Strategy({
clientID: process.env.JIRA_OAUTH_CLIENT_ID,
clientSecret: process.env.JIRA_OAUTH_CLIENT_SECRET,
callbackURL: process.env.JIRA_OAUTH_CALLBACK_URL,
scope: 'offline_access read:jira-work read:jira-user write:jira-work',
},
function(accessToken, refreshToken, profile, cb) {
try {
winstonLogger.info('Authentication attempt', {
email: profile.email,
timestamp: new Date()
});
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
const cloudId = profile.accessibleResources.find(
site => site.url.includes(process.env.JIRA_URL)
)?.id;
if (!cloudId) {
winstonLogger.error('No matching Jira site found', {
url: process.env.JIRA_URL,
resources: profile.accessibleResources
});
return cb(new Error('No matching Jira site found'));
}
profile.cloudId = cloudId;
cb(null, profile);
} catch (error) {
winstonLogger.error('Authentication error', {
error: error.message,
stack: error.stack
});
cb(error);
}
});
authStrategy._oauth2.setAgent(new https.Agent({
rejectUnauthorized: false,
}));
passport.use(authStrategy);
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
app.use(passport.initialize());
app.use(passport.session());
var authRouter = require('./routes/auth');
app.use('/auth', authRouter);
}
app.use('/', indexRouter);
var configRouter = require('./routes/config');
app.use('/config', configRouter);
const configController = require('./controllers/configController');
// error handler
app.use(function(err, req, res, next) {
winstonLogger.error('Application error', {
error: err.message,
stack: err.stack,
path: req.path
});
next(err);
});
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = err;
// render the error page
res.status(err.status || 500).json(res.locals.error).send();
//res.render('error');
});
module.exports = app;