Skip to content

Instantly share code, notes, and snippets.

@ArnauMrJeff
Last active September 7, 2020 15:59
Show Gist options
  • Save ArnauMrJeff/99e9d8d9d0d8ac4d6ce2bdc7d1292577 to your computer and use it in GitHub Desktop.
Save ArnauMrJeff/99e9d8d9d0d8ac4d6ce2bdc7d1292577 to your computer and use it in GitHub Desktop.
Apple ID Sign In: CreatePublicKeyApple
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwsHeader;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
private PublicKey createPublicKeyApple(String keyIdentifier) throws InvalidKeySpecException, NoSuchAlgorithmException {
// Http Requests Client
var applePublicKeysList = httpClient.getListPublicKeys()
var applePublicKey = applePublicKeysList.getKeys().stream()
.filter(publicKey -> keyIdentifier.equals(publicKey.getKid()))
.findFirst();
if (!applePublicKey.isPresent()) throw new AppleKeysException("Not matching key");
var modulus = new BigInteger(1, Base64.getUrlDecoder().decode(applePublicKey.get().getN()));
var exponent = new BigInteger(1, Base64.getUrlDecoder().decode(applePublicKey.get().getE()));
return KeyFactory.getInstance(applePublicKey.get().getKty()).generatePublic(new RSAPublicKeySpec(modulus, exponent));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment