|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 | "log/slog" |
| 7 | + "net/http" |
7 | 8 |
|
8 | 9 | "golang.org/x/oauth2/google" |
9 | 10 | admin "google.golang.org/api/admin/directory/v1" |
@@ -34,38 +35,79 @@ var ( |
34 | 35 | // ErrUserIDNil is returned when the user ID is nil. |
35 | 36 | ErrUserIDNil = fmt.Errorf("google: user id is required") |
36 | 37 |
|
| 38 | + // ErrUserEmailNil is returned when the user email is nil. |
| 39 | + ErrUserEmailNil = fmt.Errorf("google: user email is required") |
| 40 | + |
37 | 41 | // ErrGroupIDNil is returned when the group ID is nil. |
38 | 42 | ErrGroupIDNil = fmt.Errorf("google: group id is required") |
| 43 | + |
| 44 | + // ErrServiceAccountNil is returned when the service account credentials are nil. |
| 45 | + ErrServiceAccountNil = fmt.Errorf("google: service account credentials are required") |
| 46 | + |
| 47 | + // ErrUserAgentNil is returned when the user agent is nil. |
| 48 | + ErrUserAgentNil = fmt.Errorf("google: user agent is required") |
| 49 | + |
| 50 | + // ErrGoogleClientNil is returned when the google client is nil. |
| 51 | + ErrGoogleClientNil = fmt.Errorf("google: google client is required") |
39 | 52 | ) |
40 | 53 |
|
41 | 54 | // DirectoryService represent the Google Directory API client. |
42 | 55 | type DirectoryService struct { |
43 | 56 | svc *admin.Service |
44 | 57 | } |
45 | 58 |
|
| 59 | +type DirectoryServiceConfig struct { |
| 60 | + Client *http.Client |
| 61 | + UserEmail string |
| 62 | + ServiceAccount []byte |
| 63 | + Scopes []string |
| 64 | + UserAgent string |
| 65 | +} |
| 66 | + |
46 | 67 | // NewService create a Google Directory Service. |
47 | 68 | // References: |
48 | 69 | // - https://pkg.go.dev/google.golang.org/api/admin/directory/v1 |
49 | 70 | // Examples of scope: |
50 | 71 | // - "https://www.googleapis.com/auth/admin.directory.group.readonly" |
51 | 72 | // - "https://www.googleapis.com/auth/admin.directory.group.member.readonly" |
52 | 73 | // - "https://www.googleapis.com/auth/admin.directory.user.readonly" |
53 | | -func NewService(ctx context.Context, userEmail string, serviceAccount []byte, scope ...string) (*admin.Service, error) { |
54 | | - if len(scope) == 0 { |
| 74 | +func NewService(ctx context.Context, config DirectoryServiceConfig) (*admin.Service, error) { |
| 75 | + if config.Client == nil { |
| 76 | + return nil, ErrGoogleClientNil |
| 77 | + } |
| 78 | + |
| 79 | + if config.UserEmail == "" { |
| 80 | + return nil, ErrUserEmailNil |
| 81 | + } |
| 82 | + |
| 83 | + if config.ServiceAccount == nil { |
| 84 | + return nil, ErrServiceAccountNil |
| 85 | + } |
| 86 | + |
| 87 | + if len(config.Scopes) == 0 { |
55 | 88 | return nil, ErrGoogleClientScopeNil |
56 | 89 | } |
57 | 90 |
|
58 | | - creds, err := google.CredentialsFromJSONWithParams(ctx, serviceAccount, google.CredentialsParams{ |
59 | | - Scopes: scope, |
60 | | - Subject: userEmail, |
| 91 | + if config.UserAgent == "" { |
| 92 | + return nil, ErrUserAgentNil |
| 93 | + } |
| 94 | + |
| 95 | + creds, err := google.CredentialsFromJSONWithParams(ctx, config.ServiceAccount, google.CredentialsParams{ |
| 96 | + Scopes: config.Scopes, |
| 97 | + Subject: config.UserEmail, |
61 | 98 | }) |
62 | 99 | if err != nil { |
63 | | - return nil, fmt.Errorf("google: error getting config for Service Account: %v", err) |
| 100 | + return nil, fmt.Errorf("google: %v", err) |
64 | 101 | } |
65 | 102 |
|
66 | | - svc, err := admin.NewService(ctx, option.WithTokenSource(creds.TokenSource)) |
| 103 | + svc, err := admin.NewService( |
| 104 | + ctx, |
| 105 | + option.WithTokenSource(creds.TokenSource), |
| 106 | + option.WithUserAgent(config.UserAgent), |
| 107 | + option.WithHTTPClient(config.Client), |
| 108 | + ) |
67 | 109 | if err != nil { |
68 | | - return nil, fmt.Errorf("google: error creating service: %v", err) |
| 110 | + return nil, fmt.Errorf("google: %v", err) |
69 | 111 | } |
70 | 112 |
|
71 | 113 | return svc, nil |
|
0 commit comments