Last active
September 7, 2020 15:59
-
-
Save ArnauMrJeff/99e9d8d9d0d8ac4d6ce2bdc7d1292577 to your computer and use it in GitHub Desktop.
Apple ID Sign In: CreatePublicKeyApple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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