3

我正在努力DeviceCheck。生成JSON网络令牌&我正在使用SwiftJWT库。但我不知道我是如何生成JWTDeviceCheck

代码:

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(标头:SwiftJWT.Header(typ:可选(“JWT”),alg:可选(“ES256”),jku:无,jwk:无,孩子:可选(“KEY_ID”),x5u:无,x5c:无, 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 我在邮递员中得到以下响应:

无法验证授权令牌

我该如何解决这个问题?

4

1 回答 1

2

我找到了这个问题的答案。我使用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
    }
于 2019-06-17T12:22:49.347 回答