Skip to content

Commit

Permalink
refactor: add utils
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Branje <[email protected]>
  • Loading branch information
jeroenbranje committed Jan 23, 2024
1 parent 9a178be commit 47b18b8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
10 changes: 3 additions & 7 deletions apps/envited.ascs.digital/app/api/user/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { equals, isNil } from 'ramda'
import { isNil } from 'ramda'

import { getServerSession } from '../../../../common/auth/session'
import { db } from '../../../../common/database/queries'
import { Role } from '../../../../common/types'
import { badRequest, internalServerError, ok, unauthorized } from '../../../../common/utils'
import { isOwnUser, userIsIssuedByLoggedInUser } from '../../utils'

export async function GET(request: Request, { params: { id } }: { params: { id: string } }) {
try {
Expand All @@ -16,11 +16,7 @@ export async function GET(request: Request, { params: { id } }: { params: { id:
const connection = await db()
const [user] = await connection.getUserById(id)

if (
!equals(Role.federator)(session?.user.role) &&
!equals(id)(session?.user.pkh) &&
!equals(user.issuerId)(session?.user.pkh)
) {
if (!userIsIssuedByLoggedInUser(user)(session?.user.pkh) && !isOwnUser(user)(session?.user.pkh)) {
return badRequest()
}

Expand Down
40 changes: 40 additions & 0 deletions apps/envited.ascs.digital/app/api/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,44 @@ describe('api/utils', () => {
expect(result).toEqual('AscsUser')
})
})

describe('userIsIssuedByLoggedInUser', () => {
it('should check if user is issued by logged in user', () => {
// when ... we want to check if the user is issued by the session user
const user = {
id: 'USER_ID',
issuerId: 'PKH',
}

const session = {
user: {
pkh: 'PKH'
}
}
// then ... we should get the result as expected
const result = SUT.userIsIssuedByLoggedInUser(user)(session)

expect(result).toEqual(true)
})
})

describe('isOwnUser', () => {
it('should check if logged in user is own user', () => {
// when ... we want to check if the user is the same as session user
const user = {
id: 'PKH',
issuerId: 'ISSUER_ID',
}

const session = {
user: {
pkh: 'PKH'
}
}
// then ... we should get the result as expected
const result = SUT.isOwnUser(user)(session)

expect(result).toEqual(true)
})
})
})
10 changes: 9 additions & 1 deletion apps/envited.ascs.digital/app/api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { pathOr } from 'ramda'
import { equals, pathOr, prop } from 'ramda'

import { Session, User } from '../../common/types/types'

export const extractIdFromCredential = pathOr('', ['credentialSubject', 'id'])

export const extractIssuerIdFromCredential = pathOr('', ['issuer', 'id'])

export const extractTypeFromCredential = pathOr('', ['credentialSubject', 'type'])

export const isOwnUser = (user: User) => (session: Session) =>
equals(prop('id')(user))(pathOr('', ['user', 'pkh'])(session))

export const userIsIssuedByLoggedInUser = (user: User) => (session: Session) =>
equals(prop('issuerId')(user))(pathOr('', ['user', 'pkh'])(session))
11 changes: 11 additions & 0 deletions apps/envited.ascs.digital/common/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ export enum CredentialType {
AscsMember = 'AscsMember',
AscsUser = 'AscsUser',
}

export interface User {
id: string
issuerId: string
}

export interface Session {
user: {
pkh: string
}
}

0 comments on commit 47b18b8

Please sign in to comment.