Skip to content

Commit

Permalink
refactor: get tokens route / controller separation (#997)
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan authored Sep 20, 2019
1 parent 0808593 commit 6d7f73b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/api/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { VerifyAccountToken } from './accounts/VerifyToken'
import { GetHealth } from './health/GetHealth'

import { CreateToken } from './tokens/CreateToken'
import { GetToken } from './tokens/GetToken'
import { GetTokens } from './tokens/GetTokens'
import { RemoveToken, RemoveTokenSchema } from './tokens/RemoveToken'

import { CreateWork, CreateWorkSchema } from './works/CreateWork'
Expand Down Expand Up @@ -159,7 +159,7 @@ export const Router = ({
router.get(Path.ACCOUNTS_VERIFY_TOKEN, VerifyAccountToken(accountController))

router.post(Path.TOKENS, CreateToken(maxApiTokens, accountController))
router.get(Path.TOKENS, GetToken(accountController))
router.get(Path.TOKENS, GetTokens(accountController))
router.del(Path.TOKENS_TOKENID, validate({ params: RemoveTokenSchema }), RemoveToken(accountController))

router.post(
Expand Down
21 changes: 0 additions & 21 deletions src/api/tokens/GetToken.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/api/tokens/GetTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AccountController } from '../../controllers/AccountController'

export const GetTokens = (accountController: AccountController) => async (
ctx: any,
next: any,
): Promise<void> => {
const { user } = ctx.state

const apiTokens = await accountController.getTokens(user)

ctx.body = {
apiTokens,
}
}
8 changes: 8 additions & 0 deletions src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface AccountController {
email: string,
newPassword: string,
) => Promise<string>
readonly getTokens: (account: Account) => Promise<ReadonlyArray<string>>
readonly addToken: (id: string, network: Network) => Promise<string>
readonly removeToken: (user: Account, tokenId: string) => Promise<void>
readonly poeAddressChallenge: (issuer: string) => Promise<string>
Expand Down Expand Up @@ -270,6 +271,12 @@ export const AccountController = ({
return createJWT({ accountId: id }, Token.Login)
}

const getTokens = async (account: Account) => {
const apiTokensPromise = account.apiTokens.map(({ token }) => Vault.decrypt(token))
const testApiTokensPromise = account.testApiTokens.map(({ token }) => Vault.decrypt(token))
return Promise.all([...apiTokensPromise, ...testApiTokensPromise])
}

const addToken = async (accountId: string, network: Network) => {
const apiToken = await createJWT({ accountId, network }, getApiKeyByNetwork(network))
const testOrMainApiToken = getTokenByNetwork(network, apiToken)
Expand Down Expand Up @@ -348,6 +355,7 @@ export const AccountController = ({
sendPasswordResetEmail,
changePassword,
changePasswordWithToken,
getTokens,
addToken,
removeToken,
poeAddressChallenge,
Expand Down

0 comments on commit 6d7f73b

Please sign in to comment.