webauthn-lib 4.7:如何为PublicKeyCredentialCreationOptions设置算法?
解决webauthn-lib 4.7中PublicKeyCredentialCreationOptions缺少$pubKeyCredParams参数的问题
你遇到的问题是因为PublicKeyCredentialCreationOptions::create()方法要求必须传入$pubKeyCredParams参数——这是一个至少包含一个公钥算法配置的数组,和Algorithm Manager中配置的ES256、RS256等算法完全对应,只需要转换成PublicKeyCredentialParam对象的数组即可。
具体格式和代码示例
每个算法项需要用PublicKeyCredentialParam类实例化,参数规则:
type:固定为public-key(WebAuthn标准强制要求)alg:对应的COSE算法标识符,比如ES256对应-7,RS256对应-257,也可以直接使用库中预定义的常量(如Algorithm::ES256、Algorithm::RS256)
修改你的代码如下:
use Webauthn\PublicKeyCredentialParam; use Webauthn\Algorithm; $user_entity = new PublicKeyCredentialUserEntity($user->getAccountName(), $user->id(), $user->getDisplayName()); $rp_entity = PublicKeyCredentialRpEntity::create('My app', 'example.com', null); $challenge = random_bytes(16); // 构建公钥算法列表 $pubKeyCredParams = [ new PublicKeyCredentialParam('public-key', Algorithm::ES256), // 可选:添加更多兼容算法 // new PublicKeyCredentialParam('public-key', Algorithm::RS256), ]; $credential_options = PublicKeyCredentialCreationOptions::create( $rp_entity, $user_entity, $challenge, $pubKeyCredParams, // 其他可选参数(如excludeCredentials)可按需传入 // $exclude_credentials );
和Algorithm Manager配置的一致性
Algorithm Manager中配置的ES256、RS256等算法,和这里使用的Algorithm::ES256、Algorithm::RS256常量完全对应,底层指向的COSE算法数值一致,不需要额外转换,直接使用库提供的常量就能保证兼容性。
内容的提问来源于stack exchange,提问作者Patrick Kenny
相关产品推荐
相关产品推荐

