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

[TRA-15153] Migrer sur le nouveau portail API de l'INSEE #3602

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions .env.model
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ NGINX_NETWORK_MODE=host|bridge
# Secret key to hash token
API_TOKEN_SECRET=*********

# Secret for INSEE SIRENE API
INSEE_SECRET=*********
# ID et SECRET de l'application sur le portail https://portail-api.insee.fr/
INSEE_CLIENT_ID=*********
INSEE_CLIENT_SECRET=*********
# Identifiant et mot de passe de https://portail-api.insee.fr/
INSEE_USERNAME=*************
INSEE_PASSWORD=*************

# Permet de court-circuiter l'API INSEE en cas de maintenance
INSEE_MAINTENANCE=true|false
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ env:
QUEUE_MONITOR_TOKEN: token
DATABASE_URL: "postgresql://trackdechets:password@postgres:5432/e2e?schema=default$default"
API_TOKEN_SECRET: any_secret
INSEE_SECRET: unset
INSEE_CLIENT_ID: unset
INSEE_CLIENT_SECRET: unset
INSEE_USERNAME: unset
INSEE_PASSWORD: unset

SESSION_SECRET: any_secret
SESSION_NAME: connect.sid
SESSION_COOKIE_HOST: trackdechets.local
Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ et le projet suit un schéma de versionning inspiré de [Calendar Versioning](ht

- Documentation API Developers : Page Not Found, si on n'y accède pas via l'arborescence [PR 3621](https://github.com/MTES-MCT/trackdechets/pull/3621)

#### :house: Interne

- Migration vers le nouveau portail API de l'INSEE [PR 3602](https://github.com/MTES-MCT/trackdechets/pull/3602)

# [2024.9.1] 24/09/2024

#### :rocket: Nouvelles fonctionnalités
Expand Down
3 changes: 2 additions & 1 deletion back/src/companies/sirene/insee/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
StatutDiffusionEtablissement
} from "../../../generated/graphql/types";

const SIRENE_API_BASE_URL = "https://api.insee.fr/entreprises/sirene/V3.11";
const SIRENE_API_BASE_URL = "https://api.insee.fr/api-sirene/prive/3.11";

export const SEARCH_COMPANIES_MAX_SIZE = 20;

/**
Expand Down
22 changes: 17 additions & 5 deletions back/src/companies/sirene/insee/token.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
import { redisClient, setInCache } from "../../../common/redis";

const SIRENE_API_TOKEN_URL = "https://api.insee.fr/token";
const SIRENE_API_TOKEN_URL =
"https://auth.insee.net/auth/realms/apim-gravitee/protocol/openid-connect/token";
export const INSEE_TOKEN_KEY = "insee_token";
const { INSEE_SECRET } = process.env;
const { INSEE_CLIENT_SECRET, INSEE_CLIENT_ID, INSEE_USERNAME, INSEE_PASSWORD } =
process.env;

/**
* Generates INSEE Sirene API token
*/
async function generateToken(): Promise<string> {
const headers = {
Authorization: `Basic ${INSEE_SECRET}`,
"Content-Type": "application/x-www-form-urlencoded"
};

// Création des paramètres de la requête avec URLSearchParams
const params = new URLSearchParams();
params.append("grant_type", "password");
params.append("client_id", INSEE_CLIENT_ID);
params.append("client_secret", INSEE_CLIENT_SECRET);
params.append("username", INSEE_USERNAME);
params.append("password", INSEE_PASSWORD);

const response = await axios.post<{ access_token: string }>(
SIRENE_API_TOKEN_URL,
"grant_type=client_credentials",
params,
{ headers }
);

return response.data.access_token;
}

// Le token expire au bout de 300 secondes
const INSEE_TOKEN_EX = 300;

/**
* Generates a token and save it to redis cache
*/
async function renewToken(): Promise<void> {
const token = await generateToken();
await setInCache(INSEE_TOKEN_KEY, token);
await setInCache(INSEE_TOKEN_KEY, token, { EX: INSEE_TOKEN_EX });
}

/**
Expand Down
5 changes: 4 additions & 1 deletion libs/back/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ export const schema = z.object({
TD_COMPANY_ELASTICSEARCH_IGNORE_SSL: z.string().optional().refine(isBoolean),
VERIFY_COMPANY: z.string().refine(isBoolean),
ALLOW_TEST_COMPANY: z.string().refine(isBoolean),
INSEE_SECRET: z.string(),
INSEE_CLIENT_ID: z.string(),
INSEE_CLIENT_SECRET: z.string(),
INSEE_USERNAME: z.string(),
INSEE_PASSWORD: z.string(),
// -------
// S3
S3_ENDPOINT: z.string(),
Expand Down
3 changes: 2 additions & 1 deletion scripts/terraform/terraform.tfvars.model
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
api_environment_secrets = {
API_TOKEN_SECRET="XXXXXXXX"
SESSION_SECRET="XXXXXXXX"
INSEE_SECRET="XXXXXXXX"
INSEE_CLIENT_SECRET="XXXXXXXX"
INSEE_CLIENT_PASSWORD="XXXXXXXX"
TD_COMPANY_ELASTICSEARCH_CACERT=<<EOT
XXXXX
EOT
Expand Down
Loading