-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (99 loc) · 3.21 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
/**
Authors: Michael Qin
https://github.com/MikeQin/rest-api
*/
const express = require("express");
const app = express();
const basicAuth = require('express-basic-auth')
const port = process.env.PORT || 80;
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
app.use(basicAuth({
users: {
'B2cRestClientId': 'admin'
},
unauthorizedResponse: getUnauthorizedResponse,
realm: 'Azure B2C REST API Demo'
}));
function getUnauthorizedResponse(req) {
var msg;
if (req.auth) {
msg = 'Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected!';
}
else {
msg = 'No credentials provided!';
}
console.log('[*] ' + msg);
return msg;
}
function log(claims) {
console.log("---- log claims ---- ");
console.log("email: " + claims.email);
}
function validate(claims, errors) {
if (!claims.userName) {
errors.push('userName is not defined!');
}
}
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.get("/", function(req, res) {
res.status(200).send('Hello World from b2c-rest-api');
});
app.post("/echo", function(req, res) {
res.status(200).json({userName: req.body.userName});
});
app.post("/api/migrate", function(req, res) {
var inputClaims = req.body;
var errorsArr = new Array();
//log(inputClaims);
validate(inputClaims, errorsArr);
// Inputs
var userName = inputClaims.userName;
var password = inputClaims.password;
var status = inputClaims.status;
if (errorsArr.length > 0) {
res.status(400).json({ errors: errorsArr });
}
else {
var hashed = bcrypt.hashSync(password, 10);
console.log('login status = ' + status);
// Outputs
var outputClaims = {
userName: userName,
password: password,
displayName: 'Joe Smith',
firstName: 'Joe',
lastName: 'Smith',
status: setStatus(status),
hashedPassword: hashed
};
console.log('set status = ' + status);
res.status(200).json(outputClaims);
}
});
function setStatus(loginStatus) {
let status = 0; // not migrated
switch(loginStatus) {
case 0: // not migrated, 1st time login
// migrate user logic goes here
// ...
// 1) after successful login
status = 1;
// 2) if not found, then status = 3; // new user
// 3) if login error, throw new Error ('login error');
// TODO: can covisint distinguish 2) & 3) ??
break;
case 1: // 1st time after migration
status = 2; // set: already migrated
break;
case 2: // 2 - already migrated
case 3: // 3 - new user
status = loginStatus;
break;
default:
throw new Error('Error on user status. status = ' + loginStatus);
}
return status;
}
app.listen(port, () => console.log("Listening on port " + port));