From 146854e381601c53325241ff11fb8b2af017be0b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 12 Jun 2016 15:54:17 +0800 Subject: [PATCH] fixed #90 Support load p12 format of certificate file. Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 15 ++++++++++++++- gorush/notification_test.go | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index ef9a463..eb7d12e 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -7,6 +7,7 @@ import ( apns "github.com/sideshow/apns2" "github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/payload" + "path/filepath" "time" ) @@ -140,8 +141,20 @@ func CheckPushConf() error { func InitAPNSClient() error { if PushConf.Ios.Enabled { var err error + ext := filepath.Ext(PushConf.Ios.PemPath) - CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) + LogAccess.Debug("certificate ext is ", ext) + + switch ext { + case ".p12": + CertificatePemIos, err = certificate.FromP12File(PushConf.Ios.PemPath, PushConf.Ios.Password) + case ".pem": + CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) + default: + err = errors.New("Wrong Certificate key extension.") + } + + // CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) if err != nil { LogError.Error("Cert Error:", err.Error()) diff --git a/gorush/notification_test.go b/gorush/notification_test.go index e6cb466..176dd6d 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -427,7 +427,7 @@ func TestDisabledIosNotifications(t *testing.T) { assert.Equal(t, 2, count) } -func TestMissingIosCertificate(t *testing.T) { +func TestWrongIosCertificateExt(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true @@ -435,13 +435,14 @@ func TestMissingIosCertificate(t *testing.T) { err := InitAPNSClient() assert.Error(t, err) + assert.Equal(t, "Wrong Certificate key extension.", err.Error()) } func TestAPNSClientDevHost(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.PemPath = "../certificate/certificate-valid.p12" InitAPNSClient() assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)