本記事では、Apple Push Notification サーバーへ認証された HTTP/2 リクエストを送信するため、sgcWebSockets JWT クライアントを設定する方法を紹介します。
ステートレスな認証トークンを使用して、Apple Push Notification サービス(APNs)との通信を安全に保護します。
まず Apple Developer アカウントから暗号化キーとキー ID を取得する必要があります。登録が成功すると、10 文字のキー ID と、.p8 拡張子を持つ認証トークン署名キーが得られます。
sgcWebSockets の JWT クライアントを使用して ES256 アルゴリズムで JWT を生成する必要があります。トークンは HTTP/2 リクエストごとに生成するのではなく、20 分未満で更新せず、60 分を超えないようにする必要があります。
JWT クライアントの設定
JWT クライアントを以下の値で設定します:
- JWTOptions.Header.Algorithm: is the encryption algorithm you used to encrypt the token. APNs supports only the ES256 algorithm.
- JWTOptions.Header.kid: is the 10-character Key ID obtained from your developer account.
- JWTOptions.Payload.iss: the value for which is the 10-character Team ID you use for developing your company's apps. Obtain this value from your developer account.
- JWTOptions.Payload.iat: The "issued at" time, whose value indicates the time at which this JSON token was generated. Specify the value as the number of seconds since Epoch, in UTC. The value must be no more than one hour from the current time.
- JWTOptions.RefreshTokenAfter: set the value in seconds to 40 minutes (60*40).
トークンベース接続を使用する場合、apns-topic ヘッダーをアプリのバンドル ID / アプリ ID(例: com.example.application)とともに送信する必要があります。
oHTTP := TsgcHTTP2Client.Create(nil);
oHTTP.TLSOptions.IOHandler := iohOpenSSL;
oJWT := TsgcHTTP_JWT_Client.Create(nil);
oHTTP.Authentication.Token.JWT := oJWT;
oJWT.JWTOptions.Header.alg := jwtES256;
oJWT.JWTOptions.Header.kid := 'apple key id';
oJWT.JWTOptions.Payload.iss := 'issuer';
oJWT.JWTOptions.Payload.iat := StrToInt64(GetDateTimeUnix(Now, False));
oJWT.JWTOptions.Algorithms.ES.PrivateKey.LoadFromFile('AuthKey_**.p8');
oJWT.JWTOptions.RefreshTokenAfter := 60*40;
oHTTP.Request.CustomHeaders.Clear;
oHTTP.Request.CustomHeaders.Add('apns-topic: com.example.application');
