Skip to content
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

Add Option to synchronize Admin & Restricted states from OIDC/OAuth2 along with Setting Scopes #16766

Merged
merged 29 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
58 changes: 58 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,36 @@ var (
Name: "skip-local-2fa",
Usage: "Set to true to skip local 2fa for users authenticated by this source",
},
cli.StringSliceFlag{
Name: "scopes",
Value: nil,
Usage: "Scopes to request when to authenticate against this OAuth2 source",
},
cli.StringFlag{
Name: "required-claim-name",
Value: "",
Usage: "Claim name that has to be set to allow users to login with this source",
},
cli.StringFlag{
Name: "required-claim-value",
Value: "",
Usage: "Claim value that has to be set to allow users to login with this source",
},
cli.StringFlag{
Name: "group-claim-name",
Value: "",
Usage: "Claim name providing group names for this source",
},
cli.StringFlag{
Name: "admin-group",
Value: "",
Usage: "Group Claim value for administrator users",
},
cli.StringFlag{
Name: "restricted-group",
Value: "",
Usage: "Group Claim value for restricted users",
},
}

microcmdAuthUpdateOauth = cli.Command{
Expand Down Expand Up @@ -624,6 +654,12 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source {
CustomURLMapping: customURLMapping,
IconURL: c.String("icon-url"),
SkipLocalTwoFA: c.Bool("skip-local-2fa"),
Scopes: c.StringSlice("scopes"),
RequiredClaimName: c.String("required-claim-name"),
RequiredClaimValue: c.String("required-claim-value"),
GroupClaimName: c.String("group-claim-name"),
AdminGroup: c.String("admin-group"),
RestrictedGroup: c.String("restricted-group"),
}
}

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

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

if c.IsSet("required-claim-name") {
oAuth2Config.RequiredClaimName = c.String("required-claim-name")

}
if c.IsSet("required-claim-value") {
oAuth2Config.RequiredClaimValue = c.String("required-claim-value")
}

if c.IsSet("group-claim-name") {
oAuth2Config.GroupClaimName = c.String("group-claim-name")
}
if c.IsSet("admin-group") {
oAuth2Config.AdminGroup = c.String("admin-group")
}
if c.IsSet("restricted-group") {
oAuth2Config.RestrictedGroup = c.String("restricted-group")
}

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

Expand Down
14 changes: 14 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,13 @@ 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)
- `--required-claim-name`: Claim name that has to be set to allow users to login with this source. (Optional)
- `--required-claim-value`: Claim value that has to be set to allow users to login with this source. (Optional)
- `--group-claim-name`: Claim name providing group names for this source. (Optional)
- `--admin-group`: Group Claim value for administrator users. (Optional)
- `--restricted-group`: Group Claim value for restricted users. (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 +140,13 @@ 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.
- `--required-claim-name`: Claim name that has to be set to allow users to login with this source. (Optional)
- `--required-claim-value`: Claim value that has to be set to allow users to login with this source. (Optional)
- `--group-claim-name`: Claim name providing group names for this source. (Optional)
- `--admin-group`: Group Claim value for administrator users. (Optional)
- `--restricted-group`: Group Claim value for restricted users. (Optional)
- Examples:
- `gitea admin auth update-oauth --id 1 --name external-github-updated`
- `add-ldap`: Add new LDAP (via Bind DN) authentication source
Expand Down
38 changes: 6 additions & 32 deletions models/external_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/structs"

"github.com/markbates/goth"
"xorm.io/builder"
)

Expand Down Expand Up @@ -105,42 +103,18 @@ func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
return id, nil
}

// UpdateExternalUser updates external user's information
func UpdateExternalUser(user *User, gothUser goth.User) error {
loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
if err != nil {
return err
}
externalLoginUser := &ExternalLoginUser{
ExternalID: gothUser.UserID,
UserID: user.ID,
LoginSourceID: loginSource.ID,
RawData: gothUser.RawData,
Provider: gothUser.Provider,
Email: gothUser.Email,
Name: gothUser.Name,
FirstName: gothUser.FirstName,
LastName: gothUser.LastName,
NickName: gothUser.NickName,
Description: gothUser.Description,
AvatarURL: gothUser.AvatarURL,
Location: gothUser.Location,
AccessToken: gothUser.AccessToken,
AccessTokenSecret: gothUser.AccessTokenSecret,
RefreshToken: gothUser.RefreshToken,
ExpiresAt: gothUser.ExpiresAt,
}

has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
// UpdateExternalUserByExternalID updates an external user's information
func UpdateExternalUserByExternalID(external *ExternalLoginUser) error {
has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).
NoAutoCondition().
Exist(externalLoginUser)
Exist(external)
if err != nil {
return err
} else if !has {
return ErrExternalLoginUserNotExist{user.ID, loginSource.ID}
return ErrExternalLoginUserNotExist{external.UserID, external.LoginSourceID}
}

_, err = db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).AllCols().Update(externalLoginUser)
_, err = db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).AllCols().Update(external)
return err
}

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
5 changes: 5 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,11 @@ auths.oauth2_emailURL = Email URL
auths.skip_local_two_fa = Skip local 2FA
auths.skip_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.oauth2_required_claim_name = Required Claim Name
auths.oauth2_required_claim_name_helper = Set this name to only allow users to login from this source if the user has a claim with this name
auths.oauth2_required_claim_value = Required Claim Value
auths.oauth2_required_claim_value_helper = Set this value to only allow users to login from this source if the user hasthe claim above with this value
zeripath marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 5 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/models/login"
Expand Down Expand Up @@ -184,6 +185,9 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
OpenIDConnectAutoDiscoveryURL: form.OpenIDConnectAutoDiscoveryURL,
CustomURLMapping: customURLMapping,
IconURL: form.Oauth2IconURL,
Scopes: strings.Split(form.Oauth2Scopes, ","),
RequiredClaimName: form.Oauth2RequiredClaimName,
RequiredClaimValue: form.Oauth2RequiredClaimValue,
SkipLocalTwoFA: form.SkipLocalTwoFA,
}
}
Expand Down Expand Up @@ -325,8 +329,8 @@ func EditAuthSource(ctx *context.Context) {
break
}
}

}

ctx.HTML(http.StatusOK, tplAuthEdit)
}

Expand Down
Loading