Skip to content

Commit

Permalink
Merge pull request #22 from aktsk/modify-how-to-return-server-response
Browse files Browse the repository at this point in the history
Modify how to return server response
  • Loading branch information
mizzy authored Aug 27, 2018
2 parents 3f18b20 + a94ba13 commit 162e6e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
18 changes: 13 additions & 5 deletions receipt/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
// Result is the validation result
type Result struct {
Status int `json:"status"`
Environment string `json:"environment"`
Receipt *Receipt `json:"receipt"`
Environment string `json:"environment,omitempty"`
Receipt *Receipt `json:"receipt,omitempty"`
LatestReceiptInfo []InApp `json:"latest_receipt_info,omitempty"`
LatestReceipt string `json:"latest_receipt,omitempty"`
IsRetryable bool `json:"is-retryable,omitempty"`
}

// ParseWithAppleRootCert parses base 64 encoded receipt data with Apple Inc Root Certificate
func ParseWithAppleRootCert(data string) (*Receipt, error) {
func GetAppleRootCert() (*x509.Certificate, error) {
statikFS, err := fs.New()
if err != nil {
return nil, err
Expand All @@ -44,7 +43,16 @@ func ParseWithAppleRootCert(data string) (*Receipt, error) {
return nil, err
}

return Parse(rootCert, data)
return rootCert, nil
}

// ParseWithAppleRootCert parses base 64 encoded receipt data with Apple Inc Root Certificate
func ParseWithAppleRootCert(data string) (*Receipt, error) {
cert, err := GetAppleRootCert()
if err != nil {
return nil, err
}
return Parse(cert, data)
}

// Parse parsed base 64 encoded receipt data with a given certificate
Expand Down
39 changes: 25 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,40 @@ func Parse(cert *x509.Certificate) func(http.ResponseWriter, *http.Request) {
json.NewDecoder(r.Body).Decode(&request)

var rcpt *receipt.Receipt
var result receipt.Result
var err error

if cert == nil {
rcpt, err = receipt.ParseWithAppleRootCert(request.ReceiptData)
} else {
rcpt, err = receipt.Parse(cert, request.ReceiptData)
}

if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
cert, err = receipt.GetAppleRootCert()
if err != nil {
log.Print(err)
result.Status = 21100
result.IsRetryable = true
writeResult(w, result)
return
}
}

result, err := rcpt.Validate()
rcpt, err = receipt.Parse(cert, request.ReceiptData)

resultBody, err := json.Marshal(result)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
result.Status = 21002
} else {
result, err = rcpt.Validate()
}

w.Write(resultBody)
writeResult(w, result)
}
}

func writeResult(w http.ResponseWriter, result receipt.Result) {
resultBody, err := json.Marshal(result)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Write(resultBody)
}
36 changes: 25 additions & 11 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@ import (
"github.com/aktsk/nolmandy/receipt"
)

func TestServer(t *testing.T) {
func TestInvalidReceipt(t *testing.T) {
result := request(t, "invalid receipt")

if result.Status != 21002 {
t.Fatalf("Status should be 21002, not %d", result.Status)
}
}

func TestValidReceipt(t *testing.T) {
result := request(t, receiptData)

if result.Status != 0 {
t.Fatalf("Status should be 0, not %d", result.Status)
}

rcpt := result.Receipt

if rcpt.ReceiptType != "ProductionSandbox" {
t.Fatalf("Wrong receipt_type: %s", rcpt.ReceiptType)
}
}

func request(t *testing.T, data string) receipt.Result {
certDER, _ := pem.Decode([]byte(certificate))
cert, err := x509.ParseCertificate(certDER.Bytes)
if err != nil {
Expand All @@ -23,7 +45,7 @@ func TestServer(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(Parse(cert)))
defer s.Close()

req := Request{ReceiptData: receiptData}
req := Request{ReceiptData: data}
reqBody, err := json.Marshal(req)
if err != nil {
t.Fatal(err)
Expand All @@ -42,15 +64,7 @@ func TestServer(t *testing.T) {
var result receipt.Result
json.Unmarshal(respBody, &result)

if result.Status != 0 {
t.Fatal("Status is not 0")
}

rcpt := result.Receipt

if rcpt.ReceiptType != "ProductionSandbox" {
t.Fatalf("Wrong receipt_type: %s", rcpt.ReceiptType)
}
return result
}

var receiptData = `
Expand Down

0 comments on commit 162e6e1

Please sign in to comment.