Skip to content

Commit

Permalink
69: Renamed workflow -> runer and endpoint -> workflow. Solves
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-chripko committed May 24, 2023
1 parent 4c96713 commit b22dafb
Show file tree
Hide file tree
Showing 20 changed files with 299 additions and 299 deletions.
53 changes: 0 additions & 53 deletions src/logout/endpoint.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/logout/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import workflow from './workflow'
import { endpoint, requestSchema, responseSchema } from './endpoint'
import runner from './runner'
import { workflow, requestSchema, responseSchema } from './workflow'

export { workflow, endpoint, requestSchema, responseSchema }
export { runner, workflow, requestSchema, responseSchema }
18 changes: 18 additions & 0 deletions src/logout/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import jsonwebtoken from 'jsonwebtoken'

import { IJwtPayload } from '../types/interfaces'
import { State } from '../State'

/**
* Logout wokflow method, used in the `Logout.endpoint`
* Invalidates whole refresh token family. Access token is still valid after calling this endpoint.
* @param authHeader
*/
export default async function runner(authHeader: string) {
const [, accessToken] = authHeader.split(' ')

// NOTE: token is valid, cause it already passed through verification (by passport)
const decodedAccessTokenData = <IJwtPayload>jsonwebtoken.decode(accessToken)

await State.getInstance().refreshTokenRepository.invalidateRefreshTokenFamily(decodedAccessTokenData.uid, decodedAccessTokenData.fid)
}
57 changes: 46 additions & 11 deletions src/logout/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import jsonwebtoken from 'jsonwebtoken'
import { Request, Response, NextFunction } from 'express'
import Joi from 'joi'

import { IJwtPayload } from '../types/interfaces'
import { State } from '../State'
import { fullMessagesResponse } from '../utils/joiSchemas'
import { MESSAGE_TYPE } from '../utils/enums'
import runner from './runner'
import { ErrorBuilder } from '../utils/ErrorBuilder'
import { customTFunction } from '../utils/translations'

/**
* Logout wokflow method, used in the `Logout.endpoint`
* Invalidates whole refresh token family. Access token is still valid after calling this endpoint.
* @param authHeader
* Logout endpoint request schema - empty
*/
export default async function workflow(authHeader: string) {
const [, accessToken] = authHeader.split(' ')
export const requestSchema = Joi.object({
body: Joi.object(),
query: Joi.object(),
params: Joi.object()
})

// NOTE: token is valid, cause it already passed through verification (by passport)
const decodedAccessTokenData = <IJwtPayload>jsonwebtoken.decode(accessToken)
/**
* Logout endpoint response schema - full message
*/
export const responseSchema = fullMessagesResponse

/**
* Logout endpoint
* Usage: `router.post('/logout', ApiAuth.guard(), schemaMiddleware(Logout.requestSchema), Logout.endpoint)`
* @param req
* @param res
* @param next
*/
export async function workflow(req: Request, res: Response, next: NextFunction) {
try {
const authHeader = req.headers.authorization

const t = req.t ?? customTFunction
if (!authHeader) {
throw new ErrorBuilder(401, t('Unauthorized'))
}

await runner(authHeader)

await State.getInstance().refreshTokenRepository.invalidateRefreshTokenFamily(decodedAccessTokenData.uid, decodedAccessTokenData.fid)
return res.json({
messages: [
{
type: MESSAGE_TYPE.SUCCESS,
message: t('You were successfully logged out')
}
]
})
} catch (err) {
return next(err)
}
}
53 changes: 0 additions & 53 deletions src/logoutEverywhere/endpoint.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/logoutEverywhere/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import workflow from './workflow'
import { endpoint, requestSchema, responseSchema } from './endpoint'
import runner from './runner'
import { workflow, requestSchema, responseSchema } from './workflow'

export { workflow, endpoint, requestSchema, responseSchema }
export { runner, workflow, requestSchema, responseSchema }
24 changes: 24 additions & 0 deletions src/logoutEverywhere/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import jsonwebtoken from 'jsonwebtoken'

import { IJwtPayload } from '../types/interfaces'
import { State } from '../State'

/**
* Logout from everywhere wokflow method, used in the `Logout.endpoint`
* Invalidates all user refresh tokens by calling `refreshTokenRepository.invalidateUserRefreshTokens`. If this method is not provided and this endpoint is used, library throws exception.
* All users access token are still valid after calling this endpoint.
* @param authHeader
*/
export default async function runner(authHeader: string) {
const [, accessToken] = authHeader.split(' ')

// NOTE: token is valid, cause it already passed through verification (by passport)
const decodedAccessTokenData = <IJwtPayload>jsonwebtoken.decode(accessToken)

const state = State.getInstance()
if (!state.refreshTokenRepository.invalidateUserRefreshTokens) {
throw new Error("'invalidateUserRefreshTokens' is not implemented on UserTokenRepository")
}

await state.refreshTokenRepository.invalidateUserRefreshTokens(decodedAccessTokenData.uid)
}
61 changes: 45 additions & 16 deletions src/logoutEverywhere/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
import jsonwebtoken from 'jsonwebtoken'
import { Request, Response, NextFunction } from 'express'
import Joi from 'joi'

import { IJwtPayload } from '../types/interfaces'
import { State } from '../State'
import { fullMessagesResponse } from '../utils/joiSchemas'
import { MESSAGE_TYPE } from '../utils/enums'
import runner from './runner'
import { ErrorBuilder } from '../utils/ErrorBuilder'
import { customTFunction } from '../utils/translations'

/**
* Logout from everywhere wokflow method, used in the `Logout.endpoint`
* Invalidates all user refresh tokens by calling `refreshTokenRepository.invalidateUserRefreshTokens`. If this method is not provided and this endpoint is used, library throws exception.
* All users access token are still valid after calling this endpoint.
* @param authHeader
* Logout from everywhere endpoint request schema - empty
*/
export default async function workflow(authHeader: string) {
const [, accessToken] = authHeader.split(' ')
export const requestSchema = Joi.object({
body: Joi.object(),
query: Joi.object(),
params: Joi.object()
})

// NOTE: token is valid, cause it already passed through verification (by passport)
const decodedAccessTokenData = <IJwtPayload>jsonwebtoken.decode(accessToken)
/**
* Logout from everywhere endpoint response schema - full message
*/
export const responseSchema = fullMessagesResponse

const state = State.getInstance()
if (!state.refreshTokenRepository.invalidateUserRefreshTokens) {
throw new Error("'invalidateUserRefreshTokens' is not implemented on UserTokenRepository")
}
/**
* Logout from everywhere endpoint
* Usage: `router.post('/logout-everywhere', ApiAuth.guard(), schemaMiddleware(LogoutEverywhere.requestSchema), LogoutEverywhere.endpoint)`
* @param req
* @param res
* @param next
*/
export async function workflow(req: Request, res: Response, next: NextFunction) {
try {
const authHeader = req.headers.authorization

const t = req.t ?? customTFunction
if (!authHeader) {
throw new ErrorBuilder(401, t('Unauthorized'))
}

await state.refreshTokenRepository.invalidateUserRefreshTokens(decodedAccessTokenData.uid)
await runner(authHeader)

return res.json({
messages: [
{
type: MESSAGE_TYPE.SUCCESS,
message: t('You were successfully logged out')
}
]
})
} catch (err) {
return next(err)
}
}
50 changes: 0 additions & 50 deletions src/passwordReset/endpoint.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/passwordReset/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getToken from './getToken'
import guard from './guard'
import { endpoint, requestSchema, responseSchema } from './endpoint'
import { workflow, requestSchema, responseSchema } from './workflow'
import { strategy, strategyVerifyFunction, secretOrKeyProvider } from './strategy'
import workflow from './workflow'
import runner from './runner'

export { getToken, endpoint, requestSchema, responseSchema, guard, strategy, strategyVerifyFunction, secretOrKeyProvider, workflow }
export { getToken, requestSchema, responseSchema, guard, strategy, strategyVerifyFunction, secretOrKeyProvider, workflow, runner }
19 changes: 19 additions & 0 deletions src/passwordReset/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createHash } from '../utils/jwt'
import { State } from '../State'
import { ID } from '../types/interfaces'

/**
* Workflow method used in the `PasswordReset.endpoint`.
* Internally hashes user new password and subsequently call `userRepository.updateUserPassword` with this hash.
* It also invalidates all user refresh tokens, if `userRepository.invalidateUserRefreshTokens` method is provided.
* @param password
* @param userID
*/
export default async function runner(password: string, userID: ID): Promise<void> {
const hash = await createHash(password)

const state = State.getInstance()
await state.userRepository.updateUserPassword(userID, hash)

await state.refreshTokenRepository.invalidateUserRefreshTokens?.(userID)
}
Loading

0 comments on commit b22dafb

Please sign in to comment.