Skip to content

Commit

Permalink
Merge pull request #15 from axiomzen/kan/#13-support-p8
Browse files Browse the repository at this point in the history
Kan/#13 support p8
  • Loading branch information
Kay-Zee authored Sep 29, 2017
2 parents 07c424e + d69bdbe commit 147e760
Show file tree
Hide file tree
Showing 26 changed files with 1,797 additions and 17 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type SectionIos struct {
Password string `yaml:"password"`
Production bool `yaml:"production"`
MaxRetry int `yaml:"max_retry"`
P8Key string `yaml:"p8_key"`
KeyID string `yaml:"key_id"`
TeamID string `yaml:"team_id"`
}

// SectionLog is sub section of config.
Expand Down Expand Up @@ -168,6 +171,9 @@ func BuildDefaultPushConf() ConfYaml {
conf.Ios.Password = ""
conf.Ios.Production = false
conf.Ios.MaxRetry = 0
conf.Ios.P8Key = ""
conf.Ios.KeyID = "ABC123DEFG"
conf.Ios.TeamID = "DEF123GHIJ"

// log
conf.Log.Format = "string"
Expand Down
3 changes: 3 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ ios:
password: "" # certificate password, default as empty string.
production: false
max_retry: 0 # resend fail notification, default value zero is disabled
p8_key: "" # empty string here means ignore
key_id: "ABC123DEFG"
team_id: "DEF123GHIJ"

log:
format: "string" # string or json
Expand Down
3 changes: 3 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.Password)
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Production)
assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Ios.MaxRetry)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.P8Key)
assert.Equal(suite.T(), "ABC123DEFG", suite.ConfGorushDefault.Ios.KeyID)
assert.Equal(suite.T(), "DEF123GHIJ", suite.ConfGorushDefault.Ios.TeamID)

// log
assert.Equal(suite.T(), "string", suite.ConfGorushDefault.Log.Format)
Expand Down
35 changes: 35 additions & 0 deletions gorush/notification_apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ package gorush

import (
"errors"
"fmt"
"path/filepath"
"time"

apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload"
"github.com/sideshow/apns2/token"
)

// InitAPNSClient use for initialize APNs Client.
func InitAPNSClient() error {
if PushConf.Ios.Enabled {

if PushConf.Ios.P8Key != "" {
p8key := fmt.Sprintf("-----BEGIN PRIVATE KEY-----\n%s\n-----END PRIVATE KEY-----", PushConf.Ios.P8Key)
authKey, err := token.AuthKeyFromBytes([]byte(p8key))
if err != nil {
LogError.Error("P8 Error:", err.Error())
return err
}
token := &token.Token{
AuthKey: authKey,
// KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
KeyID: PushConf.Ios.KeyID,
// TeamID from developer account (View Account -> Membership)
TeamID: PushConf.Ios.TeamID,
}
ApnsClient = apns.NewTokenClient(token)
return nil
}
var err error
ext := filepath.Ext(PushConf.Ios.KeyPath)

Expand All @@ -21,6 +41,21 @@ func InitAPNSClient() error {
CertificatePemIos, err = certificate.FromP12File(PushConf.Ios.KeyPath, PushConf.Ios.Password)
case ".pem":
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.KeyPath, PushConf.Ios.Password)
case ".p8":
authKey, err := token.AuthKeyFromFile(PushConf.Ios.KeyPath)
if err != nil {
LogError.Error("P8 Error:", err.Error())
return err
}
token := &token.Token{
AuthKey: authKey,
// KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
KeyID: PushConf.Ios.KeyID,
// TeamID from developer account (View Account -> Membership)
TeamID: PushConf.Ios.TeamID,
}
ApnsClient = apns.NewTokenClient(token)
return nil
default:
err = errors.New("wrong certificate key extension")
}
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 147e760

Please sign in to comment.