-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.js
103 lines (81 loc) · 2.64 KB
/
authentication.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
if( Meteor.isServer ) {
Meteor.startup(function () {
Future = Npm.require('fibers/future');
cache = new ApiCache('rest', 600);
Meteor.methods({
'plone.login': function(token, id) {
var ploneFuture = new Future();
var ploneAuthEnabled = RocketChat.settings.get('Allow Plone authentication');
if( !ploneAuthEnabled ) {
return;
}
var plonesite = RocketChat.settings.get('Plone authentication endpoint');
if( plonesite.lastIndexOf('/') !== plonesite.length - 1 ) {
plonesite += '/';
}
var url = plonesite + '@@verify-token?token=' + token + '&user=' + id;
var cacheKey = plonesite + id;
var cacheId = cache.get(cacheKey);
if( cacheId ) {
return {
type: 'plone',
userId: cacheId
};
}
HTTP.call(
'POST',
url,
function(Accounts, error, res) {
if( res === null ) {
ploneFuture.return();
}else{
var contents = JSON.parse(res.content);
if( contents.status === "failure" ) {
ploneFuture.return(false);
}
var id = Accounts.findUserByUsername(contents.user);
if( id === undefined ) {
ploneFuture.return();
}else{
id = id._id;
cache.set(cacheKey, id);
ploneFuture.return(
{
type: 'plone',
userId: id
}
);
}
}
}.bind(ploneFuture, Accounts)
);
return ploneFuture.wait();
}
});
});
Accounts.registerLoginHandler("plone", function(loginRequest) {
if( loginRequest.token === undefined ) {
return undefined;
}
var account = Accounts.findUserByUsername(loginRequest.user.name);
if( account === undefined ) {
var id = Accounts.createUser({
email: loginRequest.user.email,
password: Random.id(),
username: loginRequest.user.name
});
//var rooms = RocketChat.models.Rooms.findByType('c').fetch();
//var user = RocketChat.models.Users.findOneById(id);
return {
userId: id,
type: 'plone'
};
}else{
var future = new Future();
Meteor.call('plone.login', loginRequest.token, account.username, function(err, res) {
future.return(res);
}.bind(future));
return future.wait();
}
});
}