I'm trying to implement a free trial mechanism using Apple's new DeviceCheck mechanism in iOS11. I've implemented the server part in RAILs 4. There's a fair bit of code, so I've put it in a gist: https://gist.github.com/jmfriend/b86f52f8f0649ad4cae176c08b77f000
I get the error: "Missing or badly formatted authorization token". That suggests that I'm doing something wrong when generating the JWT for the AuthKey_#####.p8 file.
This code is also in the gist, but for ease of reference given it's probably where the issue is this is the code that handles the p8 file:
def auth_header
# The authentication key must must use the ES256 algorithm and be in the Base 64 URL–encoded JSON web token format.
# If your token doesn't use this format, you receive a BAD_AUTHENTICATION_TOKEN HTTP error.
"Bearer #{auth_token}"
end
def auth_token
@auth_token ||= fetch_auth_token
end
def fetch_auth_token
header = {
typ: "JWT", # Must be specified; not in documentation
alg: "ES256",
kid: key_id
}
body = {
iss: team_id,
iat: DateTime.now().to_time.to_i ,
exp: DateTime.now().to_time.to_i + 43_200 # 12hrs # Time.now.to_i
}
authentication_token = JWT.encode(body, auth_key, 'ES256', header_files = header)
authentication_token
end
def auth_key
file = File.read(developer_token_file)
key = OpenSSL::PKey::EC.new(file)
key.check_key
key
end