-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleJWT.go
72 lines (52 loc) · 1.28 KB
/
AppleJWT.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
package main
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/golang-jwt/jwt"
)
type ConfigSettings struct {
PrivateKeyFile string `json:"PrivateKeyFile"`
KeyID string `json:"KeyID"`
IssuerID string `json:"IssuerID"`
}
func ReadConfig(ConfigFileName string) (*ConfigSettings, error) {
file, err := ioutil.ReadFile(ConfigFileName)
if err != nil {
return nil, err
}
config := new(ConfigSettings)
err = json.Unmarshal([]byte(file), &config)
return config, err
}
func CreateAppleJWT(settings *ConfigSettings) (string, error) {
bytes, err := ioutil.ReadFile(settings.PrivateKeyFile)
if err != nil {
fmt.Println(err)
}
x509Encoded, _ := pem.Decode(bytes)
parsedKey, err := x509.ParsePKCS8PrivateKey(x509Encoded.Bytes)
if err != nil {
log.Fatal(err)
}
ecdsaPrivateKey, ok := parsedKey.(*ecdsa.PrivateKey)
if !ok {
panic("not ecdsa private key")
}
token := jwt.NewWithClaims(jwt.SigningMethodES256, jwt.MapClaims{
"iss": settings.IssuerID,
"exp": time.Now().Add(time.Minute * 10).Unix(),
"aud": "appstoreconnect-v1",
})
token.Header["kid"] = settings.KeyID
tokenString, err := token.SignedString(ecdsaPrivateKey)
if err != nil {
log.Fatal(err)
}
return tokenString, nil
}