-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow admin login using demo auth (#6808)
This PR introduces a configuration option (`authentication.demoAllowAdminLogin`) that allows you to log in as admin when using demo authentication. To do this, use the username `admin`. ## About the changes The `admin` user currently cannot be accessed in `demo` authentication mode, as the auth mode requires only an email to log in, and the admin user is not created with an email. This change allows for logging in as the admin user only if an `AUTH_DEMO_ALLOW_ADMIN_LOGIN` is set to `true` (or the corresponding `authDemoAllowAdminLogin` config is enabled). <!-- Does it close an issue? Multiple? --> Closes #6398 ### Important files [demo-authentication.ts](https://github.com/Unleash/unleash/compare/main...00Chaotic:unleash:feat/allow_admin_login_using_demo_auth?expand=1#diff-c166f00f0a8ca4425236b3bcba40a8a3bd07a98d067495a0a092eec26866c9f1R25) ## Discussion points Can continue discussion of [this comment](#6447 (comment)) in this PR. --------- Co-authored-by: Thomas Heartman <[email protected]>
- Loading branch information
1 parent
9ba6be6
commit 13aa58e
Showing
8 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import dbInit from '../../test/e2e/helpers/database-init'; | ||
import { IAuthType } from '../server-impl'; | ||
import { setupAppWithCustomAuth } from '../../test/e2e/helpers/test-helper'; | ||
import type { ITestDb } from '../../test/e2e/helpers/database-init'; | ||
import type { IUnleashStores } from '../types'; | ||
|
||
let db: ITestDb; | ||
let stores: IUnleashStores; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('demo_auth_serial'); | ||
stores = db.stores; | ||
}); | ||
|
||
afterAll(async () => { | ||
await db?.destroy(); | ||
}); | ||
|
||
const getApp = (adminLoginEnabled: boolean) => | ||
setupAppWithCustomAuth(stores, () => {}, { | ||
authentication: { | ||
demoAllowAdminLogin: adminLoginEnabled, | ||
type: IAuthType.DEMO, | ||
createAdminUser: true, | ||
}, | ||
}); | ||
|
||
test('the demoAllowAdminLogin flag should not affect regular user login/creation', async () => { | ||
const app = await getApp(true); | ||
return app.request | ||
.post(`/auth/demo/login`) | ||
.send({ email: '[email protected]' }) | ||
.expect(200) | ||
.expect((res) => { | ||
expect(res.body.email).toBe('[email protected]'); | ||
expect(res.body.id).not.toBe(1); | ||
}); | ||
}); | ||
|
||
test('if the demoAllowAdminLogin flag is disabled, using `admin` should have the same result as any other invalid email', async () => { | ||
const app = await getApp(false); | ||
|
||
const nonAdminUsername = 'not-an-email'; | ||
const adminUsername = 'admin'; | ||
|
||
const nonAdminUser = await app.request | ||
.post(`/auth/demo/login`) | ||
.send({ email: nonAdminUsername }); | ||
|
||
const adminUser = await app.request | ||
.post(`/auth/demo/login`) | ||
.send({ email: adminUsername }); | ||
|
||
expect(nonAdminUser.status).toBe(adminUser.status); | ||
|
||
for (const user of [nonAdminUser, adminUser]) { | ||
expect(user.body).toMatchObject({ | ||
error: expect.stringMatching(/^Could not sign in with /), | ||
}); | ||
} | ||
}); | ||
|
||
test('should allow you to login as admin if the demoAllowAdminLogin flag enabled', async () => { | ||
const app = await getApp(true); | ||
return app.request | ||
.post(`/auth/demo/login`) | ||
.send({ email: 'admin' }) | ||
.expect(200) | ||
.expect((res) => { | ||
expect(res.body.id).toBe(1); | ||
expect(res.body.username).toBe('admin'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters