3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[auth0] auth0/node-jwks-rsaを使ってJWT token JSON Web Keyを検証する時にgetSigningKeyをawaitする方法

Last updated at Posted at 2020-09-18

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で直ったっぽい。まだ、リリースされてない。

v1.10.0で直った

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?