我跟随this article在我们的Node.JS应用中实施Azure B2C。 我找回了JWT令牌并尝试验证签名。 使用jsonwebtoken npm模块验证我的令牌。 此外,我从OpenID Connect元数据端点获得了公钥。它们是JSON,看起来像这样:
return map.entrySet().stream().anyMatch(entry -> entry.getKey().equals(id) && value.isPresent() && entry.getValue().equals(value.get()));
因此,当我试图通过' n'从适当的键到
的值{ "keys": [{
"kid": "some kid value",
"nbf": some number,
"use": "sig",
"kty": "RSA",
"e": "AQAB",
"n": "some long key"
}, {
"kid": "some kid value",
"nbf": some number,
"use": "sig",
"kty": "RSA",
"e": "AQAB",
"n": "some long key"
}, {
"kid": "some kid value",
"nbf": some number,
"use": "sig",
"kty": "RSA",
"e": "AQAB",
"n": "some long key"
}]
}
我得到了
错误:PEM_read_bio_PUBKEY失败
我觉得我传递了错误的密钥,但我无法找到有关如何使用此公钥元数据验证令牌的任何解释。 文章中唯一有用的一句话:
如何执行签名验证的说明超出了本文档的范围。如果您需要,可以使用许多开源库来帮助您。
如何验证签名?
答案 0 :(得分:4)
所以,我在Passport-Azure-AD
的源代码中找到了答案行号142.有一个函数rsaPublicKeyPem(key1,key2)
const aadutils = require('./aadutils');
const jwt = require('jsonwebtoken');
//key is an object from public endpoint. Just follow the tutorial
const pubKey = aadutils.rsaPublicKeyPem(key.n, key.e);
jwt.verify(id_token, pubKey, { algorithms: ['RS256'] }, function(err, decoded) {
//do what you want next
});
我复制了整个aadutils库并用键
调用了这个函数@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View mContentView = inflater.inflate(R.layout.layout_trophy, null);
initUI(mContentView);
mContentView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
mBehavior = BottomSheetBehavior.from(bottomSheet);
if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
mBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = LayoutUtils.getScreenHeight(getActivity());
final double desiredHeight = PurchaseConstants.SLIDING_ANCHOR_POINT * height;
mContentView.getLayoutParams().height = height;
mBehavior.setPeekHeight((int) desiredHeight);
}
}
});
return mContentView;
}
我的签名已经过验证。