0
votes

I am working on Node.js in which I am using superagent to call oauth token generation request and I am manually parsing a response from which I am extracting access_token, refresh_token, token_type, expires_in,etc.

below is the snippet for oauth token generation via superagent call

superagent
    .post('https://myapi.com/oauth_token.do')
    .proxy(proxy)
    .type('form')
    .send({grant_type: 'password' })
    .send({client_id: 'client-id-value' })
    .send({client_secret: 'client-secreat-value' })
    .send({username: 'user' })
    .send({password: 'pass' })
    .set('Accept', 'application/json')
    .end(function(err, res){
    if (err || !res.ok) {           
        // error handling..
    } else {
        // response parsing..
    }); 


I am looking for the node.js module which will take care of oauth token generation. Is there any node js module for the same ?
If possible could you please share the sample as well.

Thanks.

2

2 Answers

0
votes

For token generation you can try either of following:

rand-token or node-hat

0
votes

auth0 provides good libraries. In node, I use jsonwebtoken. If you scroll down the https://jwt.io/ page, they list a ton of libraries and features.

Here is what signing a JWT looks like with their library.

import jwt from "jsonwebtoken";

// setup...

const token = jwt.sign(oPayload, bufferPrivateKey, 
  { algorithm: 'ES256',
    issuer: 'ME-MYSELF-I',
    expiresIn: '30 days'
  });