-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.go
174 lines (154 loc) · 4.21 KB
/
token.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package appleapi
import (
"bytes"
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
"net/http"
"time"
"github.com/gbrlsnchs/jwt/v3"
)
var (
ErrAuthKeyNotPem = errors.New("token: AuthKey must be a valid .p8 PEM file")
ErrAuthKeyNotECDSA = errors.New("token: AuthKey must be of type ecdsa.PrivateKey")
ErrAuthKeyNil = errors.New("token: AuthKey was nil")
)
const PlatformIos = "IOS"
const PlatformMac = "MAC_OS"
func ReadPrivate(bytes []byte) (*ecdsa.PrivateKey, error) {
block, _ := pem.Decode(bytes)
if block == nil {
return nil, ErrAuthKeyNotPem
}
//log.Printf("blockKey:%v", block.Bytes)
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
key, err = x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, err
}
}
switch pk := key.(type) {
case *ecdsa.PrivateKey:
return pk, nil
default:
return nil, ErrAuthKeyNotECDSA
}
}
// AuthKeyFromFile loads a .p8 certificate from a local file and returns a
// *ecdsa.PrivateKey.
func AuthKeyFromFile(fileName string) (*ecdsa.PrivateKey, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
return ReadPrivate(bytes)
}
type ErrorResponse struct {
Error []struct {
Code string `json:"code,omitempty"`
Status string `json:"status,omitempty"`
Id string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Source map[string]string `json:"source,omitempty"`
} `json:"errors,omitempty"`
}
type PagingInformation struct {
Paging struct {
Total int `json:"total,omitempty"` //The total number of resources matching your request.
Limit int `json:"limit,omitempty"` //The maximum number of resources to return per page, from 0 to 200.
} `json:"paging,omitempty"`
}
type PagedDocumentLinks struct {
First string `json:"first,omitempty"`
Next string `json:"next,omitempty"`
Self string `json:"self,omitempty"`
}
type TypeId struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
}
type DocumentLinks struct {
Self string `json:"self,omitempty"`
}
type ResourceLinks struct {
Self string `json:"self,omitempty"`
}
type Token struct {
Secret string
Kid string
Iss string
bearer string
created time.Time
key *jwt.ECDSASHA
}
type ApiPayload struct {
Aud string `json:"aud,omitempty"`
Exp int64 `json:"exp,omitempty"`
Iss string `json:"iss,omitempty"`
}
func (t *Token) getAuthorization() (string, error) {
if t.key == nil {
pk, err := ReadPrivate([]byte(t.Secret))
if err != nil {
return "", err
}
t.key = jwt.NewES256(jwt.ECDSAPrivateKey(pk))
}
now := time.Now()
since := now.Sub(t.created).Seconds()
if since > 3000 || t.bearer == "" {
t.created = now
p1 := &ApiPayload{
Aud: "appstoreconnect-v1",
// 注意,apple建议20分钟,最长支持60分钟
Exp: now.Add(time.Minute * 20).Unix(),
Iss: t.Iss,
}
bearer, err := jwt.Sign(p1, t.key, jwt.KeyID(t.Kid))
if err != nil {
return "", err
}
t.bearer = string(bearer)
}
return t.bearer, nil
}
func (t *Token) Verify(bearer string) (jwt.Header, error) {
pl := &ApiPayload{}
return jwt.Verify([]byte(bearer), t.key, pl)
}
func (t *Token) WebGet(url string) ([]byte, error) {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
auth, _ := t.getAuthorization()
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+auth)
resp, err := client.Do(req)
if err != nil {
return nil, nil
}
//if resp.StatusCode != http.StatusOK {
// return nil, fmt.Errorf("invalid respoinse code: %s", resp.Status)
//}
body, err := ioutil.ReadAll(resp.Body)
return body, nil
}
func (t *Token) WebPost(url string, reqJson []byte) ([]byte, error) {
client := &http.Client{}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(reqJson))
auth, _ := t.getAuthorization()
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+auth)
resp, err := client.Do(req)
if err != nil {
return nil, nil
}
//if resp.StatusCode != http.StatusOK {
// return nil, fmt.Errorf("invalid respoinse code: %s", resp.Status)
//}
body, err := ioutil.ReadAll(resp.Body)
return body, nil
}