|
| 1 | +package gcputil |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "google.golang.org/api/option" |
| 12 | + htransport "google.golang.org/api/transport/http" |
| 13 | +) |
| 14 | + |
| 15 | +const identityPlatformBasePath = "https://identitytoolkit.googleapis.com/admin/v2/" |
| 16 | + |
| 17 | +// ProjectConfig represents the configuration of a project in Identity Platform |
| 18 | +type ProjectConfig struct { |
| 19 | + Name string `json:"name"` |
| 20 | + AutoDeleteAnonymousUsers bool `json:"autodeleteAnonymousUsers"` |
| 21 | + SignIn *SignInConfig `json:"signIn,omitempty"` |
| 22 | + MFA *MFAConfig `json:"mfa,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +type SignInConfig struct { |
| 26 | + Anonymous *ProviderConfig `json:"anonymous,omitempty"` |
| 27 | + Email *ProviderConfig `json:"email,omitempty"` |
| 28 | + Phone *ProviderConfig `json:"phoneNumber,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +type MFAConfig struct { |
| 32 | + State string `json:"state"` |
| 33 | +} |
| 34 | + |
| 35 | +type ProviderConfig struct { |
| 36 | + Enabled bool `json:"enabled"` |
| 37 | +} |
| 38 | + |
| 39 | +// IdentityPlatformService provides methods to interact with the Identity Platform API |
| 40 | +type IdentityPlatformService struct { |
| 41 | + client *http.Client |
| 42 | + BasePath string |
| 43 | + UserAgent string |
| 44 | +} |
| 45 | + |
| 46 | +// NewIdentityPlatformService creates a new Identity Platform client |
| 47 | +func NewIdentityPlatformService(ctx context.Context, opts ...option.ClientOption) (*IdentityPlatformService, error) { |
| 48 | + client, endpoint, err := htransport.NewClient(ctx, opts...) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + s := &IdentityPlatformService{client: client, BasePath: identityPlatformBasePath} |
| 54 | + if endpoint != "" { |
| 55 | + s.BasePath = endpoint |
| 56 | + } |
| 57 | + |
| 58 | + return s, nil |
| 59 | +} |
| 60 | + |
| 61 | +// GetProjectConfig retrieves the configuration of a project |
| 62 | +func (s *IdentityPlatformService) GetProjectConfig(ctx context.Context, projectID string) (*ProjectConfig, error) { |
| 63 | + url := fmt.Sprintf("%sprojects/%s/config", s.BasePath, projectID) |
| 64 | + logrus.Tracef("url: %s", url) |
| 65 | + |
| 66 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("error creating request: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + resp, err := s.client.Do(req) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("error requesting project config: %v", err) |
| 74 | + } |
| 75 | + defer resp.Body.Close() |
| 76 | + |
| 77 | + if resp.StatusCode != http.StatusOK { |
| 78 | + return nil, fmt.Errorf("API request error: status %d", resp.StatusCode) |
| 79 | + } |
| 80 | + |
| 81 | + var config ProjectConfig |
| 82 | + if err = json.NewDecoder(resp.Body).Decode(&config); err != nil { |
| 83 | + return nil, fmt.Errorf("error decoding response: %v", err) |
| 84 | + } |
| 85 | + return &config, nil |
| 86 | +} |
| 87 | + |
| 88 | +// UpdateProjectConfig updates the configuration of a project |
| 89 | +func (s *IdentityPlatformService) UpdateProjectConfig(ctx context.Context, projectID string, config *ProjectConfig) (*ProjectConfig, error) { |
| 90 | + url := fmt.Sprintf("%sprojects/%s/config", s.BasePath, projectID) |
| 91 | + logrus.Tracef("url: %s", url) |
| 92 | + |
| 93 | + body, err := json.Marshal(config) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("error marshaling request body: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewReader(body)) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("error creating request: %v", err) |
| 101 | + } |
| 102 | + req.Header.Set("Content-Type", "application/json") |
| 103 | + |
| 104 | + resp, err := s.client.Do(req) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("error updating project config: %v", err) |
| 107 | + } |
| 108 | + defer resp.Body.Close() |
| 109 | + |
| 110 | + if resp.StatusCode != http.StatusOK { |
| 111 | + return nil, fmt.Errorf("API request error: status %d", resp.StatusCode) |
| 112 | + } |
| 113 | + |
| 114 | + var updatedConfig ProjectConfig |
| 115 | + if err = json.NewDecoder(resp.Body).Decode(&updatedConfig); err != nil { |
| 116 | + return nil, fmt.Errorf("error decoding response: %v", err) |
| 117 | + } |
| 118 | + return &updatedConfig, nil |
| 119 | +} |
| 120 | + |
| 121 | +type ListDefaultSupportedOAuthIdpConfigsResponse struct { |
| 122 | + DefaultSupportedIdpConfigs []*DefaultSupportedOAuthIdpConfig `json:"defaultSupportedIdpConfigs"` |
| 123 | +} |
| 124 | + |
| 125 | +type DefaultSupportedOAuthIdpConfig struct { |
| 126 | + Name *string `json:"name,omitempty"` |
| 127 | + Enabled *bool `json:"enabled,omitempty"` |
| 128 | + ClientID *string `json:"clientId,omitempty"` |
| 129 | + ClientSecret *string `json:"clientSecret,omitempty"` |
| 130 | +} |
| 131 | + |
| 132 | +type ListOAuthIdpConfigsResponse struct { |
| 133 | + OAuthIdpConfigs []*OAuthIdpConfig `json:"oauthIdpConfigs"` |
| 134 | +} |
| 135 | + |
| 136 | +type OAuthIdpConfig struct { |
| 137 | + Name *string `json:"name"` |
| 138 | + ClientId *string `json:"clientId"` |
| 139 | + Issuer *string `json:"issuer"` |
| 140 | + DisplayName *string `json:"displayName"` |
| 141 | + Enabled *bool `json:"enabled"` |
| 142 | +} |
| 143 | + |
| 144 | +// ListDefaultSupportedOAuthIdpConfigs lists the OAuth IDP configurations of a project |
| 145 | +func (s *IdentityPlatformService) ListDefaultSupportedOAuthIdpConfigs(ctx context.Context, projectID string) (*ListDefaultSupportedOAuthIdpConfigsResponse, error) { |
| 146 | + url := fmt.Sprintf("%sprojects/%s/defaultSupportedIdpConfigs", s.BasePath, projectID) |
| 147 | + logrus.Tracef("url: %s", url) |
| 148 | + |
| 149 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 150 | + if err != nil { |
| 151 | + return nil, fmt.Errorf("error creating request: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + resp, err := s.client.Do(req) |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("error requesting OAuth IDP configs: %v", err) |
| 157 | + } |
| 158 | + defer resp.Body.Close() |
| 159 | + |
| 160 | + if resp.StatusCode != http.StatusOK { |
| 161 | + return nil, fmt.Errorf("API request error: status %d", resp.StatusCode) |
| 162 | + } |
| 163 | + |
| 164 | + var configs *ListDefaultSupportedOAuthIdpConfigsResponse |
| 165 | + if err = json.NewDecoder(resp.Body).Decode(&configs); err != nil { |
| 166 | + return nil, fmt.Errorf("error decoding response: %v", err) |
| 167 | + } |
| 168 | + return configs, nil |
| 169 | +} |
| 170 | + |
| 171 | +// DeleteDefaultSupportedOAuthIdpConfig deletes an OAuth IDP configuration of a project |
| 172 | +func (s *IdentityPlatformService) DeleteDefaultSupportedOAuthIdpConfig(ctx context.Context, projectID, configID string) error { |
| 173 | + url := fmt.Sprintf("%sprojects/%s/defaultSupportedIdpConfigs/%s", s.BasePath, projectID, configID) |
| 174 | + logrus.Tracef("url: %s", url) |
| 175 | + |
| 176 | + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil) |
| 177 | + if err != nil { |
| 178 | + return fmt.Errorf("error creating request: %v", err) |
| 179 | + } |
| 180 | + |
| 181 | + resp, err := s.client.Do(req) |
| 182 | + if err != nil { |
| 183 | + return fmt.Errorf("error deleting OAuth IDP config: %v", err) |
| 184 | + } |
| 185 | + defer resp.Body.Close() |
| 186 | + |
| 187 | + if resp.StatusCode != http.StatusOK { |
| 188 | + return fmt.Errorf("API request error: status %d", resp.StatusCode) |
| 189 | + } |
| 190 | + |
| 191 | + return nil |
| 192 | +} |
| 193 | + |
| 194 | +// ListOAuthIdpConfigs lists the OAuth IDP configurations of a project |
| 195 | +func (s *IdentityPlatformService) ListOAuthIdpConfigs(ctx context.Context, projectID string) (*ListOAuthIdpConfigsResponse, error) { |
| 196 | + url := fmt.Sprintf("%sprojects/%s/oauthIdpConfigs", s.BasePath, projectID) |
| 197 | + logrus.Tracef("url: %s", url) |
| 198 | + |
| 199 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 200 | + if err != nil { |
| 201 | + return nil, fmt.Errorf("error creating request: %v", err) |
| 202 | + } |
| 203 | + |
| 204 | + resp, err := s.client.Do(req) |
| 205 | + if err != nil { |
| 206 | + return nil, fmt.Errorf("error requesting OAuth IDP configs: %v", err) |
| 207 | + } |
| 208 | + defer resp.Body.Close() |
| 209 | + |
| 210 | + if resp.StatusCode != http.StatusOK { |
| 211 | + return nil, fmt.Errorf("API request error: status %d", resp.StatusCode) |
| 212 | + } |
| 213 | + |
| 214 | + var configs *ListOAuthIdpConfigsResponse |
| 215 | + if err = json.NewDecoder(resp.Body).Decode(&configs); err != nil { |
| 216 | + return nil, fmt.Errorf("error decoding response: %v", err) |
| 217 | + } |
| 218 | + return configs, nil |
| 219 | +} |
| 220 | + |
| 221 | +// DeleteOAuthIdpConfig deletes an OAuth IDP configuration of a project |
| 222 | +func (s *IdentityPlatformService) DeleteOAuthIdpConfig(ctx context.Context, projectID, configID string) error { |
| 223 | + url := fmt.Sprintf("%sprojects/%s/oauthIdpConfigs/%s", s.BasePath, projectID, configID) |
| 224 | + logrus.Tracef("url: %s", url) |
| 225 | + |
| 226 | + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil) |
| 227 | + if err != nil { |
| 228 | + return fmt.Errorf("error creating request: %v", err) |
| 229 | + } |
| 230 | + |
| 231 | + resp, err := s.client.Do(req) |
| 232 | + if err != nil { |
| 233 | + return fmt.Errorf("error deleting OAuth IDP config: %v", err) |
| 234 | + } |
| 235 | + defer resp.Body.Close() |
| 236 | + |
| 237 | + if resp.StatusCode != http.StatusOK { |
| 238 | + return fmt.Errorf("API request error: status %d", resp.StatusCode) |
| 239 | + } |
| 240 | + |
| 241 | + return nil |
| 242 | +} |
0 commit comments