Skip to content

7. De RdvMedecins-server, een blackbox

Voorbeelden 22 tot en met 25, gevolgd door de casus, communiceren met een server: de JSON-server van de applicatie [RdvMedecins]. Dit is de [NestJS]-server uit de vorige les, waarvan de controllers geen HTML-pagina's meer genereren maar JSON-antwoorden produceren. De bedrijfslaag en de datatoegangslaag blijven ongewijzigd.

In deze cursus is deze server een blackbox: we hoeven niet te weten hoe hij is geschreven. Het enige wat telt, is de API, dat wil zeggen de lijst met HTTP-verzoeken die hij accepteert en de antwoorden die hij teruggeeft. Dit is de typische situatie voor een clientontwikkelaar: de API is een contract en het serverteam kan achter dat contract alles wijzigen wat het wil. De broncode is meegeleverd (map [rdvmedecins-nestjs-json]) en uitgebreid van commentaar voorzien, voor de nieuwsgierige lezer.

Dit hoofdstuk legt uit hoe u de server installeert, beschrijft de API ervan en bevraagt de server met [curl].

7.1. De applicatie RdvMedecins

De applicatie beheert afspraken voor een medische praktijk. Ze kent drie rollen:

Rol
Wie
Wat ze kunnen doen
[ADMIN]
De beheerder ([admin])
Afspraken inplannen en annuleren voor alle patiënten; artsen en cliënten beheren
[DOCTOR]
Artsen ([pelissier], [tatou], [dupont])
Afspraken inplannen en annuleren voor alle patiënten; alle namen in de agenda's zien
[USER]
patiënten ([martin], [durand], [bistrou], [german])
afspraken voor zichzelf maken; hun afspraken annuleren; hun account beheren. In de agenda kunnen ze de namen van andere patiënten niet zien

De demo-accounts gebruiken hun gebruikersnaam als wachtwoord. De beheerregels worden door de server gecontroleerd, ongeacht de client:

  • geen afspraken in het verleden;
  • slechts één komende afspraak per patiënt;
  • een tijdslot kan per dag maar één keer worden geboekt;
  • twee artsen (of twee cliënten) kunnen niet dezelfde voor- en achternaam hebben; een gebruikersnaam hoort bij slechts één account;
  • een arts met afspraken kan niet worden verwijderd;
  • een record dat intussen door iemand anders is gewijzigd, kan niet worden overschreven (optimistische vergrendeling);
  • aan een patiënt wordt nooit de naam van een andere patiënt toegewezen.

7.2. Installatie

U hebt [Node.js] en een [MySQL]-server (of [MariaDB]) nodig: de installatie ervan wordt beschreven in de bijlage.

1. De database. Het script [database/dbrdvmedecins2.sql] maakt de database [dbrdvmedecins2], de tabellen en de voorbeeldgegevens aan. Met [Laragon] voert u het uit vanuit [HeidiSQL] (menu File / Run SQL file); op de opdrachtregel:

mysql -u root -p < database/dbrdvmedecins2.sql

Het script begint altijd met een lege database: u kunt het opnieuw uitvoeren om de oorspronkelijke gegevens te herstellen.

2. Bibliotheken. In een consolevenster dat is geopend in de map [rdvmedecins-nestjs-json]:

npm install

3. Configuratie. Kopieer het bestand [.env.example] naar [.env] (onder [Windows]: [copy .env.example .env]) en wijzig het vervolgens:

# -----------------------------------------------------------------------
# Voorbeeldconfiguratiebestand.
# Kopieer dit bestand naar [.env] en pas dan de waarden aan op uw machine.
# Het bestand [.env] mag nooit worden gedeeld of gecommit (wachtwoorden).
# -----------------------------------------------------------------------

# luisterpoort van de JSON-server (de Angular-client bereikt hem via de proxy van ng serve)
PORT=8080

# verbindingsparameters voor de MySQL-database [dbrdvmedecins2]
# (script: database/dbrdvmedecins2.sql)
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE=dbrdvmedecins2
# true: toont in de console de SQL-query's die naar MySQL worden gestuurd
DB_LOGGING=false

# -----------------------------------------------------------------------
# authenticatie
# -----------------------------------------------------------------------
# geheime sleutel om JWT-tokens te ondertekenen/verifiëren - MOET worden gewijzigd
# in productie (een lange willekeurige string, nooit gecommit).
JWT_SECRET=change-me-to-a-long-random-string
# geldigheid van een JWT-token, in seconden (7200 s = 2 uur). De cookie
# die het token draagt heeft dezelfde levensduur.
JWT_EXPIRES_IN_SECONDS=7200
# in productie op true zetten (site geserveerd via HTTPS): de authenticatiecookie
# wordt dan nooit verstuurd via een onversleutelde verbinding.
COOKIE_SECURE=false

# -----------------------------------------------------------------------
# bescherming tegen bots
# -----------------------------------------------------------------------
# login: aantal mislukte pogingen (verkeerde captcha of wachtwoord) dat is toegestaan voor
# dezelfde login vanaf hetzelfde IP-adres, voordat dit paar wordt geblokkeerd...
LOGIN_MAX_FAILURES=5
# ... gedurende deze periode (een IP-adres wordt ook geblokkeerd als het
# 5 keer meer mislukte pogingen verzamelt, alle logins samen)
LOGIN_BLOCK_MINUTES=15
# aanmaken van een clientaccount: maximumaantal accounts per uur dat vanaf
# hetzelfde IP-adres wordt aangemaakt. In een lokaal zijn alle computers vaak via
# hetzelfde adres actief: verhoog indien nodig.
REGISTRATION_MAX_PER_HOUR=10

# TESTMODUS van de captcha: als deze variabele een waarde heeft, wordt die waarde
# geaccepteerd als antwoord op ELKE captcha. Handig om
# de API met curl aan te roepen (de afbeelding is niet te "lezen" in een console).
# In productie LEEG laten.
CAPTCHA_TEST=

# -----------------------------------------------------------------------
# productie
# -----------------------------------------------------------------------
# map van de gecompileerde Angular-client, door deze server geserveerd (standaard: client/)
# CLIENT_DIR=client
# op true zetten als de server achter een proxy staat (nginx...)
TRUST_PROXY=false
  • regel 8: de server luistert op poort 8080;
  • regel 12–16: de verbinding met [MySQL]. Met [Laragon] heeft de gebruiker [root] geen wachtwoord: de standaardwaarden zijn prima;
  • regel 25: de geheime sleutel waarmee de authenticatietokens worden ondertekend;
  • regel 38–45: beveiligingen tegen bots (vergrendeling na meerdere mislukte aanmeldpogingen, aantal accounts dat per uur wordt aangemaakt);
  • regel 51: de CAPTCHA-testmodus. De aanmeldpagina vereist dat gebruikers de tekens uit een afbeelding overtypen, wat vanuit een console onmogelijk is. Als deze variabele een waarde heeft, wordt die waarde als antwoord op elke CAPTCHA geaccepteerd, vergelijkbaar met de „testsleutels” die [Google] levert voor zijn [reCAPTCHA]. Voor de tests met [curl] in dit hoofdstuk voert u [CAPTCHA_TEST=test] in. Laat de variabele in productie LEEG;
  • regel 57–59: twee instellingen voor productie (zie het hoofdstuk „Uitrol”).

4. Starten.

npm run start:dev

De console toont (fragment):

1
2
3
4
5
6
7
8
[Nest] LOG [NestFactory] Starting Nest application...
[Nest] WARN [RdvMedecins] captcha TEST MODE active (CAPTCHA_TEST in .env): never use it in production
[Nest] LOG [RoutesResolver] ScheduleController {/api/schedule}:
[Nest] LOG [RouterExplorer] Mapped {/api/schedule, GET} route
[Nest] LOG [RoutesResolver] AppointmentsController {/api/appointments}:
...
[Nest] LOG [NestApplication] Nest application successfully started
[Nest] LOG [RdvMedecins] JSON server started: http://localhost:8080/api

Als de database onbereikbaar is ([MySQL]-server gestopt, onjuist wachtwoord), stopt de server onmiddellijk met een foutmelding over de verbinding.

7.3. Antwoordformaat

Alle URL's beginnen met [/api]. Een geslaagd verzoek krijgt een 2xx-statuscode en in de body de gevraagde gegevens in JSON-formaat. Een geweigerd verzoek krijgt een 4xx- of 5xx-statuscode en in de body gegevens in hetzelfde formaat:

1
2
3
4
5
6
{
  "statusCode": 400,
  "key": "ERRORS.INVALID_FORM",
  "params": { "min": "6" },
  "fields": { "lastName": "ERRORS.LASTNAME_REQUIRED", "password": "ERRORS.PASSWORD_TOO_SHORT" }
}
  • [statusCode]: de HTTP-code, herhaald in de body;
  • [key]: de vertaalsleutel van het bericht. De server geeft nooit tekst terug die moet worden weergegeven: hij kent de taal van de gebruiker niet meer. De client vertaalt de sleutel met behulp van zijn woordenboeken (zie voorbeeld 21);
  • [params] (optioneel): berichtparameters ([At least {min} characters], [Try again in {minutes} minute(s)]);
  • [fields] (optioneel): bij een geweigerd formulier de fout voor elk veld.

Gebruikte codes:

Code
Betekenis
Voorbeelden van sleutels
400
Ongeldig verzoek
[ERRORS.INVALID_FORM] (+ fields), [ERRORS.INVALID_DATE], [ERRORS.WRONG_CAPTCHA], [ERRORS.CHOOSE_PATIENT], [ERRORS.WRONG_CURRENT_PASSWORD], [ERRORS.BAD_REQUEST]...
401
niet geauthenticeerd
[ERRORS.AUTH_REQUIRED] (geen token), [ERRORS.SESSION_EXPIRED] (ongeldig of verlopen token), [LOGIN.ERROR]
403
Toegang geweigerd
[ERRORS.FORBIDDEN] (onvoldoende rol), [ERRORS.CAPTCHA_BEFORE_ACCOUNT] (geen toegangsbewijs)
404
Niet gevonden
[ERRORS.NOT_FOUND], [ERRORS.DOCTOR_NOT_FOUND], [ERRORS.APPOINTMENT_NOT_FOUND]...
409
Schending van een bedrijfsregel
[ERRORS.PAST_DATE], [ERRORS.SLOT_ALREADY_BOOKED], [ERRORS.LOGIN_TAKEN], [ERRORS.CONCURRENT_UPDATE]...
415
POST die niet in JSON is
[ERRORS.JSON_REQUIRED]
429
Te veel pogingen
[ERRORS.TOO_MANY_ATTEMPTS], [ERRORS.TOO_MANY_ACCOUNTS] (+ params.minutes)
500
serverfout
[ERRORS.SERVER_ERROR]

7.4. Authenticatie

De client authenticeert zich met [POST /api/auth/login], met een gebruikersnaam, een wachtwoord en een CAPTCHA-antwoord. In ruil daarvoor plaatst de server een cookie in de browser die een ondertekend JWT-token bevat:

Set-Cookie: rdvmedecins_jwt=eyJhbGciOiJIUzI1NiIs...; Max-Age=7200; Path=/api; HttpOnly; SameSite=Strict

De kenmerken ervan zijn belangrijk voor de beveiliging:

  • [HttpOnly]: de [JavaScript]-code van de pagina kan deze cookie niet lezen. Een kwaadaardig script dat in de pagina is geïnjecteerd, kan het token dus niet stelen. Gevolg voor de client: hij kan niet zelf bepalen of hij is aangemeld. Hij vraagt het aan de server ([GET /api/auth/me]);
  • [Path=/api]: de browser voegt de cookie alleen toe aan verzoeken aan de API;
  • [SameSite=Strict]: de browser voegt dit token alleen toe aan verzoeken die VANUIT een pagina van deze site worden verzonden. Een kwaadaardige pagina op een andere site kan de browser er niet toe brengen namens de gebruiker te handelen (een zogeheten CSRF-aanval);
  • [Max-Age=7200]: de cookie verloopt na twee uur, op hetzelfde moment als het token.

De browser stuurt deze cookie vervolgens automatisch mee met elk verzoek aan [/api]. De client [Angular] hoeft dus niets te doen om zich te authenticeren, mits de pagina's en de API dezelfde origin delen (hetzelfde protocol, dezelfde machine, dezelfde poort). Dit is de rol van de proxy op de ontwikkelserver van [Angular] (zie voorbeeld 22), en van de server zelf in productie (zie het hoofdstuk „Uitrol”).

Een tweede beveiliging tegen CSRF-aanvallen: de server weigert (415) elk POST-verzoek waarvan de body niet in JSON is. Een gecompromitteerd HTML-formulier op een andere site kan geen JSON verzenden.

7.5. De API

Methode
Adres
Rollen
Verzonden body / antwoord
GET
[/api/captcha]
openbaar
Antwoord: [{ id, svg }]
POST
[/api/auth/login]
openbaar
[{ login, password, captchaId, captcha }]; antwoord: [{ login, name, role }] + cookie
POST
[/api/auth/logout]
openbaar
[{}]; verwijdert de cookie
GET
[/api/auth/me]
aangemeld
antwoord: [{ login, name, role }]
GET
[/api/schedule?doctorId=&day=]
alle
Antwoord: de agenda (zonder doctorId: voor een DOCTOR de eigen agenda)
POST
[/api/appointments]
alle
[{ day, slotId, clientId }] (clientId wordt genegeerd voor een USER); 201
DELETE
[/api/appointments/:id]
alle
Een USER annuleert alleen zijn eigen afspraken
GET
[/api/doctors]
alle
de lijst (met gebruikersnaam voor ADMIN)
GET
[/api/clients]
ADMIN, DOCTOR
de lijst (met gebruikersnaam voor ADMIN)
GET
[/api/doctors/:id], [/api/clients/:id]
ADMIN
een record
POST
[/api/doctors], [/api/clients]
ADMIN
[{ title, firstName, lastName, login?, password? }]; 201
PUT
[/api/doctors/:id], [/api/clients/:id]
ADMIN
[{ title, firstName, lastName, login?, password?, version }]
DELETE
[/api/doctors/:id], [/api/clients/:id]
ADMIN
Antwoord: [{}]
POST
[/api/register/access]
openbaar
[{ captchaId, captcha }]; geeft een token uit (cookie)
GET
[/api/register/access]
openbaar
antwoord: [{ valid }]
POST
[/api/register]
openbaar + toegangsbewijs
[{ title, firstName, lastName, login, password, confirmation }]; 201
GET
[/api/my-area]
USER
Antwoord: [{ patient, appointments }]
GET
[/api/my-account]
USER
Antwoord: [{ title, firstName, lastName, login, version }]
PUT
[/api/my-account]
USER
[{ title, firstName, lastName, login, password?, confirmation?, currentPassword, version }]
DELETE
[/api/my-account]
USER
[{ currentPassword }]; verwijdert het account en de afspraken ervan

Een item van de agenda ([GET /api/schedule]) beschrijft een tijdslot EN wat de aangemelde gebruiker ermee mag doen:

1
2
3
4
{ "id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0,
  "free": false,
  "patient": { "title": "Mme", "firstName": "Alice", "lastName": "DURAND" },
  "mine": false, "bookable": false, "appointmentId": 2 }
  • [free]: is het tijdslot op die dag beschikbaar?
  • [patient]: de patiënt voor de afspraak. Voor een aangemelde patiënt is dit gelijk aan [null], behalve voor ZIJN EIGEN afspraken: de namen van andere patiënten worden nooit verzonden;
  • [mine]: dit is de afspraak van de aangemelde patiënt;
  • [bookable]: kan de gebruiker dit tijdslot boeken (beschikbaar, niet in het verleden en, voor een patiënt, geen komende afspraak)?
  • [appointmentId]: ingevuld als de gebruiker deze afspraak kan annuleren.

De server BESLIST; de client toont. En zelfs als een client zou valsspelen (door een knop „Boeken” weer te geven terwijl dat niet zou moeten), zou de server bij het boeken alles nogmaals controleren.

7.6. De API bevragen met curl

De map [curl] op de server bevat de JSON-verzoekbodies uit dit hoofdstuk, met één bestand per verzoek. Bijvoorbeeld [curl/login-admin.json]:

{ "login": "admin", "password": "admin", "captchaId": "-", "captcha": "test" }

De opdrachten worden ingevoerd in een console die is geopend in de directory [rdvmedecins-nestjs-json]. Enkele opties van [curl]:

  • [-i]: toon ook de antwoordheaders; [-s]: zonder voortgangsbalk;
  • [--json @file]: stuur de inhoud van het bestand als POST, met de header [Content-Type: application/json] (curl 7.82 en later; gebruik bij een oudere versie [-H "Content-Type: application/json" -d @file]);
  • [-X PUT], [-X DELETE]: de HTTP-methode;
  • [-c cookies.txt]: sla de ontvangen cookies op in dit bestand; [-b cookies.txt]: stuur ze terug. Zo „blijft [curl] aangemeld” van de ene opdracht naar de volgende, net zoals een browser dat zou doen.

Onder [PowerShell] moet u [curl.exe] invoeren en niet [curl] (dat daar naar een andere opdracht verwijst). De enigszins lange JSON-antwoorden zijn voor dit document opgemaakt en de headers [Date], [ETag], [Connection], en [Keep-Alive] zijn verwijderd.

7.6.1. Aanmelden

Eerst de CAPTCHA (de afbeelding is bijgesneden):

1
2
3
4
5
6
7
$ curl -i http://localhost:8080/api/captcha
HTTP/1.1 200 OK
Cache-Control: no-store
Content-Type: application/json; charset=utf-8
Content-Length: 11304

{"id": "e33886ae-1a8b-4302-8cbc-cd7015e82f1f", "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"150..."}

Zonder token geeft een beschermde URL een 401-antwoord terug:

1
2
3
4
5
6
$ curl -i http://localhost:8080/api/doctors
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Content-Length: 47

{"statusCode": 401, "key": "ERRORS.AUTH_REQUIRED"}

Een onjuiste CAPTCHA, gevolgd door een onjuist wachtwoord:

1
2
3
4
5
6
$ curl -i --json @curl/login-wrong-captcha.json http://localhost:8080/api/auth/login
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 91

{"statusCode": 400, "key": "ERRORS.WRONG_CAPTCHA", "fields": {"captcha": "ERRORS.WRONG_CAPTCHA"}}
1
2
3
4
5
6
$ curl -i --json @curl/login-wrong-password.json http://localhost:8080/api/auth/login
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Content-Length: 38

{"statusCode": 401, "key": "LOGIN.ERROR"}

Een geslaagde aanmelding stelt de cookie in, die [curl] opslaat in [cookies.txt]:

1
2
3
4
5
6
7
$ curl -i -c cookies.txt --json @curl/login-admin.json http://localhost:8080/api/auth/login
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=eyJhbGciOiJIUzI1NiIs...; Max-Age=7200; Path=/api; Expires=Mon, 28 Sep 2026 12:49:12 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 55

{"login": "admin", "name": "Administrator", "role": "ADMIN"}

Met deze cookie weet de server wie we zijn:

1
2
3
4
5
6
$ curl -i -b cookies.txt http://localhost:8080/api/auth/me
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 55

{"login": "admin", "name": "Administrator", "role": "ADMIN"}

Een vervalst (of verlopen) token leidt tot een verlopen sessie en de server geeft de browser opdracht de cookie te verwijderen:

1
2
3
4
5
6
7
$ curl -i -b "rdvmedecins_jwt=abc.def.ghi" http://localhost:8080/api/auth/me
HTTP/1.1 401 Unauthorized
Set-Cookie: rdvmedecins_jwt=; Path=/api; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 49

{"statusCode": 401, "key": "ERRORS.SESSION_EXPIRED"}

7.6.2. Artsen, cliënten, agenda

De lijst met artsen, gezien door ADMIN (met gebruikersnamen):

$ curl -i -b cookies.txt http://localhost:8080/api/doctors
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 282

[
  {"id": 3, "version": 0, "title": "Melle", "firstName": "Sophie", "lastName": "DUPONT", "login": "dupont"},
  {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER", "login": "pelissier"},
  {"id": 2, "version": 0, "title": "Mr", "firstName": "Paul", "lastName": "TATOU", "login": "tatou"}
]

De agenda van dr. PELISSIER voor 15 januari 2030:

$ curl -s -b cookies.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-01-15"
{
  "doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
  "day": "2030-01-15",
  "slots": [
    {"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
  ],
  "info": null
}

Een datum die niet bestaat:

1
2
3
4
5
6
$ curl -i -b cookies.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-02-30"
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 46

{"statusCode": 400, "key": "ERRORS.INVALID_DATE"}

7.6.3. Afspraken

ADMIN reserveert tijdslot 3 voor patiënt nr. 2 (Alice DURAND). De body van het verzoek:

{ "day": "2030-01-15", "slotId": 3, "clientId": 2 }
1
2
3
4
5
6
$ curl -i -b cookies.txt --json @curl/appointment-admin.json http://localhost:8080/api/appointments
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 51

{"id": 2, "day": "2030-01-15", "slotId": 3, "clientId": 2}

De agenda toont nu deze afspraak, samen met de naam van de patiënt en de aanwijzingen om de afspraak te annuleren ([appointmentId]):

$ curl -s -b cookies.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-01-15"
{
  "doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
  "day": "2030-01-15",
  "slots": [
    {"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": false, "patient": {"title": "Mme", "firstName": "Alice", "lastName": "DURAND"}, "mine": false, "bookable": false, "appointmentId": 2},
    {"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
  ],
  "info": null
}

Probeer het opnieuw: de patiënt heeft al een komende afspraak. Een afspraak in het verleden wordt ook geweigerd:

1
2
3
4
5
6
$ curl -i -b cookies.txt --json @curl/appointment-admin.json http://localhost:8080/api/appointments
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 66

{"statusCode": 409, "key": "ERRORS.PATIENT_HAS_UPCOMING_APPOINTMENT"}
1
2
3
4
5
6
$ curl -i -b cookies.txt --json @curl/appointment-past.json http://localhost:8080/api/appointments
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 43

{"statusCode": 409, "key": "ERRORS.PAST_DATE"}

Een standaard HTML-formulier (hier gesimuleerd met [-d], dat [application/x-www-form-urlencoded] verzendt) wordt geweigerd:

1
2
3
4
5
6
$ curl -i -b cookies.txt -d "day=2030-01-15&slotId=4&clientId=2" http://localhost:8080/api/appointments
HTTP/1.1 415 Unsupported Media Type
Content-Type: application/json; charset=utf-8
Content-Length: 47

{"statusCode": 415, "key": "ERRORS.JSON_REQUIRED"}

7.6.4. Het perspectief van een patiënt

U meldt u af en meldt u vervolgens aan als [martin]:

1
2
3
4
5
6
7
$ curl -i -b cookies.txt -c cookies.txt --json @curl/empty.json http://localhost:8080/api/auth/logout
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=; Path=/api; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2

{}
$ curl -s -c cookies-martin.txt --json @curl/login-martin.json http://localhost:8080/api/auth/login
{"login": "martin", "name": "Jean MARTIN", "role": "USER"}

Dezelfde agenda, gezien door een patiënt: tijdslot 3 is bezet (["free": false]), maar ["patient"] is gelijk aan [null]: de naam van Alice, DURAND, is niet verzonden:

$ curl -s -b cookies-martin.txt "http://localhost:8080/api/schedule?doctorId=1&day=2030-01-15"
{
  "doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
  "day": "2030-01-15",
  "slots": [
    {"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": false, "patient": null, "mine": false, "bookable": false, "appointmentId": null},
    {"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
  ],
  "info": null
}

Een patiënt heeft geen toegang tot de lijst met cliënten:

1
2
3
4
5
6
$ curl -i -b cookies-martin.txt http://localhost:8080/api/clients
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 43

{"statusCode": 403, "key": "ERRORS.FORBIDDEN"}

Hij reserveert voor zichzelf (de body bevat geen [clientId]), eenmaal, niet tweemaal:

{ "day": "2030-01-15", "slotId": 5 }
1
2
3
4
5
6
$ curl -i -b cookies-martin.txt --json @curl/appointment-martin.json http://localhost:8080/api/appointments
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 51

{"id": 3, "day": "2030-01-15", "slotId": 5, "clientId": 1}
1
2
3
4
5
6
$ curl -i -b cookies-martin.txt --json @curl/appointment-martin-2.json http://localhost:8080/api/appointments
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 55

{"statusCode": 409, "key": "SCHEDULE.ALREADY_BOOKED_INFO"}

Zijn persoonlijke omgeving:

$ curl -s -b cookies-martin.txt http://localhost:8080/api/my-area
{
  "patient": {"title": "Mr", "firstName": "Jean", "lastName": "MARTIN"},
  "appointments": [
    {
      "id": 3,
      "day": "2030-01-15",
      "slot": {"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40},
      "doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"}
    }
  ]
}

Hij kan de afspraak van een andere patiënt niet annuleren. De server antwoordt met „niet gevonden”, net zoals bij een afspraak die niet bestaat: hij onthult niets over de afspraken van andere patiënten:

1
2
3
4
5
6
$ curl -i -b cookies-martin.txt -X DELETE http://localhost:8080/api/appointments/2
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Content-Length: 55

{"statusCode": 404, "key": "ERRORS.APPOINTMENT_NOT_FOUND"}

Zijn eigen afspraak wel:

1
2
3
4
5
6
$ curl -i -b cookies-martin.txt -X DELETE http://localhost:8080/api/appointments/3
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2

{}

7.6.5. Het perspectief van een arts

Zonder [doctorId] ontvangt een aangemelde arts zijn EIGEN agenda, met alle namen:

$ curl -s -b cookies-pelissier.txt "http://localhost:8080/api/schedule?day=2030-01-15"
{
  "doctor": {"id": 1, "version": 0, "title": "Mme", "firstName": "Marie", "lastName": "PELISSIER"},
  "day": "2030-01-15",
  "slots": [
    {"id": 1, "startHour": 8, "startMinute": 0, "endHour": 8, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 2, "startHour": 8, "startMinute": 20, "endHour": 8, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 3, "startHour": 8, "startMinute": 40, "endHour": 9, "endMinute": 0, "free": false, "patient": {"title": "Mme", "firstName": "Alice", "lastName": "DURAND"}, "mine": false, "bookable": false, "appointmentId": 2},
    {"id": 4, "startHour": 9, "startMinute": 0, "endHour": 9, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 5, "startHour": 9, "startMinute": 20, "endHour": 9, "endMinute": 40, "free": false, "patient": {"title": "Mr", "firstName": "Jean", "lastName": "MARTIN"}, "mine": false, "bookable": false, "appointmentId": 3},
    {"id": 6, "startHour": 9, "startMinute": 40, "endHour": 10, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 7, "startHour": 10, "startMinute": 0, "endHour": 10, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 8, "startHour": 10, "startMinute": 20, "endHour": 10, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 9, "startHour": 10, "startMinute": 40, "endHour": 11, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 10, "startHour": 11, "startMinute": 0, "endHour": 11, "endMinute": 20, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 11, "startHour": 11, "startMinute": 20, "endHour": 11, "endMinute": 40, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null},
    {"id": 12, "startHour": 11, "startMinute": 40, "endHour": 12, "endMinute": 0, "free": true, "patient": null, "mine": false, "bookable": true, "appointmentId": null}
  ],
  "info": null
}

Hij beheert geen artsen:

1
2
3
4
5
6
$ curl -i -b cookies-pelissier.txt --json @curl/doctor-add.json http://localhost:8080/api/doctors
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 43

{"statusCode": 403, "key": "ERRORS.FORBIDDEN"}

7.6.6. Artsen en cliënten beheren (ADMIN)

Een toevoeging. Als de gebruikersnaam ontbreekt, is de standaardwaarde de naam in kleine letters; het wachtwoord is de gebruikersnaam:

{ "title": "Mme", "firstName": "Claire", "lastName": "LEROY" }
1
2
3
4
5
6
$ curl -i -b cookies.txt --json @curl/doctor-add.json http://localhost:8080/api/doctors
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 90

{"id": 4, "version": 1, "title": "Mme", "firstName": "Claire", "lastName": "LEROY", "login": "leroy"}

Ongeldige body: het antwoord vermeldt de fout voor elk veld:

{ "title": "Dr", "firstName": "", "lastName": "A-LAST-NAME-FAR-TOO-LONG-FOR-THE-DB", "login": "Claire L" }
$ curl -i -b cookies.txt --json @curl/doctor-invalid.json http://localhost:8080/api/doctors
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 218

{
  "statusCode": 400,
  "key": "ERRORS.INVALID_FORM",
  "params": {"min": "6"},
  "fields": {"login": "ERRORS.INVALID_LOGIN", "title": "ERRORS.INVALID_TITLE", "firstName": "ERRORS.FIRSTNAME_REQUIRED", "lastName": "ERRORS.LASTNAME_TOO_LONG"}
}

Een homoniem: beide betrokken velden worden gemarkeerd:

1
2
3
4
5
6
$ curl -i -b cookies.txt --json @curl/doctor-duplicate.json http://localhost:8080/api/doctors
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 151

{"statusCode": 409, "key": "ERRORS.DOCTOR_ALREADY_EXISTS", "fields": {"firstName": "ERRORS.DOCTOR_ALREADY_EXISTS", "lastName": "ERRORS.DOCTOR_ALREADY_EXISTS"}}

Een wijziging. De [version] is de versie die uit het record is gelezen (hier 1):

{ "title": "Mme", "firstName": "Claire", "lastName": "LEROY-MARTIN", "version": 1 }
1
2
3
4
5
6
$ curl -i -b cookies.txt -X PUT --json @curl/doctor-edit.json http://localhost:8080/api/doctors/4
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 97

{"id": 4, "version": 2, "title": "Mme", "firstName": "Claire", "lastName": "LEROY-MARTIN", "login": "leroy"}

Het record heeft nu versie 2. Dezelfde wijziging opnieuw verzenden (met versie 1) zou een record overschrijven dat intussen door iemand anders is gewijzigd: de optimistische vergrendeling voorkomt dit:

1
2
3
4
5
6
$ curl -i -b cookies.txt -X PUT --json @curl/doctor-edit.json http://localhost:8080/api/doctors/4
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 51

{"statusCode": 409, "key": "ERRORS.CONCURRENT_UPDATE"}

Een arts met afspraken kan niet worden verwijderd; de andere wel:

1
2
3
4
5
6
$ curl -i -b cookies.txt -X DELETE http://localhost:8080/api/doctors/1
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=utf-8
Content-Length: 57

{"statusCode": 409, "key": "ERRORS.DOCTOR_HAS_APPOINTMENTS"}
1
2
3
4
5
6
$ curl -i -b cookies.txt -X DELETE http://localhost:8080/api/doctors/4
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2

{}

7.6.7. Een account aanmaken

Zonder toegangsbewijs is het onmogelijk:

1
2
3
4
5
6
$ curl -i --json @curl/registration.json http://localhost:8080/api/register
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 56

{"statusCode": 403, "key": "ERRORS.CAPTCHA_BEFORE_ACCOUNT"}

Eerst lost u een CAPTCHA op; de server geeft in een cookie een token uit dat 15 minuten geldig is:

1
2
3
4
5
6
7
$ curl -i -c cookies-registration.txt --json @curl/captcha-test.json http://localhost:8080/api/register/access
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_registration=73a037d1-78f2-4e8e-a3a5-b6660a4f7fd5; Max-Age=900; Path=/api/register; Expires=Mon, 28 Sep 2026 11:04:12 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 2

{}
$ curl -s -b cookies-registration.txt http://localhost:8080/api/register/access
{"valid": true}

De bevestiging van het wachtwoord moet identiek zijn:

1
2
3
4
5
6
$ curl -i -b cookies-registration.txt --json @curl/registration-confirmation.json http://localhost:8080/api/register
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 98

{"statusCode": 400, "key": "ERRORS.INVALID_FORM", "fields": {"confirmation": "ERRORS.PASSWORDS_DIFFER"}}
{ "title": "Mme", "firstName": "Julie", "lastName": "BERNARD", "login": "jbernard", "password": "secret1", "confirmation": "secret1" }
1
2
3
4
5
6
7
$ curl -i -b cookies-registration.txt -c cookies-registration.txt --json @curl/registration.json http://localhost:8080/api/register
HTTP/1.1 201 Created
Set-Cookie: rdvmedecins_registration=; Path=/api/register; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 20

{"login": "jbernard"}

Het toegangsbewijs is gebruikt: het kan niet worden gebruikt om een tweede account aan te maken:

1
2
3
4
5
6
$ curl -i -b cookies-registration.txt --json @curl/registration.json http://localhost:8080/api/register
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 56

{"statusCode": 403, "key": "ERRORS.CAPTCHA_BEFORE_ACCOUNT"}

7.6.8. Uw account beheren

De nieuwe patiënt meldt zich aan en leest haar accountgegevens:

$ curl -s -b cookies-jbernard.txt http://localhost:8080/api/my-account
{"title": "Mme", "firstName": "Julie", "lastName": "BERNARD", "login": "jbernard", "version": 1}

Voor een wijziging is het huidige wachtwoord vereist. De server geeft een nieuw token uit, dat de nieuwe naam bevat:

{ "title": "Mme", "firstName": "Julie", "lastName": "BERNARD-DUVAL", "login": "jbernard", "currentPassword": "secret1", "version": 1 }
1
2
3
4
5
6
7
$ curl -i -b cookies-jbernard.txt -c cookies-jbernard.txt -X PUT --json @curl/my-account.json http://localhost:8080/api/my-account
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=eyJhbGciOiJIUzI1NiIs...; Max-Age=7200; Path=/api; Expires=Mon, 28 Sep 2026 12:49:13 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 63

{"login": "jbernard", "name": "Julie BERNARD-DUVAL", "role": "USER"}

Als het account wordt verwijderd, wordt ook de cookie verwijderd:

1
2
3
4
5
6
7
$ curl -i -b cookies-jbernard.txt -c cookies-jbernard.txt -X DELETE --json @curl/my-account-delete.json http://localhost:8080/api/my-account
HTTP/1.1 200 OK
Set-Cookie: rdvmedecins_jwt=; Path=/api; Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2

{}
1
2
3
4
5
6
$ curl -i -b cookies-jbernard.txt http://localhost:8080/api/auth/me
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Content-Length: 47

{"statusCode": 401, "key": "ERRORS.AUTH_REQUIRED"}

7.6.9. Enkele fouten

Een onbekende route, een body die geen geldige JSON is:

1
2
3
4
5
6
$ curl -i -b cookies-martin.txt http://localhost:8080/api/does-not-exist
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Content-Length: 43

{"statusCode": 404, "key": "ERRORS.NOT_FOUND"}
1
2
3
4
5
6
$ curl -i -b cookies.txt -H "Content-Type: application/json" -d "{not json" http://localhost:8080/api/appointments
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 45

{"statusCode": 400, "key": "ERRORS.BAD_REQUEST"}

We weten nu alles wat we over de server moeten weten om onze client te schrijven.