Cleaning up

This commit is contained in:
Hampus Kraft
2020-03-26 21:31:03 +01:00
parent 6aaf5949d1
commit 9639081fe1
16 changed files with 126 additions and 122 deletions

2
.prettierignore Normal file
View File

@@ -0,0 +1,2 @@
dist
coverage

View File

@@ -1,103 +1,103 @@
- args:
tables:
- object_relationships:
- name: account
using:
foreign_key_constraint_on: account_id
- name: provider
using:
foreign_key_constraint_on: auth_provider
table:
name: account_providers
schema: auth
- object_relationships:
- name: account
using:
foreign_key_constraint_on: account_id
- name: roleByRole
using:
foreign_key_constraint_on: role
table:
name: account_roles
schema: auth
- array_relationships:
- name: account_providers
using:
foreign_key_constraint_on:
column: account_id
table:
name: account_providers
schema: auth
- name: account_roles
using:
foreign_key_constraint_on:
column: account_id
table:
name: account_roles
schema: auth
- name: refresh_tokens
using:
foreign_key_constraint_on:
column: account_id
table:
name: refresh_tokens
schema: auth
object_relationships:
- name: role
using:
foreign_key_constraint_on: default_role
- name: user
using:
foreign_key_constraint_on: user_id
table:
name: accounts
schema: auth
- array_relationships:
- name: account_providers
using:
foreign_key_constraint_on:
column: auth_provider
table:
name: account_providers
schema: auth
table:
name: providers
schema: auth
- object_relationships:
- name: account
using:
foreign_key_constraint_on: account_id
table:
name: refresh_tokens
schema: auth
- array_relationships:
- name: account_roles
using:
foreign_key_constraint_on:
column: role
table:
name: account_roles
schema: auth
- name: accounts
using:
foreign_key_constraint_on:
column: default_role
table:
name: accounts
schema: auth
table:
name: roles
schema: auth
- array_relationships:
- name: accounts
using:
foreign_key_constraint_on:
column: user_id
table:
name: accounts
schema: auth
table:
name: users
schema: public
- object_relationships:
- name: account
using:
foreign_key_constraint_on: account_id
- name: provider
using:
foreign_key_constraint_on: auth_provider
table:
name: account_providers
schema: auth
- object_relationships:
- name: account
using:
foreign_key_constraint_on: account_id
- name: roleByRole
using:
foreign_key_constraint_on: role
table:
name: account_roles
schema: auth
- array_relationships:
- name: account_providers
using:
foreign_key_constraint_on:
column: account_id
table:
name: account_providers
schema: auth
- name: account_roles
using:
foreign_key_constraint_on:
column: account_id
table:
name: account_roles
schema: auth
- name: refresh_tokens
using:
foreign_key_constraint_on:
column: account_id
table:
name: refresh_tokens
schema: auth
object_relationships:
- name: role
using:
foreign_key_constraint_on: default_role
- name: user
using:
foreign_key_constraint_on: user_id
table:
name: accounts
schema: auth
- array_relationships:
- name: account_providers
using:
foreign_key_constraint_on:
column: auth_provider
table:
name: account_providers
schema: auth
table:
name: providers
schema: auth
- object_relationships:
- name: account
using:
foreign_key_constraint_on: account_id
table:
name: refresh_tokens
schema: auth
- array_relationships:
- name: account_roles
using:
foreign_key_constraint_on:
column: role
table:
name: account_roles
schema: auth
- name: accounts
using:
foreign_key_constraint_on:
column: default_role
table:
name: accounts
schema: auth
table:
name: roles
schema: auth
- array_relationships:
- name: accounts
using:
foreign_key_constraint_on:
column: user_id
table:
name: accounts
schema: auth
table:
name: users
schema: public
version: 2
type: replace_metadata

View File

@@ -38,6 +38,7 @@ async function activateUser({ query }: Request, res: Response): Promise<unknown>
if (!affected_rows) {
console.error('Invalid or expired ticket')
if (REDIRECT_URL_ERROR) {
return res.redirect(302, REDIRECT_URL_ERROR as string)
}

View File

@@ -15,7 +15,6 @@ async function deleteUser({ headers }: Request, res: Response): Promise<unknown>
let hasuraData: HasuraData
const decodedToken = verify(headers.authorization)
const account_id = decodedToken['https://hasura.io/jwt/claims']['x-hasura-user-id']
try {

View File

@@ -1,10 +1,11 @@
/* eslint-disable jest/no-standalone-expect */
import 'jest-extended'
import { AUTO_ACTIVATE, HIBP_ENABLED, SERVER_URL, SMTP_ENABLED } from '@shared/config'
import { HasuraAccountData, generateRandomString } from '@shared/helpers'
import { deleteMailHogEmail, mailHogSearch } from '@shared/test-email'
import { HasuraAccountData, generateRandomString } from '@shared/helpers'
import { request as admin } from '@shared/request'
import { app } from '../../server'
import request from 'supertest'
@@ -40,7 +41,7 @@ it('should tell the account already exists', async () => {
body: { message }
} = await agent.post('/auth/register').send({ email, password })
expect(status).toEqual(400)
expect(message).toEqual('account already exists')
expect(message).toEqual('Account already exists.')
})
/**

View File

@@ -1,6 +1,6 @@
import 'jest-extended'
import { request, account } from '@shared/test-utils'
import { account, request } from '@shared/test-utils'
import { authenticator } from 'otplib'

View File

@@ -1,7 +1,8 @@
import 'jest-extended'
import { request, account } from '@shared/test-utils'
import { HasuraAccountData, generateRandomString } from '@shared/helpers'
import { account, request } from '@shared/test-utils'
import { request as admin } from '@shared/request'
import { selectAccountByEmail } from '@shared/queries'

View File

@@ -3,8 +3,8 @@ import { Request, Response } from 'express'
import { resetPasswordWithOldPasswordSchema, resetPasswordWithTicketSchema } from '@shared/schema'
import {
selectAccountById,
updatePasswordWithTicket,
updatePasswordWithAccountId
updatePasswordWithAccountId,
updatePasswordWithTicket
} from '@shared/queries'
import Boom from '@hapi/boom'

View File

@@ -14,7 +14,7 @@ async function registerAccount({ body }: Request, res: Response): Promise<unknow
const account = await selectAccount(body)
if (account) {
throw Boom.badRequest('account already exists')
throw Boom.badRequest('Account already exists.')
}
await checkHibp(password)

View File

@@ -1,5 +1,5 @@
import { Request, Response } from 'express'
import { AccountData, asyncWrapper, createHasuraJwt, newRefreshExpiry } from '@shared/helpers'
import { Request, Response } from 'express'
import { selectRefreshToken, updateRefreshToken } from '@shared/queries'
import Boom from '@hapi/boom'
@@ -27,20 +27,19 @@ async function refreshToken({ cookies, signedCookies }: Request, res: Response):
}
const refreshTokens = hasuraData.auth_refresh_tokens
if (!refreshTokens || !refreshTokens.length) {
throw Boom.unauthorized('Invalid or expired refresh token.')
}
const new_refresh_token = uuidv4()
const {
account: { id }
} = hasuraData.auth_refresh_tokens[0]
const { account } = hasuraData.auth_refresh_tokens[0]
try {
await request(updateRefreshToken, {
old_refresh_token: refresh_token,
new_refresh_token_data: {
account_id: id,
account_id: account.id,
refresh_token: new_refresh_token,
expires_at: new Date(newRefreshExpiry())
}
@@ -56,7 +55,7 @@ async function refreshToken({ cookies, signedCookies }: Request, res: Response):
})
return res.send({
jwt_token: createHasuraJwt(hasuraData.auth_refresh_tokens[0].account),
jwt_token: createHasuraJwt(account),
jwt_expires_in: newJwtExpiry
})
}

View File

@@ -1,6 +1,6 @@
import 'jest-extended'
import { request, account } from '@shared/test-utils'
import { account, request } from '@shared/test-utils'
it('should refresh the token', async () => {
const { body, status } = await request.post('/auth/token/refresh')

View File

@@ -48,7 +48,7 @@ async function revokeFile(req: Request, res: Response): Promise<unknown> {
try {
await s3.copyObject(new_params).promise()
} catch (e) {
throw Boom.badImplementation('Could not generate token')
throw Boom.badImplementation('Unable to generate token.')
}
// return info about the uploaded file

View File

@@ -11,7 +11,7 @@ import { verify } from '@shared/jwt'
async function uploadFile(req: Request, res: Response): Promise<unknown> {
if (!req.files?.file) {
throw Boom.badRequest('No file')
throw Boom.notFound()
}
// get file being uploaded

View File

@@ -22,7 +22,7 @@ if (RSA_TYPES.includes(JWT_ALGORITHM)) {
jwtKey.toPEM(true)
} catch (error) {
throw Boom.badImplementation(
`Invalid RSA private key in the JWT_SECRET_KEY environment variable.`
'Invalid RSA private key in the JWT_SECRET_KEY environment variable.'
)
}
} else {
@@ -36,7 +36,7 @@ if (RSA_TYPES.includes(JWT_ALGORITHM)) {
}
} else if (SHA_TYPES.includes(JWT_ALGORITHM)) {
if (!jwtKey) {
throw Boom.badImplementation(`Empty JWT secret key.`)
throw Boom.badImplementation('Empty JWT secret key.')
}
} else {
throw Boom.badImplementation(`Invalid JWT algorithm: ${JWT_ALGORITHM}`)
@@ -92,7 +92,7 @@ interface Token {
export function verify(authorization: string | undefined): Token {
try {
if (!authorization) {
throw Boom.unauthorized('Missing Authorization header')
throw Boom.unauthorized('Missing Authorization header.')
}
const token = authorization.replace('Bearer ', '')
return JWT.verify(token, jwtKey) as Token

View File

@@ -1,4 +1,5 @@
import fetch, { Response } from 'node-fetch'
import { SMTP_HOST } from '@shared/config'
interface MailhogEmailAddress {

View File

@@ -1,11 +1,11 @@
import { AUTO_ACTIVATE } from '@shared/config'
import { HasuraAccountData, generateRandomString } from '@shared/helpers'
import { SuperTest, Test, agent } from 'supertest'
import { HasuraAccountData, generateRandomString } from '@shared/helpers'
import { AUTO_ACTIVATE } from '@shared/config'
import { request as admin } from '@shared/request'
import { app } from '../server'
import { selectAccountByEmail } from '@shared/queries'
import { deleteEmailsOfAccount } from '@shared/test-email'
import { selectAccountByEmail } from '@shared/queries'
export let request: SuperTest<Test>