-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleStoreApi.go
257 lines (208 loc) · 5.44 KB
/
AppleStoreApi.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
type AppConnectUsers = struct {
Data []Datum `json:"data"`
Links AppConnectUsersLinks `json:"links"`
}
type Datum = struct {
Type string `json:"type"`
Attributes Attributes `json:"attributes"`
}
type Attributes = struct {
Username string `json:"username"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Roles []string `json:"roles"`
}
type AppConnectUsersLinks = struct {
Self string `json:"self"`
Next string `json:"next"`
}
// struct generated via https://mholt.github.io/json-to-go/
type AppConnectErrors struct {
Errors []struct {
Status string `json:"status"`
Code string `json:"code"`
Title string `json:"title"`
Detail string `json:"detail"`
} `json:"errors"`
}
type User struct {
UserName string
FirstName string
LastName string
Roles string
}
func CheckUsers(config *ConfigSettings, users []string) {
var FoundMatch = false
var Status = ""
teamMembers, err := GetUserList(config)
if err == nil {
// Walk through the users list
FoundMatch = false
var lastUser = ""
for _, user := range users {
if lastUser != user {
lastUser = user
var foundMember User
Status = "NOMATCH"
for _, teamMember := range teamMembers {
FoundMatch = strings.EqualFold(teamMember.UserName, user)
if FoundMatch {
Status = "MATCH"
foundMember = teamMember
fmt.Printf("%s, %s %s %s, %s\n",
Status,
foundMember.UserName,
foundMember.FirstName,
foundMember.LastName,
foundMember.Roles)
} else {
foundMember.UserName = user
}
}
}
}
}
}
func GetUserList(config *ConfigSettings) ([]User, error) {
var users []User
var err error
token, err := CreateAppleJWT(config)
if err != nil {
fmt.Println(err)
}
client := &http.Client{}
var nextUrl string = "https://api.appstoreconnect.apple.com/v1/users?limit=100"
for {
req, err := http.NewRequest("GET", nextUrl, nil)
if err != nil {
fmt.Print(err.Error())
os.Exit(3)
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
os.Exit(3)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error while reading the response bytes:", err)
os.Exit(3)
}
// Check for the AppConnect API returning an error first
var appConnectErrors AppConnectErrors
err = json.Unmarshal(body, &appConnectErrors)
if err != nil {
log.Println("Error while deserializing the response bytes:", err)
os.Exit(3)
}
// If there is an error object in the body, print it and exit
if len(appConnectErrors.Errors) > 0 {
firstError := appConnectErrors.Errors[0]
log.Println("Status:", firstError.Status)
log.Println("Error accessing API:", firstError.Detail)
os.Exit(4)
}
// Otherwise keep going
var appConnectUsers AppConnectUsers
err = json.Unmarshal(body, &appConnectUsers)
if err != nil {
log.Println("Error while deserializing the response bytes:", err)
os.Exit(3)
}
for _, s := range appConnectUsers.Data {
var user User
user.UserName = s.Attributes.Username
user.FirstName = s.Attributes.FirstName
user.LastName = s.Attributes.LastName
user.Roles = strings.Join(s.Attributes.Roles, ", ")
users = append(users, user)
}
nextUrl = appConnectUsers.Links.Next
if len(nextUrl) == 0 {
break
}
}
return users, err
}
func CheckUserList(config *ConfigSettings, Username string) {
token, err := CreateAppleJWT(config)
if err != nil {
fmt.Println(err)
}
client := &http.Client{}
var nextUrl string = "https://api.appstoreconnect.apple.com/v1/users?limit=100"
var FoundMatch = false
for {
req, err := http.NewRequest("GET", nextUrl, nil)
if err != nil {
fmt.Print(err.Error())
os.Exit(3)
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
os.Exit(3)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error while reading the response bytes:", err)
os.Exit(3)
}
// Check for the AppConnect API returning an error first
var appConnectErrors AppConnectErrors
err = json.Unmarshal(body, &appConnectErrors)
if err != nil {
log.Println("Error while deserializing the response bytes:", err)
os.Exit(3)
}
// If there is an error object in the body, print it and exit
if len(appConnectErrors.Errors) > 0 {
firstError := appConnectErrors.Errors[0]
log.Println("Status:", firstError.Status)
log.Println("Error accessing API:", firstError.Detail)
os.Exit(4)
}
// Otherwise keep going
var appConnectUsers AppConnectUsers
err = json.Unmarshal(body, &appConnectUsers)
if err != nil {
log.Println("Error while deserializing the response bytes:", err)
os.Exit(3)
}
for _, s := range appConnectUsers.Data {
FoundMatch = strings.EqualFold(s.Attributes.Username, Username)
if FoundMatch {
fmt.Printf("Found %s, %s %s, %s\n",
s.Attributes.Username,
s.Attributes.FirstName,
s.Attributes.LastName,
strings.Join(s.Attributes.Roles, ", "))
break
}
}
if FoundMatch {
break
}
nextUrl = appConnectUsers.Links.Next
if len(nextUrl) == 0 {
break
}
}
if !FoundMatch {
fmt.Println("No match for " + Username)
}
}