Utilities to simplify Sign in with Apple for Vapor projects.
Link to your project like so:
dependencies: [
...
.package(url: "https://github.com/mpdifran/vapor-sign-in-with-apple.git", from: "1.0.0"),
],
targets: [
.executableTarget(
name: "App",
dependencies: [
...
.product(name: "SignInWithApple", package: "vapor-sign-in-with-apple"),
]
)
],
Create an ApplePrivateKey
with the name and contents of the JWT Apple provides you. Register a new key on the developer portal here.
let siwaJWKID = "12345ABCDE" // Example key name
let siwaPrivateKey = "<contents of file as String>" // Store this in an environment variable, do not check into source control.
let privateKey = try ApplePrivateKey(
kid: JWKIdentifier(string: siwaJWKId),
privateKey: siwaPrivateKey
)
Use the following method to generate refresh and access tokens from Apple's servers. Apple's documentation on this process can be found here.
let details = AppleTokenGenerationDetails(
teamIdentifier: 123456, // Your Apple Team ID.
appIdentifier: com.example.app, // Application Bundle ID.
identityToken: "ABCDEF", // Identity Token generated by Sign in with Apple on the client.
authorizationCode: "1234", // Authorization code generated by Sign in with Apple on the client.
privateKey: privateKey // See above for generation details.
)
let tokenResponse = try await request.signInWithApple.generateAppleTokens(details: details)
// Store tokens
Use the following method to validate an existing refresh token obtained by the above method. Apple's documentation on this process can be found here.
let details = AppleTokenValidationDetails(
teamIdentifier: 123456, // Your Apple Team ID.
appIdentifier: com.example.app, // Application Bundle ID.
identityToken: "ABCDEF", // Identity Token generated by Sign in with Apple on the client.
refreshToken: "1A2B3C", // Refresh token stored from previous call to `generateAppleTokens(details:)`.
privateKey: privateKey // See above for generation details.
)
let tokenResponse = try await request.signInWithApple.validateAppleTokens(details: details)
// Store tokens