---
title: "Authentication"
url: "https://portal.api.aareon.com/guides/authentication"
image: "https://portal.api.aareon.com/_og/d/c_Ocean.takumi,title_Authentication,props_eyJ0aGVtZSI6eyJtb2RlIjoibGlnaHQiLCJjb2xvcnMiOnsicHJpbWFyeSI6IiMwNTExNjMifX19,p_Ii9ndWlkZXMvYXV0aGVudGljYXRpb24i,s_bitQtYEcDBXMM8ft.png"
---

# Authentication

Wir verwenden als Authentifizierungsverfahren den Client Credentials Flow. Dabei authentifiziert sich die Applikation selbst gegenüber unserem Identity Provider.

> **Hinweis**: Wir verwenden ein Signed JWT mit Client Secret anstelle von client\_id und client\_secret im Klartext, da dies die Übertragung sicherer macht und Replay-Angriffe verhindert.

## [Voraussetzungen](#voraussetzungen)

-   Ein eingerichteter Client (pro Applikation)
-   Das zugehörige Client Secret
-   Der individuelle API-Key-Scope für Kunde und Applikation
-   URL des Token-Endpunkts ([https://api.aareon.com/erpapi/oauth2/token](https://api.aareon.com/erpapi/oauth2/token))

## [Ablauf](#ablauf)

1.  Die Applikation erstellt ein JWT (JSON Web Token) mit folgenden Claims:

-   iss: Client ID der Applikation
-   sub: Client ID der Applikation
-   aud: [https://auth.asw.aareon.com/realms/global/protocol/openid-connect/token](https://auth.asw.aareon.com/realms/global/protocol/openid-connect/token)
-   iat: Zeitpunkt der Erstellung
-   exp: Ablaufzeit (max +5 Minuten)

2.  Das JWT wird mit dem Client Secret signiert (HS256).
3.  Dieses signierte JWT wird an den Token-Endpunkt geschickt – zusammen mit dem gewünschten Scope.
4.  Der Server prüft die Signatur und stellt ein Access Token aus.
5.  Mit diesem Access Token werden API-Aufrufe authentifiziert

```python
#!/usr/bin/env python3
import jwt
import time
import uuid  # Import der UUID-Bibliothek
import requests  # Stellen Sie sicher, dass requests installiert ist
# Konfiguration
client_id = '' #Client ID
client_secret = ''  # Client Secret Als Schlüssel zum Signieren des JWT
apiKey = '' # apiKey per customer
audience = 'https://auth.asw.aareon.com/realms/global/protocol/openid-connect/token'
token_endpoint = 'https://api.aareon.com/erpapi/oauth2/token'  # Token Endpoint für dev/test Umgebung vor erpapi ein/dev oder /test hinzuzfügen
# Erstellen des Claims Sets
claims = {
    'iss': client_id,
    'sub': client_id,
    'aud': audience,
    'exp': int(time.time()) + 300,  # Token läuft in 5 Minuten ab
    'iat': int(time.time()),
    'jti': str(uuid.uuid4())  # Generiert eine einzigartige UUID
}
# JWT erstellen und signieren
encoded_jwt = jwt.encode(claims, client_secret, algorithm='HS256')
#print(encoded_jwt)
# Anfrage zum Token Endpoint senden
data = {
    'grant_type': 'client_credentials',
    'client_assertion_type': 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
    'client_assertion': encoded_jwt,
    'scope':  apiKey
}
response = requests.post(token_endpoint, data=data)
access_token = response.json().get('access_token')
print(access_token)
```