mock

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2021 License: MIT Imports: 16 Imported by: 0

README

APNs Mock Server

Server example:

key, err := mock.AuthKeyFromFile("AuthKey_XXXXXXXXXX.p8")
if err != nil {
	log.Fatal(err)
}

handler := &mock.Handler{}
handler.TokenPublicKey = func(keyID, teamID string) *ecdsa.PublicKey {
	if keyID == "XXXXXXXXXX" && teamID == "XXXXXXXXXX" {
		return &key.PublicKey
	}
	return nil
}
handler.DeviceToken = func(token string) *mock.DeviceToken {
	if token == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" {
		return &mock.DeviceToken{
			Token:        token,
			Topic:        "com.example.app",
			Unregistered: 1625475525573,
		}
	}
	return &mock.DeviceToken{
		Token: token,
		Topic: "com.example.app",
	}
}
handler.Push = func(push *mock.Push) {
	fmt.Println(push.Status, push.Reason)
}

http.ListenAndServe(":80", handler)

Client example with apns-go:

key, err := apns.AuthKeyFromFile("AuthKey_XXXXXXXXXX.p8")
if err != nil {
	log.Fatal(err)
}

token := apns.NewToken(key, "XXXXXXXXXX", "XXXXXXXXXX")
client := apns.NewClient(token, nil)

n := &apns.Notification{
	DeviceToken: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
	Host:        "http://localhost",
	Topic:       "com.example.app",
	Payload: apns.BuildPayload(&apns.APS{
		Alert: "Hi",
	}, nil),
}

fmt.Println(client.Push(n))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthKeyFromBytes

func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error)

func AuthKeyFromFile

func AuthKeyFromFile(filename string) (*ecdsa.PrivateKey, error)

func VerifyJWT

func VerifyJWT(token string, pub *ecdsa.PublicKey) (bool, error)

Types

type DeviceToken

type DeviceToken struct {
	Token        string
	Topic        string
	Unregistered int64
}

type DeviceTokenFunc

type DeviceTokenFunc func(token string) *DeviceToken

type Handler

type Handler struct {
	TokenPublicKey TokenPublicKeyFunc
	DeviceToken    DeviceTokenFunc
	Push           PushFunc
}

func NewHandler

func NewHandler(keyFunc TokenPublicKeyFunc, tokenFunc DeviceTokenFunc, pushFunc PushFunc) *Handler

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Push

type Push struct {
	ID        string
	Headers   http.Header
	Payload   []byte
	Status    int
	Reason    string
	Timestamp int64
}

type PushFunc

type PushFunc func(push *Push)

type Token

type Token struct {
	KeyID    string
	TeamID   string
	IssuedAt int64
}

func DecodeToken

func DecodeToken(bearer string) (*Token, error)

func (*Token) Expired

func (t *Token) Expired() bool

type TokenPublicKeyFunc

type TokenPublicKeyFunc func(keyID, teamID string) *ecdsa.PublicKey

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL