9
9
"net/http"
10
10
"net/url"
11
11
"regexp"
12
+ "strings"
12
13
"time"
13
14
14
15
"golang.org/x/oauth2"
@@ -494,7 +495,36 @@ func ConvertConfig(entries []codersdk.ExternalAuthConfig, accessURL *url.URL) ([
494
495
495
496
// applyDefaultsToConfig applies defaults to the config entry.
496
497
func applyDefaultsToConfig (config * codersdk.ExternalAuthConfig ) {
497
- defaults := defaults [codersdk .EnhancedExternalAuthProvider (config .Type )]
498
+ configType := codersdk .EnhancedExternalAuthProvider (config .Type )
499
+ if configType == "bitbucket" {
500
+ // For backwards compatibility, we need to support the "bitbucket" string.
501
+ configType = codersdk .EnhancedExternalAuthProviderBitBucketCloud
502
+ defer func () {
503
+ // The config type determines the config ID (if unset). So change the legacy
504
+ // type to the correct new type after the defaults have been configured.
505
+ config .Type = string (codersdk .EnhancedExternalAuthProviderBitBucketCloud )
506
+ }()
507
+ }
508
+ // If static defaults exist, apply them.
509
+ if defaults , ok := staticDefaults [configType ]; ok {
510
+ copyDefaultSettings (config , defaults )
511
+ return
512
+ }
513
+
514
+ // Dynamic defaults
515
+ switch codersdk .EnhancedExternalAuthProvider (config .Type ) {
516
+ case codersdk .EnhancedExternalAuthProviderBitBucketServer :
517
+ copyDefaultSettings (config , bitbucketServerDefaults (config ))
518
+ return
519
+ default :
520
+ // No defaults for this type. We still want to run this apply with
521
+ // an empty set of defaults.
522
+ copyDefaultSettings (config , codersdk.ExternalAuthConfig {})
523
+ return
524
+ }
525
+ }
526
+
527
+ func copyDefaultSettings (config * codersdk.ExternalAuthConfig , defaults codersdk.ExternalAuthConfig ) {
498
528
if config .AuthURL == "" {
499
529
config .AuthURL = defaults .AuthURL
500
530
}
@@ -542,7 +572,43 @@ func applyDefaultsToConfig(config *codersdk.ExternalAuthConfig) {
542
572
}
543
573
}
544
574
545
- var defaults = map [codersdk.EnhancedExternalAuthProvider ]codersdk.ExternalAuthConfig {
575
+ func bitbucketServerDefaults (config * codersdk.ExternalAuthConfig ) codersdk.ExternalAuthConfig {
576
+ defaults := codersdk.ExternalAuthConfig {
577
+ DisplayName : "Bitbucket Server" ,
578
+ Scopes : []string {"PUBLIC_REPOS" , "REPO_READ" , "REPO_WRITE" },
579
+ DisplayIcon : "/icon/bitbucket.svg" ,
580
+ }
581
+ // Bitbucket servers will have some base url, e.g. https://bitbucket.coder.com.
582
+ // We will grab this from the Auth URL. This choice is a bit arbitrary,
583
+ // but we need to require at least 1 field to be populated.
584
+ if config .AuthURL == "" {
585
+ // No auth url, means we cannot guess the urls.
586
+ return defaults
587
+ }
588
+
589
+ auth , err := url .Parse (config .AuthURL )
590
+ if err != nil {
591
+ // We need a valid URL to continue with.
592
+ return defaults
593
+ }
594
+
595
+ // Populate Regex, ValidateURL, and TokenURL.
596
+ // Default regex should be anything using the same host as the auth url.
597
+ defaults .Regex = fmt .Sprintf (`^(https?://)?%s(/.*)?$` , strings .ReplaceAll (auth .Host , "." , `\.` ))
598
+
599
+ tokenURL := auth .ResolveReference (& url.URL {Path : "/rest/oauth2/latest/token" })
600
+ defaults .TokenURL = tokenURL .String ()
601
+
602
+ // validate needs to return a 200 when logged in and a 401 when unauthenticated.
603
+ // This endpoint returns the count of the number of PR's in the authenticated
604
+ // user's inbox. Which will work perfectly for our use case.
605
+ validate := auth .ResolveReference (& url.URL {Path : "/rest/api/latest/inbox/pull-requests/count" })
606
+ defaults .ValidateURL = validate .String ()
607
+
608
+ return defaults
609
+ }
610
+
611
+ var staticDefaults = map [codersdk.EnhancedExternalAuthProvider ]codersdk.ExternalAuthConfig {
546
612
codersdk .EnhancedExternalAuthProviderAzureDevops : {
547
613
AuthURL : "https://app.vssps.visualstudio.com/oauth2/authorize" ,
548
614
TokenURL : "https://app.vssps.visualstudio.com/oauth2/token" ,
@@ -551,7 +617,7 @@ var defaults = map[codersdk.EnhancedExternalAuthProvider]codersdk.ExternalAuthCo
551
617
Regex : `^(https?://)?dev\.azure\.com(/.*)?$` ,
552
618
Scopes : []string {"vso.code_write" },
553
619
},
554
- codersdk .EnhancedExternalAuthProviderBitBucket : {
620
+ codersdk .EnhancedExternalAuthProviderBitBucketCloud : {
555
621
AuthURL : "https://bitbucket.org/site/oauth2/authorize" ,
556
622
TokenURL : "https://bitbucket.org/site/oauth2/access_token" ,
557
623
ValidateURL : "https://api.bitbucket.org/2.0/user" ,
0 commit comments