Slide 12
Slide 12 text
!
import Foundation
import JWT
struct JWTEncoder {
func encode(issuerID: UUID, keyID: String) throws -> String {
let object = UnsafeMutablePointer.allocate(capacity: MemoryLayout.size)
jwt_new(object)
defer { jwt_free(object.pointee) }
let keyPointer = convertToCString(privateKey)
defer { keyPointer.deallocate() }
jwt_set_alg(object.pointee,
JWT_ALG_ES256,
keyPointer,
Int32(privateKey.utf16.count + 1))
// https://github.com/benmcollins/libjwt/pull/71
jwt_add_header(object.pointee, "kid", keyID)
jwt_add_grant(object.pointee, "iss", issuerID.uuidString.lowercased())
let expirationDate = Date().addingTimeInterval(expirationInterval)
jwt_add_grant_int(object.pointee, "exp", Int(expirationDate.timeIntervalSince1970))
jwt_add_grant(object.pointee, "aud", "appstoreconnect-v1")
guard let encodedCString = jwt_encode_str(object.pointee) else {
throw Error.decodeError
}
return String(cString: encodedCString)
}
}