-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const config = require("../config") | ||
|
||
module.exports.getWhitelistedDomains = async () => { | ||
const URL = config.GRIST_API_DOMAINS_URL | ||
const API_KEY = config.GRIST_API_KEY | ||
const result = await fetch(URL, {headers: {Authorization: `Bearer ${API_KEY}`}}) | ||
const data = await result.json() | ||
if(data.status !== "success") { | ||
throw new Error("Error while fetching GRIST API") | ||
} | ||
return data.items | ||
} |
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,40 @@ | ||
const { expect } = require("chai") | ||
const sinon = require("sinon") | ||
|
||
const extractEmailDomain = require("./format") | ||
|
||
describe("format", function() { | ||
|
||
describe("extractEmailDomain", () => { | ||
test("devrait retourner le domaine d\"une adresse email valide", () => { | ||
const email = "[email protected]"; | ||
const domain = extractEmailDomain(email); | ||
expect(domain).toBe("example.com"); | ||
}); | ||
|
||
test("devrait retourner le domaine d\"une adresse email académique", () => { | ||
const email = "[email protected]"; | ||
const domain = extractEmailDomain(email); | ||
expect(domain).toBe("university.edu"); | ||
}); | ||
|
||
test("devrait lever une erreur pour une adresse email sans @", () => { | ||
expect(() => { | ||
extractEmailDomain("invalidEmail"); | ||
}).toThrow("Adresse email invalide"); | ||
}); | ||
|
||
test("devrait lever une erreur pour un domaine manquant", () => { | ||
expect(() => { | ||
extractEmailDomain("john.doe@"); | ||
}).toThrow("Domaine email invalide"); | ||
}); | ||
|
||
test("devrait lever une erreur pour une chaîne vide", () => { | ||
expect(() => { | ||
extractEmailDomain("); | ||
}).toThrow("Adresse email invalide"); | ||
}); | ||
}); | ||
|
||
}) |
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 |
---|---|---|
|
@@ -12,6 +12,8 @@ const { generators, Issuer } = require("openid-client") | |
const config = require("../config.js") | ||
const urls = require("../urls") | ||
const db = require("../lib/db") | ||
const format = require("../lib/format") | ||
const { getWhitelistedDomains } = require("./domains.js") | ||
|
||
const urlCallback = urls.createConf | ||
|
||
|
@@ -119,6 +121,18 @@ module.exports.finishAuth = async (req) => { | |
return { error: "L'identification a échoué. Entrez votre adresse mail ci-dessous pour recommencer." } | ||
} | ||
const email = userinfo.email | ||
|
||
try { | ||
const domain = format.extractEmailDomain(email) | ||
const whitelistedDomains = await getWhitelistedDomains() | ||
if(!whitelistedDomains.includes(domain)){ | ||
throw new Error(`The domain ${domain} is not whitelisted.`) | ||
} | ||
} catch(e){ | ||
console.error(`error when validating email ${email}`,e) | ||
return { error: `L'adresse e-mail ${email} n'est pas autorisée à utiliser ce service. Si vous êtes agent de l'État, contactez-nous à [email protected]` } | ||
} | ||
|
||
const user = {id_token: tokenSet.id_token, state: request.state} | ||
|
||
req.session.user = user | ||
|