Skip to content

Commit

Permalink
refactor: update token validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonictw committed Oct 25, 2024
1 parent b451281 commit 204eeca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/routes/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ router.head("/:token_id_prefix/:token_id_suffix",
} = req.params;

// Check token exists by the token ID
const token = await Token.findById(tokenIdPrefix).exec();
if (!token) {
const tokenState = await Token.findById(tokenIdPrefix).exec();
if (!tokenState) {
res.sendStatus(StatusCodes.NOT_FOUND);
return;
}

// Find user by the user ID
const user = await User.findById(token.userId).exec();
const user = await User.findById(tokenState.userId).exec();
if (!user) {
res.sendStatus(StatusCodes.NOT_FOUND);
return;
Expand Down
40 changes: 30 additions & 10 deletions src/utils/xara_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const {
usePrivateKey,
} = require("../init/keypair");

// Import user model
const User = require("../models/user");

// Import token model
const Token = require("../models/token");

Expand Down Expand Up @@ -74,14 +77,14 @@ async function issue(userData) {
};

const userId = userData._id;
const userVersion = userData.revision;
const userRevision = userData.revision;

const privateKey = usePrivateKey();
const guardSecret = getMust("SARA_GUARD_SECRET");

const token = new Token({userId});
const tokenIdPrefix = (await token.save()).id;
const tokenIdSuffix = userVersion;
const tokenIdSuffix = userRevision;
const tokenId = [
tokenIdPrefix,
tokenIdSuffix,
Expand Down Expand Up @@ -114,7 +117,7 @@ function update(token, userData) {
};

const userId = userData._id.toString();
const userVersion = userData.revision;
const userRevision = userData.revision;

const publicKey = usePublicKey();
const privateKey = usePrivateKey();
Expand All @@ -128,8 +131,6 @@ function update(token, userData) {
const {payload: saraTokenPayload} =
verify(originalSaraToken, publicKey, validateOptions);

console.log(userId, saraTokenPayload.sub);

if (userId !== saraTokenPayload.sub) {
throw new Error("unexpect user id");
}
Expand All @@ -148,11 +149,10 @@ function update(token, userData) {
] = saraTokenPayload.jti.split("/", 2);
const tokenId = [
originalTokenIdPrefix,
userVersion,
userRevision,
].join("/");

console.log(originalTokenIdSuffix, userVersion);
if (userVersion <= parseInt(originalTokenIdSuffix)) {
if (userRevision <= parseInt(originalTokenIdSuffix)) {
throw new Error("unexpect user version");
}

Expand All @@ -172,10 +172,11 @@ function update(token, userData) {
* Validate token
* @module sara_token
* @function
* @async
* @param {string} token - The token to valid.
* @return {object}
* @return {Promise<object>}
*/
function validate(token) {
async function validate(token) {
const publicKey = usePublicKey();
const result = {
userId: null,
Expand All @@ -195,6 +196,25 @@ function validate(token) {
throw new Error("unexpect guard token");
}

const [
tokenIdPrefix,
tokenIdSuffix,
] = payload.jti.split("/", 2);

const tokenState = await Token.findById(tokenIdPrefix);
if (!tokenState) {
throw new Error("token not found");
}

const user = await User.findById(tokenState.userId);
if (!user) {
throw new Error("user not found");
}

if (user.revision !== parseInt(tokenIdSuffix)) {
throw new Error("user revision mismatch");
}

result.userId = payload.sub;
result.payload = {
profile: payload.user,
Expand Down

0 comments on commit 204eeca

Please sign in to comment.