Skip to content

Commit d89a59d

Browse files
committed
Improved APNS server certificate logic
Now you can use the old style Development or Production certificates, but it will throw an exception if you pick the wrong server environment for either.
1 parent 58de954 commit d89a59d

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

PushSharp.Apple/ApnsConfiguration.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public ApnsConfiguration (ApnsServerEnvironment serverEnvironment, X509Certifica
4848

4949
void Initialize (ApnsServerEnvironment serverEnvironment, X509Certificate2 certificate)
5050
{
51+
ServerEnvironment = serverEnvironment;
52+
5153
var production = serverEnvironment == ApnsServerEnvironment.Production;
5254

5355
Host = production ? APNS_PRODUCTION_HOST : APNS_SANDBOX_HOST;
@@ -89,10 +91,16 @@ void CheckIsApnsCertificate ()
8991

9092
if (!issuerName.Contains ("Apple"))
9193
throw new ApnsConnectionException ("Your Certificate does not appear to be issued by Apple! Please check to ensure you have the correct certificate!");
92-
if (!commonName.Contains ("Apple Push Services:")
94+
95+
if (!Regex.IsMatch (commonName, "Apple.*?Push Services")
9396
&& !commonName.Contains ("Website Push ID:"))
94-
throw new ApnsConnectionException ("Your Certificate is not in the new combined Sandbox/Production APNS certificate format, please create a new single certificate to use");
97+
throw new ApnsConnectionException ("Your Certificate is not a valid certificate for connecting to Apple's APNS servers");
98+
99+
if (commonName.Contains ("Development") && ServerEnvironment != ApnsServerEnvironment.Sandbox)
100+
throw new ApnsConnectionException ("You are using a certificate created for connecting only to the Sandbox APNS server but have selected a different server environment to connect to.");
95101

102+
if (commonName.Contains ("Production") && ServerEnvironment != ApnsServerEnvironment.Production)
103+
throw new ApnsConnectionException ("You are using a certificate created for connecting only to the Production APNS server but have selected a different server environment to connect to.");
96104
} else {
97105
throw new ApnsConnectionException ("You must provide a Certificate to connect to APNS with!");
98106
}
@@ -168,6 +176,12 @@ public void OverrideFeedbackServer (string host, int port)
168176
/// </summary>
169177
public TimeSpan KeepAliveRetryPeriod { get; set; }
170178

179+
/// <summary>
180+
/// Gets the configured APNS server environment
181+
/// </summary>
182+
/// <value>The server environment.</value>
183+
public ApnsServerEnvironment ServerEnvironment { get; private set; }
184+
171185
public enum ApnsServerEnvironment {
172186
Sandbox,
173187
Production

0 commit comments

Comments
 (0)