oauth - Check Oauth2 token exists and valid in Node.js Rest Client With Bluebird -


i writing node.js client rest api uses oauth2. using bluebird , promises (and sending access token in header) , wondering when time check if access token granted (exists) or still valid (not expired).

so far, have come this:

'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; 

i new both oauth/oauth2 , node.js , wondering if checking access token in right place , how/where can check if expired or not. thanks!

first of there 2 way check whether access token expired or not

  • by knowing token_expiration value oauth app.in case need keep task running on app determine wheter access_token expired or not.(not recommended way of handling access token)
  • handle response authorization server stating acces token has been expired.in case need new access token presenting refresh token.

you can write 'tokenpersistancefunction' called when oauth values(access_token,refresh_token) updated.

i have modified code reflect these changes

    function tokenpersistancefunction(updatedoauth){          // here updated oauth values          // save these 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) {                  // have indication oauth server, access_token expired.                 // can check response here know whether access_token expired or not.                  // if access_token expired, make request refresh access token.                 // in case                  if(accesstokenisexpired){                     // function make request refresh access_token presenting refresh_token                     return <functionthatrefreshesaccesstoken>( refreshaccesstokenoptions,tokenpersistancefunction)                   .then(function(result){                        //extract access_token, refresh_token response                       // call 'tokenpersistancefunction' store these token in db.                        return tokenpersistancefunction(updatedoauth);                    })                   .then(function(savedoauthtokenssuccess){                       // have updated oauth tokens, can make request resource                       // call return 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;             });     }; 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -