Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ceaf11c
Add setting to OAuth handlers to override local 2FA settings
zeripath Aug 21, 2021
ddc1966
Fix regression from #16544
zeripath Aug 20, 2021
ca74f82
Add scopes settings
zeripath Aug 20, 2021
c282f85
fix trace logging in auth_openid
zeripath Aug 20, 2021
0d4874f
add required claim options
zeripath Aug 20, 2021
bd86307
Move UpdateExternalUser to externalaccount
zeripath Aug 20, 2021
0e27070
Allow OAuth2/OIDC to set Admin/Restricted status
zeripath Aug 20, 2021
b51c09c
Allow use of the same group claim name for the prohibit login value
zeripath Aug 21, 2021
11791e5
fixup! Move UpdateExternalUser to externalaccount
zeripath Aug 21, 2021
de07c25
Merge branch 'main' into oidc-claims
zeripath Aug 29, 2021
ea37fd0
Merge branch 'main' into oidc-claims
zeripath Aug 29, 2021
a730e6b
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Sep 25, 2021
4818a72
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Sep 27, 2021
8b8abaa
as per wxiaoguang
zeripath Sep 27, 2021
7a88d06
add label back in
zeripath Oct 14, 2021
1f0d1a0
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Oct 14, 2021
7cd84d7
adjust localisation
zeripath Oct 14, 2021
d119bc8
Merge branch 'main' into oidc-claims
6543 Oct 15, 2021
125747a
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Nov 20, 2021
a5d3887
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Nov 27, 2021
7545100
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Nov 28, 2021
47ae8f5
placate lint
zeripath Nov 28, 2021
3f4df7c
Merge branch 'main' into oidc-claims
lunny Dec 10, 2021
bc558ed
Merge branch 'main' into oidc-claims
lunny Dec 10, 2021
36da1a6
Merge branch 'main' into oidc-claims
lunny Dec 13, 2021
6041ea8
Merge branch 'main' into oidc-claims
lunny Dec 13, 2021
706532a
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Dec 13, 2021
33e3af2
Merge branch 'main' into oidc-claims
lunny Dec 14, 2021
323782f
Merge branch 'main' into oidc-claims
techknowlogick Dec 14, 2021
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
Prev Previous commit
Next Next commit
Add scopes settings
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Aug 21, 2021
commit ca74f82564b4a74f2b9a19f4d50a754a928de242
10 changes: 10 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ var (
Name: "override-local-2fa",
Usage: "Set to true to override local 2fa settings",
},
cli.StringSliceFlag{
Name: "scopes",
Value: nil,
Usage: "Scopes to request when to authenticate against this OAuth2 source",
},
}

microcmdAuthUpdateOauth = cli.Command{
Expand Down Expand Up @@ -621,6 +626,7 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source {
CustomURLMapping: customURLMapping,
IconURL: c.String("icon-url"),
OverrideLocalTwoFA: c.Bool("override-local-2fa"),
Scopes: c.StringSlice("scopes"),
}
}

Expand Down Expand Up @@ -677,6 +683,10 @@ func runUpdateOauth(c *cli.Context) error {
oAuth2Config.IconURL = c.String("icon-url")
}

if c.IsSet("scopes") {
oAuth2Config.Scopes = c.StringSlice("scopes")
}

// update custom URL mapping
var customURLMapping = &oauth2.CustomURLMapping{}

Expand Down
4 changes: 4 additions & 0 deletions docs/content/doc/usage/command-line.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Admin operations:
- `--custom-profile-url`: Use a custom Profile URL (option for GitLab/GitHub).
- `--custom-email-url`: Use a custom Email URL (option for GitHub).
- `--icon-url`: Custom icon URL for OAuth2 login source.
- `--override-local-2fa`: Allow source to override local 2fa. (Optional)
- `--scopes`: Addtional scopes to request for this OAuth2 source. (Optional)
- Examples:
- `gitea admin auth add-oauth --name external-github --provider github --key OBTAIN_FROM_SOURCE --secret OBTAIN_FROM_SOURCE`
- `update-oauth`:
Expand All @@ -133,6 +135,8 @@ Admin operations:
- `--custom-profile-url`: Use a custom Profile URL (option for GitLab/GitHub).
- `--custom-email-url`: Use a custom Email URL (option for GitHub).
- `--icon-url`: Custom icon URL for OAuth2 login source.
- `--override-local-2fa`: Allow source to override local 2fa. (Optional)
- `--scopes`: Addtional scopes to request for this OAuth2 source.
- Examples:
- `gitea admin auth update-oauth --id 1 --name external-github-updated`
- `add-ldap`: Add new LDAP (via Bind DN) authentication source
Expand Down
1 change: 1 addition & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ func NewFuncMap() []template.FuncMap {
"MermaidMaxSourceCharacters": func() int {
return setting.MermaidMaxSourceCharacters
},
"Join": strings.Join,
}}
}

Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,7 @@ auths.oauth2_emailURL = Email URL
auths.override_local_two_fa = Override local 2FA
auths.override_local_two_fa_helper = Leaving unset means local users with 2FA set will still have to pass 2FA to log on
auths.oauth2_tenant = Tenant
auths.oauth2_scopes = Additional Scopes
auths.enable_auto_register = Enable Auto Registration
auths.sspi_auto_create_users = Automatically create users
auths.sspi_auto_create_users_helper = Allow SSPI auth method to automatically create new accounts for users that login for the first time
Expand Down
4 changes: 3 additions & 1 deletion routers/web/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"regexp"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth/pam"
Expand Down Expand Up @@ -182,6 +183,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
CustomURLMapping: customURLMapping,
IconURL: form.Oauth2IconURL,
OverrideLocalTwoFA: form.OverrideLocalTwoFA,
Scopes: strings.Split(form.Oauth2Scopes, ","),
}
}

Expand Down Expand Up @@ -322,8 +324,8 @@ func EditAuthSource(ctx *context.Context) {
break
}
}

}

ctx.HTML(http.StatusOK, tplAuthEdit)
}

Expand Down
32 changes: 19 additions & 13 deletions services/auth/source/oauth2/providers_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// CustomProviderNewFn creates a goth.Provider using a custom url mapping
type CustomProviderNewFn func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error)
type CustomProviderNewFn func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error)

// CustomProvider is a GothProvider that has CustomURL features
type CustomProvider struct {
Expand All @@ -35,7 +35,7 @@ func (c *CustomProvider) CustomURLSettings() *CustomURLSettings {
func (c *CustomProvider) CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error) {
custom := c.customURLSettings.OverrideWith(source.CustomURLMapping)

return c.newFn(source.ClientID, source.ClientSecret, callbackURL, custom)
return c.newFn(source.ClientID, source.ClientSecret, callbackURL, custom, source.Scopes)
}

// NewCustomProvider is a constructor function for custom providers
Expand All @@ -60,8 +60,7 @@ func init() {
ProfileURL: availableAttribute(github.ProfileURL),
EmailURL: availableAttribute(github.EmailURL),
},
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
scopes := []string{}
func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
if setting.OAuth2Client.EnableAutoRegistration {
scopes = append(scopes, "user:email")
}
Expand All @@ -73,8 +72,9 @@ func init() {
AuthURL: availableAttribute(gitlab.AuthURL),
TokenURL: availableAttribute(gitlab.TokenURL),
ProfileURL: availableAttribute(gitlab.ProfileURL),
}, func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
return gitlab.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, "read_user"), nil
}, func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
scopes = append(scopes, "read_user")
return gitlab.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, scopes...), nil
}))

RegisterGothProvider(NewCustomProvider(
Expand All @@ -83,8 +83,8 @@ func init() {
AuthURL: requiredAttribute(gitea.AuthURL),
ProfileURL: requiredAttribute(gitea.ProfileURL),
},
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
return gitea.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL), nil
func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
return gitea.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, scopes...), nil
}))

RegisterGothProvider(NewCustomProvider(
Expand All @@ -93,25 +93,31 @@ func init() {
AuthURL: requiredAttribute(nextcloud.AuthURL),
ProfileURL: requiredAttribute(nextcloud.ProfileURL),
},
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
return nextcloud.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL), nil
func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
return nextcloud.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, scopes...), nil
}))

RegisterGothProvider(NewCustomProvider(
"mastodon", "Mastodon", &CustomURLSettings{
AuthURL: requiredAttribute(mastodon.InstanceURL),
},
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
return mastodon.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL), nil
func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
return mastodon.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, scopes...), nil
}))

RegisterGothProvider(NewCustomProvider(
"azureadv2", "Azure AD v2", &CustomURLSettings{
Tenant: requiredAttribute("organizations"),
},
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
azureScopes := make([]azureadv2.ScopeType, len(scopes))
for i, scope := range scopes {
azureScopes[i] = azureadv2.ScopeType(scope)
}

return azureadv2.New(clientID, secret, callbackURL, azureadv2.ProviderOptions{
Tenant: azureadv2.TenantType(custom.Tenant),
Scopes: azureScopes,
}), nil
},
))
Expand Down
7 changes: 6 additions & 1 deletion services/auth/source/oauth2/providers_openid.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func (o *OpenIDProvider) Image() string {

// CreateGothProvider creates a GothProvider from this Provider
func (o *OpenIDProvider) CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error) {
provider, err := openidConnect.New(source.ClientID, source.ClientSecret, callbackURL, source.OpenIDConnectAutoDiscoveryURL, setting.OAuth2Client.OpenIDConnectScopes...)
scopes := setting.OAuth2Client.OpenIDConnectScopes
if len(scopes) == 0 {
scopes = append(scopes, source.Scopes...)
}

provider, err := openidConnect.New(source.ClientID, source.ClientSecret, callbackURL, source.OpenIDConnectAutoDiscoveryURL, scopes...)
if err != nil {
log.Warn("Failed to create OpenID Connect Provider with name '%s' with url '%s': %v", providerName, source.OpenIDConnectAutoDiscoveryURL, err)
}
Expand Down
3 changes: 2 additions & 1 deletion services/auth/source/oauth2/providers_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type SimpleProvider struct {

// CreateGothProvider creates a GothProvider from this Provider
func (c *SimpleProvider) CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error) {
return c.newFn(source.ClientID, source.ClientSecret, callbackURL, c.scopes...), nil
scopes := append(c.scopes, source.Scopes...)
return c.newFn(source.ClientID, source.ClientSecret, callbackURL, scopes...), nil
}

// NewSimpleProvider is a constructor function for simple providers
Expand Down
2 changes: 2 additions & 0 deletions services/auth/source/oauth2/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Source struct {
IconURL string
OverrideLocalTwoFA bool

Scopes []string

// reference to the loginSource
loginSource *models.LoginSource
}
Expand Down
1 change: 1 addition & 0 deletions services/forms/auth_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type AuthenticationForm struct {
Oauth2IconURL string
Oauth2Tenant string
OverrideLocalTwoFA bool
Oauth2Scopes string
SSPIAutoCreateUsers bool
SSPIAutoActivateUsers bool
SSPIStripDomainNames bool
Expand Down
5 changes: 5 additions & 0 deletions templates/admin/auth/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@
<input id="{{.Name}}_email_url" value="{{.CustomURLSettings.EmailURL.Value}}" data-available="{{.CustomURLSettings.EmailURL.Available}}" data-required="{{.CustomURLSettings.EmailURL.Required}}" type="hidden" />
<input id="{{.Name}}_tenant" value="{{.CustomURLSettings.Tenant.Value}}" data-available="{{.CustomURLSettings.Tenant.Available}}" data-required="{{.CustomURLSettings.Tenant.Required}}" type="hidden" />
{{end}}{{end}}

<div class="field">
<label for="oauth2_scopes">{{.i18n.Tr "admin.auths.oauth2_scopes"}}</label>
<input id="oauth2_scopes" name="oauth2_scopes" value="{{if $cfg.Scopes}}{{Join $cfg.Scopes "," }}{{end}}">
</div>
{{end}}

<!-- SSPI -->
Expand Down
5 changes: 5 additions & 0 deletions templates/admin/auth/source/oauth.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@
<input id="{{.Name}}_email_url" value="{{.CustomURLSettings.EmailURL.Value}}" data-available="{{.CustomURLSettings.EmailURL.Available}}" data-required="{{.CustomURLSettings.EmailURL.Required}}" type="hidden" />
<input id="{{.Name}}_tenant" value="{{.CustomURLSettings.Tenant.Value}}" data-available="{{.CustomURLSettings.Tenant.Available}}" data-required="{{.CustomURLSettings.Tenant.Required}}" type="hidden" />
{{end}}{{end}}

<div class="field">
<label for="oauth2_scopes">{{.i18n.Tr "admin.auths.oauth2_scopes"}}</label>
<input id="oauth2_scopes" name="oauth2_scopes" values="{{.oauth2_scopes}}">
</div>
</div>