loopback icon indicating copy to clipboard operation
loopback copied to clipboard

will consider use jwt to replace uid2 generate access token?

Open tonnylitao opened this issue 10 years ago • 27 comments

Json Web Tokens seems better than normal 64 length uid2 string.

Why not change to jwt? https://tools.ietf.org/html/rfc7519

tonnylitao avatar Dec 20 '15 15:12 tonnylitao

+1

seanmangar avatar Jun 15 '16 10:06 seanmangar

+1

dilshanraja avatar Jun 19 '16 14:06 dilshanraja

+1 On 19 Jun 2016 20:17, "Dilshan Rajapakse" [email protected] wrote:

+1

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/strongloop/loopback/issues/1905#issuecomment-227001145, or mute the thread https://github.com/notifications/unsubscribe/AHvuCM9hEQ5fsVMi57TeK9NWr-DR3p_uks5qNVZ-gaJpZM4G42WI .

sachinhub avatar Jun 19 '16 14:06 sachinhub

+1

type0lang avatar Aug 21 '16 10:08 type0lang

I agree, at least this should be possible to integrate as a configuration flag or plugin

Freundschaft avatar Jan 26 '17 13:01 Freundschaft

+1

ghost avatar Feb 18 '17 10:02 ghost

We can implement manualy by some scripts. This is my code:

server.js

Disable bearerTokenBase64Encoded by adding this before booting:

app.use(loopback.token({
  model: app.models.accessToken,
  currentUserLiteral: 'me',
  bearerTokenBase64Encoded: false // here
}));

boot

Create a js file (ex: jwt.js) in boot directory and place this code:

'use strict';
const jwt = require('jsonwebtoken');
const secretKey = process.env.JWT_SECRET_KEY || null;

module.exports = function(app) {
  const User = app.models.User;
  const AccessToken = app.models.AccessToken;

  User.prototype.createAccessToken = function(ttl, cb) {
    const userSettings = this.constructor.settings;
    const expiresIn = Math.min(ttl || userSettings.ttl, userSettings.maxTTL);
    const accessToken = jwt.sign({id: this.id}, secretKey, {expiresIn});
    return cb ? cb(null, Object.assign(this, {accessToken})) : {id: accessToken};
  };

  User.logout = function(tokenId, fn) {
    // You may want to implement JWT blacklist here

    fn();
  };

  AccessToken.resolve = function(id, cb) {
    if (id) {
      try {
        const data = jwt.verify(id, secretKey);
        cb(null, {userId: data.id});
      } catch (err) {
        // Should override the error to 401
        cb(err);
      }
    } else {
      cb();
    }
  };
};

Remember to install jsonwebtoken package.

thanh-taro avatar Jun 22 '17 13:06 thanh-taro

@thanh-taro Thanks for your code! But when i have the token, I passed it in header of the request: Authorization JWT jwt_code I get invalid token

Can you help me more here? Thank you

vinhtq avatar Jul 04 '17 07:07 vinhtq

@vinhtq Change Authorization JWT jwt_token to Authorization Bearer jwt_token. Good luck!

thanh-taro avatar Jul 04 '17 07:07 thanh-taro

@thanh-taro Just try few hours ago. When I debug I figure out that

const data = jwt.verify(id, secretKey);
cb(null, {userId: data.id});

It can go here but I still have authorization required error. Seems like we need a way to tell loopback that we use jwt strategy instead right?

vinhtq avatar Jul 04 '17 09:07 vinhtq

@thanh-taro Sorry, I figure out. I config the role wrong for the model I want to get info out.

Now it works.

I am trying to integrate social login also using jwt. Can you give me any idea how to do so?

Basically social account has no username + password!

Thank you!

vinhtq avatar Jul 04 '17 10:07 vinhtq

@vinhtq The idea is: step 1: use social access_token from clients (FE, mobile,...) to get user information from social networks. -> you will have email, name, etc. step 2: use email (or social account ID) to create a new user (or migrate with the old one). step 3: create a JWT token and send it back to clients. You are done.

thanh-taro avatar Jul 04 '17 11:07 thanh-taro

@thanh-taro Thanks! You save my day!

vinhtq avatar Jul 10 '17 03:07 vinhtq

Any updates on that? It should be done for lb3. Using jwt would speed up loopback as user data and all roles would be in the token so no db queries at all.

murbanowicz avatar Sep 05 '17 21:09 murbanowicz

+1 I would like to know about this with respect to lb3 too!

HarisHashim avatar Oct 17 '17 04:10 HarisHashim

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 16 '17 04:12 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Dec 30 '17 04:12 stale[bot]

+1 for native support for jwt

charsi avatar Jan 22 '18 11:01 charsi

+1 for native support for jwt

gantir avatar Feb 08 '18 17:02 gantir

We're working on the next major version of loopback here: https://github.com/strongloop/loopback-next

Make an issue suggesting JWT support, or even better, ask to join the loopback-next group! We're looking for more community contributors to help build the new version. :)

kjdelisle avatar Feb 08 '18 21:02 kjdelisle