Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Apple APNS AuthKey #101

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions config/mattermost-push-proxy.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
"ApplePushUseDevelopment":false,
"ApplePushCertPrivate":"",
"ApplePushCertPassword":"",
"ApplePushTopic":"com.mattermost.Mattermost"
"ApplePushTopic":"com.mattermost.Mattermost",
"AppleAuthKeyFile": "",
"AppleAuthKeyID": "",
"AppleTeamID": ""
},
{
"Type":"apple_rn",
"ApplePushUseDevelopment":false,
"ApplePushCertPrivate":"",
"ApplePushCertPassword":"",
"ApplePushTopic":"com.mattermost.react.native"
"ApplePushTopic":"com.mattermost.react.native",
"AppleAuthKeyFile": "",
"AppleAuthKeyID": "",
"AppleTeamID": ""
}
],
"AndroidPushSettings":[
Expand Down
90 changes: 62 additions & 28 deletions server/apple_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload"
"github.com/sideshow/apns2/token"
"golang.org/x/net/http2"
)

Expand All @@ -31,13 +32,68 @@ func NewAppleNotificationServer(settings ApplePushSettings, logger *Logger, metr
}
}

func (me *AppleNotificationServer) setupProxySettings(appleCert *tls.Certificate) bool {
// Override the native transport.
proxyServer := getProxyServer()
if proxyServer != "" {
transport := &http.Transport{
Proxy: func(request *http.Request) (*url.URL, error) {
return url.Parse(proxyServer)
},
IdleConnTimeout: apns.HTTPClientTimeout,
}

if appleCert != nil {
transport.TLSClientConfig = &tls.Config{
Certificates: []tls.Certificate{*appleCert},
}
}

err := http2.ConfigureTransport(transport)
if err != nil {
me.logger.Errorf("Transport Error: %v", err)
return false
}

me.AppleClient.HTTPClient.Transport = transport
}

if appleCert != nil {
me.logger.Infof("Initializing apple notification server for type=%v with PEM certificate", me.ApplePushSettings.Type)
} else {
me.logger.Infof("Initializing apple notification server for type=%v with AuthKey", me.ApplePushSettings.Type)
}

return true
}

func (me *AppleNotificationServer) Initialize() bool {
me.logger.Infof("Initializing apple notification server for type=%v", me.ApplePushSettings.Type)
if me.ApplePushSettings.AppleAuthKeyFile != "" && me.ApplePushSettings.AppleAuthKeyID != "" && me.ApplePushSettings.AppleTeamID != "" {
authKey, err := token.AuthKeyFromFile(me.ApplePushSettings.AppleAuthKeyFile)
if err != nil {
me.logger.Panicf("Failed to initialize apple notification service with AuthKey file err=%v ", err)
}

appleToken := &token.Token{
AuthKey: authKey,
KeyID: me.ApplePushSettings.AppleAuthKeyID,
TeamID: me.ApplePushSettings.AppleTeamID,
}

if me.ApplePushSettings.ApplePushUseDevelopment {
me.AppleClient = apns.NewTokenClient(appleToken).Development()
} else {
me.AppleClient = apns.NewTokenClient(appleToken).Production()
}

// Override the native transport.
return me.setupProxySettings(nil)
}

if me.ApplePushSettings.ApplePushCertPrivate != "" {
appleCert, appleCertErr := certificate.FromPemFile(me.ApplePushSettings.ApplePushCertPrivate, me.ApplePushSettings.ApplePushCertPassword)
if appleCertErr != nil {
me.logger.Panicf("Failed to load the apple pem cert err=%v for type=%v", appleCertErr, me.ApplePushSettings.Type)
me.logger.Panicf("Failed to initialize apple notification service with pem cert err=%v for type=%v", appleCertErr, me.ApplePushSettings.Type)
return false
}

Expand All @@ -48,33 +104,11 @@ func (me *AppleNotificationServer) Initialize() bool {
}

// Override the native transport.
proxyServer := getProxyServer()
if proxyServer != "" {
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{appleCert},
}

transport := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: func(request *http.Request) (*url.URL, error) {
return url.Parse(proxyServer)
},
IdleConnTimeout: apns.HTTPClientTimeout,
}
err := http2.ConfigureTransport(transport)
if err != nil {
me.logger.Errorf("Transport Error: %v", err)
return false
}

me.AppleClient.HTTPClient.Transport = transport
}

return true
} else {
me.logger.Errorf("Apple push notifications not configured. Missing ApplePushCertPrivate. for type=%v", me.ApplePushSettings.Type)
return false
return me.setupProxySettings(&appleCert)
}

me.logger.Errorf("Apple push notifications not configured. Missing ApplePushCertPrivate. for type=%v", me.ApplePushSettings.Type)
return false
}

func (me *AppleNotificationServer) SendNotification(msg *PushNotification) PushResponse {
Expand Down
3 changes: 3 additions & 0 deletions server/config_push_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type ApplePushSettings struct {
ApplePushCertPrivate string
ApplePushCertPassword string
ApplePushTopic string
AppleAuthKeyFile string
AppleAuthKeyID string
AppleTeamID string
ApplePushUseDevelopment bool
}

Expand Down