-
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
28 changed files
with
228 additions
and
441 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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ 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 | ||
|
||
|
@@ -24,14 +25,15 @@ module.exports.getClient = async () => { | |
client_secret: config.OIDC_CLIENT_SECRET, | ||
redirect_uris: [config.HOSTNAME_WITH_PROTOCOL + urlCallback], | ||
response_types: ["code"], | ||
// id_token_signed_response_alg (default "RS256") | ||
// token_endpoint_auth_method (default "client_secret_basic") | ||
id_token_signed_response_alg: config.OIDC_ID_TOKEN_SIGNED_ALG, | ||
userinfo_signed_response_alg: config.OIDC_USER_INFO_SIGNED_ALG, | ||
// token_endpoint_auth_method (default "client_secret_basic") | ||
}) | ||
|
||
return client | ||
} | ||
|
||
module.exports.startAuth = async (email, conferenceDurationInMinutes, conferenceDayString, userTimezoneOffset) => { | ||
module.exports.startAuth = async (conferenceDurationInMinutes, conferenceDayString, userTimezoneOffset) => { | ||
const client = await this.getClient() | ||
|
||
/* todo : store the code_verifier in DB. We don't use it for now. | ||
|
@@ -49,20 +51,21 @@ module.exports.startAuth = async (email, conferenceDurationInMinutes, conference | |
const nonce = generators.random(128) | ||
|
||
const redirectUrl = client.authorizationUrl({ | ||
scope: "openid", | ||
scope: "openid uid email", | ||
state, | ||
acr_values: config.OIDC_ACR_VALUES, | ||
/* todo add this back | ||
code_challenge, | ||
code_challenge_method: 'S256', | ||
*/ | ||
nonce, | ||
login_hint: email | ||
// login_hint: email | ||
}) | ||
|
||
// todo write test : null nonce fails | ||
try { | ||
await db.insertOidcRequest(state, nonce, conferenceDurationInMinutes, conferenceDayString, userTimezoneOffset) | ||
console.log(`OIDC request créé pour ${format.hashForLogs(email)}`) | ||
console.log(`OIDC request créé pour state ${state}`) | ||
} catch(err) { | ||
console.log("Error when inserting authrequest token in DB", err) | ||
return { error: "Une erreur interne s'est produite, nous n'avons pas pu créer votre conférence." } | ||
|
@@ -94,18 +97,46 @@ module.exports.finishAuth = async (req) => { | |
return { error: "L'identification a échoué. Entrez votre adresse mail ci-dessous pour recommencer." } | ||
} | ||
|
||
const tokenSet = await client.callback( | ||
config.HOSTNAME_WITH_PROTOCOL + urlCallback, | ||
params, | ||
{ | ||
state: request.state, | ||
nonce: request.nonce | ||
// todo code_verifier: req.session.code_verifier | ||
let tokenSet | ||
try { | ||
tokenSet = await client.callback( | ||
config.HOSTNAME_WITH_PROTOCOL + urlCallback, | ||
params, | ||
{ | ||
state: request.state, | ||
nonce: request.nonce | ||
// todo code_verifier: req.session.code_verifier | ||
} | ||
) | ||
} catch(error){ | ||
console.error("error when requesting token from OIDC", error) | ||
return { error: "L'identification a échoué. Entrez votre adresse mail ci-dessous pour recommencer." } | ||
} | ||
|
||
let userinfo | ||
try { | ||
userinfo = await client.userinfo(tokenSet) | ||
} catch(error){ | ||
console.error("error when requesting userinfo from OIDC", error) | ||
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(!domain || !whitelistedDomains.includes(domain)){ | ||
console.error(`The domain ${domain} is not whitelisted.`) | ||
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 claims = tokenSet.claims() | ||
const email = claims.preferred_username | ||
} catch(e){ | ||
console.error(`error when validating email ${email}`,e) | ||
} | ||
|
||
const user = {id_token: tokenSet.id_token, state: request.state} | ||
|
||
req.session.user = user | ||
|
||
return { | ||
email, | ||
durationInMinutes: request.durationInMinutes, | ||
|
@@ -114,3 +145,8 @@ module.exports.finishAuth = async (req) => { | |
} | ||
} | ||
|
||
module.exports.getLogoutUrl = async({state, id_token_hint}) => { | ||
const client = await this.getClient() | ||
|
||
return client.endSessionUrl({id_token_hint,post_logout_redirect_uri: `${config.HOSTNAME_WITH_PROTOCOL}${urls.landing}`,state}) | ||
} |
Oops, something went wrong.