Skip to content

feat: set sane default for gitea external auth #12306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: external auth defaults for gitea
Add some sane defaults for gitea to make it easier to configure
  • Loading branch information
Emyrk committed Feb 26, 2024
commit 83f72546989d71633a4be3008548910ece76798e
34 changes: 34 additions & 0 deletions coderd/externalauth/externalauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ func applyDefaultsToConfig(config *codersdk.ExternalAuthConfig) {
case codersdk.EnhancedExternalAuthProviderJFrog:
copyDefaultSettings(config, jfrogArtifactoryDefaults(config))
return
case codersdk.EnhancedExternalAuthProviderGitea:
copyDefaultSettings(config, giteaDefaults(config))
return
default:
// No defaults for this type. We still want to run this apply with
// an empty set of defaults.
Expand Down Expand Up @@ -696,6 +699,37 @@ func jfrogArtifactoryDefaults(config *codersdk.ExternalAuthConfig) codersdk.Exte
return defaults
}

func giteaDefaults(config *codersdk.ExternalAuthConfig) codersdk.ExternalAuthConfig {
defaults := codersdk.ExternalAuthConfig{
DisplayName: "Gitea",
Scopes: []string{"read:repository", " write:repository", "read:user"},
DisplayIcon: "/icon/gitea.svg",
}
// Gitea's servers will have some base url, e.g: https://gitea.coder.com.
// If an auth url is not set, we will assume they are using the default
// public Gitea.
if config.AuthURL == "" {
config.AuthURL = "https://gitea.com/login/oauth/authorize"
}

auth, err := url.Parse(config.AuthURL)
if err != nil {
// We need a valid URL to continue with.
return defaults
}

// Default regex should be anything using the same host as the auth url.
defaults.Regex = fmt.Sprintf(`^(https?://)?%s(/.*)?$`, strings.ReplaceAll(auth.Host, ".", `\.`))

tokenURL := auth.ResolveReference(&url.URL{Path: "/login/oauth/access_token"})
defaults.TokenURL = tokenURL.String()

validate := auth.ResolveReference(&url.URL{Path: "/login/oauth/userinfo"})
defaults.ValidateURL = validate.String()

return defaults
}

var staticDefaults = map[codersdk.EnhancedExternalAuthProvider]codersdk.ExternalAuthConfig{
codersdk.EnhancedExternalAuthProviderAzureDevops: {
AuthURL: "https://app.vssps.visualstudio.com/oauth2/authorize",
Expand Down
4 changes: 3 additions & 1 deletion codersdk/externalauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (e EnhancedExternalAuthProvider) Git() bool {
EnhancedExternalAuthProviderGitLab,
EnhancedExternalAuthProviderBitBucketCloud,
EnhancedExternalAuthProviderBitBucketServer,
EnhancedExternalAuthProviderAzureDevops:
EnhancedExternalAuthProviderAzureDevops,
EnhancedExternalAuthProviderGitea:
return true
default:
return false
Expand All @@ -41,6 +42,7 @@ const (
EnhancedExternalAuthProviderBitBucketServer EnhancedExternalAuthProvider = "bitbucket-server"
EnhancedExternalAuthProviderSlack EnhancedExternalAuthProvider = "slack"
EnhancedExternalAuthProviderJFrog EnhancedExternalAuthProvider = "jfrog"
EnhancedExternalAuthProviderGitea EnhancedExternalAuthProvider = "gitea"
)

type ExternalAuth struct {
Expand Down
11 changes: 11 additions & 0 deletions docs/admin/external-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ CODER_EXTERNAL_AUTH_0_TOKEN_URL="https://gitlab.company.org/oauth/token"
CODER_EXTERNAL_AUTH_0_REGEX=gitlab\.company\.org
```

### Gitea

```env
CODER_EXTERNAL_AUTH_0_ID="gitea"
CODER_EXTERNAL_AUTH_0_TYPE=gitea
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
# If self managed, set the Auth URL to your Gitea instance
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://gitea.com/login/oauth/authorize"
```

### Self-managed git providers

Custom authentication and token URLs should be used for self-managed Git
Expand Down
2 changes: 2 additions & 0 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions site/static/icon/gitea.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.