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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| 'use strict'
var hash = require('pbkdf2-password')() var path = require('path'); var session = require('express-session');
app.use(express.urlencoded({ extended: false })) app.use(session({ resave: false, saveUninitialized: false, secret: 'shhhh, very secret' }));
var oneCodeuserAuth={ salt:"T1le0otX5tsqexqhpV6kT37asxuaWt1H/PLbYph7LkhhX2kraKxKciVlK9xb7UiDLF+gjT0gOwrv6m3tyGZv1A==", hash:"NjE68Pp8hGkLVI5jCIt2pypcZSJZAzOnKjfWZTZY9f6c98GKBAYf1e6kbyTV727blabq1TarZvFNL0++cZYHd3/G5o5KLxH6BgQN/J/P9/u1VIZn1rU1nxxCrV4/rUTdzCPoD8ngvT4odCbSg+3EPXlCGM/a7Oz+C/uCXsPwoOI=" }
function restrict(req, res, next) { if(req.path.startsWith("/auth")){ next() return ; } if(req.method=="OPTIONS"){ next() return; } if (req.session.user) { next(); return ; } else { res.redirect(req.baseUrl+'/auth/access-denied'); return ; } }
app.use(restrict)
app.get('/auth/logout', function(req, res){ req.session.destroy(function(){
res.send(`Logged out.<br/> <a href="${req.baseUrl}/auth/login">Log in</a>`) }); }); app.get('/auth/json/logout', function(req, res){ req.session.destroy(function(){
res.json({code:200}) }); }); app.get('/auth/access-denied', function(req, res){ res.send(` <h1>Access denied</h1> <hr/> <h2>What happened?</h2> <p> Some of the interfaces provided by this API involve sensitive information, so you need to enter Auth Code to use this API.</p> <a href="${req.baseUrl}/auth/login">Log in here.</a> `) });
app.get('/auth/login', function(req, res){ res.send(` Some of the interfaces provided by this API involve sensitive information, so you need to enter Auth Code to use this API.<br/> By the way, please do not casually share this API URL with others, as others can obtain your password, ID Card Num.,Phone Number and other information through this backend.<br/> <form method="post" action="./login"> <p> <label for="password">Auth Code:</label> <input type="text" name="password" id="password"> </p> <p> <input type="submit" value="Login"> </p> </form>`); }); app.get('/auth/status', function(req, res){ if(req.session.user)res.json({auth:true}); else res.json({auth:false}); });
app.post('/auth/login', function (req, res, next) { hash({ password: req.body.password, salt: oneCodeuserAuth.salt }, function (err, pass, salt, hash) { if (err) return next(err) if (hash === oneCodeuserAuth.hash){ req.session.regenerate(function(){ req.session.user = oneCodeuserAuth; res.send(`<strong style='color:green'>Authenticated.</strong><br/> Now you can go back to thost/zujuanink page to continue.<br/> <strong>Maybe you'll need to refresh that page</strong>`) }); }else{ res.send(`<strong style='color:red'>Authentication Failed. You probably entered a wrong code.</strong><br/> You can contact to the API administrator or who offered this API location to you to know what happened in detail.<br/> <a href="${req.baseUrl}/auth/login">Try again</a>`) } }); }); app.post('/auth/json/login', function (req, res, next) { hash({ password: req.body.password, salt: oneCodeuserAuth.salt }, function (err, pass, salt, hash) { if (err) return next(err) if (hash === oneCodeuserAuth.hash){ req.session.regenerate(function(){ req.session.user = oneCodeuserAuth; res.json({auth:true}) }); }else{ res.json({auth:false}) } }); });
|