Skip to content

Commit acfcbd6

Browse files
Merge pull request #870 from matomo-org/PG-4998-oauth-guide
Adds guide for OAuth2 plugin, #PG-4998
2 parents 69f06b1 + 0ded9a3 commit acfcbd6

10 files changed

Lines changed: 410 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
/docs/changelog.md
1313
/docs/changelog-*.md
1414
/docs/cache/
15-
/docs/*/cache/
15+
/docs/*/cache/
16+
.codex

app/helpers/Content/Category/IntegrateCategory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function getItems()
3131
new Guide('reporting-introduction'),
3232
new RemoteLink('Integrations', 'https://matomo.org/integrate/'),
3333
new Guide('tagmanager/introduction'),
34+
new Guide('oauth2'),
3435
new Guide('ab-tests'),
3536
new Guide('heatmap-session-recording'),
3637
new Guide('crash-analytics'),

docs/5.x/authentication-in-depth.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ There are different ways a user can authenticate in Matomo:
99
* Using username / password and the regular login form.
1010
* The [logme](https://matomo.org/faq/how-to/faq_30/) feature can be used to log someone in using username and password in the URL.
1111
* Using a `token_auth` URL parameter for our HTTP API's and widgets.
12+
* Using OAuth 2.0 bearer tokens for external applications when the OAuth 2.0 plugin is installed.
1213

1314
## Username and password login
1415

@@ -64,6 +65,14 @@ When there is a request and we can use a session, then Matomo [checks first if t
6465
* When a `token_auth` parameter is set by us, then we usually POST the token_auth. This is for security reasons so the token_auth won't appear in server logs. Otherwise a sysadmin could see the token in the logs and do all sort of actions on behalf of another user.
6566
* Remember that users should not share the token_auth as it is the same as them sharing their username/password.
6667

68+
## OAuth 2.0
69+
70+
If the [OAuth 2.0](/guides/oauth2) plugin is installed, external applications can authenticate against Matomo using OAuth 2.0 access tokens.
71+
72+
This is mainly useful when integrating third-party or custom applications that should not receive a long-lived `token_auth`. In that case the application first obtains an access token through the plugin's OAuth 2.0 endpoints and then sends that token as a bearer token when calling supported Matomo APIs.
73+
74+
Depending on the application type, you would typically use either the Authorization Code flow with PKCE or the Client Credentials flow. For the integration flow and request examples, see the [OAuth 2.0 guides](/guides/oauth2).
75+
6776
## Alternative login plugins
6877

6978
It is possible to write plugins that provide [alternative Login methods](https://plugins.matomo.org/search?query=login&post_type=product) like [LDAP](https://plugins.matomo.org/LoginLdap), [SAML](https://plugins.matomo.org/LoginSaml), etc.

docs/5.x/oauth2.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
category: Integrate
3+
subGuides:
4+
- oauth2/setup
5+
- oauth2/authorization-code
6+
- oauth2/client-credentials
7+
- oauth2/api-usage
8+
- oauth2/faq
9+
---
10+
# OAuth 2.0
11+
12+
This section contains guides that will help you authenticate external applications against Matomo using OAuth 2.0.
13+
14+
The **OAuth 2.0** plugin adds a first-party OAuth 2.0 Authorization Server to Matomo. It allows external applications to access Matomo APIs using OAuth 2.0 access tokens instead of sending a `token_auth`.
15+
16+
The plugin supports the **Authorization Code** flow with **PKCE**, **Client Credentials**, and **Refresh Token** support. Applications authenticate with bearer tokens and can be limited to the scopes granted to each OAuth client.
17+
18+
OAuth 2.0 clients can be managed in Matomo under **Administration => Platform => OAuth 2.0**. In Matomo Cloud, this screen is available under **Administration => Export => OAuth 2.0**. From there you can create, edit, pause, resume, or delete clients, and rotate secrets for confidential clients.
19+
20+
If you are looking for general API authentication details, also see [Authentication In Depth](/guides/authentication-in-depth) and [Querying the Reporting API](/guides/querying-the-reporting-api).

docs/5.x/oauth2/api-usage.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
category: Integrate
3+
title: OAuth 2.0 API Usage
4+
---
5+
# Calling Matomo APIs with OAuth 2.0
6+
7+
Once your application has obtained an access token, it can call Matomo APIs using the `Authorization` header.
8+
9+
```text
10+
Authorization: Bearer ACCESS_TOKEN
11+
```
12+
13+
## Example API request
14+
15+
```bash
16+
curl 'https://matomo.example.com/index.php' \
17+
-H 'Authorization: Bearer ACCESS_TOKEN' \
18+
-d 'module=API' \
19+
-d 'method=VisitsSummary.get' \
20+
-d 'idSite=1' \
21+
-d 'period=day' \
22+
-d 'date=today' \
23+
-d 'format=json'
24+
```
25+
26+
## OAuth 2.0 compared to `token_auth`
27+
28+
By default, many Matomo API guides use `token_auth` examples because `token_auth` is available in every Matomo installation.
29+
30+
When the OAuth 2.0 plugin is installed, external applications can use OAuth 2.0 bearer tokens instead. This avoids sharing a long-lived auth token with the external application, lets you choose a grant type that matches the integration, and makes it easier to limit and revoke access without affecting other applications.
31+
32+
If you are integrating a backend service with no user interaction, the Client Credentials flow is usually the best fit. If your application acts on behalf of a user, use the Authorization Code flow.
33+
34+
## Notes
35+
36+
* Use HTTPS whenever you send access tokens.
37+
* The plugin currently allows only one scope per request.
38+
* Keep using the standard `token_auth` flow in integrations where the OAuth 2.0 plugin is not installed.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
category: Integrate
3+
title: OAuth 2.0 Authorization Code Flow
4+
---
5+
# OAuth 2.0 Authorization Code Flow
6+
7+
Use the Authorization Code flow when your application acts on behalf of a Matomo user.
8+
9+
## Flow overview
10+
11+
The Authorization Code flow works like this:
12+
13+
1. Your application redirects the user to the Matomo authorization endpoint.
14+
2. The user logs in, reviews the requested permissions, and approves access.
15+
3. Matomo redirects back with an authorization `code`.
16+
4. Your application exchanges the code for an access token.
17+
5. Your application calls Matomo APIs using `Authorization: Bearer ACCESS_TOKEN`.
18+
19+
## PKCE for public clients
20+
21+
Public clients should use PKCE. PKCE requires a `code_verifier` and a derived `code_challenge`.
22+
23+
Example values:
24+
25+
```text
26+
code_verifier = dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
27+
code_challenge = E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
28+
```
29+
30+
Where:
31+
32+
```text
33+
code_challenge = BASE64URL(SHA256(code_verifier))
34+
```
35+
36+
## Redirect the user to the authorization endpoint
37+
38+
### Public client with PKCE
39+
40+
```text
41+
https://matomo.example.com/index.php?module=OAuth2&action=authorize
42+
&response_type=code
43+
&client_id=analytics_app
44+
&redirect_uri=https://example-app.com/oauth/callback
45+
&scope=matomo:read
46+
&state=abc123
47+
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
48+
&code_challenge_method=S256
49+
```
50+
51+
### Confidential client
52+
53+
```text
54+
https://matomo.example.com/index.php?module=OAuth2&action=authorize
55+
&response_type=code
56+
&client_id=analytics_app
57+
&redirect_uri=https://example-app.com/oauth/callback
58+
&scope=matomo:read
59+
&state=abc123
60+
```
61+
62+
The user will:
63+
64+
1. Log in to Matomo.
65+
2. Review the requested permissions.
66+
3. Click **Allow**.
67+
68+
After approval, Matomo redirects back to your application:
69+
70+
```text
71+
https://example-app.com/oauth/callback?code=AUTHORIZATION_CODE&state=abc123
72+
```
73+
74+
## Exchange the authorization code for tokens
75+
76+
The requested scope needs to match the client configuration. At the time of writing, the plugin allows only one scope per request.
77+
78+
### PKCE token request
79+
80+
```bash
81+
curl -X POST 'https://matomo.example.com/index.php?module=OAuth2&action=token' \
82+
-H 'Content-Type: application/x-www-form-urlencoded' \
83+
-d 'grant_type=authorization_code' \
84+
-d 'client_id=analytics_app' \
85+
-d 'redirect_uri=https://example-app.com/oauth/callback' \
86+
-d 'code=AUTHORIZATION_CODE' \
87+
-d 'code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'
88+
```
89+
90+
### Confidential client token request
91+
92+
```bash
93+
curl -X POST 'https://matomo.example.com/index.php?module=OAuth2&action=token' \
94+
-H 'Content-Type: application/x-www-form-urlencoded' \
95+
-d 'grant_type=authorization_code' \
96+
-d 'client_id=analytics_app' \
97+
-d 'client_secret=7fa9c0f81b8b4a12' \
98+
-d 'redirect_uri=https://example-app.com/oauth/callback' \
99+
-d 'code=AUTHORIZATION_CODE'
100+
```
101+
102+
## Example token response
103+
104+
```json
105+
{
106+
"token_type": "Bearer",
107+
"expires_in": 3600,
108+
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
109+
"refresh_token": "def50200a1b9"
110+
}
111+
```
112+
113+
## Refresh an access token
114+
115+
Use a refresh token to obtain a new access token.
116+
117+
### Public client
118+
119+
```bash
120+
curl -X POST 'https://matomo.example.com/index.php?module=OAuth2&action=token' \
121+
-H 'Content-Type: application/x-www-form-urlencoded' \
122+
-d 'grant_type=refresh_token' \
123+
-d 'client_id=analytics_app' \
124+
-d 'refresh_token=def50200a1b9'
125+
```
126+
127+
### Confidential client
128+
129+
```bash
130+
curl -X POST 'https://matomo.example.com/index.php?module=OAuth2&action=token' \
131+
-H 'Content-Type: application/x-www-form-urlencoded' \
132+
-d 'grant_type=refresh_token' \
133+
-d 'client_id=analytics_app' \
134+
-d 'client_secret=7fa9c0f81b8b4a12' \
135+
-d 'refresh_token=def50200a1b9'
136+
```
137+
138+
## What to read next
139+
140+
Once you have an access token, see [Calling Matomo APIs with OAuth 2.0](/guides/oauth2/api-usage).
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
category: Integrate
3+
title: OAuth 2.0 Client Credentials Flow
4+
---
5+
# OAuth 2.0 Client Credentials Flow
6+
7+
Use the Client Credentials flow when a backend service needs to access Matomo APIs without user interaction.
8+
9+
Typical examples include:
10+
11+
* Internal analytics dashboards
12+
* Scheduled data exports
13+
* Backend integrations
14+
15+
## Request an access token
16+
17+
```bash
18+
curl -X POST 'https://matomo.example.com/index.php?module=OAuth2&action=token' \
19+
-H 'Content-Type: application/x-www-form-urlencoded' \
20+
-d 'grant_type=client_credentials' \
21+
-d 'client_id=analytics_app' \
22+
-d 'client_secret=7fa9c0f81b8b4a12' \
23+
-d 'scope=matomo:read'
24+
```
25+
26+
## Example token response
27+
28+
```json
29+
{
30+
"token_type": "Bearer",
31+
"expires_in": 3600,
32+
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
33+
}
34+
```
35+
36+
Depending on your client configuration, a refresh token may also be available through the token endpoint for supported grant types.
37+
38+
## When to use this flow
39+
40+
Use this flow for trusted server-side applications that need server-to-server access and can keep credentials secret.
41+
42+
If the application needs a user to log in and approve access, use the [Authorization Code flow](/guides/oauth2/authorization-code) instead.

docs/5.x/oauth2/faq.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
category: Integrate
3+
title: OAuth 2.0 Developer FAQ
4+
---
5+
# OAuth 2.0 Developer FAQ
6+
7+
## Which grant types are supported?
8+
9+
The plugin supports:
10+
11+
* Authorization Code with PKCE
12+
* Client Credentials
13+
* Refresh Token
14+
15+
## Which scopes are available?
16+
17+
The plugin provides these scopes:
18+
19+
* `matomo:read`
20+
* `matomo:write`
21+
* `matomo:admin`
22+
* `matomo:superuser`
23+
24+
See the [permissions guide](/guides/permissions) for more information.
25+
26+
At the time of writing, only one scope can be requested at a time.
27+
28+
## When should I use a public client?
29+
30+
Use a public client when your application cannot safely store a client secret, for example in a browser or mobile app. Public clients should use PKCE.
31+
32+
## When should I use a confidential client?
33+
34+
Use a confidential client when your application runs on a trusted backend and can safely protect the client secret.
35+
36+
## Where do I manage OAuth clients?
37+
38+
Use one of these screens:
39+
40+
```text
41+
Administration => Platform => OAuth2 (Matomo On-Premise)
42+
Administration => Export => OAuth2 (Matomo Cloud)
43+
```
44+
45+
## Which endpoints does the plugin expose?
46+
47+
The plugin exposes these endpoints:
48+
49+
* `/index.php?module=OAuth2&action=authorize`
50+
* `/index.php?module=OAuth2&action=token`
51+
52+
Optional cleaner routes can also be configured:
53+
54+
* `/oauth2/authorize`
55+
* `/oauth2/token`
56+
57+
## When is a client secret shown?
58+
59+
For confidential clients, the client secret is shown in full only when the client is created or when the secret is rotated. After that, the secret is masked in the UI.
60+
61+
## Can I rotate a client secret?
62+
63+
Yes. Confidential clients support secret rotation from the edit screen. Rotate the secret if you need a new value or if the existing one may have been exposed.
64+
65+
## Can I still use `token_auth`?
66+
67+
Yes. The OAuth 2.0 plugin adds an alternative authentication method for external applications. Existing `token_auth` based integrations continue to be relevant for Matomo installations where the plugin is not enabled.

0 commit comments

Comments
 (0)