Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
don't extract topic automatically (#51)
Browse files Browse the repository at this point in the history
ref #50
  • Loading branch information
nathany committed Jun 9, 2016
1 parent dc4636b commit 1786a96
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
15 changes: 15 additions & 0 deletions certificate/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"strings"

"golang.org/x/crypto/pkcs12"
)
Expand Down Expand Up @@ -45,6 +46,20 @@ func Decode(p12 []byte, password string) (tls.Certificate, error) {
}, nil
}

// TopicFromCert extracts topic from a certificate's common name.
func TopicFromCert(cert tls.Certificate) string {
commonName := cert.Leaf.Subject.CommonName

var topic string
// Apple Push Services: {bundle}
// Apple Development IOS Push Services: {bundle}
n := strings.Index(commonName, ":")
if n != -1 {
topic = strings.TrimSpace(commonName[n+1:])
}
return topic
}

// verify checks if a certificate has expired
func verify(cert *x509.Certificate) error {
_, err := cert.Verify(x509.VerifyOptions{})
Expand Down
16 changes: 16 additions & 0 deletions certificate/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@ func TestMissingFile(t *testing.T) {
t.Fatal("Expected file not found, got", err)
}
}

func TestTopicFromCert(t *testing.T) {
const name = "../testdata/cert.p12"

cert, err := certificate.Load(name, "")
if err != nil {
t.Fatal(err)
}

// TODO: need a test cert with a CommonName
const expected = ""
actual := certificate.TopicFromCert(cert)
if actual != expected {
t.Errorf("Expected topic %q, got %q.", expected, actual)
}
}
8 changes: 8 additions & 0 deletions push/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Headers struct {
// Allow Apple to group messages together to reduce power consumption.
// By default messages are sent immediately.
LowPriority bool

// Topic for certificates with multiple topics.
Topic string
}

// set headers for an HTTP request
Expand All @@ -39,4 +42,9 @@ func (h *Headers) set(reqHeader http.Header) {
if h.LowPriority {
reqHeader.Set("apns-priority", "5")
} // when omitted, the default priority is 10

if h.Topic != "" {
reqHeader.Set("apns-topic", h.Topic)
}

}
4 changes: 4 additions & 0 deletions push/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestHeaders(t *testing.T) {
ID: "uuid",
Expiration: time.Unix(12622780800, 0),
LowPriority: true,
Topic: "bundle-id",
}

reqHeader := http.Header{}
Expand All @@ -19,6 +20,7 @@ func TestHeaders(t *testing.T) {
testHeader(t, reqHeader, "apns-id", "uuid")
testHeader(t, reqHeader, "apns-expiration", "12622780800")
testHeader(t, reqHeader, "apns-priority", "5")
testHeader(t, reqHeader, "apns-topic", "bundle-id")
}

func TestNilHeader(t *testing.T) {
Expand All @@ -29,6 +31,7 @@ func TestNilHeader(t *testing.T) {
testHeader(t, reqHeader, "apns-id", "")
testHeader(t, reqHeader, "apns-expiration", "")
testHeader(t, reqHeader, "apns-priority", "")
testHeader(t, reqHeader, "apns-topic", "")
}

func TestEmptyHeaders(t *testing.T) {
Expand All @@ -39,6 +42,7 @@ func TestEmptyHeaders(t *testing.T) {
testHeader(t, reqHeader, "apns-id", "")
testHeader(t, reqHeader, "apns-expiration", "")
testHeader(t, reqHeader, "apns-priority", "")
testHeader(t, reqHeader, "apns-topic", "")
}

func testHeader(t *testing.T, reqHeader http.Header, key, expected string) {
Expand Down
24 changes: 3 additions & 21 deletions push/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"golang.org/x/net/http2"
Expand All @@ -24,12 +23,11 @@ const (
type Service struct {
Host string
Client *http.Client
Topic string
}

// NewService sets up an HTTP/2 client for a certificate and extracts the
// Topic from the certificate. If you need to do something custom, you can
// always override the fields in Service, e.g. to specify your own http.Client.
// NewService sets up an HTTP/2 client for a certificate. If you need to do
// something custom, you can always override the fields in Service,
// e.g. to specify your own http.Client.
func NewService(host string, cert tls.Certificate) (*Service, error) {
client, err := newClient(cert)
if err != nil {
Expand All @@ -39,7 +37,6 @@ func NewService(host string, cert tls.Certificate) (*Service, error) {
return &Service{
Client: client,
Host: host,
Topic: topicFromCert(cert.Leaf.Subject.CommonName),
}, nil
}

Expand All @@ -58,18 +55,6 @@ func newClient(cert tls.Certificate) (*http.Client, error) {
return &http.Client{Transport: transport}, nil
}

// topicFromCert extracts bundle/topic from cert common name
// Apple Push Services: {bundle}
// Apple Development IOS Push Services: {bundle}
func topicFromCert(commonName string) string {
var bundle string
n := strings.Index(commonName, ":")
if n != -1 {
bundle = strings.TrimSpace(commonName[n+1:])
}
return bundle
}

// Push notification to APN service after performing serialization.
func (s *Service) Push(deviceToken string, headers *Headers, payload interface{}) (string, error) {
b, err := json.Marshal(payload)
Expand All @@ -89,9 +74,6 @@ func (s *Service) PushBytes(deviceToken string, headers *Headers, payload []byte
}
req.Header.Set("Content-Type", "application/json")
headers.set(req.Header)
if s.Topic != "" {
req.Header.Set("apns-topic", s.Topic)
}

resp, err := s.Client.Do(req)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions push/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ func TestNewService(t *testing.T) {
t.Fatal(err)
}

const expectedTopic = ""
if service.Topic != expectedTopic {
t.Errorf("Expected topic %q, got %q.", expectedTopic, service.Topic)
if service.Host != push.Development {
t.Errorf("Expected host %q, got %q", push.Development, service.Host)
}
}

Expand Down

0 comments on commit 1786a96

Please sign in to comment.