forked from hardillb/node-red-contrib-googlehome-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.js
128 lines (116 loc) · 3.89 KB
/
oauth.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
117
118
119
120
121
122
123
124
125
126
127
128
var oauth2orize = require('oauth2orize');
var OAuth = require('./models/oauth');
var server = oauth2orize.createServer();
server.grant(oauth2orize.grant.code({
scopeSeparator: [ ' ', ',' ]
}, function(application, redirectURI, user, ares, done) {
//console.log("grant user: ", user);
OAuth.GrantCode.findOne({application: application, user: user},function(error,grant){
if (!error && grant) {
done(null,grant.code);
} else if (!error) {
var grant = new OAuth.GrantCode({
application: application,
user: user,
scope: ares.scope
});
grant.save(function(error) {
done(error, error ? null : grant.code);
});
} else {
done(error,null);
}
});
}));
server.exchange(oauth2orize.exchange.code({
userProperty: 'appl'
}, function(application, code, redirectURI, done) {
OAuth.GrantCode.findOne({ code: code }, function(error, grant) {
if (grant && grant.active && grant.application == application.id) {
var now = (new Date().getTime())
OAuth.AccessToken.findOne({application:application, user: grant.user, expires: {$gt: now}}, function(error,token){
if (token) {
OAuth.RefreshToken.findOne({application:application, user: grant.user},function(error, refreshToken){
if (refreshToken){
var expires = Math.round((token.expires - (new Date().getTime()))/1000);
done(null,token.token, refreshToken.token,{token_type: 'Bearer', expires_in: expires});
} else {
// Shouldn't get here unless there is an error as there
// should be a refresh token if there is an access token
done(error);
}
});
} else if (!error) {
var token = new OAuth.AccessToken({
application: grant.application,
user: grant.user,
grant: grant,
scope: grant.scope
});
token.save(function(error){
var expires = Math.round((token.expires - (new Date().getTime()))/1000);
//delete old refreshToken or reuse?
OAuth.RefreshToken.findOne({application:application, user: grant.user},function(error, refreshToken){
if (refreshToken) {
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'Bearer', expires_in: expires, scope: token.scope});
} else if (!error) {
var refreshToken = new OAuth.RefreshToken({
user: grant.user,
application: grant.application
});
refreshToken.save(function(error){
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'Bearer', expires_in: expires, scope: token.scope});
});
} else {
done(error);
}
});
});
} else {
done(error);
}
});
} else {
done(error, false);
}
});
}));
server.exchange(oauth2orize.exchange.refreshToken({
userProperty: 'appl'
}, function(application, token, scope, done){
OAuth.RefreshToken.findOne({token: token}, function(error, refresh){
if (refresh && refresh.application == application.id) {
OAuth.GrantCode.findOne({user: refresh.user},function(error, grant){
if (grant && grant.active && grant.application == application.id){
var newToken = new OAuth.AccessToken({
application: refresh.application,
user: refresh.user,
grant: grant,
scope: scope
});
newToken.save(function(error){
var expires = Math.round((newToken.expires - (new Date().getTime()))/1000);
if (!error) {
done(null, newToken.token, refresh.token, {token_type: 'Bearer', expires_in: expires, scope: newToken.scope});
} else {
done(error,false);
}
});
} else {
done(error,null);
}
});
} else {
done(error, false);
}
});
}));
server.serializeClient(function(application, done) {
done(null, application.id);
});
server.deserializeClient(function(id, done) {
OAuth.Application.findById(id, function(error, application) {
done(error, error ? null : application);
});
});
module.exports = server;