-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move DELETE /tokens/:id logic to controller (#965)
- Loading branch information
1 parent
0e56b58
commit b422da3
Showing
4 changed files
with
55 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,15 @@ | ||
import * as Joi from 'joi' | ||
import { verify } from 'jsonwebtoken' | ||
|
||
import { AccountController } from '../../controllers/AccountController' | ||
import { errors } from '../../errors/errors' | ||
import { Account } from '../../models/Account' | ||
import { Vault } from '../../utils/Vault/Vault' | ||
|
||
const getApiTokens = (user: Account) => { | ||
return user.apiTokens.map(({ token }) => token) | ||
} | ||
|
||
const getTestApiTokens = (user: Account) => { | ||
return user.testApiTokens.map(({ token }) => token) | ||
} | ||
|
||
const decryptApiTokens = async (tokens: ReadonlyArray<string>) => { | ||
const allTokens = tokens.map(Vault.decrypt, Vault) | ||
return Promise.all(allTokens) | ||
} | ||
|
||
const encryptApiTokens = async (tokens: ReadonlyArray<string>) => { | ||
const allTokens = tokens.map(Vault.encrypt, Vault) | ||
return Promise.all(allTokens) | ||
} | ||
|
||
export const RemoveTokenSchema = () => ({ | ||
tokenId: Joi.string().required(), | ||
}) | ||
|
||
export const RemoveToken = (accountController: AccountController) => async (ctx: any, next: any): Promise<any> => { | ||
const logger = ctx.logger(__dirname) | ||
const { ResourceNotFound } = errors | ||
|
||
try { | ||
const { user, jwtSecret } = ctx.state | ||
const { tokenId } = ctx.params | ||
|
||
const apiTokensDecrypted = await decryptApiTokens(getApiTokens(user)) | ||
const testApiTokensDecrypted = await decryptApiTokens(getTestApiTokens(user)) | ||
|
||
const apiTokensFiltered = apiTokensDecrypted.filter((token: string) => token !== tokenId) | ||
const testApiTokensFiltered = testApiTokensDecrypted.filter((token: string) => token !== tokenId) | ||
|
||
if ( | ||
apiTokensDecrypted.length === apiTokensFiltered.length && | ||
testApiTokensDecrypted.length === testApiTokensFiltered.length | ||
) { | ||
ctx.status = ResourceNotFound.code | ||
ctx.body = ResourceNotFound.message | ||
return | ||
} | ||
|
||
const { client_token } = verify(tokenId.replace('TEST_', ''), jwtSecret, { | ||
ignoreExpiration: true, | ||
}) as { | ||
email: string | ||
client_token: string | ||
iat: number, | ||
} | ||
|
||
await Vault.revokeToken(client_token) | ||
if (apiTokensDecrypted.length !== apiTokensFiltered.length) { | ||
const apiTokensEncrypted = await encryptApiTokens(apiTokensFiltered) | ||
const updateApiTokens = apiTokensEncrypted.map((token: string) => ({ | ||
token, | ||
})) | ||
|
||
await accountController.updateByIssuer(user.issuer, { apiTokens: updateApiTokens }) | ||
} else { | ||
const testApiTokensEncrypted = await encryptApiTokens(testApiTokensFiltered) | ||
const updateTestApiTokens = testApiTokensEncrypted.map((token: string) => ({ | ||
token, | ||
})) | ||
|
||
await accountController.updateByIssuer(user.issuer, { testApiTokens: updateTestApiTokens }) | ||
} | ||
const { user } = ctx.state | ||
const { tokenId } = ctx.params | ||
|
||
ctx.status = 200 | ||
} catch (exception) { | ||
logger.error(exception, 'api.RemoveToken') | ||
ctx.status = 500 | ||
} | ||
await accountController.removeToken(user, tokenId) | ||
ctx.status = 200 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters