public void ClearJwtToken(ApnSettings settings) { var matching = GetJwtToken(settings); if (matching != null) { tokens.TryRemove(matching, out _); } }
/// <summary> /// Apple push notification sender constructor /// </summary> /// <param name="settings">Apple Push Notification settings</param> /// <param name="http">HTTP client instance</param> public ApnSender(ApnSettings settings, HttpClient http) { this.settings = settings ?? throw new ArgumentNullException(nameof(settings)); this.http = http ?? throw new ArgumentNullException(nameof(http)); if (http.BaseAddress == null) { http.BaseAddress = new Uri(servers[settings.ServerType]); } }
public string GetJwtToken(ApnSettings settings) { var(token, date) = tokens.GetOrAdd(settings.AppBundleIdentifier, _ => new Tuple <string, DateTime>(CreateJwtToken(settings), DateTime.UtcNow)); if (date < DateTime.UtcNow.AddMinutes(-tokenExpiresMinutes)) { tokens.TryRemove(settings.AppBundleIdentifier, out _); return(GetJwtToken(settings)); } return(token); }
public string CreateJwtToken(ApnSettings settings) { var header = JsonHelper.Serialize(new { alg = "ES256", kid = CleanP8Key(settings.P8PrivateKeyId) }); var payload = JsonHelper.Serialize(new { iss = settings.TeamId, iat = ToEpoch(DateTime.UtcNow) }); var headerBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(header)); var payloadBasae64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(payload)); var unsignedJwtData = $"{headerBase64}.{payloadBasae64}"; var unsignedJwtBytes = Encoding.UTF8.GetBytes(unsignedJwtData); using var dsa = ECDsa.Create(); dsa.ImportPkcs8PrivateKey(Convert.FromBase64String(CleanP8Key(settings.P8PrivateKey)), out _); var signature = dsa.SignData(unsignedJwtBytes, 0, unsignedJwtBytes.Length, HashAlgorithmName.SHA256); return($"{unsignedJwtData}.{Convert.ToBase64String(signature)}"); }
/// <summary> /// Apple push notification sender constructor /// </summary> /// <param name="settings">Apple Push Notification settings</param> public ApnSender(ApnSettings settings, HttpClient http) { this.settings = settings ?? throw new ArgumentNullException(nameof(settings)); this.http = http ?? throw new ArgumentNullException(nameof(http)); }