2017-02-03 3 views
0

Node.JS 앱에 Azure B2C를 구현하려면 this article을 따르고 있습니다. JWT 토큰을 다시 가져 와서 서명을 확인하려고합니다. jsonwebtoken npm 모듈을 사용하여 내 토큰을 확인하십시오. 또한 OpenID Connect 메타 데이터 엔드 포인트에서 공개 키를 받았습니다. 그들은 JSON에 있고 다음과 같습니다NodeJS가있는 Azure AD B2C의 JWT 서명 확인

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

@ 주니어, 아니요, [this link] (https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/discovery/v2.0/keys?p=b2c_1_sign_in)의 키를 가져오고 있습니다 [this 튜토리얼] (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. 기능 rsaPublicKeyPem 내가 라이브러리 전체 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 
}); 
이 기능이라고

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; 
}; 
  • (키 1, 키 2)가 수 (142)

    라인 검증 됨.

  • 관련 문제