我正在研究DeviceCheck
.生成JSON
网络令牌,我正在使用SwiftJWT
库。但我不知道我是如何为DeviceCheck
产生JWT
.
法典:
let contents = try String(contentsOfFile: filepath)
let headers = Header(kid: key_id)
struct MyClaims: Claims {
var iss: String
var iat: Date
var exp: Date
}
let jwt = JWT(header: headers, claims: MyClaims(iss: iss_id, iat: Date(timeIntervalSinceNow: 3600), exp: Date(timeIntervalSinceNow: 3600)))
let privateKey = contents.data(using: .utf8)!
let rsaJWTEncoder = JWTEncoder(jwtSigner: JWTSigner.es256(privateKey: privateKey))
var jwtString = try rsaJWTEncoder.encodeToString(jwt)
打印JWT
时,我得到以下响应:
JWT(header: SwiftJWT.Header(typ: Optional("JWT"), alg: 可选("ES256"), jku: nil, jwk: nil, kid: 可选("KEY_ID"), x5u: nil, x5c: nil, x5t: nil, x5tS256: nil, cty: nil, crit: nil), 声明:SwiftiOSDeviceCheck.ViewController。(未知上下文在 $104614dbc)。(未知上下文为 $104614e28)。MyClaims(iss: "ISS_ID", IAT: 2019-06-03 11:55:53 +0000, exp: 2019-06-03 11:55:53 +0000))
从 API 我得到的响应低于邮递员:
无法验证授权令牌
如何解决此问题?
我找到了这个问题的答案。我用了CupertinoJWS而不是SwiftJWT。
let jwt = JWT(keyID: keyID, teamID: teamID, issueDate: Date(), expireDuration: 60 * 60)
do {
let token = try jwt.sign(with: p8Key)
// Use the token in the authorization header in your requests connecting to Apple’s API server.
// e.g. urlRequest.addValue(_ value: "bearer (token)", forHTTPHeaderField field: "authorization")
print("Generated JWT: (token)")
return token
} catch {
// Handle error
}