Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facing issue while generating Authorization bearer token for DeviceCheck API in swift

I'm working on DeviceCheck. To generate JSON web token & I'm using SwiftJWT library. But I don't know how I generate JWT for DeviceCheck.

Code:

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)

While printing JWT I'm getting below response:

JWT(header: SwiftJWT.Header(typ: Optional("JWT"), alg: Optional("ES256"), jku: nil, jwk: nil, kid: Optional("KEY_ID"), x5u: nil, x5c: nil, x5t: nil, x5tS256: nil, cty: nil, crit: nil), claims: SwiftiOSDeviceCheck.ViewController.(unknown context at $104614dbc).(unknown context at $104614e28).MyClaims(iss: "ISS_ID", iat: 2019-06-03 11:55:53 +0000, exp: 2019-06-03 11:55:53 +0000))

and from API I'm getting below response in postman:

Unable to verify authorization token

How can I fix this issue?

like image 770
Vikram Chaudhary Avatar asked Mar 31 '26 07:03

Vikram Chaudhary


1 Answers

I found the answer of this issue. I used CupertinoJWS instead of 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
    }
like image 127
Vikram Chaudhary Avatar answered Apr 02 '26 21:04

Vikram Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!