Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update user activation route with proper scopes #8117

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions packages/gateway/src/features/user/root-resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,19 @@ describe('User root resolvers', () => {
})
})
describe('activateUser mutation', () => {
const regsiterToken = jwt.sign(
{ scope: ['SCOPES.REGISTER'] },
readFileSync('./test/cert.key'),
{
subject: 'ba7022f0ff4822',
algorithm: 'RS256',
issuer: 'opencrvs:auth-service',
audience: 'opencrvs:gateway-user'
}
)
const newUserHeaders = {
Authorization: `Bearer ${regsiterToken}`
}
it('activates the pending user', async () => {
fetch.mockResponses(
[
Expand All @@ -677,7 +690,7 @@ describe('User root resolvers', () => {
securityQNAs: [{ questionKey: 'HOME_TOWN', answer: 'test' }]
},
{
headers: undefined
headers: newUserHeaders
}
)

Expand All @@ -699,13 +712,51 @@ describe('User root resolvers', () => {
securityQNAs: [{ questionKey: 'HOME_TOWN', answer: 'test' }]
},
{
headers: undefined
headers: newUserHeaders
}
)
).rejects.toThrowError(
"Something went wrong on user-mgnt service. Couldn't activate given user"
)
})
it('fails to activate user if user is not token owner', async () => {
const regsiterToken = jwt.sign(
{ scope: ['SCOPES.REGISTER'] },
readFileSync('./test/cert.key'),
{
subject: 'abcdefgh',
algorithm: 'RS256',
issuer: 'opencrvs:auth-service',
audience: 'opencrvs:gateway-user'
}
)
const newUserHeaders = {
Authorization: `Bearer ${regsiterToken}`
}
fetch.mockResponses(
[
JSON.stringify({
userId: 'abcdefgh'
}),
{ status: 201 }
],
[JSON.stringify({})]
)

return expect(
resolvers.Mutation!.activateUser(
{},
{
userId: 'ba7022f0ff4822',
password: 'test',
securityQNAs: [{ questionKey: 'HOME_TOWN', answer: 'test' }]
},
{
headers: newUserHeaders
}
)
).rejects.toThrowError('User can not be activated')
})
})

describe('changePassword mutation', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/gateway/src/features/user/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ export const resolvers: GQLResolver = {
{ userId, password, securityQNAs },
{ headers: authHeader }
) {
if (
!isTokenOwner(authHeader, userId) &&
!hasScope(authHeader, SCOPES.USER_UPDATE)
)
throw new Error('User can not be activated')

const res = await fetch(`${USER_MANAGEMENT_URL}activateUser`, {
method: 'POST',
body: JSON.stringify({ userId, password, securityQNAs }),
Expand Down
3 changes: 0 additions & 3 deletions packages/user-mgnt/src/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,6 @@ export const getRoutes = () => {
options: {
tags: ['api'],
description: 'Activate an existing pending user',
auth: {
scope: [SCOPES.USER_UPDATE]
},
validate: {
payload: activateUserRequestSchema
}
Expand Down
Loading