fixed #90 Support load p12 format of certificate file.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-06-12 15:54:17 +08:00
parent 27445087b4
commit 146854e381
2 changed files with 17 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
apns "github.com/sideshow/apns2" apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload" "github.com/sideshow/apns2/payload"
"path/filepath"
"time" "time"
) )
@ -140,8 +141,20 @@ func CheckPushConf() error {
func InitAPNSClient() error { func InitAPNSClient() error {
if PushConf.Ios.Enabled { if PushConf.Ios.Enabled {
var err error 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 { if err != nil {
LogError.Error("Cert Error:", err.Error()) LogError.Error("Cert Error:", err.Error())

View File

@ -427,7 +427,7 @@ func TestDisabledIosNotifications(t *testing.T) {
assert.Equal(t, 2, count) assert.Equal(t, 2, count)
} }
func TestMissingIosCertificate(t *testing.T) { func TestWrongIosCertificateExt(t *testing.T) {
PushConf = config.BuildDefaultPushConf() PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true PushConf.Ios.Enabled = true
@ -435,13 +435,14 @@ func TestMissingIosCertificate(t *testing.T) {
err := InitAPNSClient() err := InitAPNSClient()
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "Wrong Certificate key extension.", err.Error())
} }
func TestAPNSClientDevHost(t *testing.T) { func TestAPNSClientDevHost(t *testing.T) {
PushConf = config.BuildDefaultPushConf() PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true PushConf.Ios.Enabled = true
PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" PushConf.Ios.PemPath = "../certificate/certificate-valid.p12"
InitAPNSClient() InitAPNSClient()
assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host) assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)