|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the APNSwift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 the APNSwift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of APNSwift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Crypto |
| 16 | +import NIOSSL |
| 17 | +import NIOTLS |
| 18 | +import AsyncHTTPClient |
| 19 | + |
| 20 | +/// The configuration of an ``APNSClient``. |
| 21 | +public struct APNSClientConfiguration { |
| 22 | + /// The authentication method used by the ``APNSClient``. |
| 23 | + public struct AuthenticationMethod { |
| 24 | + internal enum Method { |
| 25 | + case jwt(privateKey: P256.Signing.PrivateKey, teamIdentifier: String, keyIdentifier: String) |
| 26 | + case tls(privateKey: NIOSSLPrivateKeySource, certificateChain: [NIOSSLCertificateSource]) |
| 27 | + } |
| 28 | + |
| 29 | + /// Token-based authentication method. |
| 30 | + /// |
| 31 | + /// This authentication method is bound to a single connection since APNs will reject connections |
| 32 | + /// that use tokens signed by different keys. |
| 33 | + /// |
| 34 | + /// - Parameters: |
| 35 | + /// - privateKey: The private encryption key obtained through the developer portal. |
| 36 | + /// - keyIdentifier: The private encryption key identifier obtained through the developer portal. |
| 37 | + /// - teamIdentifier: The team id. |
| 38 | + public static func jwt( |
| 39 | + privateKey: P256.Signing.PrivateKey, keyIdentifier: String, |
| 40 | + teamIdentifier: String |
| 41 | + ) -> Self { |
| 42 | + Self(method: .jwt(privateKey: privateKey, teamIdentifier: teamIdentifier, keyIdentifier: keyIdentifier)) |
| 43 | + } |
| 44 | + |
| 45 | + /// Certificate based authentication method. |
| 46 | + /// |
| 47 | + /// - Parameters: |
| 48 | + /// - privateKey: The private key associated with the leaf certificate. |
| 49 | + /// - certificateChain: The certificates to offer during negotiation. If not present, no certificates will be offered. |
| 50 | + public static func tls( |
| 51 | + privateKey: NIOSSLPrivateKeySource, |
| 52 | + certificateChain: [NIOSSLCertificateSource] |
| 53 | + ) -> Self { |
| 54 | + Self(method: .tls(privateKey: privateKey, certificateChain: certificateChain)) |
| 55 | + } |
| 56 | + |
| 57 | + internal var method: Method |
| 58 | + } |
| 59 | + |
| 60 | + /// The APNs environment. |
| 61 | + public struct Environment { |
| 62 | + /// The production APNs environment. |
| 63 | + public static let production = Self(url: "https://api.push.apple.com") |
| 64 | + |
| 65 | + /// The sandbox APNs environment. |
| 66 | + public static let sandbox = Self(url: "https://api.development.push.apple.com") |
| 67 | + |
| 68 | + /// Creates an APNs environment with a custom URL. |
| 69 | + /// |
| 70 | + /// - Note: This is mostly used for testing purposes. |
| 71 | + public static func custom(url: String) -> Self { |
| 72 | + Self(url: url) |
| 73 | + } |
| 74 | + |
| 75 | + /// The environment's URL. |
| 76 | + let url: String |
| 77 | + } |
| 78 | + |
| 79 | + /// The authentication method used by the ``APNSClient``. |
| 80 | + public var authenticationMethod: AuthenticationMethod |
| 81 | + |
| 82 | + /// The environment used by the ``APNSClient``. |
| 83 | + public var environment: Environment |
| 84 | + |
| 85 | + /// Upstream proxy, defaults to no proxy. |
| 86 | + public var proxy: HTTPClient.Configuration.Proxy? |
| 87 | + |
| 88 | + /// Initializes a new ``APNSClient.Configuration``. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - authenticationMethod: The authentication method used by the ``APNSClient``. |
| 92 | + /// - environment: The environment used by the ``APNSClient``. |
| 93 | + public init( |
| 94 | + authenticationMethod: AuthenticationMethod, |
| 95 | + environment: Environment |
| 96 | + ) { |
| 97 | + self.authenticationMethod = authenticationMethod |
| 98 | + self.environment = environment |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments