will consider use jwt to replace uid2 generate access token?
Json Web Tokens seems better than normal 64 length uid2 string.
Why not change to jwt? https://tools.ietf.org/html/rfc7519
+1
+1
+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 .
+1
I agree, at least this should be possible to integrate as a configuration flag or plugin
+1
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 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 Change Authorization JWT jwt_token to Authorization Bearer jwt_token. Good luck!
@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?
@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 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 Thanks! You save my day!
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.
+1 I would like to know about this with respect to lb3 too!
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.
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.
+1 for native support for jwt
+1 for native support for jwt
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. :)