WebAuthn geavanceerd gebruiksvoorbeeld

· Componenten

Hieronder vind je een uitgebreider Delphi-voorbeeld dat custom endpoints, challenge-beleid, database-gebaseerde credentialopslag, FIDO Metadata-validatie en cross-origin iframe-ondersteuning demonstreert. De code belicht geavanceerde event-afhandeling om beveiligingsbeleid af te dwingen. 

sgcWebSockets WebAuthn-server-voorbeeld

procedure TForm1.ConfigureWebAuthn;
begin
  // Component setup
  FWebAuthn := TsgcWSAPIServer_WebAuthn.Create(nil);
  FWebAuthn.Server := FHTTPServer;
  FWebAuthn.Enabled := True;
  // Endpoint remapping
  FWebAuthn.EndpointOptions.AuthenticationOptions := '/auth/options';
  FWebAuthn.EndpointOptions.AuthenticationVerify  := '/auth/verify';
  FWebAuthn.EndpointOptions.RegistrationOptions   := '/reg/options';
  FWebAuthn.EndpointOptions.RegistrationVerify    := '/reg/verify';
  // Relying-party definition
  with FWebAuthn.WebAuthnOptions do
  begin
    RelyingParty := 'secure.example.com';
    Origins      := 'https://app.example.com;https://login.example.net';
    TopOrigins   := 'https://host.example.org';
    AllowCrossOrigins := True;
    // Cryptographic & UX policies
    Algorithms       := 'ES256,RS256';
    UserVerification := 'preferred';
    Attestation      := 'direct';
    TimeoutMS        := 60000;
    // Challenge settings
    ChallengeOptions.ChallengeSize := 64; // 512-bit challenges
    ChallengeOptions.RandomFunc    := MyCryptoRandom; // custom RNG
    // Metadata Service configuration
    MDS.Enabled            := True;
    MDS.MDS_FileName       := 'mds.json';
    MDS.RootCert_FileName  := 'root.pem';
  end;
  // Hook events
  FWebAuthn.OnWebAuthnRegistrationOptionsRequest := AuthnRegOptionsRequest;
  FWebAuthn.OnWebAuthnRegistrationVerify         := AuthnRegVerify;
  FWebAuthn.OnWebAuthnRegistrationSuccessful     := AuthnRegSuccess;
  FWebAuthn.OnWebAuthnAuthenticationOptionsRequest := AuthnOptionsRequest;
  FWebAuthn.OnWebAuthnAuthenticationVerify         := AuthnVerify;
  FWebAuthn.OnWebAuthnAuthenticationSuccessful     := AuthnSuccess;
end;

Event-implementaties

procedure TForm1.AuthnRegOptionsRequest(Sender: TObject;
  const Request: TsgcWebAuthnRequestOptions; Response: TsgcWebAuthnResponseOptions);
begin
  // Verify user is eligible for registration
  if UserExists(Request.Username) then
    raise Exception.Create('Username already registered');
  // Optionally assign a user handle (binary identifier)
  Response.User.ID := HexToBin(UserGUIDToHex(GenerateGUID));
  Response.AuthenticatorSelection.AuthenticatorAttachment := 'platform';
end;
procedure TForm1.AuthnRegVerify(Sender: TObject; const Credential: TsgcWebAuthnCredential; var Success: Boolean);
begin
  // Perform extra attestation validation against MDS entries
  Success := ValidateAttestationWithMDS(Credential);
end;
procedure TForm1.AuthnRegSuccess(Sender: TObject; const Credential: TsgcWebAuthnCredential);
begin
  // Persist credential details in database
  SaveCredentialToDB(
    Credential.Username,
    Credential.CredentialID,
    Credential.PublicKey,
    Credential.SignCount,
    Credential.UserHandle
  );
end;
procedure TForm1.AuthnOptionsRequest(Sender: TObject;
  const Request: TsgcWebAuthnRequestOptions; Response: TsgcWebAuthnResponseOptions);
begin
  // Retrieve all credential IDs for user
  Response.AllowCredentials := LoadCredentialIdsFromDB(Request.Username);
end;
procedure TForm1.AuthnVerify(Sender: TObject; const Credential: TsgcWebAuthnCredential; var Success: Boolean);
var
  StoredCounter: Cardinal;
begin
  // Ensure sign counter increases
  StoredCounter := GetSignCounterFromDB(Credential.CredentialID);
  if Credential.SignCount <= StoredCounter then
    Success := False
  else
    Success := True;
end;
procedure TForm1.AuthnSuccess(Sender: TObject; const Credential: TsgcWebAuthnCredential);
begin
  UpdateSignCounterInDB(Credential.CredentialID, Credential.SignCount);
  IssueSessionToken(Credential.Username);
end;

Belangrijkste highlights

  1. Challenge-hardening – door de challenge-grootte uit te breiden en een cryptografisch veilige RNG te gebruiken, worden replay-aanvallen verder beperkt.
  2. Custom user handles – door een unieke binaire user-handle toe te wijzen, kan de authenticator een privacybewarende identifier opslaan onafhankelijk van gebruikersnamen.
  3. Metadata-gebaseerde attestation-validatie – de routine ValidateAttestationWithMDS controleert het authenticator-model, statusrapporten en revocation-lijsten, en zorgt ervoor dat alleen vertrouwde apparaten worden geregistreerd.
  4. Sign-counter-handhavingAuthnVerify weigert reacties die de teller van de authenticator niet strikt verhogen, en detecteert zo gekloonde credentials.
  5. Database-integratie – credential-gegevens, sign-counters en sessietokens worden opgeslagen en bijgewerkt via externe persistentiefuncties, en laten zien hoe je het component integreert met een real-world backend.
  6. Cross-origin iframe-ondersteuning – ingeschakeld via AllowCrossOrigins en geconfigureerde TopOrigins, waardoor WebAuthn-flows gestart vanuit embedded frames mogelijk zijn (bv. een login-widget op een ander domein).
  7. Attestation-beleid – directe attestation gekoppeld aan MDS zorgt ervoor dat alleen goedgekeurde authenticators kunnen registreren, handig voor enterprise-compliance-scenario's.
  8. Transport-selectie – hoewel niet getoond, kunnen events de toegestane transports beperken (bv. USB,NFC,BLE) om aan te passen welke typen authenticators zijn toegestaan.