AppInstallations returns a list of app installations for the given token. If the provider does not support app installations, it returns nil.
(ctx context.Context, token string)
| 532 | // AppInstallations returns a list of app installations for the given token. |
| 533 | // If the provider does not support app installations, it returns nil. |
| 534 | func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk.ExternalAuthAppInstallation, bool, error) { |
| 535 | if c.AppInstallationsURL == "" { |
| 536 | return nil, false, nil |
| 537 | } |
| 538 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.AppInstallationsURL, nil) |
| 539 | if err != nil { |
| 540 | return nil, false, err |
| 541 | } |
| 542 | req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 543 | res, err := c.InstrumentedOAuth2Config.Do(ctx, promoauth.SourceAppInstallations, req) |
| 544 | if err != nil { |
| 545 | return nil, false, err |
| 546 | } |
| 547 | defer res.Body.Close() |
| 548 | // It's possible the installation URL is misconfigured, so we don't |
| 549 | // want to return an error here. |
| 550 | if res.StatusCode != http.StatusOK { |
| 551 | return nil, false, nil |
| 552 | } |
| 553 | installs := []codersdk.ExternalAuthAppInstallation{} |
| 554 | if c.Type == string(codersdk.EnhancedExternalAuthProviderGitHub) { |
| 555 | var ghInstalls struct { |
| 556 | Installations []*github.Installation `json:"installations"` |
| 557 | } |
| 558 | err = json.NewDecoder(res.Body).Decode(&ghInstalls) |
| 559 | if err != nil { |
| 560 | return nil, false, err |
| 561 | } |
| 562 | for _, installation := range ghInstalls.Installations { |
| 563 | account := installation.GetAccount() |
| 564 | if account == nil { |
| 565 | continue |
| 566 | } |
| 567 | installs = append(installs, codersdk.ExternalAuthAppInstallation{ |
| 568 | ID: int(installation.GetID()), |
| 569 | ConfigureURL: installation.GetHTMLURL(), |
| 570 | Account: codersdk.ExternalAuthUser{ |
| 571 | ID: account.GetID(), |
| 572 | Login: account.GetLogin(), |
| 573 | AvatarURL: account.GetAvatarURL(), |
| 574 | ProfileURL: account.GetHTMLURL(), |
| 575 | Name: account.GetName(), |
| 576 | }, |
| 577 | }) |
| 578 | } |
| 579 | } |
| 580 | return installs, true, nil |
| 581 | } |
| 582 | |
| 583 | func (c *Config) RevokeToken(ctx context.Context, link database.ExternalAuthLink) (bool, error) { |
| 584 | if c.RevokeURL == "" { |
no test coverage detected