Skip to content

Commit

Permalink
refactor: interface JWTData (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan authored Jun 6, 2019
1 parent e139c4b commit 0e56b58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../errors/errors'
import { tokenMatch } from '../helpers/token'
import { uuid4 } from '../helpers/uuid'
import { isJWTData, JWTData } from '../interfaces/JWTData'
import { Network } from '../interfaces/Network'
import { Account } from '../models/Account'
import { processPassword, passwordMatches } from '../utils/Password'
Expand Down Expand Up @@ -84,9 +85,7 @@ export const AccountController = ({
}: Arguments): AccountController => {
const authorizeRequest = async (token: string) => {
try {
const decoded = verify(token.replace('TEST_', ''), configuration.jwtSecret)
const { client_token, email } = decoded as any

const { client_token, email } = decodeJWT(token)
const tokenData = await Vault.verifyToken(client_token)
const account = await findByEmail(email)
return { jwt: configuration.jwtSecret, tokenData, account }
Expand Down Expand Up @@ -273,6 +272,17 @@ export const AccountController = ({
return sign({ email, client_token, network }, configuration.jwtSecret)
}

const decodeJWT = (token: string): JWTData => {
const decoded: unknown = verify(token.replace('TEST_', ''), configuration.jwtSecret)

if (!isJWTData(decoded)) {
logger.error({ decoded }, 'Unrecognized JWT')
throw new Error(`Unrecognized JWT`)
}

return decoded
}

const poeAddressChallenge = async (issuer: string) => {
const { email } = await accountDao.findOne({ issuer })
const poeAddressMessage = `Proof of POE ${email} ${new Date().toISOString()}`
Expand Down
11 changes: 11 additions & 0 deletions src/interfaces/JWTData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Network } from './Network'

export interface JWTData {
readonly iat: number
readonly client_token: string
readonly email: string
readonly network?: Network
}

export const isJWTData = (a: any): a is JWTData =>
typeof a === 'object' && typeof(a.client_token) === 'string' || typeof(a.email) === 'string'

0 comments on commit 0e56b58

Please sign in to comment.