Skip to content

Commit

Permalink
refactor(verifyToken): reduce function complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Juiced66 committed Oct 31, 2024
1 parent 899d6e2 commit 71de3c6
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions lib/core/security/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,33 @@ export class TokenRepository extends ObjectRepository<Token> {
}

if (isApiKey) {
const fingerprint = sha256(token);
return this._verifyApiKey(decoded, token)

Check failure on line 365 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Insert `;`

Check failure on line 365 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Missing semicolon
}

let userToken;

try {
userToken = await this.loadForUser(decoded._id, token);
} catch (err) {
if (err instanceof UnauthorizedError) {
throw err;
}
throw securityError.getFrom(err, "verification_error", err.message);
}

if (userToken === null) {
throw securityError.get("invalid");
}

if (userToken.singleUse) {
await this.expire(userToken);
}

return userToken;
}

async _verifyApiKey(decoded, token: string) {
const fingerprint = sha256(token);

const userApiKeys = await ApiKey.search({

Check failure on line 393 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Delete `··`
query: {

Check failure on line 394 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Delete `··`
Expand All @@ -372,11 +398,7 @@ export class TokenRepository extends ObjectRepository<Token> {
},

Check failure on line 398 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Replace `········` with `······`
});

Check failure on line 399 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Delete `··`

if (userApiKeys.length === 0) {
throw securityError.get("invalid");
}

const targetApiKey = userApiKeys.find(
const targetApiKey = userApiKeys?.find(

Check failure on line 401 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Delete `··`
(apiKey) => apiKey.fingerprint === fingerprint,
);

Expand All @@ -395,28 +417,6 @@ export class TokenRepository extends ObjectRepository<Token> {
});

return userToken;
}

let userToken;

try {
userToken = await this.loadForUser(decoded._id, token);
} catch (err) {
if (err instanceof UnauthorizedError) {
throw err;
}
throw securityError.getFrom(err, "verification_error", err.message);
}

if (userToken === null) {
throw securityError.get("invalid");
}

if (userToken.singleUse) {
await this.expire(userToken);
}

return userToken;
}

removeTokenPrefix(token: string) {
Expand Down

0 comments on commit 71de3c6

Please sign in to comment.