Created
January 10, 2023 06:27
-
-
Save mefarazath/e1d1e2bd1d142456769b598a04abdec9 to your computer and use it in GitHub Desktop.
Create Apple Client Secret
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 com.nimbusds.jose.*; | |
import com.nimbusds.jose.crypto.*; | |
import com.nimbusds.jose.jwk.*; | |
import com.nimbusds.jwt.*; | |
import java.io.*; | |
import java.security.*; | |
import java.security.spec.*; | |
import java.time.*; | |
import java.util.*; | |
public class JWTExample { | |
public static void main(String[] args) throws Exception { | |
// Your private key from Apple | |
File keyFile = new File("key.txt"); | |
// Your 10-character Team ID | |
String teamId = ""; | |
// Your Services ID, e.g. idp.applesignintest.com | |
String clientId = ""; | |
// 10-char Key ID value from the Keys section | |
String keyId = ""; | |
// Read the key file and create an EC key | |
byte[] keyBytes = Files.readAllBytes(keyFile.toPath()); | |
KeyFactory kf = KeyFactory.getInstance("EC"); | |
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); | |
PrivateKey privateKey = kf.generatePrivate(spec); | |
// Create the JWT header with the key ID | |
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.ES256) | |
.keyID(keyId) | |
.build(); | |
// Set the claims for the JWT | |
Date now = new Date(); | |
Date exp = Date.from(Instant.ofEpochSecond(now.getTime() / 1000 + 86400 * 180)); | |
JWTClaimsSet claims = new JWTClaimsSet.Builder() | |
.issuer(teamId) | |
.issueTime(now) | |
.expirationTime(exp) | |
.audience("https://appleid.apple.com") | |
.subject(clientId) | |
.build(); | |
// Sign the JWT with the private key | |
JWSSigner signer = new ECDSASigner(privateKey); | |
SignedJWT signedJWT = new SignedJWT(header, claims); | |
signedJWT.sign(signer); | |
// Print the resulting JWT | |
System.out.println(signedJWT.serialize()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment