【发布时间】:2018-07-04 22:19:42
【问题描述】:
我正在尝试在 iOS11 中使用 Apple 的新 DeviceCheck 机制实现免费试用机制。我已经在 RAILs 4 中实现了服务器部分。有相当多的代码,所以我把它放在一个要点中:https://gist.github.com/jmfriend/b86f52f8f0649ad4cae176c08b77f000
我收到错误消息:“授权令牌丢失或格式错误”。这表明我在为 AuthKey_#####.p8 文件生成 JWT 时做错了。
这段代码也在要点中,但为了便于参考,问题可能出在这就是处理 p8 文件的代码:
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
【问题讨论】:
-
发现一个错误,header_files应该是header_fields。不幸的是,仍然出现同样的错误。
-
现在已经针对实时 Apple 服务器进行了尝试,并得到了不同的响应。奇怪的是,不再收到“缺少或格式错误的授权令牌”。但是,我收到 200 响应消息:“无法找到位状态”。这不是 JSON,所以破坏了我期望 JSON 的代码。有谁知道这个消息是什么意思?它在官方文档中,但我没有解释为什么你会得到它。
标签: ruby-on-rails json jwt ios11 devicecheck