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] en [Angular]: de server van de cursus [NestJS], waarvan de controllers geen HTML-pagina's meer genereren maar JSON-antwoorden opleveren. Lezers van deze lessen kennen dit hoofdstuk al; het is hier vrijwel ongewijzigd overgenomen. Zij hoeven zich alleen te richten op de paragraaf over authenticatie, die uitlegt wat er voor een mobiele applicatie verandert.
In deze cursus is deze server een blackbox: u hoeft niet te weten hoe hij is geschreven. Het gaat alleen om de API, dat wil zeggen de lijst met HTTP-verzoeken die hij accepteert en de antwoorden die hij teruggeeft. 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.
Dit hoofdstuk legt uit hoe u de server installeert, beschrijft de API en bevraagt de server met [curl].
8.1. De applicatie RdvMedecins
De applicatie beheert de afspraken van een medische praktijk. Er zijn drie rollen:
Rol | Wie | Wat zij kunnen doen |
[ADMIN] | De beheerder ([admin]) | Afspraken inplannen en annuleren voor alle patiënten; artsen en clients 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 |
Demoaccounts 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 clients) mogen 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.
8.2. Installatie
U hebt [Node.js] en een [MySQL]-server (of [MariaDB]) nodig: de installatie ervan wordt beschreven in de bijlage.
- 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 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 beginstand te herstellen.
- Bibliotheken. In een consolevenster dat is geopend in de map [rdvmedecins-nestjs-json]:
- 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 vervolgens 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-queries 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 over een onversleutelde verbinding verstuurd.
COOKIE_SECURE=false
# -----------------------------------------------------------------------
# bescherming tegen bots
# -----------------------------------------------------------------------
# login: 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 duur (een IP-adres wordt ook geblokkeerd als het 5 keer
# meer mislukkingen verzamelt, alle logins samen)
LOGIN_BLOCK_MINUTES=15
# het aanmaken van klantaccounts: maximaal aantal accounts dat per uur wordt aangemaakt
# vanaf hetzelfde IP-adres. In een leslokaal 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 aan te roepen (u kunt de afbeelding niet in een console "lezen").
# In productie LEEG laten.
CAPTCHA_TEST=
# -----------------------------------------------------------------------
# productie
# -----------------------------------------------------------------------
# map van de gecompileerde webclient, 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;
- regels 12–16: de verbinding met [MySQL]. Met [Laragon] heeft de gebruiker [root] geen wachtwoord: de standaardwaarden volstaan;
- regel 25: de geheime sleutel waarmee de authenticatietokens worden ondertekend;
- regels 38–45: maatregelen tegen bots (blokkering na meerdere mislukte inlogpogingen, aantal accounts dat per uur wordt aangemaakt);
- Regel 51: testmodus voor de CAPTCHA. De inlogpagina vraagt u de tekens van 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, vergelijkbaar met de „testsleutels” die [Google] aanbiedt voor [reCAPTCHA]. Voor de tests met [curl] in dit hoofdstuk geeft u op [CAPTCHA_TEST=test]. De geautomatiseerde tests die de schermafbeeldingen voor deze cursus hebben gegenereerd, gebruiken deze waarde ook. Laat de variabele in productie LEEG;
- regels 57–59: twee instellingen voor productie (zie het hoofdstuk „Uitrol”).
- 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 bereikbaar 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 object met ALTIJD hetzelfde formaat:
| {
"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 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 per 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 toegangspas) |
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 de ontwerpkeuzes worden bepaald door de risico's die specifiek zijn voor webbrowsers:
- [HttpOnly]: de code van de pagina ([JavaScript]) KAN dit 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 zelf niet vaststellen 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? Er is geen browser: niemand bewaart het cookie of stuurt het uit zichzelf terug. De app neemt deze rol over door de header [Set-Cookie] in de antwoorden te lezen en de header [Cookie] in de verzoeken te schrijven (voorbeeld 23). De attributen [HttpOnly] en [SameSite] gelden alleen voor browsers: ze hebben geen effect op de app, evenmin als op [curl]. Dezelfde [Flutter]-applicatie zal echter, wanneer ze voor het web is gecompileerd, werken zoals browsers dat doen: het cookie wordt door de browser beheerd, mits de applicatie en de API dezelfde oorsprong hebben (zie voorbeeld 22 en het hoofdstuk „Uitrol”).
8.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 het cookie |
GET | [/api/auth/me] | ingelogd | antwoord: [{ login, name, role }] |
GET | [/api/schedule?doctorId=&day=] | allen | Antwoord: de agenda (zonder doctorId: voor een DOCTOR, zijn eigen agenda) |
POST | [/api/appointments] | allen | [{ day, slotId, clientId }] (clientId wordt genegeerd voor een USER); 201 |
DELETE | [/api/appointments/:id] | allen | Een USER annuleert alleen zijn eigen afspraken |
GET | [/api/doctors] | allen | 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 + toegangspas | [{ 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 regel van de agenda ([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 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 afspraak)?
- [appointmentId]: ingevuld als de gebruiker deze afspraak mag annuleren.
De server BESLIST; de client toont. En zelfs als een client vals zou spelen (door een knop „Boeken” te tonen terwijl dat niet hoort), zou de server bij het boeken alles opnieuw controleren.
8.6. De API bevragen met curl
De map [curl] op de server bevat de JSON-requestbodies van 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]: 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 later; 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] 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 vrij lange antwoorden in JSON zijn voor dit document opgemaakt en de headers [Date], [ETag], [Connection], en [Keep-Alive] zijn weggelaten.
8.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: 11428
{"id": "968678a8-25f0-4892-9fc4-bd93533dab33", "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"150..."}
|
Zonder token geeft een beveiligde URL een 401-antwoord terug:
| $ 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 het cookie, dat [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 dit cookie weet de server wie wij 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 instrueert de browser het 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"}
|
8.6.2. Artsen, clients, 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"}
|
8.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, 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. Ook een afspraak in het verleden wordt geweigerd:
| $ 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 geweigerd:
| $ 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]:
| $ 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 clients:
| $ 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]), éé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 voor 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
{}
|
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
}
|
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"}
|
8.6.6. Artsen en clients 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 per 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 indienen (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
{}
|
8.6.7. Een account aanmaken
Zonder toegangspas 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=60f5fa35-f939-4c87-b5a2-7c2f38054cc3; 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"}
|
De toegangspas is gebruikt: hij 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"}
|
8.6.8. Uw account beheren
De nieuwe patiënt logt in en leest haar accountrecord:
| $ 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 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 }
|
| $ 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 het 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"}
|
8.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.