Skip to content

Commit 7d1246e

Browse files
committed
fix: Allow custom Git OAuth URLs
Fixes an issue reported in Discord where custom endpoints weren't working.
1 parent b217f2c commit 7d1246e

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

cli/deployment/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,20 @@ func readSliceFromViper[T any](vip *viper.Viper, key string, value any) []T {
462462
if prop == "-" {
463463
prop = fve.Tag.Get("yaml")
464464
}
465-
value := vip.Get(fmt.Sprintf("%s.%d.%s", key, entry, prop))
465+
configKey := fmt.Sprintf("%s.%d.%s", key, entry, prop)
466+
value := vip.Get(configKey)
466467
if value == nil {
467468
continue
468469
}
469470
if instance == nil {
470471
newType := reflect.Indirect(reflect.New(elementType))
471472
instance = &newType
472473
}
474+
switch instance.Field(i).Type().String() {
475+
case "[]string":
476+
value = vip.GetStringSlice(configKey)
477+
default:
478+
}
473479
instance.Field(i).Set(reflect.ValueOf(value))
474480
}
475481
if instance == nil {

cli/deployment/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func TestConfig(t *testing.T) {
158158
"CODER_GITAUTH_0_AUTH_URL": "https://auth.com",
159159
"CODER_GITAUTH_0_TOKEN_URL": "https://token.com",
160160
"CODER_GITAUTH_0_REGEX": "github.com",
161+
"CODER_GITAUTH_0_SCOPES": "read write",
161162

162163
"CODER_GITAUTH_1_ID": "another",
163164
"CODER_GITAUTH_1_TYPE": "gitlab",
@@ -177,6 +178,7 @@ func TestConfig(t *testing.T) {
177178
AuthURL: "https://auth.com",
178179
TokenURL: "https://token.com",
179180
Regex: "github.com",
181+
Scopes: []string{"read", "write"},
180182
}, {
181183
ID: "another",
182184
Type: "gitlab",

coderd/gitauth/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ func ConvertConfig(entries []codersdk.GitAuthConfig, accessURL *url.URL) ([]*Con
8686
Scopes: scope[typ],
8787
}
8888

89+
if entry.AuthURL != "" {
90+
oauth2Config.Endpoint.AuthURL = entry.AuthURL
91+
}
92+
if entry.TokenURL != "" {
93+
oauth2Config.Endpoint.TokenURL = entry.TokenURL
94+
}
95+
if entry.Scopes != nil && len(entry.Scopes) > 0 {
96+
oauth2Config.Scopes = entry.Scopes
97+
}
98+
8999
var oauthConfig httpmw.OAuth2Config = oauth2Config
90100
// Azure DevOps uses JWT token authentication!
91101
if typ == codersdk.GitProviderAzureDevops {

coderd/gitauth/config_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,18 @@ func TestConvertYAML(t *testing.T) {
7575
require.Equal(t, tc.Output, output)
7676
})
7777
}
78+
79+
t.Run("CustomScopesAndEndpoint", func(t *testing.T) {
80+
t.Parallel()
81+
config, err := gitauth.ConvertConfig([]codersdk.GitAuthConfig{{
82+
Type: codersdk.GitProviderGitLab,
83+
ClientID: "id",
84+
ClientSecret: "secret",
85+
AuthURL: "https://auth.com",
86+
TokenURL: "https://token.com",
87+
Scopes: []string{"read"},
88+
}}, &url.URL{})
89+
require.NoError(t, err)
90+
require.Equal(t, "https://auth.com?client_id=id&redirect_uri=%2Fgitauth%2Fgitlab%2Fcallback&response_type=code&scope=read", config[0].AuthCodeURL(""))
91+
})
7892
}

codersdk/deploymentconfig.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ type TLSConfig struct {
108108
}
109109

110110
type GitAuthConfig struct {
111-
ID string `json:"id"`
112-
Type string `json:"type"`
113-
ClientID string `json:"client_id"`
114-
ClientSecret string `json:"-" yaml:"client_secret"`
115-
AuthURL string `json:"auth_url"`
116-
TokenURL string `json:"token_url"`
117-
Regex string `json:"regex"`
111+
ID string `json:"id"`
112+
Type string `json:"type"`
113+
ClientID string `json:"client_id"`
114+
ClientSecret string `json:"-" yaml:"client_secret"`
115+
AuthURL string `json:"auth_url"`
116+
TokenURL string `json:"token_url"`
117+
Regex string `json:"regex"`
118+
Scopes []string `json:"scopes"`
118119
}
119120

120121
type Flaggable interface {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy