|
| 1 | +package clientcredentials |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "golang.org/x/oauth2" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "go.mongodb.org/atlas-sdk/v20241023002/auth" |
| 14 | + "go.mongodb.org/atlas-sdk/v20241023002/internal/core" |
| 15 | + "golang.org/x/oauth2/clientcredentials" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // TokenAPIPath for getting OAuth Access Token from server |
| 20 | + TokenAPIPath = "/api/oauth/token" //nolint:gosec //url only |
| 21 | + // serverTokenURL for Token Atlas API |
| 22 | + serverTokenURL = core.DefaultCloudURL + TokenAPIPath |
| 23 | + // RevokeAPIPath for revoking OAuth Access Token from server |
| 24 | + RevokeAPIPath = "/api/oauth/revoke" |
| 25 | + |
| 26 | + // serverRevokeURL for Revoke Atlas API |
| 27 | + serverRevokeURL = core.DefaultCloudURL + RevokeAPIPath |
| 28 | + userAgent = "User-Agent" |
| 29 | +) |
| 30 | + |
| 31 | +func NewConfig(clientID, clientSecret string) *Config { |
| 32 | + c := &Config{} |
| 33 | + c.ClientID = clientID |
| 34 | + c.ClientSecret = clientSecret |
| 35 | + c.RevokeURL = serverRevokeURL |
| 36 | + c.TokenURL = serverTokenURL |
| 37 | + c.AuthStyle = oauth2.AuthStyleInHeader |
| 38 | + c.userAgent = core.DefaultUserAgent |
| 39 | + |
| 40 | + return c |
| 41 | +} |
| 42 | + |
| 43 | +// Config describes a 2-legged OAuth2 flow, with both the |
| 44 | +// client application information and the server's endpoint URLs. |
| 45 | +// |
| 46 | +// NOTE: Config values are used only internally |
| 47 | +// and should not be overridden by clients |
| 48 | +type Config struct { |
| 49 | + clientcredentials.Config |
| 50 | + RevokeURL string |
| 51 | + userAgent string |
| 52 | +} |
| 53 | + |
| 54 | +func (c *Config) Client(ctx context.Context) *http.Client { |
| 55 | + client := c.Config.Client(ctx) |
| 56 | + client.Transport = &Transport{ |
| 57 | + Base: client.Transport, |
| 58 | + UserAgent: core.DefaultUserAgent, |
| 59 | + } |
| 60 | + return client |
| 61 | +} |
| 62 | + |
| 63 | +// RevokeToken revokes OAuth Token |
| 64 | +func (c *Config) RevokeToken(ctx context.Context, t *auth.Token) error { |
| 65 | + if c.RevokeURL == "" { |
| 66 | + return errors.New("endpoint missing RevokeURL") |
| 67 | + } |
| 68 | + if !t.Valid() { |
| 69 | + return nil // nothing to do |
| 70 | + } |
| 71 | + v := url.Values{ |
| 72 | + "token": {t.AccessToken}, |
| 73 | + "token_type_hint": {"access_token"}, |
| 74 | + } |
| 75 | + |
| 76 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.RevokeURL, strings.NewReader(v.Encode())) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + req.SetBasicAuth(url.QueryEscape(c.ClientID), url.QueryEscape(c.ClientSecret)) |
| 81 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 82 | + req.Header.Set(userAgent, c.userAgent) |
| 83 | + |
| 84 | + client := http.DefaultClient |
| 85 | + resp, err := client.Do(req) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + defer resp.Body.Close() |
| 90 | + |
| 91 | + if resp.StatusCode != http.StatusOK { |
| 92 | + if resp.StatusCode == http.StatusTooManyRequests { |
| 93 | + msg, _ := io.ReadAll(resp.Body) |
| 94 | + formattedMessage := fmt.Sprintf("%s %s: HTTP %v Detail: %v Reason: %v", |
| 95 | + http.MethodPost, c.RevokeURL, resp.StatusCode, |
| 96 | + "Token Revocation request was rate limited", string(msg)) |
| 97 | + return errors.New(formattedMessage) |
| 98 | + } |
| 99 | + formattedMessage := fmt.Sprintf("%s %s: HTTP %v Detail: %v Reason: %v", |
| 100 | + http.MethodPost, c.RevokeURL, resp.StatusCode, |
| 101 | + "Failed to revoke Access Token when fetching new OAuth Token from remote server", |
| 102 | + resp.Header.Get("www-authenticate")) |
| 103 | + return errors.New(formattedMessage) |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
0 commit comments