Skip to content
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
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CONNECT_CLIENT_SECRET=connect_client_secret # can be found on scalingo
CONNECT_URL=connect_url # can be found on scalingo
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/ngc # no default
JWT_SECRET=your_secret
MATOMO_SECURE_TOKEN=matomo_token # can be found on scalingo
MATOMO_TOKEN=matomo_token # can be found on scalingo
MATOMO_URL=matomo_url # can be found on scalingo
NODE_ENV=development
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const config = {
matomo: {
url: ensureEnvVar(process.env.MATOMO_URL, 'https://stats.data.gouv.fr'),
token: ensureEnvVar(process.env.MATOMO_TOKEN, ''),
secureToken: ensureEnvVar(process.env.MATOMO_SECURE_TOKEN, ''),
},
redis: {
url: ensureEnvVar(process.env.REDIS_URL, 'redis://localhost:6379'),
Expand Down
27 changes: 11 additions & 16 deletions src/routes/stats/statsRoute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import apicache from 'apicache'
import axios from 'axios'
import type { NextFunction, Request, Response } from 'express'
import express from 'express'
import { config } from '../../config'
Expand Down Expand Up @@ -43,7 +44,6 @@ router
.get(
cache('1 day'),
async (req: Request, res: Response, next: NextFunction) => {
let url
try {
const rawRequestParams = decodeURIComponent(
req.query.requestParams as string
Expand All @@ -62,28 +62,23 @@ router

const authorizedMethod = authorizedMethods.includes(matomoMethod)

const authorizedSiteId = requestParams.get('idSite') === '153'

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

url =
config.thirdParty.matomo.url +
'?' +
requestParams +
'&token_auth=' +
config.thirdParty.matomo.token

console.log('will make matomo request', requestParams)

const response = await fetch(url)
requestParams.set('idSite', '20')

const json = (await response.json()) as {
label: string
subtable: { url: string }[]
}[]
const { data: json } = await axios.post<
{
label: string
subtable: { url: string }[]
}[]
>(config.thirdParty.matomo.url + '?' + requestParams, {
token_auth: config.thirdParty.matomo.secureToken,
})

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