Skip to content

Commit 6b866b3

Browse files
authored
feat: set sane default for gitea external auth (#12306)
* feat: external auth defaults for gitea Add some sane defaults for gitea to make it easier to configure
1 parent 70ccefc commit 6b866b3

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-1
lines changed

coderd/externalauth/externalauth.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ func applyDefaultsToConfig(config *codersdk.ExternalAuthConfig) {
566566
case codersdk.EnhancedExternalAuthProviderJFrog:
567567
copyDefaultSettings(config, jfrogArtifactoryDefaults(config))
568568
return
569+
case codersdk.EnhancedExternalAuthProviderGitea:
570+
copyDefaultSettings(config, giteaDefaults(config))
571+
return
569572
default:
570573
// No defaults for this type. We still want to run this apply with
571574
// an empty set of defaults.
@@ -696,6 +699,37 @@ func jfrogArtifactoryDefaults(config *codersdk.ExternalAuthConfig) codersdk.Exte
696699
return defaults
697700
}
698701

702+
func giteaDefaults(config *codersdk.ExternalAuthConfig) codersdk.ExternalAuthConfig {
703+
defaults := codersdk.ExternalAuthConfig{
704+
DisplayName: "Gitea",
705+
Scopes: []string{"read:repository", " write:repository", "read:user"},
706+
DisplayIcon: "/icon/gitea.svg",
707+
}
708+
// Gitea's servers will have some base url, e.g: https://gitea.coder.com.
709+
// If an auth url is not set, we will assume they are using the default
710+
// public Gitea.
711+
if config.AuthURL == "" {
712+
config.AuthURL = "https://gitea.com/login/oauth/authorize"
713+
}
714+
715+
auth, err := url.Parse(config.AuthURL)
716+
if err != nil {
717+
// We need a valid URL to continue with.
718+
return defaults
719+
}
720+
721+
// Default regex should be anything using the same host as the auth url.
722+
defaults.Regex = fmt.Sprintf(`^(https?://)?%s(/.*)?$`, strings.ReplaceAll(auth.Host, ".", `\.`))
723+
724+
tokenURL := auth.ResolveReference(&url.URL{Path: "/login/oauth/access_token"})
725+
defaults.TokenURL = tokenURL.String()
726+
727+
validate := auth.ResolveReference(&url.URL{Path: "/login/oauth/userinfo"})
728+
defaults.ValidateURL = validate.String()
729+
730+
return defaults
731+
}
732+
699733
var staticDefaults = map[codersdk.EnhancedExternalAuthProvider]codersdk.ExternalAuthConfig{
700734
codersdk.EnhancedExternalAuthProviderAzureDevops: {
701735
AuthURL: "https://app.vssps.visualstudio.com/oauth2/authorize",

codersdk/externalauth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func (e EnhancedExternalAuthProvider) Git() bool {
2424
EnhancedExternalAuthProviderGitLab,
2525
EnhancedExternalAuthProviderBitBucketCloud,
2626
EnhancedExternalAuthProviderBitBucketServer,
27-
EnhancedExternalAuthProviderAzureDevops:
27+
EnhancedExternalAuthProviderAzureDevops,
28+
EnhancedExternalAuthProviderGitea:
2829
return true
2930
default:
3031
return false
@@ -41,6 +42,7 @@ const (
4142
EnhancedExternalAuthProviderBitBucketServer EnhancedExternalAuthProvider = "bitbucket-server"
4243
EnhancedExternalAuthProviderSlack EnhancedExternalAuthProvider = "slack"
4344
EnhancedExternalAuthProviderJFrog EnhancedExternalAuthProvider = "jfrog"
45+
EnhancedExternalAuthProviderGitea EnhancedExternalAuthProvider = "gitea"
4446
)
4547

4648
type ExternalAuth struct {

docs/admin/external-auth.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ CODER_EXTERNAL_AUTH_0_TOKEN_URL="https://gitlab.company.org/oauth/token"
124124
CODER_EXTERNAL_AUTH_0_REGEX=gitlab\.company\.org
125125
```
126126

127+
### Gitea
128+
129+
```env
130+
CODER_EXTERNAL_AUTH_0_ID="gitea"
131+
CODER_EXTERNAL_AUTH_0_TYPE=gitea
132+
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxxx
133+
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
134+
# If self managed, set the Auth URL to your Gitea instance
135+
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://gitea.com/login/oauth/authorize"
136+
```
137+
127138
### Self-managed git providers
128139

129140
Custom authentication and token URLs should be used for self-managed Git

site/src/api/typesGenerated.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/theme/icons.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"gateway.svg",
3636
"gcp.png",
3737
"git.svg",
38+
"gitea.svg",
3839
"github.svg",
3940
"gitlab.svg",
4041
"go.svg",

site/static/icon/gitea.svg

Lines changed: 12 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)