-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
192 lines (169 loc) · 5.91 KB
/
Copy pathapp.js
File metadata and controls
192 lines (169 loc) · 5.91 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// ********** Dependencies **********
const express = require('express');
const bodyParser = require('body-parser');
require('fetch');
const path = require('path');
const Molewakamole = require('./molewakamole/molewakamole');
require('dotenv').config();
// const logger = require('morgan');
// const cookieParser = require('cookie-parser');
// const session = require('express-session');
// const bodyParser = require('body-parser');
// **** Passport ****
// const passport = require('passport');
// const FortyTwoStrategy = require('passport-42').Strategy;
// const ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
// ********** Code **********
// **** 42 Strategy with passport ****
// Configure the 42 strategy for use by Passport.
//
// OAuth 2.0-based strategies require a `verify` function which receives the
// credential (`accessToken`) for accessing the 42 API on the user's
// behalf, along with the user's profile. The function must invoke `cb`
// with a user object, which will be set at `req.user` in route handlers after
// authentication.
// passport.use(new FortyTwoStrategy(
// {
// clientID: process.env['CLIENT_ID'],
// clientSecret: process.env['SECRET'],
// callbackURL: process.env['CALLBACK_URL']
// },
// function(accessToken, refreshToken, profile, cb) {
// // In this example, the user's 42 profile is supplied as the user
// // record. In a production-quality application, the 42 profile should
// // be associated with a user record in the application's database, which
// // allows for account linking and authentication with other identity
// // providers.
// // console.log(profile); // TODO debug
// return cb(null, profile);
// }
// ));
// **** Passport session persistence ****
// Configure Passport authenticated session persistence.
//
// In order to restore authentication state across HTTP requests, Passport needs
// to serialize users into and deserialize users out of the session. In a
// production-quality application, this would typically be as simple as
// supplying the user ID when serializing, and querying the user record by ID
// from the database when deserializing. However, due to the fact that this
// example does not have a database, the complete 42 profile is serialized
// and deserialized.
// passport.serializeUser(function(user, cb) {
// cb(null, user);
// });
// passport.deserializeUser(function(obj, cb) {
// cb(null, obj);
// });
// **** Express ****
const app = express();
// app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// **** View engine setup ****
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// Use application-level middleware for common functionality, including
// logging, parsing, and session handling.
// app.use(logger('dev'));
// app.use(cookieParser());
// app.use(bodyParser.urlencoded({ extended: false }));
// // TODO secret?
// app.use(session({ resave: false, saveUninitialized: false, secret: '!terceS' }));
// app.use(express.static(path.join(__dirname, 'public')));
// **** Session ****
// Initialize Passport and restore authentication state, if any, from the
// session.
// app.use(passport.initialize());
// app.use(passport.session());
// ********** Routes **********
app.get(
'/',
// ensureLoggedIn(),
(req, res) => {
res.redirect('/app');
}
);
// app.get(
// '/login',
// passport.authenticate('42')
// );
// // TODO if login fails, loop may occur
// app.get(
// '/callback',
// passport.authenticate('42',
// {
// failureRedirect: '/login',
// // successRedirect: '/app'
// }
// ),
// async (req, res) => {
// // TODO
// console.log("code: " + req.query.code);
// let token = await fetch(
// process.env.AUTH_URL,
// {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// grant_type: 'authorization_code',
// client_id: process.env.CLIENT_ID,
// client_secret: process.env.SECRET,
// code: req.query.code,
// redirect_uri: process.env.CALLBACK_URL,
// })
// }
// ).then(res => res.json()).then((data) => {
// console.log(data);
// res.redirect('/app');
// });
// // TODO
// // res.redirect('/app');
// }
// );
app.get(
'/app',
// ensureLoggedIn(),
(req, res) => {
res.render('app', { user: req.user });
}
)
app.get(
'/terminal',
(req, res) => {
res.render('terminal', { user: req.user });
}
)
// const API = new API42(process.env.CLIENT_ID, process.env.SECRET);
// app.post('/api/request', (req, res) => {
// API.get(req.body.endpoint, req.body.filters).then(result => {
// console.log(result);
// console.log("Sending result");
// res.render('api/location', {data: result[0]})
// }).catch(err => {
// res.status(503).send(JSON.stringify(err));
// });
// });
const mole = new Molewakamole(process.env.CLIENT_ID, process.env.SECRET);
app.post('/api/request', (req, res) => {
mole.get(req, res);
});
// **** Errors ****
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// ********** Module **********
module.exports = app;