authenticate.js
2.4 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
var async = require('async');
exports = module.exports = function (logger, rest) {
function strategy(req, username, password, done) {
var user = {
refreshToken: "",
token: "",
service_catalog: {},
user_info: {},
err: null
};
async.waterfall([
function (callback) {//get temp token
logger.info("get temp token");
var tempParams = {
"grant_type": req.body.grant_type,
"username": req.body.username,
"scope": req.body.scope,
"password": req.body.password,
"img_code": req.body.img_code,
};
if (req.body.smscode)
tempParams['sms_code'] = req.body.smscode;
logger.info("url:" + rest.getBaseUrl('auth/tokens', 'uaa'));
logger.info("params", tempParams);
console.log(tempParams)
rest.restful.postJson(rest.getBaseUrl('auth/tokens', 'uaa'), tempParams, {
headers: { 'Content-Type': 'application/json', "Accept": "application/json" }
})
.on('success', function (data, response) {
logger.info("tokens-success", data);
if (data.access_token) {
user.token = data.access_token;
user.refreshToken = data.refresh_token;
user.user_info = {
'user': data.user,
'tenant': data.tenant,
'perms': [],
'scope': data.scope,
'oss': {}
};
user.service_catalog = data.service_catalog
callback(null, data)
} else {
var err = new Error('name or pass err');
user.err = data.message;
callback(err, data);
}
}).on('error', function (err, response) {
// logger.info("tokens-error",data);
var err = new Error('name or pass err');
callback(err, response);
}).on('fail', function (data, response) {
logger.info("tokens-fail", data);
var err = new Error('name or pass err');
callback(data, response);
});
}], function (err, result) {
logger.info(err);
if (err) {
return done(null, false, err);
} else {
return done(null, user);
}
});
}
function serializeUser(user, done) {
logger.info("passport.serializeUser");
done(null, user);
}
function deserializeUser(user, done) {
var user = {
refreshToken: "",
token: "",
info: {},
err: null
};
console.info("passport.deserializeUser");
done(null, user);
}
return {
strategy: strategy,
serializeUser: serializeUser,
deserializeUser: deserializeUser
};
};
exports['@singleton'] = true;
exports['@require'] = ['igloo/logger', 'utils/rest'];