Skip to content

Commit d789170

Browse files
renothinglafriks
authored andcommitted
fix wrong email when use gitea as OAuth2 provider (#7640) (#7647)
when you use gitea as OAuth2 provider, the /api/v1/user should return user primary email as identifier, which is unique in OAuth2 clients. this patch use convert.ToUser replace all u.APIFormat in api requests, return primary email when caller is yourself or admin.
1 parent 9bbe3eb commit d789170

File tree

12 files changed

+32
-27
lines changed

12 files changed

+32
-27
lines changed

models/user.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ func (u *User) UpdateTheme(themeName string) error {
195195
return UpdateUserCols(u, "theme")
196196
}
197197

198-
// getEmail returns an noreply email, if the user has set to keep his
198+
// GetEmail returns an noreply email, if the user has set to keep his
199199
// email address private, otherwise the primary email address.
200-
func (u *User) getEmail() string {
200+
func (u *User) GetEmail() string {
201201
if u.KeepEmailPrivate {
202202
return fmt.Sprintf("%s@%s", u.LowerName, setting.Service.NoReplyAddress)
203203
}
@@ -210,7 +210,7 @@ func (u *User) APIFormat() *api.User {
210210
ID: u.ID,
211211
UserName: u.Name,
212212
FullName: u.FullName,
213-
Email: u.getEmail(),
213+
Email: u.GetEmail(),
214214
AvatarURL: u.AvatarLink(),
215215
Language: u.Language,
216216
IsAdmin: u.IsAdmin,
@@ -425,7 +425,7 @@ func (u *User) GetFollowing(page int) ([]*User, error) {
425425
func (u *User) NewGitSig() *git.Signature {
426426
return &git.Signature{
427427
Name: u.GitName(),
428-
Email: u.getEmail(),
428+
Email: u.GetEmail(),
429429
When: time.Now(),
430430
}
431431
}

routers/api/v1/admin/user.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
9191
if form.SendNotify && setting.MailService != nil {
9292
models.SendRegisterNotifyMail(ctx.Context.Context, u)
9393
}
94-
95-
ctx.JSON(201, u.APIFormat())
94+
ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
9695
}
9796

9897
// EditUser api for modifying a user's information
@@ -181,7 +180,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
181180
}
182181
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
183182

184-
ctx.JSON(200, u.APIFormat())
183+
ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
185184
}
186185

187186
// DeleteUser api for deleting a user
@@ -326,7 +325,7 @@ func GetAllUsers(ctx *context.APIContext) {
326325

327326
results := make([]*api.User, len(users))
328327
for i := range users {
329-
results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
328+
results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin)
330329
}
331330

332331
ctx.JSON(200, &results)

routers/api/v1/convert/convert.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func ToTeam(team *models.Team) *api.Team {
229229
}
230230

231231
// ToUser convert models.User to api.User
232-
func ToUser(user *models.User, signed, admin bool) *api.User {
232+
func ToUser(user *models.User, signed, authed bool) *api.User {
233233
result := &api.User{
234234
ID: user.ID,
235235
UserName: user.Name,
@@ -239,7 +239,12 @@ func ToUser(user *models.User, signed, admin bool) *api.User {
239239
LastLogin: user.LastLoginUnix.AsTime(),
240240
Created: user.CreatedUnix.AsTime(),
241241
}
242-
if signed && (!user.KeepEmailPrivate || admin) {
242+
// hide primary email if API caller isn't user itself or an admin
243+
if !signed {
244+
result.Email = ""
245+
} else if user.KeepEmailPrivate && !authed {
246+
result.Email = user.GetEmail()
247+
} else {
243248
result.Email = user.Email
244249
}
245250
return result

routers/api/v1/org/member.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/routers/api/v1/convert"
1516
"code.gitea.io/gitea/routers/api/v1/user"
1617
)
1718

@@ -46,7 +47,7 @@ func listMembers(ctx *context.APIContext, publicOnly bool) {
4647

4748
apiMembers := make([]*api.User, len(members))
4849
for i, member := range members {
49-
apiMembers[i] = member.APIFormat()
50+
apiMembers[i] = convert.ToUser(member, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
5051
}
5152
ctx.JSON(200, apiMembers)
5253
}

routers/api/v1/org/team.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func GetTeamMembers(ctx *context.APIContext) {
257257
}
258258
members := make([]*api.User, len(team.Members))
259259
for i, member := range team.Members {
260-
members[i] = member.APIFormat()
260+
members[i] = convert.ToUser(member, ctx.IsSigned, ctx.User.IsAdmin)
261261
}
262262
ctx.JSON(200, members)
263263
}
@@ -288,7 +288,7 @@ func GetTeamMember(ctx *context.APIContext) {
288288
if ctx.Written() {
289289
return
290290
}
291-
ctx.JSON(200, u.APIFormat())
291+
ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
292292
}
293293

294294
// AddTeamMember api for add a member to a team

routers/api/v1/repo/collaborators.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/context"
1313

1414
api "code.gitea.io/gitea/modules/structs"
15+
"code.gitea.io/gitea/routers/api/v1/convert"
1516
)
1617

1718
// ListCollaborators list a repository's collaborators
@@ -42,7 +43,7 @@ func ListCollaborators(ctx *context.APIContext) {
4243
}
4344
users := make([]*api.User, len(collaborators))
4445
for i, collaborator := range collaborators {
45-
users[i] = collaborator.APIFormat()
46+
users[i] = convert.ToUser(collaborator.User, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
4647
}
4748
ctx.JSON(200, users)
4849
}

routers/api/v1/repo/hook.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ func TestHook(ctx *context.APIContext) {
130130
convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
131131
},
132132
Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
133-
Pusher: ctx.User.APIFormat(),
134-
Sender: ctx.User.APIFormat(),
133+
Pusher: convert.ToUser(ctx.User, ctx.IsSigned, false),
134+
Sender: convert.ToUser(ctx.User, ctx.IsSigned, false),
135135
}); err != nil {
136136
ctx.Error(500, "PrepareWebhook: ", err)
137137
return

routers/api/v1/repo/star.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/gitea/modules/context"
99

1010
api "code.gitea.io/gitea/modules/structs"
11+
"code.gitea.io/gitea/routers/api/v1/convert"
1112
)
1213

1314
// ListStargazers list a repository's stargazers
@@ -38,7 +39,7 @@ func ListStargazers(ctx *context.APIContext) {
3839
}
3940
users := make([]*api.User, len(stargazers))
4041
for i, stargazer := range stargazers {
41-
users[i] = stargazer.APIFormat()
42+
users[i] = convert.ToUser(stargazer, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
4243
}
4344
ctx.JSON(200, users)
4445
}

routers/api/v1/repo/subscriber.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/gitea/modules/context"
99

1010
api "code.gitea.io/gitea/modules/structs"
11+
"code.gitea.io/gitea/routers/api/v1/convert"
1112
)
1213

1314
// ListSubscribers list a repo's subscribers (i.e. watchers)
@@ -38,7 +39,7 @@ func ListSubscribers(ctx *context.APIContext) {
3839
}
3940
users := make([]*api.User, len(subscribers))
4041
for i, subscriber := range subscribers {
41-
users[i] = subscriber.APIFormat()
42+
users[i] = convert.ToUser(subscriber, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
4243
}
4344
ctx.JSON(200, users)
4445
}

routers/api/v1/user/follower.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/routers/api/v1/convert"
1213
)
1314

1415
func responseAPIUsers(ctx *context.APIContext, users []*models.User) {
1516
apiUsers := make([]*api.User, len(users))
1617
for i := range users {
17-
apiUsers[i] = users[i].APIFormat()
18+
apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
1819
}
1920
ctx.JSON(200, &apiUsers)
2021
}

routers/api/v1/user/key.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defa
2222
apiKey.KeyType = "user"
2323

2424
if defaultUser.ID == key.OwnerID {
25-
apiKey.Owner = defaultUser.APIFormat()
25+
apiKey.Owner = convert.ToUser(defaultUser, true, true)
2626
} else {
2727
user, err := models.GetUserByID(key.OwnerID)
2828
if err != nil {
2929
return apiKey, err
3030
}
31-
apiKey.Owner = user.APIFormat()
31+
apiKey.Owner = convert.ToUser(user, true, true)
3232
}
3333
} else {
3434
apiKey.KeyType = "unknown"

routers/api/v1/user/user.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ func GetInfo(ctx *context.APIContext) {
104104
return
105105
}
106106

107-
// Hide user e-mail when API caller isn't signed in.
108-
if !ctx.IsSigned {
109-
u.Email = ""
110-
}
111-
ctx.JSON(200, u.APIFormat())
107+
ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.ID == u.ID || ctx.User.IsAdmin))
112108
}
113109

114110
// GetAuthenticatedUser get current user's information
@@ -121,7 +117,7 @@ func GetAuthenticatedUser(ctx *context.APIContext) {
121117
// responses:
122118
// "200":
123119
// "$ref": "#/responses/User"
124-
ctx.JSON(200, ctx.User.APIFormat())
120+
ctx.JSON(200, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil))
125121
}
126122

127123
// GetUserHeatmapData is the handler to get a users heatmap

0 commit comments

Comments
 (0)