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

Disable super admin refresh tokens #4492

Merged
merged 2 commits into from
May 14, 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
39 changes: 39 additions & 0 deletions packages/server/src/oauth/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,45 @@ describe('OAuth2 Token', () => {
expect(res.body['hub.topic']).not.toBeDefined();
expect(res.body['hub.url']).not.toBeDefined();
});

test('Refresh tokens disabled for super admins', async () => {
// Create a super admin project
const { project } = await createTestProject({ project: { superAdmin: true } });

// Create a test user
const email = `test-${randomUUID()}@example.com`;
const password = 'test-password';
await inviteUser({
project,
resourceType: 'Practitioner',
firstName: 'Test',
lastName: 'Test',
email,
password,
});

const res = await request(app).post('/auth/login').type('json').send({
email,
password,
codeChallenge: 'xyz',
codeChallengeMethod: 'plain',
scope: 'openid offline', // Request offline access
});
expect(res.status).toBe(200);

const res2 = await request(app).post('/oauth2/token').type('form').send({
grant_type: 'authorization_code',
code: res.body.code,
code_verifier: 'xyz',
});
expect(res2.status).toBe(200);
expect(res2.body.token_type).toBe('Bearer');
expect(res2.body.scope).toBe('openid offline');
expect(res2.body.expires_in).toBe(3600);
expect(res2.body.id_token).toBeDefined();
expect(res2.body.access_token).toBeDefined();
expect(res2.body.refresh_token).toBeUndefined();
});
});

class MockJoseMultipleMatchingError extends Error {
Expand Down
11 changes: 9 additions & 2 deletions packages/server/src/oauth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,17 @@ export async function setLoginMembership(login: Login, membershipId: string): Pr
logAuthEvent(LoginEvent, project.id as string, membership.profile, login.remoteAddress, AuditEventOutcome.Success);

// Everything checks out, update the login
return systemRepo.updateResource<Login>({
const updatedLogin: Login = {
...login,
membership: createReference(membership),
});
};

if (project.superAdmin) {
// Disable refresh tokens for super admins
updatedLogin.refreshSecret = undefined;
}

return systemRepo.updateResource<Login>(updatedLogin);
}

/**
Expand Down
Loading