forked from openclaw/gogcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_common.go
More file actions
52 lines (46 loc) · 2.07 KB
/
admin_common.go
File metadata and controls
52 lines (46 loc) · 2.07 KB
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
package cmd
import (
"strings"
"github.com/steipete/gogcli/internal/errfmt"
"github.com/steipete/gogcli/internal/googleapi"
)
var newAdminDirectoryService = googleapi.NewAdminDirectory
const (
adminRoleMember = "MEMBER"
adminRoleOwner = "OWNER"
adminRoleManager = "MANAGER"
)
func requireAdminAccount(flags *RootFlags) (string, error) {
account, err := requireAccount(flags)
if err != nil {
return "", err
}
if isConsumerAccount(account) {
return "", errfmt.NewUserFacingError(
"Admin SDK Directory API requires a Google Workspace account with domain-wide delegation; consumer accounts (gmail.com/googlemail.com) are not supported.",
nil,
)
}
return account, nil
}
// wrapAdminDirectoryError provides helpful error messages for common Admin SDK issues.
func wrapAdminDirectoryError(err error, account string) error {
errStr := err.Error()
if strings.Contains(errStr, "accessNotConfigured") ||
strings.Contains(errStr, "Admin SDK API has not been used") {
return errfmt.NewUserFacingError("Admin SDK API is not enabled; enable it at: https://console.developers.google.com/apis/api/admin.googleapis.com/overview", err)
}
if strings.Contains(errStr, "insufficientPermissions") ||
strings.Contains(errStr, "insufficient authentication scopes") ||
strings.Contains(errStr, "Not Authorized") {
return errfmt.NewUserFacingError("Insufficient permissions for Admin SDK API; ensure your service account has domain-wide delegation enabled with admin.directory.user, admin.directory.group, and admin.directory.group.member scopes", err)
}
if strings.Contains(errStr, "domain_wide_delegation") ||
strings.Contains(errStr, "invalid_grant") {
return errfmt.NewUserFacingError("Domain-wide delegation not configured or invalid; ensure your service account has domain-wide delegation enabled in Google Workspace Admin Console", err)
}
if isConsumerAccount(account) {
return errfmt.NewUserFacingError("Admin SDK Directory API requires a Google Workspace account with domain-wide delegation; consumer accounts (gmail.com/googlemail.com) are not supported.", err)
}
return err
}