Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unused keys in https://appleid.apple.com/auth/keys leading to invalid signatures #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

codlab
Copy link

@codlab codlab commented Feb 14, 2020

This PR focuses on fixing the invalid signature when dealing with jwt token signed by the non 0 index key of https://appleid.apple.com/auth/keys

@alexabidri
Copy link

alexabidri commented Feb 15, 2020

Hi codlab, thanks for the work. I also got an issue in production.

I did the fix on my side, I think the best way is to move from node-rsa to jwks-rsa. As mentioned in the apple documentation, to get the public keys, we need to deal properly
with json web keys to get the signin keys.

https://developer.apple.com/documentation/signinwithapplerestapi/fetch_apple_s_public_key_for_verifying_token_signature

https://developer.apple.com/documentation/signinwithapplerestapi/jwkset/keys

I did the same implementation mentionned in this link https://auth0.com/blog/implement-sign-in-with-apple-using-auth0-extensibility/

  function (accessToken, ctx, cb) {
    const jwt = require('[email protected]');
    const jwksClient = require('[email protected]');

    const client = jwksClient({
      jwksUri: 'https://appleid.apple.com/auth/keys',
      cache: true
    });

    const idToken = ctx.id_token;
    const decoded = jwt.decode(idToken, {complete: true});
    const {kid, alg} = decoded.header;

    client.getSigningKey(kid, (err, key) => {
      if (err) {
        console.log(err);
        return callback(err);
      }
      const signingKey = key.publicKey || key.rsaPublicKey;

      jwt.verify(idToken, signingKey, {
        issuer: 'https://appleid.apple.com',
        audience: 'com.mycustomdomain.webapp',
        algorithms: [alg]
      }, (err, profile) => {
        if (err) return cb(err);
        profile.id = profile.sub;
        cb(null, profile);
      });
    });
  }

I can do the PR if you agree

tomislavherman added a commit to tomislavherman/node-apple-signin that referenced this pull request Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants