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

Keycloak improvements #1417

Merged
merged 3 commits into from
Sep 1, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ describe("KeycloakAuthService", () => {
expect(user).toEqual(dbUser);
});

it("should trim whitespace from username", async () => {
await service.authenticate(" " + TEST_USER + " ", TEST_PASSWORD);
expect(mockHttpClient.post).toHaveBeenCalledWith(
jasmine.anything(),
jasmine.stringContaining(`username=${TEST_USER}&`),
jasmine.anything()
);
});

it("should store access token in memory and refresh token in local storage", async () => {
await service.authenticate(TEST_USER, TEST_PASSWORD);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class KeycloakAuthService extends AuthService {

authenticate(username: string, password: string): Promise<DatabaseUser> {
return this.keycloakReady
.then(() => this.credentialAuth(username, password))
.then(() => this.credentialAuth(username.trim(), password))
.then((token) => this.processToken(token));
}

Expand Down
16 changes: 16 additions & 0 deletions src/app/core/session/session-service/local-session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ describe("LocalSessionService", () => {
expect(localSession.loginState.value).toBe(LoginState.LOGGED_IN);
});

it("should be case-insensitive and ignore spaces in username", async () => {
expect(localSession.loginState.value).toBe(LoginState.LOGGED_OUT);
const user: DatabaseUser = {
name: "UserName",
roles: [],
};
localSession.saveUser(user, TEST_PASSWORD);

await localSession.login(" Username ", TEST_PASSWORD);

expect(localSession.loginState.value).toBe(LoginState.LOGGED_IN);
expect(localSession.getCurrentUser().name).toBe("UserName");

localSession.removeUser("username");
});

it("should fail login with correct username but wrong password", async () => {
await localSession.login(TEST_USER, "wrong password");

Expand Down
9 changes: 7 additions & 2 deletions src/app/core/session/session-service/local-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export class LocalSession extends SessionService {
* @param password Password
*/
public async login(username: string, password: string): Promise<LoginState> {
const user: LocalUser = JSON.parse(window.localStorage.getItem(username));
const user: LocalUser = JSON.parse(
window.localStorage.getItem(username.trim().toLowerCase())
);
if (user) {
if (passwordEqualsEncrypted(password, user.encryptedPassword)) {
await this.handleSuccessfulLogin(user);
Expand Down Expand Up @@ -118,7 +120,10 @@ export class LocalSession extends SessionService {
roles: user.roles,
encryptedPassword: encryptPassword(password),
};
window.localStorage.setItem(localUser.name, JSON.stringify(localUser));
window.localStorage.setItem(
localUser.name.trim().toLowerCase(),
JSON.stringify(localUser)
);
// Update when already logged in
if (this.getCurrentUser()?.name === localUser.name) {
this.currentDBUser = localUser;
Expand Down
4 changes: 1 addition & 3 deletions src/assets/keycloak.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"realm": "ndb-dev",
"realm": "dev",
"auth-server-url": "https://keycloak.aam-digital.com/",
"ssl-required": "external",
"resource": "app",
"public-client": true,
"verify-token-audience": true,
"use-resource-role-mappings": true,
"confidential-port": 0
}