forked from hardillb/node-red-alexa-home-skill-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.js
151 lines (139 loc) · 4.78 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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) {
//console.log("Grant, existing grant code found");
//console.log("%j", 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);
}
});
// var grant = new OAuth.GrantCode({
// application: application,
// user: user,
// scope: ares.scope
// });
// grant.save(function(error) {
// done(error, error ? null : grant.code);
// });
}));
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) {
//console.log("exchange, found grant code")
OAuth.AccessToken.findOne({application:application, user: grant.user, active: true}, function(error,token){
if (token) {
//console.log("Active access token found");
//console.log("%j", 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});
//console.log("refresh token found, sent 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
console.log("no refresh token found for existing access token");
console.log("%j",error);
done(error);
}
});
} else if (!error) {
console.log("exchange, no access token found");
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) {
console.log("Should never get here, new accessToken with old refresh token");
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'Bearer', expires_in: expires, scope: token.scope});
} else if (!error) {
//console.log("creating new refresh token")
var refreshToken = new OAuth.RefreshToken({
user: grant.user,
application: grant.application
});
refreshToken.save(function(error){
//console.log("sending new token access and refresh token");
done(error, error ? null : token.token, refreshToken.token, error ? null : { token_type: 'Bearer', expires_in: expires, scope: token.scope });
});
} else {
console.log("err1");
done(error);
}
});
});
} else {
console.log("err2");
done(error);
}
});
} else {
console.log("err3");
done(error, false);
}
});
}));
server.exchange(oauth2orize.exchange.refreshToken({
userProperty: 'appl'
}, function(application, token, scope, done){
//console.log("Yay!");
OAuth.RefreshToken.findOne({token: token}, function(error, refresh){
if (refresh && refresh.application == application.id) {
OAuth.GrantCode.findOne({},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: grant.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;