|
9 | 9 | "net/http"
|
10 | 10 | "net/url"
|
11 | 11 | "regexp"
|
| 12 | + "strings" |
12 | 13 | "time"
|
13 | 14 |
|
14 | 15 | "golang.org/x/oauth2"
|
@@ -409,7 +410,7 @@ func ConvertConfig(entries []codersdk.ExternalAuthConfig, accessURL *url.URL) ([
|
409 | 410 | // Applies defaults to the config entry.
|
410 | 411 | // This allows users to very simply state that they type is "GitHub",
|
411 | 412 | // apply their client secret and ID, and have the UI appear nicely.
|
412 |
| - applyDefaultsToConfig(&entry) |
| 413 | + configDefaults(&entry) |
413 | 414 |
|
414 | 415 | valid := httpapi.NameValid(entry.ID)
|
415 | 416 | if valid != nil {
|
@@ -490,8 +491,22 @@ func ConvertConfig(entries []codersdk.ExternalAuthConfig, accessURL *url.URL) ([
|
490 | 491 | }
|
491 | 492 |
|
492 | 493 | // applyDefaultsToConfig applies defaults to the config entry.
|
493 |
| -func applyDefaultsToConfig(config *codersdk.ExternalAuthConfig) { |
494 |
| - defaults := defaults[codersdk.EnhancedExternalAuthProvider(config.Type)] |
| 494 | +func configDefaults(config *codersdk.ExternalAuthConfig) { |
| 495 | + // If static defaults exist, apply them. |
| 496 | + if defaults, ok := staticDefaults[codersdk.EnhancedExternalAuthProvider(config.Type)]; ok { |
| 497 | + applyDefaultsToConfig(config, defaults) |
| 498 | + return |
| 499 | + } |
| 500 | + |
| 501 | + // Dynamic defaults |
| 502 | + switch codersdk.EnhancedExternalAuthProvider(config.Type) { |
| 503 | + case codersdk.EnhancedExternalAuthProviderBitBucketServer: |
| 504 | + applyDefaultsToConfig(config, bitbucketServerDefaults(config)) |
| 505 | + return |
| 506 | + } |
| 507 | +} |
| 508 | + |
| 509 | +func applyDefaultsToConfig(config *codersdk.ExternalAuthConfig, defaults codersdk.ExternalAuthConfig) { |
495 | 510 | if config.AuthURL == "" {
|
496 | 511 | config.AuthURL = defaults.AuthURL
|
497 | 512 | }
|
@@ -539,7 +554,43 @@ func applyDefaultsToConfig(config *codersdk.ExternalAuthConfig) {
|
539 | 554 | }
|
540 | 555 | }
|
541 | 556 |
|
542 |
| -var defaults = map[codersdk.EnhancedExternalAuthProvider]codersdk.ExternalAuthConfig{ |
| 557 | +func bitbucketServerDefaults(config *codersdk.ExternalAuthConfig) codersdk.ExternalAuthConfig { |
| 558 | + defaults := codersdk.ExternalAuthConfig{ |
| 559 | + DisplayName: "Bitbucket Server", |
| 560 | + Scopes: []string{"PUBLIC_REPOS", "REPO_READ", "REPO_WRITE"}, |
| 561 | + DisplayIcon: "/icon/bitbucket.svg", |
| 562 | + } |
| 563 | + // Bitbucket servers will have some base url, e.g. https://bitbucket.coder.com. |
| 564 | + // We will grab this from the Auth URL. This choice is a bit arbitrary, |
| 565 | + // but we need to require at least 1 field to be populated. |
| 566 | + if config.AuthURL == "" { |
| 567 | + // No auth url, means we cannot guess the urls. |
| 568 | + return defaults |
| 569 | + } |
| 570 | + |
| 571 | + auth, err := url.Parse(config.AuthURL) |
| 572 | + if err != nil { |
| 573 | + // We need a valid URL to continue with. |
| 574 | + return defaults |
| 575 | + } |
| 576 | + |
| 577 | + // Populate Regex, ValidateURL, and TokenURL. |
| 578 | + // Default regex should be anything using the same host as the auth url. |
| 579 | + defaults.Regex = fmt.Sprintf(`^(https?://)?%s(/.*)?$`, strings.ReplaceAll(auth.Host, ".", `\.`)) |
| 580 | + |
| 581 | + tokenURL := auth.ResolveReference(&url.URL{Path: "/rest/oauth2/latest/token"}) |
| 582 | + defaults.TokenURL = tokenURL.String() |
| 583 | + |
| 584 | + // validate needs to return a 200 when logged in and a 401 when unauthenticated. |
| 585 | + // This endpoint returns the count of the number of PR's in the authenticated |
| 586 | + // user's inbox. Which will work perfectly for our use case. |
| 587 | + validate := auth.ResolveReference(&url.URL{Path: "/rest/api/latest/inbox/pull-requests/count"}) |
| 588 | + defaults.ValidateURL = validate.String() |
| 589 | + |
| 590 | + return defaults |
| 591 | +} |
| 592 | + |
| 593 | +var staticDefaults = map[codersdk.EnhancedExternalAuthProvider]codersdk.ExternalAuthConfig{ |
543 | 594 | codersdk.EnhancedExternalAuthProviderAzureDevops: {
|
544 | 595 | AuthURL: "https://app.vssps.visualstudio.com/oauth2/authorize",
|
545 | 596 | TokenURL: "https://app.vssps.visualstudio.com/oauth2/token",
|
|
0 commit comments