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

Feat/get trusted device cookie #554

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
70 changes: 48 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
process.env.SENTRY_DSN =
process.env.SENTRY_DSN ||
'https://[email protected]/44'
Expand Down Expand Up @@ -802,11 +803,14 @@ class AmeliConnector extends CookieKonnector {

async classicLogin(fields) {
// First request to get the cookie
const baseReq = await this.request({
url: urlService.getLoginUrl(),
resolveWithFullResponse: true,
followAllRedirects: true
})
const baseReq = await this.request(
// 'https://assure.ameli.fr/PortailAS/appmanager/PortailAS/assure?_somtc=true',
{
url: urlService.getLoginUrl(),
resolveWithFullResponse: true,
followAllRedirects: true
}
)
const baseReqBody = baseReq.body.html()
let nextUrl = baseReq.request.href
const $LoginForm = cheerio.load(baseReqBody)
Expand Down Expand Up @@ -839,9 +843,6 @@ class AmeliConnector extends CookieKonnector {
const loginReq1 = await this.request({
method: 'POST',
url: nextUrl,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
resolveWithFullResponse: true,
form: { ...firstForm }
})
Expand Down Expand Up @@ -887,7 +888,6 @@ class AmeliConnector extends CookieKonnector {
const envoiOTP = 'Recevoir+un+code+de+s%C3%A9curit%C3%A9'
const triggerOTPForm = `${sharedBodyForm}authStep=${getOTPStep}&envoiOTP=${envoiOTP}`
const loginReq2 = await this.request.post({
// method: 'POST',
url: nextUrl,
resolveWithFullResponse: true,
form: triggerOTPForm
Expand All @@ -910,13 +910,34 @@ class AmeliConnector extends CookieKonnector {
const loginReqOTP = await this.request.post({
url: nextUrl,
resolveWithFullResponse: true,
followAllRedirects: true,
followAllRedirects: false,
followRedirect: false,
simple: false,
form: sendOTPForm
})
let nextOTPUrl
if (loginReqOTP.statusCode === 303) {
await this.saveSession()
const $loginreqOTPBody = cheerio.load(loginReqOTP.body.html())
nextOTPUrl = $loginreqOTPBody('a').attr('href')
}
console.log('loginReqOTP', loginReqOTP)
console.log('loginReqOTP', loginReqOTP.body.html())
console.log('loginReqOTP', loginReqOTP.statusCode)
console.log('nextOPTUrl', nextOTPUrl)
const postLoginRedirectReq = await this.request(nextOTPUrl, {
resolveWithFullResponse: true,
followAllRedirects: true
})

document.querySelector('pause')

const loginReqOTPBody = loginReqOTP.body.html()
const $loginOTPStep = cheerio.load(loginReqOTPBody)
if ($loginOTPStep('a[title="Déconnexion du compte ameli"]').length === 0) {
const postLoginRedirectReqBody = postLoginRedirectReq.body.html()
const $postLoginRedirectReqStep = cheerio.load(postLoginRedirectReqBody)
if (
$postLoginRedirectReqStep('a[title="Déconnexion du compte ameli"]')
.length === 0
) {
throw new Error('Something went wrong when asking for OTP code')
}
log('info', 'Login successfull !')
Expand All @@ -926,7 +947,7 @@ class AmeliConnector extends CookieKonnector {

// All the login failed part is has been redone, but for this case, we couldn't tell for sure it's always functional
// So we keeping this arround for later use
// const visibleZoneAlerte = $loginOTPStep('.zone-alerte').filter(
// const visibleZoneAlerte = $postLoginRedirectReqStep('.zone-alerte').filter(
// (i, el) => !$(el).hasClass('invisible')
// )
// // User seems not affiliated anymore to Régime Général
Expand All @@ -937,21 +958,26 @@ class AmeliConnector extends CookieKonnector {
// }

// The user must validate the CGU form
const $cgu = $loginOTPStep('#nouvelles_cgu_1erreurBoxAccepte')
const $cgu = $postLoginRedirectReqStep('#nouvelles_cgu_1erreurBoxAccepte')
if ($cgu.length > 0) {
log('debug', $cgu.attr('content'))
throw new Error('USER_ACTION_NEEDED.CGU_FORM')
}
// Default case. Something unexpected went wrong after the login
if ($loginOTPStep('[title="Déconnexion du compte ameli"]').length !== 1) {
if (
$postLoginRedirectReqStep('[title="Déconnexion du compte ameli"]')
.length !== 1
) {
log('debug', 'Something unexpected went wrong after the login')
if (
$loginOTPStep.html().includes('modif_code_perso_ameli_apres_reinit')
$postLoginRedirectReqStep
.html()
.includes('modif_code_perso_ameli_apres_reinit')
) {
log('info', 'Password renew required, user action is needed')
throw new Error(errors.USER_ACTION_NEEDED)
}
const errorMessage = $loginOTPStep(
const errorMessage = $postLoginRedirectReqStep(
'.centrepage h1, .centrepage h2'
).text()
if (errorMessage) {
Expand All @@ -963,9 +989,9 @@ class AmeliConnector extends CookieKonnector {
) {
throw new Error(errors.VENDOR_DOWN)
} else {
const refreshContent = $loginOTPStep('meta[http-equiv=refresh]').attr(
'content'
)
const refreshContent = $postLoginRedirectReqStep(
'meta[http-equiv=refresh]'
).attr('content')
if (refreshContent) {
log('error', 'refreshContent')
log('error', refreshContent)
Expand All @@ -987,7 +1013,7 @@ class AmeliConnector extends CookieKonnector {
log('debug', 'Logout button not detected, but for an unknown case')
throw new Error(errors.VENDOR_DOWN)
}
return $loginOTPStep
return $postLoginRedirectReqStep
}

// eslint-disable-next-line no-unused-vars
Expand Down