fix idNot auth connexion (#70)

This commit is contained in:
Arnaud D. Natali 2023-09-20 18:49:55 +02:00 committed by GitHub
commit 721f5b6cb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 33 deletions

View File

@ -93,8 +93,6 @@ export default class DocumentsController extends ApiController {
try {
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body);
console.log(documentEntity);
//validate document
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });

View File

@ -90,7 +90,6 @@ export default class FilesController extends ApiController {
//init File resource with request body values
const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"]));
console.log(fileEntity);
//validate File
// await validateOrReject(fileEntity, { groups: ["createFile"] });

View File

@ -7,7 +7,6 @@ import { JwtPayload } from "jsonwebtoken";
import IdNotService from "@Services/common/IdNotService/IdNotService";
@Controller()
@Service()
export default class UserController extends ApiController {
@ -29,6 +28,10 @@ export default class UserController extends ApiController {
const idNotToken = await this.idNotService.getIdNotToken(code);
const user = await this.idNotService.getOrCreateUser(idNotToken);
if(!user) {
this.httpUnauthorized(response);
return;
}
await this.idNotService.updateUser(user.uid);
await this.idNotService.updateOffice(user.office_uid);
@ -36,7 +39,7 @@ export default class UserController extends ApiController {
const accessToken = this.authService.generateAccessToken(payload);
const refreshToken = this.authService.generateRefreshToken(payload);
this.httpSuccess(response, {accessToken, refreshToken});
this.httpSuccess(response, { accessToken, refreshToken });
} catch (error) {
console.log(error);
this.httpInternalError(response);
@ -77,7 +80,7 @@ export default class UserController extends ApiController {
let accessToken;
this.authService.verifyRefreshToken(token, (err, userPayload) => {
if (err) {
console.log(err)
console.log(err);
this.httpUnauthorized(response);
return;
}
@ -89,7 +92,7 @@ export default class UserController extends ApiController {
});
//success
this.httpSuccess(response, {accessToken});
this.httpSuccess(response, { accessToken });
} catch (error) {
this.httpInternalError(response);
return;

View File

@ -39,7 +39,7 @@ export default class CronService {
}
}
public async updateUsers() {
const cronJob = new CronJob("*/15 * * * *", async () => { // Every 15 minutes
const cronJob = new CronJob("0 0 * * *", async () => { // Once a day at midnight
try {
await this.idNotService.updateOffices();
await this.idNotService.updateUsers();

View File

@ -62,6 +62,9 @@ interface IOfficeData {
statutEntite: {
name: string;
};
typeEntite: {
name: string;
}
}
interface IOfficeLocation {
@ -165,19 +168,43 @@ export default class IdNotService extends BaseService {
const searchParams = new URLSearchParams({
key: this.variables.IDNOT_API_KEY,
});
const userRawData = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` +
let userData = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` +
searchParams,
{
method: "GET",
},
)).json() as any;
if (userRawData.totalResultCount === 0) {
await this.userService.updateCheckedAt(user.uid!);
//await this.userService.delete(user.uid!);
return;
)).json() as IRattachementData;
if (!userData.statutDuRattachement) {
const rattachements = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` +
searchParams,
{
method: "GET",
},
)).json() as any;
if (rattachements.totalResultCount === 0) {
await this.userService.updateCheckedAt(user.uid!);
//await this.userService.delete(user.uid!);
return;
}
const rattachementsResults = rattachements.result as IRattachementData[];
rattachementsResults.forEach(async (rattachement) => {
if (rattachement.statutDuRattachement) {
const officeData = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` +
searchParams,
{
method: "GET",
},
)).json() as IOfficeData;
if(officeData.typeEntite.name === "office") {
userData = rattachement;
}
}
});
}
const userData = userRawData.result[0] as IRattachementData;
const roleFromIdNot = await this.getRole(userData.typeLien.name);
let updates = 0;
@ -186,24 +213,22 @@ export default class IdNotService extends BaseService {
user.role = roleFromIdNot;
}
if (user.office_membership!.idNot !== userData.entiteUrl.split("/")[5]!) {
if (user.office_membership!.idNot !== userData.entite.ou) {
updates++;
let officeData = (await this.officeService.get({ where: { idNot: userData.entiteUrl.split("/")[5]! } }))[0];
let officeData = (await this.officeService.get({ where: { idNot:userData.entite.ou } }))[0];
if (!officeData) {
const officeIdNotData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entiteUrl}?` + searchParams, { method: "GET" })
).json()) as IOfficeData;
const officeLocationData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
).json()) as IOfficeLocation;
const office = {
idNot: userData.entiteUrl.split("/")[5]!,
name: officeIdNotData.denominationSociale
? officeIdNotData.denominationSociale
: `office ${userData.entiteUrl.split("/")[5]!}`,
crpcen: officeIdNotData.codeCrpcen,
office_status: this.getOfficeStatus(officeIdNotData.statutEntite.name),
idNot: userData.entite.ou,
name: userData.entite.denominationSociale,
crpcen: userData.entite.codeCrpcen,
office_status: this.getOfficeStatus(userData.entite.statutEntite.name),
address: {
address: officeIdNotData.departementResidence[0]!.libelle, //officeLocationData.result[0]!.adrPostale4,
city: "city", //officeLocationData.result[0]!.adrPostaleVille,
zip_code: Number(officeIdNotData.departementResidence[0]!.code),
address: officeLocationData.result[0]!.adrGeo4,
city: officeLocationData.result[0]!.adrGeoVille.split(" ")[0] ?? officeLocationData.result[0]!.adrGeoVille, //officeLocationData.result[0]!.adrPostaleVille,
zip_code: Number(officeLocationData.result[0]!.adrGeoCodePostal),
created_at: null,
updated_at: null,
},
@ -272,10 +297,19 @@ export default class IdNotService extends BaseService {
})
).json()) as IRattachementData;
if(!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") {
return null;
}
const officeLocationData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
).json()) as IOfficeLocation;
// if(officeLocationData.result[0]!.adrGeoCodePostal.slice(0,2) !== "35") {
// return null;
// }
const role = await this.getRole(userData.typeLien.name);
const userToAdd = {