-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorized_entitlements.go
103 lines (80 loc) · 2.37 KB
/
authorized_entitlements.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
package api
import (
"crypto/rsa"
_ "embed"
"encoding/json"
"fmt"
"github.com/brawaru/marct/network"
"github.com/brawaru/marct/utils"
RSA "github.com/dvsekhvalnov/jose2go/keys/rsa"
_ "github.com/lestrrat-go/jwx"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
)
const (
EntitlementMinecraftProduct = "product_minecraft"
EntitlementMinecraftGame = "game_minecraft"
EntitlementMinecraftBedrockProduct = "product_minecraft_bedrock"
EntitlementMinecraftBedrockGame = "game_minecraft_bedrock"
)
type Entitlement struct {
Name string `json:"name"`
Source string `json:"source"`
}
type EntitlementsResponse struct {
Items []Entitlement `json:"items"`
Signature string `json:"signature"`
KeyId string `json:"keyId"`
RequestId string `json:"requestId"`
}
const entitlementsUrl = "https://api.minecraftservices.com/entitlements/license"
func (a *AuthorizedAPI) GetEntitlements() (*EntitlementsResponse, error) {
req, reqCreateErr := a.newRequest("GET", entitlementsUrl, nil)
if reqCreateErr != nil {
return nil, reqCreateErr
}
query := req.URL.Query()
query.Set("requestId", utils.NewUUID())
req.URL.RawQuery = query.Encode()
resp, respErr := network.PerformRequest(req, network.WithRetries())
if respErr != nil {
return nil, respErr
}
if resp.StatusCode != 200 {
if resp.StatusCode == 400 {
var e APIError
if json.NewDecoder(resp.Body).Decode(&e) == nil {
return nil, &e
}
}
return nil, fmt.Errorf("bad response code: %v (%s)", resp.StatusCode, resp.Status)
}
defer utils.DClose(resp.Body)
var e EntitlementsResponse
return &e, json.NewDecoder(resp.Body).Decode(&e)
}
//go:embed minecraft_pubkey.txt
var mcPubKey []byte
var mcKey *rsa.PublicKey
func init() {
var keyReadErr error
mcKey, keyReadErr = RSA.ReadPublic(mcPubKey)
if keyReadErr != nil {
panic(keyReadErr)
}
}
func (r *EntitlementsResponse) ReadSignature() (jwt.Token, error) {
// FIXME: borked code
key := jwk.NewRSAPublicKey()
if rawKeyReadErr := key.FromRaw(mcKey); rawKeyReadErr != nil {
return nil, fmt.Errorf("cannot create jwk from rsa key: %w", rawKeyReadErr)
}
_ = key.Set("kid", "1")
keySet := jwk.NewSet()
keySet.Add(key)
token, parseErr := jwt.ParseString(r.Signature, jwt.WithKeySet(keySet))
if parseErr != nil {
return nil, fmt.Errorf("cannot parse jwt: %w", parseErr)
}
return token, nil
}