WebAuthn 高度な使用例

· コンポーネント

以下は、カスタムエンドポイント・チャレンジポリシー・データベース連携の資格情報ストレージ・FIDO メタデータ検証・クロスオリジン iframe サポートを示す包括的な Delphi サンプルです。セキュリティポリシーを適用するための高度なイベント処理を示しています。 

sgcWebSockets WebAuthn サーバーサンプル

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;

イベント実装

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;

重要なポイント

  1. チャレンジの強化 – チャレンジサイズを拡大し暗号学的に安全な RNG を使用することで、リプレイ攻撃をさらに軽減します。
  2. カスタムユーザーハンドル – 一意のバイナリユーザーハンドルを割り当てることで、オーセンティケーターがユーザー名とは独立したプライバシー保護識別子を保存できます。
  3. メタデータベースの構成証明検証ValidateAttestationWithMDS ルーティンがオーセンティケーターモデル・ステータスレポート・失効リストを照合し、信頼済みデバイスのみが登録されるようにします。
  4. 署名カウンター強制AuthnVerify はオーセンティケーターのカウンターを厳密にインクリメントしないレスポンスを拒否し、クローン認証情報を検出します。
  5. データベース統合 – 資格情報データ・署名カウンター・セッショントークンが外部永続化関数を介して保存・更新され、実際のバックエンドとコンポーネントを統合する方法を示しています。
  6. クロスオリジン iframe サポートAllowCrossOrigins と設定された TopOrigins によって有効化され、埋め込みフレーム(例:別ドメインのログインウィジェット)から開始される WebAuthn フローを許可します。
  7. 構成証明ポリシー – MDS と組み合わせた直接構成証明により、承認済みのオーセンティケーターのみが登録でき、エンタープライズコンプライアンスシナリオに有用です。
  8. トランスポート選択 – 未表示ですが、イベントで許容されるトランスポート(例:USB,NFC,BLE)を制限し、許可するオーセンティケーターの種類を調整できます。