-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
184 lines (160 loc) · 5.6 KB
/
auth.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
BasicStrategy = require('passport-http').BasicStrategy,
ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy,
BearerStrategy = require('passport-http-bearer').Strategy,
OAuth2Client = require('./models/oauth2client'),
OAuth2Token = require('./models/oauth2token'),
User = require('./models/user'),
speakeasy = require('speakeasy'),
QRCode = require('qrcode');
// Local authentication strategy for passportjs, used for web logins
passport.use(new LocalStrategy({
usernameField: 'username'
},
function (username, password, done) {
User.authenticate(username, password, function (err, user, params) {
if (err || !user) {
return done(err, user, params);
}
// If 2FA is enabled, verify the token
if (user.twoFactorEnabled) {
if (!params || !params.otp) {
return done(null, false, { message: '2FA token required' });
}
const isVerified = user.verifyTwoFactorToken(params.otp);
if (!isVerified) {
return done(null, false, { message: 'Invalid 2FA token' });
}
}
return done(null, user, params);
});
}));
// Standard basic authentication strategy, used for REST-based logins
passport.use(new BasicStrategy(
function (username, password, done) {
User.authenticate(username, password, function (err, user, params) {
if (err || !user) {
return done(err, user, params);
}
// If 2FA is enabled, verify the token
if (user.twoFactorEnabled) {
if (!params || !params.otp) {
return done(null, false, { message: '2FA token required' });
}
const isVerified = user.verifyTwoFactorToken(params.otp);
if (!isVerified) {
return done(null, false, { message: 'Invalid 2FA token' });
}
}
return done(null, user, params);
});
}
));
// Authentication strategy used by OAuth clients, uses a custom name 'oAuthBasic'
passport.use('oAuthBasic', new BasicStrategy(
function (username, password, done) {
OAuth2Client.findOne({
clientId: username
}, function (error, client) {
if (error) {
return done(error);
}
if (!client) {
return done(null, false);
}
if (client.clientSecret !== password) {
return done(null, false);
}
return done(null, client);
});
}
));
// A client-password strategy for authorizing requests for tokens
passport.use(new ClientPasswordStrategy(
function (clientId, clientSecret, done) {
OAuth2Client.findOne({
clientId: clientId
}, function (error, client) {
if (error) {
return done(error);
}
if (!client) {
return done(null, false);
}
if (client.clientSecret !== clientSecret) {
return done(null, false);
}
return done(null, client);
});
}
));
// A bearer strategy to authorize API requests by OAuth2 tokens
passport.use(new BearerStrategy(
function (accessToken, done) {
OAuth2Token.findOne({
token: accessToken
}, function (error, oauth2token) {
if (error) {
return done(error);
}
if (!oauth2token) {
return done(null, false);
}
User.findOne({
_id: oauth2token.user
}, function (error, openhabUser) {
if (error) {
return done(error);
}
if (!openhabUser) {
return done(null, false);
}
var info = {
scope: oauth2token.scope
};
done(null, openhabUser, info);
});
});
}
));
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
// Route to enable 2FA
module.exports.enableTwoFactor = async function (req, res) {
try {
const user = await User.findById(req.user.id); // Ensure this retrieves the correct user
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
const secret = User.generateTwoFactorSecret();
user.twoFactorSecret = secret.base32;
user.twoFactorEnabled = true;
await user.save();
const qrCodeUrl = await QRCode.toDataURL(secret.otpauth_url);
res.json({ qrCodeUrl });
} catch (error) {
res.status(500).json({ message: 'Error enabling 2FA' });
}
};
// Middleware to verify 2FA OTP
module.exports.verifyTwoFactor = function (req, res, next) {
if (!req.user.twoFactorEnabled) {
return next(); // 2FA not enabled, proceed to the next middleware
}
const otp = req.body.otp;
if (!otp) {
return res.status(401).json({ message: '2FA token required' });
}
const isVerified = req.user.verifyTwoFactorToken(otp);
if (!isVerified) {
return res.status(401).json({ message: 'Invalid 2FA token' });
}
next(); // 2FA verified, proceed to the next middleware
};