Skip to content

Commit

Permalink
N21-1219 adds isExternalUser to current user
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegns committed Nov 1, 2023
1 parent b1ed486 commit 8b903fd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface CreateJwtPayload {
systemId?: string; // without this the user needs to change his PW during first login
support?: boolean;
// support UserId is missed see featherJS
isExternalUser: boolean;
}

export interface JwtPayload extends CreateJwtPayload {
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/modules/authentication/interface/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export interface ICurrentUser {

/** True if a support member impersonates the user */
impersonated?: boolean;

/** True if the user is an external user e.g. an oauth user */
isExternalUser: boolean;
}

export interface OauthCurrentUser extends ICurrentUser {
/** Contains the idToken of the external idp. Will be set during oAuth2 login and used for rp initiated logout */
externalIdToken?: string;

/** True if the user is an external user e.g. an oauth user */
isExternalUser: true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('CurrentUserMapper', () => {
schoolId: 'dummySchoolId',
userId: 'dummyUserId',
support: true,
isExternalUser: true,
sub: 'dummyAccountId',
jti: 'random string',
aud: 'some audience',
Expand Down Expand Up @@ -219,6 +220,16 @@ describe('CurrentUserMapper', () => {
impersonated: jwtPayload.support,
});
});

it('should return current user with default for isExternalUser', () => {
const { jwtPayload } = setup();

const currentUser = CurrentUserMapper.jwtToICurrentUser(jwtPayload);

expect(currentUser).toMatchObject({
isExternalUser: jwtPayload.isExternalUser,
});
});
});

describe('when JWT is provided without optional claims', () => {
Expand All @@ -228,6 +239,7 @@ describe('CurrentUserMapper', () => {
roles: ['mockRoleId'],
schoolId: 'dummySchoolId',
userId: 'dummyUserId',
isExternalUser: false,
sub: 'dummyAccountId',
jti: 'random string',
aud: 'some audience',
Expand All @@ -251,6 +263,17 @@ describe('CurrentUserMapper', () => {
roles: [jwtPayload.roles[0]],
schoolId: jwtPayload.schoolId,
userId: jwtPayload.userId,
isExternalUser: false,
});
});

it('should return current user with default for isExternalUser', () => {
const { jwtPayload } = setup();

const currentUser = CurrentUserMapper.jwtToICurrentUser(jwtPayload);

expect(currentUser).toMatchObject({
isExternalUser: false,
});
});
});
Expand All @@ -265,6 +288,7 @@ describe('CurrentUserMapper', () => {
schoolId: 'dummySchoolId',
userId: 'dummyUserId',
impersonated: true,
isExternalUser: false,
};

const createJwtPayload: CreateJwtPayload = CurrentUserMapper.mapCurrentUserToCreateJwtPayload(currentUser);
Expand All @@ -276,6 +300,7 @@ describe('CurrentUserMapper', () => {
schoolId: currentUser.schoolId,
userId: currentUser.userId,
support: currentUser.impersonated,
isExternalUser: false,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class CurrentUserMapper {
roles: user.roles.getItems().map((role: Role) => role.id),
schoolId: user.school.id,
userId: user.id,
isExternalUser: false,
};
}

Expand Down Expand Up @@ -45,6 +46,7 @@ export class CurrentUserMapper {
roles: currentUser.roles,
systemId: currentUser.systemId,
support: currentUser.impersonated,
isExternalUser: currentUser.isExternalUser,
};
}

Expand All @@ -56,6 +58,7 @@ export class CurrentUserMapper {
schoolId: jwtPayload.schoolId,
userId: jwtPayload.userId,
impersonated: jwtPayload.support,
isExternalUser: jwtPayload.isExternalUser,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('AuthenticationService', () => {
roles: ['student'],
schoolId: 'mockSchoolId',
userId: 'mockUserId',
isExternalUser: false,
};
await authenticationService.generateJwt(mockCurrentUser);
expect(jwtService.sign).toBeCalledWith(
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/shared/testing/map-user-to-current-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const mapUserToCurrentUser = (
accountId: account ? account.id : new ObjectId().toHexString(),
systemId,
impersonated,
isExternalUser: false,
};

return currentUser;
Expand Down

0 comments on commit 8b903fd

Please sign in to comment.