Skip to content

Commit 8924128

Browse files
committed
🔐 move to stats.beta (NGC-1912)
1 parent 420a676 commit 8924128

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CONNECT_CLIENT_SECRET=connect_client_secret # can be found on scalingo
66
CONNECT_URL=connect_url # can be found on scalingo
77
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/ngc # no default
88
JWT_SECRET=your_secret
9+
MATOMO_SECURE_TOKEN=matomo_token # can be found on scalingo
910
MATOMO_TOKEN=matomo_token # can be found on scalingo
1011
MATOMO_URL=matomo_url # can be found on scalingo
1112
NODE_ENV=development

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const config = {
4747
matomo: {
4848
url: ensureEnvVar(process.env.MATOMO_URL, 'https://stats.data.gouv.fr'),
4949
token: ensureEnvVar(process.env.MATOMO_TOKEN, ''),
50+
secureToken: ensureEnvVar(process.env.MATOMO_SECURE_TOKEN, ''),
5051
},
5152
redis: {
5253
url: ensureEnvVar(process.env.REDIS_URL, 'redis://localhost:6379'),

src/routes/stats/statsRoute.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import apicache from 'apicache'
2+
import axios from 'axios'
23
import type { NextFunction, Request, Response } from 'express'
34
import express from 'express'
45
import { config } from '../../config'
@@ -43,7 +44,6 @@ router
4344
.get(
4445
cache('1 day'),
4546
async (req: Request, res: Response, next: NextFunction) => {
46-
let url
4747
try {
4848
const rawRequestParams = decodeURIComponent(
4949
req.query.requestParams as string
@@ -62,28 +62,23 @@ router
6262

6363
const authorizedMethod = authorizedMethods.includes(matomoMethod)
6464

65-
const authorizedSiteId = requestParams.get('idSite') === '153'
65+
const authorizedSiteId = requestParams.get('idSite') === '20'
6666

6767
if (!authorizedMethod || !authorizedSiteId) {
6868
res.statusCode = 401
6969
return next('Error. Not Authorized')
7070
}
7171

72-
url =
73-
config.thirdParty.matomo.url +
74-
'?' +
75-
requestParams +
76-
'&token_auth=' +
77-
config.thirdParty.matomo.token
78-
7972
console.log('will make matomo request', requestParams)
8073

81-
const response = await fetch(url)
82-
83-
const json = (await response.json()) as {
84-
label: string
85-
subtable: { url: string }[]
86-
}[]
74+
const { data: json } = await axios.post<
75+
{
76+
label: string
77+
subtable: { url: string }[]
78+
}[]
79+
>(config.thirdParty.matomo.url + '?' + requestParams, {
80+
token_auth: config.thirdParty.matomo.secureToken,
81+
})
8782

8883
// Remove secret pages that would reveal groupe names that should stay private
8984
if (rawRequestParams.includes('Page')) {

0 commit comments

Comments
 (0)