Skip to content

Commit 3332475

Browse files
committed
fix tests
1 parent b5ae054 commit 3332475

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

cmd/admin_auth_oauth.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ func oauthCLIFlags() []cli.Flag {
8787
Value: nil,
8888
Usage: "Scopes to request when to authenticate against this OAuth2 source",
8989
},
90+
&cli.StringFlag{
91+
Name: "attribute-ssh-public-key",
92+
Usage: "Claim name that provides SSH public keys",
93+
},
94+
&cli.StringFlag{
95+
Name: "attribute-full-name",
96+
Usage: "Claim name that provides user's full name",
97+
},
9098
&cli.StringFlag{
9199
Name: "required-claim-name",
92100
Value: "",
@@ -177,6 +185,8 @@ func parseOAuth2Config(c *cli.Command) *oauth2.Source {
177185
RestrictedGroup: c.String("restricted-group"),
178186
GroupTeamMap: c.String("group-team-map"),
179187
GroupTeamMapRemoval: c.Bool("group-team-map-removal"),
188+
AttributeSSHPublicKey: c.String("attribute-ssh-public-key"),
189+
AttributeFullName: c.String("attribute-full-name"),
180190
}
181191
}
182192

@@ -268,6 +278,12 @@ func (a *authService) runUpdateOauth(ctx context.Context, c *cli.Command) error
268278
if c.IsSet("group-team-map-removal") {
269279
oAuth2Config.GroupTeamMapRemoval = c.Bool("group-team-map-removal")
270280
}
281+
if c.IsSet("attribute-ssh-public-key") {
282+
oAuth2Config.AttributeSSHPublicKey = c.String("attribute-ssh-public-key")
283+
}
284+
if c.IsSet("attribute-full-name") {
285+
oAuth2Config.AttributeFullName = c.String("attribute-full-name")
286+
}
271287

272288
// update custom URL mapping
273289
customURLMapping := &oauth2.CustomURLMapping{}

cmd/admin_auth_oauth_test.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func TestAddOauth(t *testing.T) {
8888
"--restricted-group", "restricted",
8989
"--group-team-map", `{"group1": [1,2]}`,
9090
"--group-team-map-removal=true",
91+
"--attribute-ssh-public-key", "attr_ssh_pub_key",
92+
"--attribute-full-name", "attr_full_name",
9193
},
9294
source: &auth_model.Source{
9395
Type: auth_model.OAuth2,
@@ -104,15 +106,17 @@ func TestAddOauth(t *testing.T) {
104106
EmailURL: "https://example.com/email",
105107
Tenant: "some_tenant",
106108
},
107-
IconURL: "https://example.com/icon",
108-
Scopes: []string{"scope1", "scope2"},
109-
RequiredClaimName: "claim_name",
110-
RequiredClaimValue: "claim_value",
111-
GroupClaimName: "group_name",
112-
AdminGroup: "admin",
113-
RestrictedGroup: "restricted",
114-
GroupTeamMap: `{"group1": [1,2]}`,
115-
GroupTeamMapRemoval: true,
109+
IconURL: "https://example.com/icon",
110+
Scopes: []string{"scope1", "scope2"},
111+
RequiredClaimName: "claim_name",
112+
RequiredClaimValue: "claim_value",
113+
GroupClaimName: "group_name",
114+
AdminGroup: "admin",
115+
RestrictedGroup: "restricted",
116+
GroupTeamMap: `{"group1": [1,2]}`,
117+
GroupTeamMapRemoval: true,
118+
AttributeSSHPublicKey: "attr_ssh_pub_key",
119+
AttributeFullName: "attr_full_name",
116120
},
117121
TwoFactorPolicy: "skip",
118122
},
@@ -223,15 +227,17 @@ func TestUpdateOauth(t *testing.T) {
223227
EmailURL: "https://old.example.com/email",
224228
Tenant: "old_tenant",
225229
},
226-
IconURL: "https://old.example.com/icon",
227-
Scopes: []string{"old_scope1", "old_scope2"},
228-
RequiredClaimName: "old_claim_name",
229-
RequiredClaimValue: "old_claim_value",
230-
GroupClaimName: "old_group_name",
231-
AdminGroup: "old_admin",
232-
RestrictedGroup: "old_restricted",
233-
GroupTeamMap: `{"old_group1": [1,2]}`,
234-
GroupTeamMapRemoval: true,
230+
IconURL: "https://old.example.com/icon",
231+
Scopes: []string{"old_scope1", "old_scope2"},
232+
RequiredClaimName: "old_claim_name",
233+
RequiredClaimValue: "old_claim_value",
234+
GroupClaimName: "old_group_name",
235+
AdminGroup: "old_admin",
236+
RestrictedGroup: "old_restricted",
237+
GroupTeamMap: `{"old_group1": [1,2]}`,
238+
GroupTeamMapRemoval: true,
239+
AttributeSSHPublicKey: "old_ssh_pub_key",
240+
AttributeFullName: "old_full_name",
235241
},
236242
TwoFactorPolicy: "",
237243
},
@@ -257,6 +263,8 @@ func TestUpdateOauth(t *testing.T) {
257263
"--restricted-group", "restricted",
258264
"--group-team-map", `{"group1": [1,2]}`,
259265
"--group-team-map-removal=false",
266+
"--attribute-ssh-public-key", "new_ssh_pub_key",
267+
"--attribute-full-name", "new_full_name",
260268
},
261269
authSource: &auth_model.Source{
262270
ID: 1,
@@ -274,15 +282,17 @@ func TestUpdateOauth(t *testing.T) {
274282
EmailURL: "https://example.com/email",
275283
Tenant: "new_tenant",
276284
},
277-
IconURL: "https://example.com/icon",
278-
Scopes: []string{"scope1", "scope2"},
279-
RequiredClaimName: "claim_name",
280-
RequiredClaimValue: "claim_value",
281-
GroupClaimName: "group_name",
282-
AdminGroup: "admin",
283-
RestrictedGroup: "restricted",
284-
GroupTeamMap: `{"group1": [1,2]}`,
285-
GroupTeamMapRemoval: false,
285+
IconURL: "https://example.com/icon",
286+
Scopes: []string{"scope1", "scope2"},
287+
RequiredClaimName: "claim_name",
288+
RequiredClaimValue: "claim_value",
289+
GroupClaimName: "group_name",
290+
AdminGroup: "admin",
291+
RestrictedGroup: "restricted",
292+
GroupTeamMap: `{"group1": [1,2]}`,
293+
GroupTeamMapRemoval: false,
294+
AttributeSSHPublicKey: "new_ssh_pub_key",
295+
AttributeFullName: "new_full_name",
286296
},
287297
TwoFactorPolicy: "skip",
288298
},

models/auth/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func UpdateSource(ctx context.Context, source *Source) error {
334334

335335
err = registerableSource.RegisterSource()
336336
if err != nil {
337-
// restore original values since we cannot update the provider it self
337+
// restore original values since we cannot update the provider itself
338338
if _, err := db.GetEngine(ctx).ID(source.ID).AllCols().Update(originalSource); err != nil {
339339
log.Error("UpdateSource: Error while wrapOpenIDConnectInitializeError: %v", err)
340340
}

routers/web/auth/linkaccount.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ func LinkAccountPostSignIn(ctx *context.Context) {
170170
}
171171

172172
func oauth2LinkAccount(ctx *context.Context, u *user_model.User, linkAccountData *LinkAccountData, remember bool) {
173-
// no need to call updateAvatarIfNeed(ctx, gothUser.AvatarURL, u) be cause
173+
oauth2SignInSync(ctx, &linkAccountData.AuthSource, u, linkAccountData.GothUser)
174+
if ctx.Written() {
175+
return
176+
}
174177

175178
// If this user is enrolled in 2FA, we can't sign the user in just yet.
176179
// Instead, redirect them to the 2FA authentication page.

tests/integration/signin_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
auth_model "code.gitea.io/gitea/models/auth"
1213
"code.gitea.io/gitea/models/db"
1314
"code.gitea.io/gitea/models/unittest"
1415
user_model "code.gitea.io/gitea/models/user"
@@ -17,6 +18,7 @@ import (
1718
"code.gitea.io/gitea/modules/translation"
1819
"code.gitea.io/gitea/modules/web"
1920
"code.gitea.io/gitea/routers"
21+
"code.gitea.io/gitea/routers/web/auth"
2022
"code.gitea.io/gitea/services/context"
2123
"code.gitea.io/gitea/tests"
2224

@@ -104,7 +106,7 @@ func TestEnablePasswordSignInFormAndEnablePasskeyAuth(t *testing.T) {
104106

105107
mockLinkAccount := func(ctx *context.Context) {
106108
gothUser := goth.User{Email: "invalid-email", Name: "."}
107-
_ = ctx.Session.Set("linkAccountGothUser", gothUser)
109+
_ = ctx.Session.Set("linkAccountData", auth.LinkAccountData{auth_model.Source{ID: 1}, gothUser})
108110
}
109111

110112
t.Run("EnablePasswordSignInForm=false", func(t *testing.T) {

0 commit comments

Comments
 (0)