2017-02-03 31 views
0

Node.JSアプリでAzure B2Cを実装するには、私はthis articleに従っています。 私はJWTトークンを取り戻し、署名を検証しようとしました。 jsonwebtoken npmモジュールを使用して自分のトークンを確認してください。 また、OpenID Connectメタデータエンドポイントから公開鍵を取得しました。彼らはJSONであり、次のようになります。NodeJSでAzureのB2Cで署名を検証する

{ "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" 
    }] 
} 

をだから私は

jwt.verify(token, 'my n value go here', { algorithms: ['RS256'] }, callbackFunction()); 

に適切なキーから 'n' の値を渡すためにしようとしている私は

Error: PEM_read_bio_PUBKEY failed

Iを得たとき間違ったキーを渡しているような気がして、トークンを検証するためにこの公開鍵のメタデータをどのように使うことができるかについては何の説明も見つけられませんでした。 記事からのみ有用ライン:

A description of how to perform signature validation is outside the scope of this document. Many open source libraries are available to help you with this if you need it.

は、どのように私は署名を検証するのですか?

+0

ここからキーを入手しましたか:https://login.microsoftonline.com/common/discovery/keys?なぜならキーのように見える 'x5c'というプロパティがあるからです。 – juunas

+0

@juunas、いいえ、[このリンク](https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/discovery/v2.0/keys?p=b2c_1_sign_in)からキーを取得していますチュートリアル](https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-oidc)、キーにx5cフィールドはありません。 –

+0

[This記事](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-signing-key-rollover)では、トークンの検証に必要なメタデータのエンドポイントとキーについて説明しています。 –

答えて

1

だから、私はソースコードの「LIB」フォルダとはaadutils.jsファイルを見つけることがPassport-Azure-AD

  1. ゴーのソースコードで答えを見つけました。
  2. 行番号142の関数rsaPublicKeyPem(KEY1、KEY2)

    exports.rsaPublicKeyPem = (modulusB64, exponentB64) => { 
        const modulus = new Buffer(modulusB64, 'base64'); 
        const exponent = new Buffer(exponentB64, 'base64'); 
    
        const modulusHex = prepadSigned(modulus.toString('hex')); 
        const exponentHex = prepadSigned(exponent.toString('hex')); 
    
        const modlen = modulusHex.length/2; 
        const explen = exponentHex.length/2; 
    
        const encodedModlen = encodeLengthHex(modlen); 
        const encodedExplen = encodeLengthHex(explen); 
        const encodedPubkey = `30${encodeLengthHex(
         modlen + 
         explen + 
         encodedModlen.length/2 + 
         encodedExplen.length/2 + 2 
        )}02${encodedModlen}${modulusHex}02${encodedExplen}${exponentHex}`; 
    
        const derB64 = new Buffer(encodedPubkey,'hex').toString('base64'); 
    
        const pem = `-----BEGIN RSA PUBLIC KEY-----\n${derB64.match(/.{1,64}/g).join('\n')}\n-----END RSA PUBLIC KEY-----\n`; 
    
        return pem; 
    }; 
    
  3. がある私は、全体のaadutilsライブラリをコピーして、キー

    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 
    }); 
    

で、この関数を呼び出した私の署名を手に入れました検証された。

関連する問題