-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
106 lines (94 loc) · 3.93 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
const config = require("./config");
const passport = require('passport');
const ExtractJwt = require('passport-jwt').ExtractJwt;
const JwtStrategy = require('passport-jwt').Strategy;
const LocalStrategy = require('passport-local').Strategy;
const jwt = require('jsonwebtoken');
const jwtOptions = {};
const models = require('./models/index');
const crypto = require('crypto');
const userDatabase = config.databases.forum;
jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
jwtOptions.secretOrKey = config.jwtKey;
jwtOptions.issuer = 'naranawm.org';
passport.use(new JwtStrategy(jwtOptions, function(jwt_payload, done){
"use strict";
console.log(jwt_payload);
// Just returning a basic user for now TODO: build this out
return done(null, { id: 1 });
}));
// Todo: Move this into an extensions section so others can override
passport.use("learnNaviForum", new LocalStrategy(function(username, password, done){
"use strict";
if(username === undefined || password === undefined || username === "" || password === ""){
return done(null, false, { message: "Incorrect Username / Password" });
}
models.sequelize.query(`SELECT ${userDatabase.database}.${userDatabase.table}.id_member, member_name, passwd, real_name, filename FROM ${userDatabase.database}.${userDatabase.table} LEFT JOIN ${userDatabase.database}.${userDatabase.attachmentTable} ON ${userDatabase.database}.${userDatabase.table}.id_member = ${userDatabase.database}.${userDatabase.attachmentTable}.id_member WHERE member_name = :username`, {
type: models.sequelize.QueryTypes.SELECT,
replacements: {
username: username
}
}).then(results => {
"use strict";
if(results !== null && results.length === 1){
const hash = crypto.createHash("sha1");
hash.update(results[0].member_name + password);
const passwordHash = hash.digest("hex");
if(passwordHash === results[0].passwd){
// Successful Login!!!
return done(null, {
id: results[0].id_member,
name: results[0].real_name,
username: results[0].member_name,
avatar: "https://forum.learnnavi.org/avs/" + results[0].filename,
provider: "https://forum.learnnavi.org"
});
}
}
return done(null, false, { message: "Incorrect Username / Password" });
});
}));
// Used for local testing. TODO: Remove this once other authentication strategies are fully functional
passport.use("localTest", new LocalStrategy(function(username, password, done){
"use strict";
if(username === undefined || password === undefined || username === "" || password === ""){
return done(null, false, { message: "Incorrect Username / Password" });
}
if(username === "test" && password === "test"){
return done(null, {
id: 0,
name: "Test User",
username: username,
provider: "https://naranawm.org"
});
}
return done(null, false, { message: "Incorrect Username / Password" });
}));
module.exports = {
initialize: function(){
return passport.initialize();
},
authenticateJwt: function () {
return passport.authenticate("jwt", { session: false});
},
authenticateUser: function() {
"use strict";
return passport.authenticate(["localTest", "learnNaviForum"], { session: false});
},
createToken: function(req, res, next){
"use strict";
const payload = {
id: req.user.id,
name: req.user.name,
username: req.user.username,
avatar: req.user.avatar,
provider: req.user.provider
};
console.log(payload);
req.token = jwt.sign(payload, config.jwtKey, {
expiresIn: "1d",
issuer: "naranawm.org"
});
next();
}
};