passkit

package module
v0.0.0-...-763c48e Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 18 Imported by: 0

README

PassKit

This is a library for generating Apple Wallet PKPasses.

How to use

This library is a fork of alvinbaena/passkit but with some changes, like go.mozilla.org/pkcs7 instead of github.com/fullsailor/pkcs7. We wait for the original author to merge our changes, but until then, we will use this fork.

Define a pass

To define a pass you use the Pass struct, which represents the pass.json file. This struct is modeled as closely as possible to the json file, so adding data is straightforward:

c := passkit.NewBoardingPass(passkit.TransitTypeAir)
field := passkit.Field{
    Key: "key",
    Label: "label",
    Value:"value",
}

// Utility functions for adding fields to a pass
c.AddHeaderField(field)
c.AddPrimaryFields(field)
c.AddSecondaryFields(field)
c.AddAuxiliaryFields(field)
c.AddBackFields(field)

pass := passkit.Pass{
    FormatVersion:       1,
    TeamIdentifier:      "TEAMID",
    PassTypeIdentifier:  "pass.type.id",
    AuthenticationToken: "123141lkjdasj12314",
    OrganizationName:    "Your Organization",
    SerialNumber:        "1234",
    Description:         "test",
    BoardingPass:         c,
    Barcodes: []passkit.Barcode{
        {
            Format:          passkit.BarcodeFormatPDF417,
            Message:         "1312312312312312312312312312",
            MessageEncoding: "utf-8",
        },
    },
}
Templates

Usually, passes contain additional information that needs to be included in the final, signed pass, e.g.

  • Images (icons, logos, background images)
  • Translations

These templates are defined in the apple wallet developer documentation.

To create the pass structure you need a PassTemplate instance, either using streams (with InMemoryPassTemplate) or files (with FolderPassTemplate).

Using files

To create the pass bundle create an instance of FolderPassTemplate using the absolute file path of the folder containing the files:

template := passkit.NewFolderPassTemplate("/home/user/pass")

All the files in the folder will be loaded exactly as provided.

Using streams (In Memory)

The second approach is more flexible, having the option of loading files using data streams or directly downloaded from a public URL:

template := passkit.NewInMemoryPassTemplate()

template.AddFileBytes(passkit.BundleThumbnail, bytes)
template.AddFileBytesLocalized(passkit.BundleIcon, "en", bytes)
err := template.AddFileFromURL(passkit.BundleLogo, "http://example.com/file.png")
err := template.AddFileFromURLLocalized(passkit.BundleLogo, "en", "http://example.com/file.png")
err := template.AddAllFiles("/home/user/pass")

Note: There are no checks that the contents of a provided file are valid. If a PDF file is provided, but is referenced as icon.png, when viewing the pass on a device there will be issues. It also doesn't provide any authentication for the downloads, so the resources used must be public for the download to work as expected.

Signing and zipping a pass

As all passes need to be signed when bundled we need to use a Signer instance. There are two types of signers:

  • FileBasedSigner: uses a temp folder to store the signed zip file contents
  • MemoryBasedSigner: keeps the signed zip file contents in memory

To use any of the Signer instances you need an instance of SigningInformation to load the certificates used to generate the signature. There are two ways to obtain an instance. Either reading the certificates from the filesystem, or from already loaded bytes in memory:

signInfo, err := passkit.LoadSigningInformationFromFiles("/home/user/pass_cert.p12", "pass_cert_password", "/home/user/AppleWWDRCA.cer")
signInfo, err := passkit.LoadSigningInformationFromBytes(passCertBytes, "pass_cert_password", wwdrcaBytes)

Note: When loading the signing information errors will be returned if the certificates are invalid (expired, not certificates, etc)

Finally, to create the signed pass bundle you use the Pass, Signer, SigningInformation, and PassTemplate instances created previously, for example:

signer := passkit.NewMemoryBasedSigner()
signInfo, err := passkit.LoadSigningInformationFromFiles("/home/user/pass_cert.p12", "pass_cert_password", "/home/user/AppleWWDRCA.cer")
if err != nil {
    panic(err)
}

z, err := signer.CreateSignedAndZippedPassArchive(&pass, template, signInfo)
if err != nil {
    panic(err)
}

err = os.WriteFile("/home/user/pass.pkpass", z, 0644)
if err != nil {
    panic(err)
}

After this step the pass bundle is ready to be distributed as you see fit.

Documentation

Index

Constants

View Source
const (

	//PKTextAlignment
	TextAlignmentLeft    TextAlignment = "PKTextAlignmentLeft"
	TextAlignmentCenter  TextAlignment = "PKTextAlignmentCenter"
	TextAlignmentRight   TextAlignment = "PKTextAlignmentRight"
	TextAlignmentNatural TextAlignment = "PKTextAlignmentNatural"

	//PKBarcodeFormat
	BarcodeFormatQR      BarcodeFormat = "PKBarcodeFormatQR"
	BarcodeFormatPDF417  BarcodeFormat = "PKBarcodeFormatPDF417"
	BarcodeFormatAztec   BarcodeFormat = "PKBarcodeFormatAztec"
	BarcodeFormatCode128 BarcodeFormat = "PKBarcodeFormatCode128"

	//PKDataDetectorType
	DataDetectorTypePhoneNumber   DataDetectorType = "PKDataDetectorTypePhoneNumber"
	DataDetectorTypeLink          DataDetectorType = "PKDataDetectorTypeLink"
	DataDetectorTypeAddress       DataDetectorType = "PKDataDetectorTypeAddress"
	DataDetectorTypeCalendarEvent DataDetectorType = "PKDataDetectorTypeCalendarEvent"

	//PKDateStyle
	DateStyleNone   DateStyle = "PKDateStyleNone"
	DateStyleShort  DateStyle = "PKDateStyleShort"
	DateStyleMedium DateStyle = "PKDateStyleMedium"
	DateStyleLong   DateStyle = "PKDateStyleLong"
	DateStyleFull   DateStyle = "PKDateStyleFull"

	//PKNumberStyle
	NumberStyleDecimal    NumberStyle = "PKNumberStyleDecimal"
	NumberStylePercent    NumberStyle = "PKNumberStylePercent"
	NumberStyleScientific NumberStyle = "PKNumberStyleScientific"
	NumberStyleSpellOut   NumberStyle = "PKNumberStyleSpellOut"

	//PKPassPersonalizationField
	PassPersonalizationFieldName         PassPersonalizationField = "PKPassPersonalizationFieldName"
	PassPersonalizationFieldPostalCode   PassPersonalizationField = "PKPassPersonalizationFieldPostalCode"
	PassPersonalizationFieldEmailAddress PassPersonalizationField = "PKPassPersonalizationFieldEmailAddress"
	PassPersonalizationFieldPhoneNumber  PassPersonalizationField = "PKPassPersonalizationFieldPhoneNumber"

	//PKTransitType
	TransitTypeAir     TransitType = "PKTransitTypeAir"
	TransitTypeBoat    TransitType = "PKTransitTypeBoat"
	TransitTypeBus     TransitType = "PKTransitTypeBus"
	TransitTypeGeneric TransitType = "PKTransitTypeGeneric"
	TransitTypeTrain   TransitType = "PKTransitTypeTrain"
)
View Source
const (
	BundleIconRetinaHD                = "icon@3x.png"
	BundleIconRetina                  = "icon@2x.png"
	BundleIcon                        = "icon.png"
	BundleLogoRetinaHD                = "logo@3x.png"
	BundleLogoRetina                  = "logo@2x.png"
	BundleThumbnailRetinaHD           = "thumbnail@3x.png"
	BundleThumbnailRetina             = "thumbnail@2x.png"
	BundleThumbnail                   = "thumbnail.png"
	BundleStripRetinaHD               = "strip@3x.png"
	BundleStripRetina                 = "strip@2x.png"
	BundleStrip                       = "strip.png"
	BundleBackgroundRetinaHD          = "background@3x.png"
	BundleBackgroundRetina            = "background@2x.png"
	BundleBackground                  = "background.png"
	BundleFooterRetinaHD              = "footer@3x.png"
	BundleFooterRetina                = "footer@2x.png"
	BundleFooter                      = "footer.png"
	BundlePersonalizationLogoRetinaHD = "personalizationLogo@3x.png"
	BundlePersonalizationLogoRetina   = "personalizationLogo@2x.png"
)

Variables

Functions

This section is empty.

Types

type Barcode

type Barcode struct {
	Format          BarcodeFormat `json:"format,omitempty"`
	AltText         string        `json:"altText,omitempty"`
	Message         string        `json:"message,omitempty"`
	MessageEncoding string        `json:"messageEncoding,omitempty"`
}

func (*Barcode) GetValidationErrors

func (b *Barcode) GetValidationErrors() []string

func (*Barcode) IsValid

func (b *Barcode) IsValid() bool

type BarcodeFormat

type BarcodeFormat string

type Beacon

type Beacon struct {
	Major         int    `json:"major,omitempty"`
	Minor         int    `json:"minor,omitempty"`
	ProximityUUID string `json:"proximityUUID,omitempty"`
	RelevantText  string `json:"relevantText,omitempty"`
}

func (*Beacon) GetValidationErrors

func (b *Beacon) GetValidationErrors() []string

func (*Beacon) IsValid

func (b *Beacon) IsValid() bool

type BoardingPass

type BoardingPass struct {
	*GenericPass
	TransitType TransitType `json:"transitType,omitempty"`
}

func NewBoardingPass

func NewBoardingPass(transitType TransitType) *BoardingPass

func (*BoardingPass) GetValidationErrors

func (b *BoardingPass) GetValidationErrors() []string

func (*BoardingPass) IsValid

func (b *BoardingPass) IsValid() bool

type Coupon

type Coupon struct {
	*GenericPass
}

func NewCoupon

func NewCoupon() *Coupon

type DataDetectorType

type DataDetectorType string

type DateStyle

type DateStyle string

type EventTicket

type EventTicket struct {
	*GenericPass
}

func NewEventTicket

func NewEventTicket() *EventTicket

type Field

type Field struct {
	Key               string             `json:"key,omitempty"`
	Label             string             `json:"label,omitempty"`
	Value             interface{}        `json:"value,omitempty"`
	AttributedValue   interface{}        `json:"attributedValue,omitempty"`
	ChangeMessage     string             `json:"changeMessage,omitempty"`
	TextAlignment     TextAlignment      `json:"textAlignment,omitempty"`
	DataDetectorTypes []DataDetectorType `json:"dataDetectorTypes,omitempty"`
	CurrencyCode      string             `json:"currencyCode,omitempty"`
	NumberStyle       NumberStyle        `json:"numberStyle,omitempty"`
	DateStyle         DateStyle          `json:"dateStyle,omitempty"`
	TimeStyle         DateStyle          `json:"timeStyle,omitempty"`
	IsRelative        bool               `json:"isRelative,omitempty"`
	IgnoreTimeZone    bool               `json:"ignoresTimeZone,omitempty"`
	Row               int                `json:"row,omitempty"`
}

func (*Field) GetValidationErrors

func (f *Field) GetValidationErrors() []string

func (*Field) IsValid

func (f *Field) IsValid() bool

type FolderPassTemplate

type FolderPassTemplate struct {
	// contains filtered or unexported fields
}

func NewFolderPassTemplate

func NewFolderPassTemplate(templateDir string) *FolderPassTemplate

func (*FolderPassTemplate) GetAllFiles

func (f *FolderPassTemplate) GetAllFiles() (map[string][]byte, error)

func (*FolderPassTemplate) ProvisionPassAtDirectory

func (f *FolderPassTemplate) ProvisionPassAtDirectory(tmpDirPath string) error

type GenericPass

type GenericPass struct {
	HeaderFields    []Field `json:"headerFields,omitempty"`
	PrimaryFields   []Field `json:"primaryFields,omitempty"`
	SecondaryFields []Field `json:"secondaryFields,omitempty"`
	AuxiliaryFields []Field `json:"auxiliaryFields,omitempty"`
	BackFields      []Field `json:"backFields,omitempty"`
}

func NewGenericPass

func NewGenericPass() *GenericPass

func (*GenericPass) AddAuxiliaryFields

func (gp *GenericPass) AddAuxiliaryFields(field Field)

func (*GenericPass) AddBackFields

func (gp *GenericPass) AddBackFields(field Field)

func (*GenericPass) AddHeaderField

func (gp *GenericPass) AddHeaderField(field Field)

func (*GenericPass) AddPrimaryFields

func (gp *GenericPass) AddPrimaryFields(field Field)

func (*GenericPass) AddSecondaryFields

func (gp *GenericPass) AddSecondaryFields(field Field)

func (*GenericPass) GetValidationErrors

func (gp *GenericPass) GetValidationErrors() []string

func (*GenericPass) IsValid

func (gp *GenericPass) IsValid() bool

type InMemoryPassTemplate

type InMemoryPassTemplate struct {
	// contains filtered or unexported fields
}

func NewInMemoryPassTemplate

func NewInMemoryPassTemplate() *InMemoryPassTemplate

func (*InMemoryPassTemplate) AddAllFiles

func (m *InMemoryPassTemplate) AddAllFiles(directoryWithFilesToAdd string) error

func (*InMemoryPassTemplate) AddFileBytes

func (m *InMemoryPassTemplate) AddFileBytes(name string, data []byte)

func (*InMemoryPassTemplate) AddFileBytesLocalized

func (m *InMemoryPassTemplate) AddFileBytesLocalized(name, locale string, data []byte)

func (*InMemoryPassTemplate) AddFileFromURL

func (m *InMemoryPassTemplate) AddFileFromURL(name string, u url.URL) error

func (*InMemoryPassTemplate) AddFileFromURLLocalized

func (m *InMemoryPassTemplate) AddFileFromURLLocalized(name, locale string, u url.URL) error

func (*InMemoryPassTemplate) GetAllFiles

func (m *InMemoryPassTemplate) GetAllFiles() (map[string][]byte, error)

func (*InMemoryPassTemplate) ProvisionPassAtDirectory

func (m *InMemoryPassTemplate) ProvisionPassAtDirectory(tmpDirPath string) error

type Location

type Location struct {
	Latitude     float64 `json:"latitude,omitempty"`
	Longitude    float64 `json:"longitude,omitempty"`
	Altitude     float64 `json:"altitude,omitempty"`
	RelevantText string  `json:"relevantText,omitempty"`
}

func (*Location) GetValidationErrors

func (l *Location) GetValidationErrors() []string

func (*Location) IsValid

func (l *Location) IsValid() bool

type NFC

type NFC struct {
	Message                string `json:"message,omitempty"`
	EncryptionPublicKey    string `json:"encryptionPublicKey,omitempty"`
	RequiresAuthentication bool   `json:"requiresAuthentication,omitempty"`
}

type NumberStyle

type NumberStyle string

type PWAssociatedApp

type PWAssociatedApp struct {
	Title        string
	IdGooglePlay string
	IdAmazon     string
}

func (*PWAssociatedApp) GetValidationErrors

func (a *PWAssociatedApp) GetValidationErrors() []string

func (*PWAssociatedApp) IsValid

func (a *PWAssociatedApp) IsValid() bool

type Pass

type Pass struct {
	FormatVersion              int                    `json:"formatVersion,omitempty"`
	SerialNumber               string                 `json:"serialNumber,omitempty"`
	PassTypeIdentifier         string                 `json:"passTypeIdentifier,omitempty"`
	WebServiceURL              string                 `json:"webServiceURL,omitempty"`
	AuthenticationToken        string                 `json:"authenticationToken,omitempty"`
	Description                string                 `json:"description,omitempty"`
	TeamIdentifier             string                 `json:"teamIdentifier,omitempty"`
	OrganizationName           string                 `json:"organizationName,omitempty"`
	LogoText                   string                 `json:"logoText,omitempty"`
	ForegroundColor            string                 `json:"foregroundColor,omitempty"`
	BackgroundColor            string                 `json:"backgroundColor,omitempty"`
	LabelColor                 string                 `json:"labelColor,omitempty"`
	GroupingIdentifier         string                 `json:"groupingIdentifier,omitempty"`
	Beacons                    []Beacon               `json:"beacons,omitempty"`
	Locations                  []Location             `json:"locations,omitempty"`
	Barcodes                   []Barcode              `json:"barcodes,omitempty"`
	EventTicket                *EventTicket           `json:"eventTicket,omitempty"`
	Coupon                     *Coupon                `json:"coupon,omitempty"`
	StoreCard                  *StoreCard             `json:"storeCard,omitempty"`
	BoardingPass               *BoardingPass          `json:"boardingPass,omitempty"`
	Generic                    *GenericPass           `json:"generic,omitempty"`
	AppLaunchURL               string                 `json:"appLaunchURL,omitempty"`
	AssociatedStoreIdentifiers []int64                `json:"associatedStoreIdentifiers,omitempty"`
	UserInfo                   map[string]interface{} `json:"userInfo,omitempty"`
	MaxDistance                int64                  `json:"maxDistance,omitempty"`
	RelevantDate               *time.Time             `json:"relevantDate,omitempty"`
	ExpirationDate             *time.Time             `json:"expirationDate,omitempty"`
	Voided                     bool                   `json:"voided,omitempty"`
	Nfc                        *NFC                   `json:"nfc,omitempty"`
	SharingProhibited          bool                   `json:"sharingProhibited,omitempty"`
	// contains filtered or unexported fields
}

func (*Pass) GetValidationErrors

func (p *Pass) GetValidationErrors() []string

func (*Pass) IsValid

func (p *Pass) IsValid() bool

func (*Pass) SetBackgroundColorHex

func (p *Pass) SetBackgroundColorHex(hex string) error

func (*Pass) SetBackgroundColorRGB

func (p *Pass) SetBackgroundColorRGB(r, g, b uint8) error

func (*Pass) SetForegroundColorHex

func (p *Pass) SetForegroundColorHex(hex string) error

func (*Pass) SetForegroundColorRGB

func (p *Pass) SetForegroundColorRGB(r, g, b uint8) error

func (*Pass) SetLabelColorHex

func (p *Pass) SetLabelColorHex(hex string) error

func (*Pass) SetLabelColorRGB

func (p *Pass) SetLabelColorRGB(r, g, b uint8) error

type PassPersonalizationField

type PassPersonalizationField string

type PassTemplate

type PassTemplate interface {
	ProvisionPassAtDirectory(tmpDirPath string) error
	GetAllFiles() (map[string][]byte, error)
}

type Personalization

type Personalization struct {
	RequiredPersonalizationFields []PassPersonalizationField `json:"requiredPersonalizationFields"`
	Description                   string                     `json:"description"`
	TermsAndConditions            string                     `json:"termsAndConditions"`
}

func (*Personalization) GetValidationErrors

func (pz *Personalization) GetValidationErrors() []string

func (*Personalization) IsValid

func (pz *Personalization) IsValid() bool

type Signer

type Signer interface {
	CreateSignedAndZippedPassArchive(p *Pass, t PassTemplate, i *SigningInformation) ([]byte, error)
	CreateSignedAndZippedPersonalizedPassArchive(p *Pass, pz *Personalization, t PassTemplate, i *SigningInformation) ([]byte, error)
	SignManifestFile(manifestJson []byte, i *SigningInformation) ([]byte, error)
}

func NewFileBasedSigner

func NewFileBasedSigner() Signer

func NewMemoryBasedSigner

func NewMemoryBasedSigner() Signer

type SigningInformation

type SigningInformation struct {
	// contains filtered or unexported fields
}

func LoadSigningInformationFromBytes

func LoadSigningInformationFromBytes(pkcs12KeyStoreFile []byte, keyStorePassword string, appleWWDRCAFile []byte) (*SigningInformation, error)

func LoadSigningInformationFromFiles

func LoadSigningInformationFromFiles(pkcs12KeyStoreFilePath, keyStorePassword, appleWWDRCAFilePath string) (*SigningInformation, error)

type StoreCard

type StoreCard struct {
	*GenericPass
}

func NewStoreCard

func NewStoreCard() *StoreCard

type TextAlignment

type TextAlignment string

type TransitType

type TransitType string

type Validateable

type Validateable interface {
	IsValid() bool
	GetValidationErrors() []string
}

Jump to

Keyboard shortcuts

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