auth0発行のログインユーザーアクセストークンの検証
通常jwks-rsaパッケージを使って改ざんされていないかを確認する
問題点
// https://github.com/auth0/node-jwks-rsa
const jwksClient = require('jwks-rsa');
const client = jwksClient({
strictSsl: true, // Default value
jwksUri: 'https://sandrino.auth0.com/.well-known/jwks.json',
requestHeaders: {}, // Optional
requestAgentOptions: {}, // Optional
timeout: 30000, // Defaults to 30s
proxy: '[protocol]://[username]:[pass]@[address]:[port]', // Optional
});
const kid = 'RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg';
client.getSigningKey(kid, (err, key) => {
const signingKey = key.getPublicKey();
// Now I can use this to configure my Express or Hapi middleware
});
このclient.getSigningKey
がコールバックを呼ぶメソッドになっており、Promiseを返さない。await出来ない。
解決方法
Promiseなメソッドに変えてやる。
https://github.com/auth0/node-jwks-rsa/issues/76#issuecomment-569486814
const { promisify } = require("util");
const jwksClient = require("jwks-rsa");
...
const client = jwksClient({ <yourOptions> });
// promisify the function:
const getSigningKey = promisify(client.getSigningKey);
// use it like any other promise:
const key = await getSigningKey(kid);
v1.9.0からは...
Readmeにもあるように
Note that all methods on the JwksClient have asynchronous equivalents, where the promisified name is suffixed with Async, e.g., client.getSigningKeyAsync(kid).then(key => { /* ... */ });
getSigningKeyAsync
でawaitすれば良いのだが...
v1.9.0でもTypescriptだと
まだ定義が更新されてないので、コンパイルエラーになる
https://github.com/auth0/node-jwks-rsa/issues/164
masterで直ったっぽい。まだ、リリースされてない。