以下是一个更全面的 Delphi 示例,演示了自定义端点、挑战策略、数据库支持的凭据存储、FIDO 元数据验证和跨域 iframe 支持。代码重点展示了用于执行安全策略的高级事件处理。
sgcWebSockets WebAuthn 服务器示例
procedure TForm1.ConfigureWebAuthn;
begin
// 组件配置
FWebAuthn := TsgcWSAPIServer_WebAuthn.Create(nil);
FWebAuthn.Server := FHTTPServer;
FWebAuthn.Enabled := True;
// 端点重映射
FWebAuthn.EndpointOptions.AuthenticationOptions := '/auth/options';
FWebAuthn.EndpointOptions.AuthenticationVerify := '/auth/verify';
FWebAuthn.EndpointOptions.RegistrationOptions := '/reg/options';
FWebAuthn.EndpointOptions.RegistrationVerify := '/reg/verify';
// 信赖方定义
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;
// 加密和用户体验策略
Algorithms := 'ES256,RS256';
UserVerification := 'preferred';
Attestation := 'direct';
TimeoutMS := 60000;
// 挑战设置
ChallengeOptions.ChallengeSize := 64; // 512-bit challenges
ChallengeOptions.RandomFunc := MyCryptoRandom; // custom RNG
// 元数据服务配置
MDS.Enabled := True;
MDS.MDS_FileName := 'mds.json';
MDS.RootCert_FileName := 'root.pem';
end;
// 绑定事件
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
// 验证用户是否有资格注册
if UserExists(Request.Username) then
raise Exception.Create('用户名已注册');
// 可选:分配用户句柄(二进制标识符)
Response.User.ID := HexToBin(UserGUIDToHex(GenerateGUID));
Response.AuthenticatorSelection.AuthenticatorAttachment := 'platform';
end;
procedure TForm1.AuthnRegVerify(Sender: TObject; const Credential: TsgcWebAuthnCredential; var Success: Boolean);
begin
// 对 MDS 条目进行额外的认证验证
Success := ValidateAttestationWithMDS(Credential);
end;
procedure TForm1.AuthnRegSuccess(Sender: TObject; const Credential: TsgcWebAuthnCredential);
begin
// 将凭据详细信息持久化到数据库
SaveCredentialToDB(
Credential.Username,
Credential.CredentialID,
Credential.PublicKey,
Credential.SignCount,
Credential.UserHandle
);
end;
procedure TForm1.AuthnOptionsRequest(Sender: TObject;
const Request: TsgcWebAuthnRequestOptions; Response: TsgcWebAuthnResponseOptions);
begin
// 检索用户的所有凭据 ID
Response.AllowCredentials := LoadCredentialIdsFromDB(Request.Username);
end;
procedure TForm1.AuthnVerify(Sender: TObject; const Credential: TsgcWebAuthnCredential; var Success: Boolean);
var
StoredCounter: Cardinal;
begin
// 确保签名计数器递增
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;
关键亮点
- 挑战强化 – 通过扩大挑战大小和使用密码学安全的随机数生成器,进一步降低重放攻击风险。
- 自定义用户句柄 – 分配唯一的二进制用户句柄允许认证器存储与用户名无关的隐私保护标识符。
- 基于元数据的认证验证 –
ValidateAttestationWithMDS例程交叉验证认证器型号、状态报告和吊销列表,确保只有受信任的设备才能注册。 - 签名计数器强制执行 –
AuthnVerify拒绝未严格递增认证器计数器的响应,检测克隆的凭据。 - 数据库集成 – 凭据数据、签名计数器和会话令牌通过外部持久化函数存储和更新,演示了如何将组件与实际后端集成。
- 跨域 Iframe 支持 – 通过
AllowCrossOrigins和配置的TopOrigins启用,允许从嵌入的框架(例如不同域上的登录部件)发起 WebAuthn 流程。 - 认证策略 – 直接认证结合 MDS 确保只有经批准的认证器才能注册,适用于企业合规场景。
- 传输选择 – 虽然未显示,但事件可以约束可接受的传输(例如
USB,NFC,BLE)以定制允许的认证器类型。
