Skip to content

Commit

Permalink
feature(fouloscopieAuthGuard): check for email validity
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Nov 14, 2021
1 parent 857842d commit d553daf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions back/flag-service/src/user/guards/FouloscopieAuthGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export class FouloscopieAuthGuard implements CanActivate {
if (!await directus.auth.static(token)) {
throw new InvalidDirectusTokenError();
}

request.userId = (await directus.users.me.read({ fields: 'id' })).id;

const user = (await directus.users.me.read({ fields: ['id', 'email_valid'] }));
if (!user.email_valid) {
return false;
}
request.userId = user.id;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('FouloscopieAuthGuard', () => {
} as ExecutionContext;
}

function testDirectusStaticAuth(handler: Function, token: string | undefined, allows: boolean, shouldCallDirectusApi: boolean, shouldPopulateRequestFields: boolean) {
function testDirectusStaticAuth(handler: Function, token: string | undefined, allows: boolean, shouldCallDirectusApi: boolean, shouldPopulateRequestFields: boolean, isEmailValid = true) {
function doesNotPopulateFields(context: ExecutionContext) {
it('does not add userId field to request', async () => {
const userId = context.switchToHttp().getRequest().userId;
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('FouloscopieAuthGuard', () => {
me: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async read(query?: QueryOne<UserItem<TypeOf<any, "directus_users">>>): Promise<PartialItem<UserItem<TypeOf<any, "directus_users">>>> {
return { id: USER_ID_SAMPLE };
return { id: USER_ID_SAMPLE, email_valid: isEmailValid };
},
},
},
Expand Down Expand Up @@ -132,6 +132,9 @@ describe('FouloscopieAuthGuard', () => {
describe('Denies access for invalid Directus static token', () => {
testDirectusStaticAuth(Test.defaultRoute, INVALID_DIRECTUS_TOKEN, false, true, false);
});
describe('Denies access for unverified email', () => {
testDirectusStaticAuth(Test.defaultRoute, INVALID_DIRECTUS_TOKEN, false, true, false, false);
});
describe('Denies access without Directus static token', () => {
testDirectusStaticAuth(Test.defaultRoute, undefined, false, false, false);
});
Expand Down
2 changes: 1 addition & 1 deletion back/flag-service/test/flag.spec-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Flag (e2e)', () => {
me: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async read(query?: QueryOne<UserItem<TypeOf<any, "directus_users">>>): Promise<PartialItem<UserItem<TypeOf<any, "directus_users">>>> {
return { id: USER_ID_SAMPLE };
return { id: USER_ID_SAMPLE, email_valid: true };
},
},
},
Expand Down

0 comments on commit d553daf

Please sign in to comment.