feat(iOS): load iOS key from base64 input. (#336)

* feat(iOS): load iOS key from base64 input.

* test: check key type exist.
This commit is contained in:
Bo-Yi Wu 2018-02-18 17:12:51 +08:00 committed by GitHub
parent c3167f77f3
commit 2d2f62d19b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 129 additions and 20 deletions

View File

@ -83,6 +83,8 @@ core:
ssl: false ssl: false
cert_path: "cert.pem" cert_path: "cert.pem"
key_path: "key.pem" key_path: "key.pem"
cert_base64: ""
key_base64: ""
http_proxy: "" # only working for FCM server http_proxy: "" # only working for FCM server
pid: pid:
enabled: false enabled: false
@ -114,6 +116,8 @@ android:
ios: ios:
enabled: false enabled: false
key_path: "key.pem" key_path: "key.pem"
key_base64: "" # load iOS key from base64 input
key_type: "pem" # could be pem, p12 or p8 type
password: "" # certificate password, default as empty string. password: "" # certificate password, default as empty string.
production: false production: false
max_retry: 0 # resend fail notification, default value zero is disabled max_retry: 0 # resend fail notification, default value zero is disabled

View File

@ -23,6 +23,8 @@ core:
ssl: false ssl: false
cert_path: "cert.pem" cert_path: "cert.pem"
key_path: "key.pem" key_path: "key.pem"
cert_base64: ""
key_base64: ""
http_proxy: "" # only working for FCM server http_proxy: "" # only working for FCM server
pid: pid:
enabled: false enabled: false
@ -54,6 +56,8 @@ android:
ios: ios:
enabled: false enabled: false
key_path: "key.pem" key_path: "key.pem"
key_base64: "" # load iOS key from base64 input
key_type: "pem" # could be pem, p12 or p8 type
password: "" # certificate password, default as empty string. password: "" # certificate password, default as empty string.
production: false production: false
max_retry: 0 # resend fail notification, default value zero is disabled max_retry: 0 # resend fail notification, default value zero is disabled
@ -107,6 +111,8 @@ type SectionCore struct {
SSL bool `yaml:"ssl"` SSL bool `yaml:"ssl"`
CertPath string `yaml:"cert_path"` CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"` KeyPath string `yaml:"key_path"`
CertBase64 string `yaml:"cert_base64"`
KeyBase64 string `yaml:"key_base64"`
HTTPProxy string `yaml:"http_proxy"` HTTPProxy string `yaml:"http_proxy"`
PID SectionPID `yaml:"pid"` PID SectionPID `yaml:"pid"`
AutoTLS SectionAutoTLS `yaml:"auto_tls"` AutoTLS SectionAutoTLS `yaml:"auto_tls"`
@ -141,6 +147,8 @@ type SectionAndroid struct {
type SectionIos struct { type SectionIos struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
KeyPath string `yaml:"key_path"` KeyPath string `yaml:"key_path"`
KeyBase64 string `yaml:"key_base64"`
KeyType string `yaml:"key_type"`
Password string `yaml:"password"` Password string `yaml:"password"`
Production bool `yaml:"production"` Production bool `yaml:"production"`
MaxRetry int `yaml:"max_retry"` MaxRetry int `yaml:"max_retry"`
@ -247,6 +255,8 @@ func LoadConf(confPath string) (ConfYaml, error) {
conf.Core.SSL = viper.GetBool("core.ssl") conf.Core.SSL = viper.GetBool("core.ssl")
conf.Core.CertPath = viper.GetString("core.cert_path") conf.Core.CertPath = viper.GetString("core.cert_path")
conf.Core.KeyPath = viper.GetString("core.key_path") conf.Core.KeyPath = viper.GetString("core.key_path")
conf.Core.CertBase64 = viper.GetString("core.cert_base64")
conf.Core.KeyBase64 = viper.GetString("core.key_base64")
conf.Core.MaxNotification = int64(viper.GetInt("core.max_notification")) conf.Core.MaxNotification = int64(viper.GetInt("core.max_notification"))
conf.Core.HTTPProxy = viper.GetString("core.http_proxy") conf.Core.HTTPProxy = viper.GetString("core.http_proxy")
conf.Core.PID.Enabled = viper.GetBool("core.pid.enabled") conf.Core.PID.Enabled = viper.GetBool("core.pid.enabled")
@ -273,6 +283,8 @@ func LoadConf(confPath string) (ConfYaml, error) {
// iOS // iOS
conf.Ios.Enabled = viper.GetBool("ios.enabled") conf.Ios.Enabled = viper.GetBool("ios.enabled")
conf.Ios.KeyPath = viper.GetString("ios.key_path") conf.Ios.KeyPath = viper.GetString("ios.key_path")
conf.Ios.KeyBase64 = viper.GetString("ios.key_base64")
conf.Ios.KeyType = viper.GetString("ios.key_type")
conf.Ios.Password = viper.GetString("ios.password") conf.Ios.Password = viper.GetString("ios.password")
conf.Ios.Production = viper.GetBool("ios.production") conf.Ios.Production = viper.GetBool("ios.production")
conf.Ios.MaxRetry = viper.GetInt("ios.max_retry") conf.Ios.MaxRetry = viper.GetInt("ios.max_retry")

View File

@ -10,6 +10,8 @@ core:
ssl: false ssl: false
cert_path: "cert.pem" cert_path: "cert.pem"
key_path: "key.pem" key_path: "key.pem"
cert_base64: ""
key_base64: ""
http_proxy: "" # only working for FCM server http_proxy: "" # only working for FCM server
pid: pid:
enabled: false enabled: false
@ -41,6 +43,8 @@ android:
ios: ios:
enabled: false enabled: false
key_path: "key.pem" key_path: "key.pem"
key_base64: "" # load iOS key from base64 input
key_type: "pem" # could be pem, p12 or p8 type
password: "" # certificate password, default as empty string. password: "" # certificate password, default as empty string.
production: false production: false
max_retry: 0 # resend fail notification, default value zero is disabled max_retry: 0 # resend fail notification, default value zero is disabled

View File

@ -47,6 +47,8 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.SSL) assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.SSL)
assert.Equal(suite.T(), "cert.pem", suite.ConfGorushDefault.Core.CertPath) assert.Equal(suite.T(), "cert.pem", suite.ConfGorushDefault.Core.CertPath)
assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Core.KeyPath) assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Core.KeyPath)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.KeyBase64)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.CertBase64)
assert.Equal(suite.T(), int64(100), suite.ConfGorushDefault.Core.MaxNotification) assert.Equal(suite.T(), int64(100), suite.ConfGorushDefault.Core.MaxNotification)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.HTTPProxy) assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.HTTPProxy)
// Pid // Pid
@ -74,6 +76,8 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
// iOS // iOS
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Enabled) assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Enabled)
assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Ios.KeyPath) assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Ios.KeyPath)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.KeyBase64)
assert.Equal(suite.T(), "pem", suite.ConfGorushDefault.Ios.KeyType)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.Password) assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.Password)
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Production) assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Production)
assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Ios.MaxRetry) assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Ios.MaxRetry)
@ -115,6 +119,8 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), false, suite.ConfGorush.Core.SSL) assert.Equal(suite.T(), false, suite.ConfGorush.Core.SSL)
assert.Equal(suite.T(), "cert.pem", suite.ConfGorush.Core.CertPath) assert.Equal(suite.T(), "cert.pem", suite.ConfGorush.Core.CertPath)
assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Core.KeyPath) assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Core.KeyPath)
assert.Equal(suite.T(), "", suite.ConfGorush.Core.CertBase64)
assert.Equal(suite.T(), "", suite.ConfGorush.Core.KeyBase64)
assert.Equal(suite.T(), int64(100), suite.ConfGorush.Core.MaxNotification) assert.Equal(suite.T(), int64(100), suite.ConfGorush.Core.MaxNotification)
assert.Equal(suite.T(), "", suite.ConfGorush.Core.HTTPProxy) assert.Equal(suite.T(), "", suite.ConfGorush.Core.HTTPProxy)
// Pid // Pid
@ -142,6 +148,8 @@ func (suite *ConfigTestSuite) TestValidateConf() {
// iOS // iOS
assert.Equal(suite.T(), false, suite.ConfGorush.Ios.Enabled) assert.Equal(suite.T(), false, suite.ConfGorush.Ios.Enabled)
assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Ios.KeyPath) assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Ios.KeyPath)
assert.Equal(suite.T(), "", suite.ConfGorush.Ios.KeyBase64)
assert.Equal(suite.T(), "pem", suite.ConfGorush.Ios.KeyType)
assert.Equal(suite.T(), "", suite.ConfGorush.Ios.Password) assert.Equal(suite.T(), "", suite.ConfGorush.Ios.Password)
assert.Equal(suite.T(), false, suite.ConfGorush.Ios.Production) assert.Equal(suite.T(), false, suite.ConfGorush.Ios.Production)
assert.Equal(suite.T(), 0, suite.ConfGorush.Ios.MaxRetry) assert.Equal(suite.T(), 0, suite.ConfGorush.Ios.MaxRetry)

View File

@ -172,13 +172,15 @@ func CheckPushConf() error {
} }
if PushConf.Ios.Enabled { if PushConf.Ios.Enabled {
if PushConf.Ios.KeyPath == "" { if PushConf.Ios.KeyPath == "" && PushConf.Ios.KeyBase64 == "" {
return errors.New("Missing iOS certificate path") return errors.New("Missing iOS certificate key")
} }
// check certificate file exist // check certificate file exist
if _, err := os.Stat(PushConf.Ios.KeyPath); os.IsNotExist(err) { if PushConf.Ios.KeyPath != "" {
return errors.New("certificate file does not exist") if _, err := os.Stat(PushConf.Ios.KeyPath); os.IsNotExist(err) {
return errors.New("certificate file does not exist")
}
} }
} }

View File

@ -3,6 +3,7 @@ package gorush
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/tls" "crypto/tls"
"encoding/base64"
"errors" "errors"
"path/filepath" "path/filepath"
"time" "time"
@ -19,23 +20,51 @@ func InitAPNSClient() error {
var err error var err error
var authKey *ecdsa.PrivateKey var authKey *ecdsa.PrivateKey
var certificateKey tls.Certificate var certificateKey tls.Certificate
ext := filepath.Ext(PushConf.Ios.KeyPath) var ext string
switch ext { if PushConf.Ios.KeyPath != "" {
case ".p12": ext = filepath.Ext(PushConf.Ios.KeyPath)
certificateKey, err = certificate.FromP12File(PushConf.Ios.KeyPath, PushConf.Ios.Password)
case ".pem":
certificateKey, err = certificate.FromPemFile(PushConf.Ios.KeyPath, PushConf.Ios.Password)
case ".p8":
authKey, err = token.AuthKeyFromFile(PushConf.Ios.KeyPath)
default:
err = errors.New("wrong certificate key extension")
}
if err != nil { switch ext {
LogError.Error("Cert Error:", err.Error()) case ".p12":
certificateKey, err = certificate.FromP12File(PushConf.Ios.KeyPath, PushConf.Ios.Password)
case ".pem":
certificateKey, err = certificate.FromPemFile(PushConf.Ios.KeyPath, PushConf.Ios.Password)
case ".p8":
authKey, err = token.AuthKeyFromFile(PushConf.Ios.KeyPath)
default:
err = errors.New("wrong certificate key extension")
}
return err if err != nil {
LogError.Error("Cert Error:", err.Error())
return err
}
} else if PushConf.Ios.KeyBase64 != "" {
ext = "." + PushConf.Ios.KeyType
key, err := base64.StdEncoding.DecodeString(PushConf.Ios.KeyBase64)
if err != nil {
LogError.Error("base64 decode error:", err.Error())
return err
}
switch ext {
case ".p12":
certificateKey, err = certificate.FromP12Bytes(key, PushConf.Ios.Password)
case ".pem":
certificateKey, err = certificate.FromPemBytes(key, PushConf.Ios.Password)
case ".p8":
authKey, err = token.AuthKeyFromBytes(key)
default:
err = errors.New("wrong certificate key type")
}
if err != nil {
LogError.Error("Cert Error:", err.Error())
return err
}
} }
if ext == ".p8" && PushConf.Ios.KeyID != "" && PushConf.Ios.TeamID != "" { if ext == ".p8" && PushConf.Ios.KeyID != "" && PushConf.Ios.TeamID != "" {

View File

@ -13,6 +13,14 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const certificateValidP12 = `MIIKlgIBAzCCClwGCSqGSIb3DQEHAaCCCk0EggpJMIIKRTCCBMcGCSqGSIb3DQEHBqCCBLgwggS0AgEAMIIErQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQID/GJtcRhjvwCAggAgIIEgE5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emZQblpQeGZBeXhxRQpaVjA1bGFBb0pBbCsvNlh0Mk80bU9CNjExc09oUkFOQ0FBU2dGVEtqd0pBQVU5NWcrKy92ektXSGt6QVZtTk1JCnRCNXZUalpPT0l3bkViNzBNc1daRkl5VUZEMVA5R3dzdHo0K2FrSFg3dkk4Qkg2aEhtQm1mWlpaCg==`
const authkeyValidP8 = `LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR0hBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJHMHdhd0lCQVFRZ0ViVnpmUG5aUHhmQXl4cUUKWlYwNWxhQW9KQWwrLzZYdDJPNG1PQjYxMXNPaFJBTkNBQVNnRlRLandKQUFVOTVnKysvdnpLV0hrekFWbU5NSQp0QjV2VGpaT09Jd25FYjcwTXNXWkZJeVVGRDFQOUd3c3R6NCtha0hYN3ZJOEJINmhIbUJtZmVRbAotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg==`
func TestDisabledAndroidIosConf(t *testing.T) { func TestDisabledAndroidIosConf(t *testing.T) {
PushConf, _ = config.LoadConf("") PushConf, _ = config.LoadConf("")
PushConf.Android.Enabled = false PushConf.Android.Enabled = false
@ -28,17 +36,17 @@ func TestMissingIOSCertificate(t *testing.T) {
PushConf.Ios.Enabled = true PushConf.Ios.Enabled = true
PushConf.Ios.KeyPath = "" PushConf.Ios.KeyPath = ""
PushConf.Ios.KeyBase64 = ""
err := CheckPushConf() err := CheckPushConf()
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "Missing iOS certificate path", err.Error()) assert.Equal(t, "Missing iOS certificate key", err.Error())
PushConf.Ios.KeyPath = "test.pem" PushConf.Ios.KeyPath = "test.pem"
err = CheckPushConf() err = CheckPushConf()
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "certificate file does not exist", err.Error()) assert.Equal(t, "certificate file does not exist", err.Error())
} }
func TestIOSNotificationStructure(t *testing.T) { func TestIOSNotificationStructure(t *testing.T) {
var dat map[string]interface{} var dat map[string]interface{}
@ -396,6 +404,14 @@ func TestWrongIosCertificateExt(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "wrong certificate key extension", err.Error()) assert.Equal(t, "wrong certificate key extension", err.Error())
PushConf.Ios.KeyPath = ""
PushConf.Ios.KeyBase64 = "abcd"
PushConf.Ios.KeyType = "abcd"
err = InitAPNSClient()
assert.Error(t, err)
assert.Equal(t, "wrong certificate key type", err.Error())
} }
func TestAPNSClientDevHost(t *testing.T) { func TestAPNSClientDevHost(t *testing.T) {
@ -406,6 +422,13 @@ func TestAPNSClientDevHost(t *testing.T) {
err := InitAPNSClient() err := InitAPNSClient()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host) assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)
PushConf.Ios.KeyPath = ""
PushConf.Ios.KeyBase64 = certificateValidP12
PushConf.Ios.KeyType = "p12"
err = InitAPNSClient()
assert.Nil(t, err)
assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)
} }
func TestAPNSClientProdHost(t *testing.T) { func TestAPNSClientProdHost(t *testing.T) {
@ -417,6 +440,13 @@ func TestAPNSClientProdHost(t *testing.T) {
err := InitAPNSClient() err := InitAPNSClient()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, apns2.HostProduction, ApnsClient.Host) assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
PushConf.Ios.KeyPath = ""
PushConf.Ios.KeyBase64 = certificateValidPEM
PushConf.Ios.KeyType = "pem"
err = InitAPNSClient()
assert.Nil(t, err)
assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
} }
func TestAPNSClientInvaildToken(t *testing.T) { func TestAPNSClientInvaildToken(t *testing.T) {
@ -426,6 +456,12 @@ func TestAPNSClientInvaildToken(t *testing.T) {
PushConf.Ios.KeyPath = "../certificate/authkey-invalid.p8" PushConf.Ios.KeyPath = "../certificate/authkey-invalid.p8"
err := InitAPNSClient() err := InitAPNSClient()
assert.Error(t, err) assert.Error(t, err)
PushConf.Ios.KeyPath = ""
PushConf.Ios.KeyBase64 = authkeyInvalidP8
PushConf.Ios.KeyType = "p8"
err = InitAPNSClient()
assert.Error(t, err)
} }
func TestAPNSClientVaildToken(t *testing.T) { func TestAPNSClientVaildToken(t *testing.T) {
@ -441,6 +477,20 @@ func TestAPNSClientVaildToken(t *testing.T) {
err = InitAPNSClient() err = InitAPNSClient()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, apns2.HostProduction, ApnsClient.Host) assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
// test base64
PushConf.Ios.Production = false
PushConf.Ios.KeyPath = ""
PushConf.Ios.KeyBase64 = authkeyValidP8
PushConf.Ios.KeyType = "p8"
err = InitAPNSClient()
assert.NoError(t, err)
assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)
PushConf.Ios.Production = true
err = InitAPNSClient()
assert.NoError(t, err)
assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
} }
func TestPushToIOS(t *testing.T) { func TestPushToIOS(t *testing.T) {