使用Bluebird检查Node.js Rest客户端中的Oauth2令牌是否存在并有效
我正在为使用OAuth2的RESTAPI编写Node.js客户端。我正在使用Bluebird和promises(并在标题中发送访问令牌),我想知道什么时候是检查访问令牌是否已被授予(存在)或仍然有效(未过期)的好时机 到目前为止,我已经得出了以下结论:使用Bluebird检查Node.js Rest客户端中的Oauth2令牌是否存在并有效,node.js,oauth,oauth-2.0,bluebird,Node.js,Oauth,Oauth 2.0,Bluebird,我正在为使用OAuth2的RESTAPI编写Node.js客户端。我正在使用Bluebird和promises(并在标题中发送访问令牌),我想知道什么时候是检查访问令牌是否已被授予(存在)或仍然有效(未过期)的好时机 到目前为止,我已经得出了以下结论: 'use strict'; var Bluebird = require('bluebird'); var request = Bluebird.promisifyAll(require('request'), { multiArgs: tru
'use strict';
var Bluebird = require('bluebird');
var request = Bluebird.promisifyAll(require('request'), { multiArgs: true });
var Oauth = require('oauth');
var OAuth2 = OAuth.OAuth2;
var _ = require('lodash');
function Client(options) {
this.options = _.assign({
url: '<API URL>',
oauth2Url: 'oauth2/token',
apiVersion: process.env.apiVersion,
consumerKey: process.env.consumerKey,
consumerSecret: process.env.consumerSecret
}, options);
if (!this.options.url) {
throw new Error('Missing client url.');
}
...
if (!this.options.consumerSecret) {
throw new Error('Missing consumer secret.');
}
if(!this.access_token){
var oauth2 = new OAuth2(
this.options.consumerKey,
this.options.consumerSecret,
this.options.url + this.options.version,
null,
this.options.oauth2Url,
null);
oauth2.getOAuthAccessToken(
'',
{'grant_type':'client_credentials'},
function (e, access_token, refresh_token, results){
this.access_token = access_token;
this.refresh_token = refresh_token;
done();
});
}
}
Client.prototype.queryApi = function (options, callback) {
return request.postAsync({
headers: {
Authorization: 'Bearer ' + access_token
},
url: this.options.url + this.options.apiVersion,
body: JSON.stringify(options)}).
then(function (result) {
var json = JSON.parse(result[1]);
if (_.isFunction(callback)) {
callback(null, json);
}
return json;
}).
catch(function (err) {
if (_.isFunction(callback)) {
callback(err);
return;
}
throw err;
});
};
module.exports = Client;
“严格使用”;
var Bluebird=require(‘Bluebird’);
var request=Bluebird.promisifyAll(require('request'),{multiArgs:true});
var Oauth=require('Oauth');
var OAuth2=OAuth.OAuth2;
var=要求('lodash');
功能客户端(选项){
this.options=\分配({
url:“”,
oauth2Url:'oauth2/token',
apiVersion:process.env.apiVersion,
consumerKey:process.env.consumerKey,
ConsumerCret:process.env.ConsumerCret
},选项);
如果(!this.options.url){
抛出新错误(“缺少客户端url”);
}
...
如果(!this.options.ConsumerCret){
抛出新错误(“缺少消费者机密”);
}
如果(!this.access_令牌){
var oauth2=新的oauth2(
这个.options.consumerKey,
this.options.ConsumerCret,
this.options.url+this.options.version,
无效的
this.options.oauth2Url,
无效);
oauth2.getOAuthAccessToken(
'',
{'grant_type':'client_credentials'},
函数(e、访问令牌、刷新令牌、结果){
this.access\u token=访问\u token;
this.refresh\u token=刷新\u token;
完成();
});
}
}
Client.prototype.queryApi=函数(选项,回调){
返回请求.postAsync({
标题:{
授权:“承载人”+访问令牌
},
url:this.options.url+this.options.apiVersion,
正文:JSON.stringify(选项)})。
然后(函数(结果){
var json=json.parse(结果[1]);
if(uu.isFunction(回调)){
回调(null,json);
}
返回json;
}).
捕获(函数(错误){
if(uu.isFunction(回调)){
回调(err);
返回;
}
犯错误;
});
};
module.exports=客户端;
我对Oauth/Oauth2和Node.js都是新手,我只是想知道我是否在正确的位置检查访问令牌,以及如何/在哪里检查它是否过期。谢谢 首先,有两种方法可以检查访问令牌是否过期
- 通过了解oauth应用程序中的令牌\过期值。在这种情况下,您需要在应用程序上保持任务运行,以确定访问\令牌是否过期。(不推荐处理访问令牌的方法)
- 处理来自授权服务器的声明您的访问令牌已过期的响应。在这种情况下,您需要通过显示刷新令牌来获取新的访问令牌李>
function tokenPersistanceFunction(updatedOauth){
// Here you will get Updated Oauth values
// Save these to DB
return saveAccessToken(updatedOauth.access_token, updatedOauth.refresh_token);
}
Client.prototype.queryApi = function (options, tokenPersistanceFunction, callback) {
return request.postAsync({
headers: {
Authorization: 'Bearer ' + access_token
},
url: this.options.url + this.options.apiVersion,
body: JSON.stringify(options)}).
then(function (result) {
// You have some indication from your oauth server, that your access_token is expired.
// You can check your response here to know whether access_token is expired or not.
// If access_token is expired, Make request to refresh access token.
// In your case
if(AccessTokenIsExpired){
// Function that will make request to refresh access_token by presenting refresh_token
return <functionThatRefreshesAccessToken>( refreshAccessTokenOptions,tokenPersistanceFunction)
.then(function(result){
//Extract access_token, refresh_token from response
// call 'tokenPersistanceFunction' to store these token in your DB.
return tokenPersistanceFunction(updatedOauth);
})
.then(function(savedOauthTokensSuccess){
// Now you have the updated Oauth tokens, you can make request to get resource
// this call will return you the actual response.
return queryApi(options, tokenPersistanceFunction, callback);
})
}else{
var json = JSON.parse(result[1]);
if (_.isFunction(callback)) {
callback(null, json);
}
return json;
}
}).
catch(function (err) {
if (_.isFunction(callback)) {
callback(err);
return;
}
throw err;
});
};
function-tokenPersistanceFunction(updateauth){
//在这里,您将获得更新的Oauth值
//将这些保存到DB
返回saveAccessToken(updateAuth.access\u令牌,updateAuth.refresh\u令牌);
}
Client.prototype.queryApi=函数(选项、令牌持久化函数、回调){
返回请求.postAsync({
标题:{
授权:“承载人”+访问令牌
},
url:this.options.url+this.options.apiVersion,
正文:JSON.stringify(选项)})。
然后(函数(结果){
//您的oauth服务器显示您的访问令牌已过期。
//您可以在此处检查您的响应,以了解访问令牌是否过期。
//如果访问令牌已过期,则请求刷新访问令牌。
//就你而言
如果(AccessTokenIsExpired){
//函数,该函数将通过显示刷新\u令牌来请求刷新访问\u令牌
返回(refreshAccessTokenOptions、tokenPersistanceFunction)
.然后(函数(结果){
//从响应中提取访问令牌、刷新令牌
//调用“tokenPersistanceFunction”将这些令牌存储在数据库中。
返回tokenPersistanceFunction(updateAuth);
})
.then(函数(SaveDoAuthTokensAccess){
//现在您有了更新的Oauth令牌,您可以请求获取资源
//此呼叫将返回实际响应。
返回queryApi(选项、tokenPersistanceFunction、回调);
})
}否则{
var json=json.parse(结果[1]);
if(uu.isFunction(回调)){
回调(null,json);
}
返回json;
}
}).
捕获(函数(错误){
if(uu.isFunction(回调)){
回调(err);
返回;
}
犯错误;
});
};