Skip to content

8. De RdvMedecins-server, een blackbox

De 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 van de cursussen [React], [Vue.js], [Angular] en [Flutter]: de server van de cursus [NestJS], waarvan de controllers geen HTML-pagina's meer genereren maar JSON-antwoorden produceren. Lezers van die cursussen kennen dit hoofdstuk al; het wordt hier vrijwel ongewijzigd overgenomen en zij kunnen zich uitsluitend richten op de paragraaf over authenticatie, waarin wordt uitgelegd wat er verandert voor een mobiele applicatie.

In deze cursus is deze server een blackbox: u hoeft 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 retourneert. Dit is de gebruikelijke situatie voor een clientontwikkelaar: de API is een contract en het serverteam kan achter dat contract alles wijzigen wat het wil. De broncode wordt meegeleverd (map [rdvmedecins-nestjs-json]) en is uitgebreid van commentaar voorzien, voor de nieuwsgierige lezer.

In dit hoofdstuk wordt uitgelegd hoe u de server installeert, wordt de API beschreven en wordt de server bevraagd met [curl].

8.1. De applicatie RdvMedecins

De applicatie beheert afspraken voor een huisartsenpraktijk. Er zijn drie rollen:

Rol
Wie
Wat mag deze rol 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 een agenda zien zij de namen van andere patiënten niet

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

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

8.2. Installatie

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

  1. De database. Het script [database/dbrdvmedecins2.sql] maakt de database [dbrdvmedecins2] aan, met de tabellen en de voorbeeldgegevens. Met [Laragon] voert u het uit vanuit [HeidiSQL] (menu Bestand / SQL-bestand uitvoeren); 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.

  1. Bibliotheken. In een consolevenster dat is geopend in de map [rdvmedecins-nestjs-json]:
npm install
  1. Configuratie. Kopieer het bestand [.env.example] naar [.env] (onder [Windows]: [copy .env.example .env]) en pas het vervolgens aan:
# -----------------------------------------------------------------------
# Voorbeeldconfiguratiebestand.
# Kopieer dit bestand naar [.env] en pas de waarden daarna aan uw machine aan.
# Het bestand [.env] mag nooit worden gedeeld of gecommit (wachtwoorden).
# -----------------------------------------------------------------------

# luisterpoort van de JSON-server
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 waarmee JWT-tokens worden ondertekend/geverifieerd - 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 via een onversleutelde verbinding verstuurd.
COOKIE_SECURE=false

# -----------------------------------------------------------------------
# bescherming tegen bots
# -----------------------------------------------------------------------
# aanmelding: aantal toegestane mislukkingen (verkeerde captcha of wachtwoord) 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 mislukkingen verzamelt, alle logins samen)
LOGIN_BLOCK_MINUTES=15
# aanmaken van klantaccounts: maximum aantal accounts dat per uur wordt aangemaakt
# vanaf hetzelfde IP-adres. In een leslokaal gaan alle computers vaak via
# hetzelfde adres naar buiten: verhoog indien nodig.
REGISTRATION_MAX_PER_HOUR=10

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

# -----------------------------------------------------------------------
# productie
# -----------------------------------------------------------------------
# map van de gecompileerde webclient, geserveerd door deze server (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;
  • regels 12–16: de verbinding met [MySQL]. Met [Laragon] heeft de gebruiker [root] geen wachtwoord: de standaardwaarden voldoen;
  • regel 25: de geheime sleutel waarmee de authenticatietokens worden ondertekend;
  • regels 38–45: beveiligingen tegen bots (blokkering na meerdere mislukte inlogpogingen, aantal accounts dat per uur wordt aangemaakt);
  • regel 51: de testmodus van de CAPTCHA. De inlogpagina vereist dat gebruikers de tekens uit een afbeelding overtypen, wat vanaf een console onmogelijk is. Als deze variabele een waarde heeft, wordt die waarde geaccepteerd als antwoord op elke CAPTCHA, net als de „testsleutels" die [Google] beschikbaar stelt voor zijn [reCAPTCHA]. Voor de tests met [curl] in dit hoofdstuk voert u [CAPTCHA_TEST=test] in. De geautomatiseerde tests waarmee de schermafbeeldingen van deze cursus zijn gemaakt, gebruiken deze waarde eveneens. Laat de variabele in productie LEEG;
  • regels 57–59: twee instellingen voor productie (zie het hoofdstuk „Uitrol").

  • 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 niet bereikbaar is ([MySQL]-server gestopt, onjuist wachtwoord), stopt de server onmiddellijk met een foutmelding over de verbinding.

8.3. Antwoordformaat

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

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 retourneert nooit tekst die moet worden weergegeven: hij kent de taal van de gebruiker niet meer. De client vertaalt de sleutel met zijn woordenboeken (zie voorbeeld 21);
  • [params] (optioneel): de parameters van het bericht ([At least {min} characters], [Try again in {minutes} minute(s)]);
  • [fields] (optioneel): bij een afgewezen 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]

8.4. Authenticatie

De client authenticeert zich met [POST /api/auth/login], met een gebruikersnaam, een wachtwoord en een antwoord op de CAPTCHA. In ruil daarvoor stuurt de server in de header [Set-Cookie] van zijn antwoord een cookie met een ondertekend JWT-token:

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

De client moet dit cookie vervolgens bij elk verzoek aan [/api] meesturen, in de header [Cookie]: zo weet de server wie het verzoek doet.

Deze server is ontworpen voor webclients en zijn ontwerpkeuzen worden bepaald door de risico's die specifiek zijn voor webbrowsers:

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

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 versturen.

En een mobiele app? Daar is geen browser: niemand bewaart het cookie of stuurt het uit zichzelf terug. De app neemt die rol over: de bibliotheek [OkHttp], die een „cookie jar" (een [CookieJar]) toevertrouwd krijgt, leest de header [Set-Cookie] uit de antwoorden en schrijft de header [Cookie] in de verzoeken (voorbeeld 23). De attributen [HttpOnly] en [SameSite] gelden alleen voor browsers: ze hebben hier geen effect, net zoals ze geen effect hebben op [curl].

8.5. 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 het cookie
GET
[/api/auth/me]
ingelogd
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 login voor ADMIN)
GET
[/api/clients]
ADMIN, DOCTOR
de lijst (met login voor ADMIN)
GET
[/api/doctors/:id], [/api/clients/:id]
ADMIN
één 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 bijbehorende afspraken

Een agenda-item ([GET /api/schedule]) beschrijft een tijdvak EN wat de ingelogde 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 tijdvak op die dag beschikbaar?
  • [patient]: de patiënt voor de afspraak. Voor een ingelogde patiënt is dit gelijk aan [null] behalve bij ZIJN EIGEN afspraken: de namen van andere patiënten worden nooit verzonden;
  • [mine]: dit is de afspraak van de ingelogde patiënt;
  • [bookable]: kan de gebruiker dit tijdvak boeken (beschikbaar, niet in het verleden en, voor een patiënt, geen toekomstige 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" te tonen terwijl dat niet zou mogen), controleert de server bij het boeken alles nogmaals.

8.6. De API bevragen met curl

De servermap [curl] bevat de JSON-bodies van de verzoeken in 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]: verstuur de inhoud van het bestand met POST, met de header [Content-Type: application/json] (curl 7.82 en hoger; bij een oudere versie gebruikt u [-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] van de ene opdracht op de andere ingelogd, net als een browser.

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

8.6.1. Inloggen

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: 11081

{"id": "e347ae38-79ff-4dea-8d6e-19610a7d69a7", "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"150..."}

Zonder token geeft een beveiligde URL een 401-antwoord:

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 login zet het cookie, dat [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 dit cookie weet de server wie wij 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 het 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"}

8.6.2. Artsen, cliënten, agenda

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

$ 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"}

8.6.3. Afspraken

ADMIN reserveert tijdvak 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 toekomstige afspraak. Een afspraak in het verleden wordt eveneens 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] verstuurt) 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"}

8.6.4. Het perspectief van een patiënt

U logt uit en logt vervolgens in 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: tijdvak 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 het tijdvak voor zichzelf (de body bevat geen [clientId]), slechts één keer, niet twee keer:

{ "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 eigen gebied:

$ 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 als 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

{}

8.6.5. Het perspectief van een arts

Zonder [doctorId] ontvangt een ingelogde 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
}

Artsen beheren is niet mogelijk:

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"}

8.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 geeft per veld de fout:

{ "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 die welke 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 indienen (met versie 1) zou een record overschrijven dat intussen door iemand anders is gewijzigd: dat voorkomt de optimistische vergrendeling:

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

{}

8.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 een token uit, 15 minuten geldig, in een cookie:

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=f71fa11a-e899-4d06-a35c-42d203dce17d; Max-Age=900; Path=/api/register; Expires=Mon, 28 Sep 2026 11:04:14 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 wachtwoordbevestiging 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"}

8.6.8. Uw account beheren

De nieuwe patiënte logt in en leest haar eigen gegevens:

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

Voor elke wijziging is het huidige wachtwoord nodig. 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:15 GMT; HttpOnly; SameSite=Strict
Content-Type: application/json; charset=utf-8
Content-Length: 63

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

Het verwijderen van het account verwijdert ook het cookie:

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"}

8.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"}

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