-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
108 lines (93 loc) · 4.5 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package asn
import (
_ "embed"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"github.com/lestrrat-go/jwx/v2/jws"
)
//go:embed testdata/Root-CA.cer
var rootCA []byte
//go:embed testdata/request.txt
var request []byte
//go:embed testdata/raw.txt
var raw []byte
const (
emptyCerPath = "testdata/empty.cer"
cerPath = "testdata/Root-CA.cer"
cerURL = "https://www.apple.com/certificateauthority/AppleRootCA-G3.cer"
)
func path(filename string) string {
return filepath.Join(filename)
}
type fakeAppleServer struct{}
func (*fakeAppleServer) RoundTrip(r *http.Request) (*http.Response, error) {
res := httptest.NewRecorder()
if r.URL.Host == "www.apple.com" {
_, _ = res.Write(rootCA)
}
if strings.Contains(r.URL.String(), "notfound") {
res.WriteHeader(http.StatusNotFound)
}
return res.Result(), nil
}
func ExampleNewKeyProvider_byFile() {
kp := NewKeyProvider(NewFileRootCAFetcher(cerPath))
opts := []jws.VerifyOption{jws.WithKeyProvider(kp)}
verified, err := jws.Verify(request, opts...)
fmt.Println(string(verified), err)
// Output:
// {"notificationType":"DID_CHANGE_RENEWAL_PREF","subtype":"DOWNGRADE","notificationUUID":"c92e001c-96d2-9ou5-q92p-32a5fy0d6g78","notificationVersion":"2.0","data":{"appAppleId":982253034,"bundleId":"hogehoge","bundleVersion":"269822910.1","environment":"Production","signedRenewalInfo":"...","signedTransactionInfo":"..."}} <nil>
}
func ExampleNewKeyProvider_byFiles() {
emptyCerPath := path(emptyCerPath)
optional := []string{cerPath}
kp := NewKeyProvider(NewFileRootCAFetcher(emptyCerPath, optional...))
opts := []jws.VerifyOption{jws.WithKeyProvider(kp)}
verified, err := jws.Verify(request, opts...)
fmt.Println(string(verified), err)
// Output:
// {"notificationType":"DID_CHANGE_RENEWAL_PREF","subtype":"DOWNGRADE","notificationUUID":"c92e001c-96d2-9ou5-q92p-32a5fy0d6g78","notificationVersion":"2.0","data":{"appAppleId":982253034,"bundleId":"hogehoge","bundleVersion":"269822910.1","environment":"Production","signedRenewalInfo":"...","signedTransactionInfo":"..."}} <nil>
}
func ExampleNewKeyProvider_byUrl() {
client := &http.Client{
Transport: &fakeAppleServer{},
}
kp := NewKeyProvider(NewHTTPRootCAFetcher(client, cerURL))
opts := []jws.VerifyOption{jws.WithKeyProvider(kp)}
verified, err := jws.Verify(request, opts...)
fmt.Println(string(verified), err)
// Output:
// {"notificationType":"DID_CHANGE_RENEWAL_PREF","subtype":"DOWNGRADE","notificationUUID":"c92e001c-96d2-9ou5-q92p-32a5fy0d6g78","notificationVersion":"2.0","data":{"appAppleId":982253034,"bundleId":"hogehoge","bundleVersion":"269822910.1","environment":"Production","signedRenewalInfo":"...","signedTransactionInfo":"..."}} <nil>
}
func ExampleNewKeyProvider_byUrls() {
const url = "http://localhost:8080/test.cer"
client := &http.Client{
Transport: &fakeAppleServer{},
}
optional := []string{cerURL}
kp := NewKeyProvider(NewHTTPRootCAFetcher(client, url, optional...))
opts := []jws.VerifyOption{jws.WithKeyProvider(kp)}
verified, err := jws.Verify(request, opts...)
fmt.Println(string(verified), err)
// Output:
// {"notificationType":"DID_CHANGE_RENEWAL_PREF","subtype":"DOWNGRADE","notificationUUID":"c92e001c-96d2-9ou5-q92p-32a5fy0d6g78","notificationVersion":"2.0","data":{"appAppleId":982253034,"bundleId":"hogehoge","bundleVersion":"269822910.1","environment":"Production","signedRenewalInfo":"...","signedTransactionInfo":"..."}} <nil>
}
func ExampleNewKeyProvider_byRaw() {
kp := NewKeyProvider(NewRawRootCAFetcher(raw))
opts := []jws.VerifyOption{jws.WithKeyProvider(kp)}
verified, err := jws.Verify(request, opts...)
fmt.Println(string(verified), err)
// Output:
// {"notificationType":"DID_CHANGE_RENEWAL_PREF","subtype":"DOWNGRADE","notificationUUID":"c92e001c-96d2-9ou5-q92p-32a5fy0d6g78","notificationVersion":"2.0","data":{"appAppleId":982253034,"bundleId":"hogehoge","bundleVersion":"269822910.1","environment":"Production","signedRenewalInfo":"...","signedTransactionInfo":"..."}} <nil>
}
func ExampleNewKeyProvider_byRaws() {
kp := NewKeyProvider(NewRawRootCAFetcher([]byte(`test`), raw))
opts := []jws.VerifyOption{jws.WithKeyProvider(kp)}
verified, err := jws.Verify(request, opts...)
fmt.Println(string(verified), err)
// Output:
// {"notificationType":"DID_CHANGE_RENEWAL_PREF","subtype":"DOWNGRADE","notificationUUID":"c92e001c-96d2-9ou5-q92p-32a5fy0d6g78","notificationVersion":"2.0","data":{"appAppleId":982253034,"bundleId":"hogehoge","bundleVersion":"269822910.1","environment":"Production","signedRenewalInfo":"...","signedTransactionInfo":"..."}} <nil>
}