-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
google.go
234 lines (201 loc) · 7 KB
/
google.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
package google
import (
"context"
"fmt"
"log/slog"
"golang.org/x/oauth2/google"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
const (
// https://cloud.google.com/storage/docs/json_api
groupsRequiredFields googleapi.Field = "nextPageToken, groups(id,name,email,etag)"
membersRequiredFields googleapi.Field = "nextPageToken, members(id,email,status,type,etag)"
listUsersRequiredFields googleapi.Field = "nextPageToken, users(id,primaryEmail,name,suspended,kind,etag,emails,addresses,organizations,phones,languages,locations)"
getUsersRequiredFields googleapi.Field = "id,primaryEmail,name,suspended,kind,etag,emails,addresses,organizations,phones,languages,locations"
)
var (
// ErrGoogleClientScopeNil is returned when the scope is nil.
ErrGoogleClientScopeNil = fmt.Errorf("google: google client scope is required")
// ErrUserIDNil is returned when the user ID is nil.
ErrUserIDNil = fmt.Errorf("google: user id is required")
// ErrGroupIDNil is returned when the group ID is nil.
ErrGroupIDNil = fmt.Errorf("google: group id is required")
)
// DirectoryService represent the Google Directory API client.
type DirectoryService struct {
svc *admin.Service
}
// NewService create a Google Directory Service.
// References:
// - https://pkg.go.dev/google.golang.org/api/admin/directory/v1
// Examples of scope:
// - "https://www.googleapis.com/auth/admin.directory.group.readonly"
// - "https://www.googleapis.com/auth/admin.directory.group.member.readonly"
// - "https://www.googleapis.com/auth/admin.directory.user.readonly"
func NewService(ctx context.Context, userEmail string, serviceAccount []byte, scope ...string) (*admin.Service, error) {
if len(scope) == 0 {
return nil, ErrGoogleClientScopeNil
}
creds, err := google.CredentialsFromJSONWithParams(ctx, serviceAccount, google.CredentialsParams{
Scopes: scope,
Subject: userEmail,
})
if err != nil {
return nil, fmt.Errorf("google: error getting config for Service Account: %v", err)
}
svc, err := admin.NewService(ctx, option.WithTokenSource(creds.TokenSource))
if err != nil {
return nil, fmt.Errorf("google: error creating service: %v", err)
}
return svc, nil
}
// NewDirectoryService create a Google Directory API client.
// References:
// - https://developers.google.com/admin-sdk/directory/v1/guides/delegation?utm_source=pocket_mylist#go
func NewDirectoryService(svc *admin.Service) (*DirectoryService, error) {
return &DirectoryService{
svc: svc,
}, nil
}
// ListUsers list all users in a Google Directory filtered by query.
func (ds *DirectoryService) ListUsers(ctx context.Context, query []string) ([]*admin.User, error) {
u := make([]*admin.User, 0)
if len(query) > 0 {
for _, q := range query {
if q != "" {
err := ds.svc.Users.List().Query(q).Customer("my_customer").Fields(listUsersRequiredFields).Pages(ctx, func(users *admin.Users) error {
u = append(u, users.Users...)
return nil
})
if err != nil {
return nil, err
}
} else {
err := ds.svc.Users.List().Customer("my_customer").Fields(listUsersRequiredFields).Pages(ctx, func(users *admin.Users) error {
u = append(u, users.Users...)
return nil
})
if err != nil {
return nil, err
}
}
}
} else {
err := ds.svc.Users.List().Customer("my_customer").Fields(listUsersRequiredFields).Pages(ctx, func(users *admin.Users) error {
u = append(u, users.Users...)
return nil
})
if err != nil {
return nil, err
}
}
slog.Debug("google: ListUsers()", "users", u)
return u, nil
}
// ListGroups list all groups in a Google Directory filtered by query.
// References:
// - https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups
func (ds *DirectoryService) ListGroups(ctx context.Context, query []string) ([]*admin.Group, error) {
g := make([]*admin.Group, 0)
if len(query) > 0 {
for _, q := range query {
if q != "" {
err := ds.svc.Groups.List().Customer("my_customer").Query(q).Fields(groupsRequiredFields).Pages(ctx, func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})
if err != nil {
return nil, err
}
} else {
err := ds.svc.Groups.List().Customer("my_customer").Fields(groupsRequiredFields).Pages(ctx, func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})
if err != nil {
return nil, err
}
}
}
} else {
err := ds.svc.Groups.List().Customer("my_customer").Fields(groupsRequiredFields).Pages(ctx, func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})
if err != nil {
return nil, err
}
}
slog.Debug("google: ListGroups()", "groups", g)
return g, nil
}
// ListGroupMembers return a list of all members given a group ID.
// references:
// - https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/list
// - https://developers.google.com/admin-sdk/directory/v1/guides/manage-group-members
// - https://cloud.google.com/identity/docs/how-to/query-memberships
func (ds *DirectoryService) ListGroupMembers(ctx context.Context, groupID string, queries ...GetGroupMembersOption) ([]*admin.Member, error) {
if groupID == "" {
return nil, ErrGroupIDNil
}
qs := getGroupMembersOptions{}
for _, q := range queries {
q(&qs)
}
m := make([]*admin.Member, 0)
mlc := ds.svc.Members.List(groupID)
if qs.includeDerivedMembership {
mlc = mlc.IncludeDerivedMembership(true)
}
if qs.maxResults > 0 {
mlc = mlc.MaxResults(qs.maxResults)
}
if qs.pageToken != "" {
mlc = mlc.PageToken(qs.pageToken)
}
if qs.roles != "" {
mlc = mlc.Roles(qs.roles)
}
err := mlc.Fields(membersRequiredFields).Pages(ctx, func(members *admin.Members) error {
for _, member := range members.Members {
// Add only active members to list
if member.Status == "ACTIVE" {
m = append(m, member)
} else {
slog.Warn("google: member not included in group because status is not ACTIVE", "email", member.Email, "status", member.Status, "groupID", groupID)
}
}
return nil
})
if err != nil {
return nil, err
}
slog.Debug("google: ListGroupMembers()", "members", m)
return m, nil
}
// GetUser return a user given a user ID.
// userID: the user's primary email address, alias email address, or unique user ID.
func (ds *DirectoryService) GetUser(ctx context.Context, userID string) (*admin.User, error) {
if userID == "" {
return nil, ErrUserIDNil
}
u, err := ds.svc.Users.Get(userID).Fields(getUsersRequiredFields).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("google: error getting user %s: %v", userID, err)
}
return u, nil
}
// GetGroup return a group given a group ID.
func (ds *DirectoryService) GetGroup(ctx context.Context, groupID string) (*admin.Group, error) {
if groupID == "" {
return nil, ErrGroupIDNil
}
g, err := ds.svc.Groups.Get(groupID).Fields(groupsRequiredFields).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("google: error getting group %s: %v", groupID, err)
}
slog.Debug("google: GetGroup()", "group", g)
return g, nil
}