Commit 229cd21c authored by Hugo NOUTS's avatar Hugo NOUTS
Browse files

two tokens now

parent f14e6972
......@@ -53,32 +53,39 @@ module.exports = new BaseKonnector(start)
async function start(fields, cozyParameters, doRetry = true) {
// var grdfPCE = ''
log('debug', 'Starting grdf adict konnector')
// if (cozyParameters) log('info', 'CozyParameters found')
// const grdfToken = await authenticate(fields.login, fields.password)
const accountId = getAccountId()
try {
const { access_token } = fields
log('debug', 'KONNECTOR FIELDS : ' + access_token)
// log(
// 'debug',
// 'KONNECTOR ACCOUNT : ' + this._account.oauth_callback_results.id_token )
// const low_scope_access_token = fields.access_token // To use for /donnees-techniques -> fréquence du compteur
const high_scope_access_token = await authenticate(
fields.client_id,
fields.client_secret
)
// is account necessary ? maybe id_token is already in fields from the begining
log('debug', 'KONNECTOR FIELDS : ' + high_scope_access_token)
log(
'debug',
'KONNECTOR ACCOUNT : ' + this._account.oauth_callback_results.id_token
)
if (
this._account &&
this._account.oauth_callback_results &&
this._account.oauth_callback_results.id_token
) {
log('debug', 'time to decode jwt')
// getPCEid(fields.id_token) ?
const id_pce = await getPCEid(
this._account.oauth_callback_results.id_token
) //19108248691849 PCEid FOR TESTING PURPOSE
// Store pce and restart
// const data = [{ id_pce: id_pce }];
log('debug', 'Fetching grdf adict daily data')
log('debug', 'THE ID_PCE SENDING TO GETDATA : ' + id_pce)
const grdfData = await getData(access_token, id_pce)
const grdfData = await getData(high_scope_access_token, id_pce)
if (grdfData.consommation) {
log('debug', 'Process grdf daily data')
......@@ -95,28 +102,21 @@ async function start(fields, cozyParameters, doRetry = true) {
// await addData(data, "io.cozy.accounts", accountId);
// return start(fields, cozyParameters, true);
} else {
// log('debug', 'no id_token found in oauth_callback_results')
// log(
// 'debug',
// 'callback_result contains: ',
// this._account.oauth_callback_results
// )
log('debug', 'no id_token found in oauth_callback_results')
log(
'debug',
'callback_result contains: ',
this._account.oauth_callback_results
)
throw errors.USER_ACTION_NEEDED_OAUTH_OUTDATED
}
} catch (err) {
log('debug', 'ERROR CATCHED IN MAIN')
log('debug', err)
log('debug', typeof(err))
log('debug', typeof(err.message) + err.message)
if (
err
// err.statusCode === 403 ||
// err.code === 403 ||
// err.statusCode === 401 ||
// err.code === 401 ||
// err.message === "403" || err == 403
err.statusCode === 403 ||
err.code === 403 ||
err.statusCode === 401 ||
err.code === 401
) {
log('debug', 'DO RETRY ' + doRetry)
if (doRetry) {
log('debug', 'asking refresh from the stack')
let body
......@@ -145,6 +145,38 @@ async function start(fields, cozyParameters, doRetry = true) {
}
}
async function authenticate(client_id, client_secret) {
let myHeaders = new Headers()
myHeaders.append('Content-Type', 'application/x-www-form-urlencoded')
myHeaders.append('Authorization', 'Basic cG9jX2FwaTpwb2NfYXBp')
let urlencoded = new URLSearchParams()
urlencoded.append('grant_type', 'client_credentials')
urlencoded.append('client_id', client_id)
urlencoded.append('client_secret', client_secret)
urlencoded.append('scope', '/adict/v1')
let requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded,
redirect: 'follow'
}
const rep = await fetch(
'https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf/access_token',
requestOptions
)
.then(response => response.text())
.then(result => {
result = JSON.parse(result)
log('info', result)
return result.access_token
})
.catch(error => log('error', error))
return rep
}
async function getPCEid(tokenToDecrypt) {
// verify a token symmetric - synchronous
log('debug', 'GETPCEID : ' + tokenToDecrypt)
......@@ -172,22 +204,12 @@ async function getData(token, idPCE) {
'&date_fin=' +
endDate
const rep = await fetch(url, requestOptions)
.then(response => {
log('debug', 'RESPONSE ERROR')
// log('debug', response.error())
if(response.status === 403){
throw new Error("403")
}
response.text()
})
.then(response => response.text())
.then(result => {
result = JSON.parse(result)
return result
})
.catch(error => {
log('debug', 'ERROR CATCHED IN GETDATA' + error)
throw error
})
.catch(error => log('error', error))
return rep
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment