7. 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 uit de vorige les, waarvan de controllers geen HTML-pagina's meer genereren maar JSON-antwoorden produceren. De bedrijfslaag en de gegevenstoegangslaag 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 wordt meegeleverd (map [rdvmedecins-nestjs-json]) en is 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 de afspraken van 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 een agenda zien ze 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 tijdslot kan per dag maar één keer worden geboekt;
- twee artsen (of twee cliënten) kunnen niet dezelfde voor- en achternaam hebben; een login 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);
- een patiënt krijgt 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 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 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.
2. Bibliotheken. In een consolevenster dat in de map [rdvmedecins-nestjs-json] is geopend:
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 daarna de waarden aan uw machine aan.
# Het bestand [.env] mag nooit worden gedeeld of gecommit (wachtwoorden).
# -----------------------------------------------------------------------
# luisterpoort van de JSON-server (de React-client bereikt hem via de Vite-proxy)
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 zijn verzonden
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 verzonden.
COOKIE_SECURE=false
# -----------------------------------------------------------------------
# bescherming tegen bots
# -----------------------------------------------------------------------
# inloggen: aantal toegestane mislukte pogingen (verkeerde captcha of wachtwoord) voor
# dezelfde login vanaf hetzelfde IP-adres, voordat dit paar wordt geblokkeerd...
LOGIN_MAX_FAILURES=5
# ... gedurende deze tijd (een IP-adres wordt ook geblokkeerd als het 5 keer
# meer mislukte pogingen verzamelt, alle logins samen)
LOGIN_BLOCK_MINUTES=15
# een clientaccount aanmaken: maximumaantal accounts dat per uur wordt aangemaakt
# vanaf hetzelfde IP-adres. In een klaslokaal gaan alle computers vaak via
# hetzelfde adres naar buiten: verhoog zo 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 te bevragen (in een console kunt u de afbeelding niet "lezen").
# In productie LEEG laten.
CAPTCHA_TEST=
# -----------------------------------------------------------------------
# productie
# -----------------------------------------------------------------------
# map van de gecompileerde React-client, 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 zijn prima;
- regel 25: de geheime sleutel waarmee de authenticatietokens worden ondertekend;
- regels 38–45: beveiligingen tegen bots (blokkering na meerdere mislukte inlogpogingen, aantal aangemaakte accounts per uur);
- Regel 51: CAPTCHA-testmodus. De inlogpagina vraagt u de tekens uit een afbeelding in te typen, wat vanuit een console onmogelijk is. Als deze variabele een waarde heeft, wordt die waarde als antwoord op elke CAPTCHA geaccepteerd, net als de „testsleutels” die [Google] aanbiedt voor [reCAPTCHA]. Voor de tests met [curl] in dit hoofdstuk voert u [CAPTCHA_TEST=test] in. Laat de variabele in productie LEEG;
- regels 57–59: twee instellingen voor productie (zie het hoofdstuk „Uitrol”).
4. Starten.
De console toont (fragment):
| [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 toegankelijk is ([MySQL]-server gestopt, onjuist wachtwoord), stopt de server onmiddellijk met een foutmelding over de verbinding.
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 code 4xx of 5xx en, in de body, een fout die ALTIJD hetzelfde formaat heeft:
| {
"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 weer te geven tekst terug: 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] |
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 attributen ervan zijn belangrijk voor de beveiliging:
- [HttpOnly]: de [JavaScript]-code van de pagina kan deze cookie niet lezen. Een kwaadaardig script dat mogelijk in de pagina is geïnjecteerd, kan het token dus niet stelen. Gevolg voor de client: hij kan zelf niet bepalen of hij is ingelogd. Hij vraagt het aan de server ([GET /api/auth/me]);
- [Path=/api]: de browser stuurt de cookie alleen mee met verzoeken aan de API;
- [SameSite=Strict]: de browser stuurt dit token alleen mee met verzoeken die VANAF een pagina van deze site worden verzonden. Een kwaadaardige pagina op een andere site kan de browser niet laten handelen namens de gebruiker (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 [React]-client hoeft dus niets te doen om zich te authenticeren, mits de pagina's en de API dezelfde oorsprong delen (hetzelfde protocol, dezelfde machine, dezelfde poort). Dat is de rol van de proxy van de ontwikkelserver, [Vite] (zie voorbeeld 22), en in productie van de server zelf (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] | ingelogd | antwoord: [{ login, name, role }] |
GET | [/api/schedule?doctorId=&day=] | alle | Antwoord: de agenda (zonder doctorId: voor een DOCTOR, zijn eigen) |
POST | [/api/appointments] | alle | [{ day, slotId, clientId }] (clientId 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 | 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 agendaregel ([GET /api/schedule]) beschrijft een tijdslot EN wat de ingelogde gebruiker ermee mag doen:
| { "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 ingelogde patiënt is dit gelijk aan [null], behalve voor ZIJN EIGEN afspraak: de namen van andere patiënten worden nooit verzonden;
- [mine]: dit is de afspraak van de ingelogde patiënt;
- [bookable]: kan de gebruiker dit tijdslot boeken (beschikbaar, niet in het verleden en, voor een patiënt, geen toekomstige afspraken)?
- [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 mag), 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-bodies van de verzoeken in dit hoofdstuk, één bestand per verzoek. Bijvoorbeeld [curl/login-admin.json]:
| { "login": "admin", "password": "admin", "captchaId": "-", "captcha": "test" }
|
Opdrachten worden ingevoerd in een console die in de directory [rdvmedecins-nestjs-json] is geopend. Enkele opties van [curl]:
- [-i]: ook de antwoordheaders tonen; [-s]: zonder voortgangsbalk;
- [--json @file]: de inhoud van het bestand met POST verzenden, met de header [Content-Type: application/json] (curl 7.82 en hoger; met een oudere versie gebruikt u [-H "Content-Type: application/json" -d @file]);
- [-X PUT], [-X DELETE]: de HTTP-methode;
- [-c cookies.txt]: de ontvangen cookies in dit bestand opslaan; [-b cookies.txt]: ze terugsturen. Zo „blijft [curl] ingelogd” van de ene opdracht naar de volgende, net als een browser.
Onder [PowerShell] moet u [curl.exe] invoeren en niet [curl] (dat daar naar een andere opdracht verwijst). De nogal lange JSON-antwoorden zijn voor dit document opgemaakt en de headers [Date], [ETag], [Connection], en [Keep-Alive] zijn verwijderd.
7.6.1. Inloggen
Eerst de CAPTCHA (de afbeelding is bijgesneden):
| $ 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: 11528
{"id": "be956764-1c67-4d23-a744-d0c2cebbb791", "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"150..."}
|
Zonder token geeft een beveiligde URL een 401-antwoord:
| $ 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:
| $ 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"}}
|
| $ 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 plaatst de cookie, die [curl] opslaat in [cookies.txt]:
| $ 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:
| $ 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:
| $ 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, zoals ADMIN die ziet (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:
| $ 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 }
|
| $ 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 instructies voor het annuleren ervan ([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 ook afgewezen:
| $ 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"}
|
| $ 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 afgewezen:
| $ 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 logt uit en logt vervolgens in als [martin]:
| $ 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:
| $ 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 maakt een reservering voor zichzelf (de body bevat geen [clientId]), één keer, niet twee keer:
| { "day": "2030-01-15", "slotId": 5 }
|
| $ 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}
|
| $ 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 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 als bij een afspraak die niet bestaat: hij onthult niets over de afspraken van andere patiënten:
| $ 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:
| $ 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 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
}
|
Hij beheert geen artsen:
| $ 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" }
|
| $ 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 naamgenoot: beide betrokken velden worden gemarkeerd:
| $ 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 }
|
| $ 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:
| $ 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:
| $ 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"}
|
| $ 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:
| $ 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:
| $ 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=fd231085-46dc-4a97-bc61-ea84494ee3c2; 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:
| $ 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" }
|
| $ 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:
| $ 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 logt in en leest haar dossier:
| $ 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 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 }
|
| $ 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"}
|
Het verwijderen van het account verwijdert ook de cookie:
| $ 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
{}
|
| $ 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:
| $ 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"}
|
| $ 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.