Skip to content

Commit ad4160c

Browse files
committed
Add private information to the user keys API
This adjusts the keys API to give out private information to user keys if the current user is the owner or an admin. Signed-off-by: Andrew Thornton <[email protected]>
1 parent c8b4c7a commit ad4160c

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

routers/api/v1/user/key.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ import (
1414
"code.gitea.io/gitea/routers/api/v1/repo"
1515
)
1616

17+
// appendPrivateInformation appends the owner and key type information to api.PublicKey
18+
func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defaultUser *models.User) (*api.PublicKey, error) {
19+
if key.Type == models.KeyTypeDeploy {
20+
apiKey.KeyType = "deploy"
21+
} else if key.Type == models.KeyTypeUser {
22+
apiKey.KeyType = "user"
23+
24+
if defaultUser.ID == key.OwnerID {
25+
apiKey.Owner = defaultUser.APIFormat()
26+
} else {
27+
user, err := models.GetUserByID(key.OwnerID)
28+
if err != nil {
29+
return apiKey, err
30+
}
31+
apiKey.Owner = user.APIFormat()
32+
}
33+
} else {
34+
apiKey.KeyType = "unknown"
35+
}
36+
apiKey.ReadOnly = key.Mode == models.AccessModeRead
37+
return apiKey, nil
38+
}
39+
1740
// GetUserByParamsName get user by name
1841
func GetUserByParamsName(ctx *context.APIContext, name string) *models.User {
1942
user, err := models.GetUserByName(ctx.Params(name))
@@ -37,8 +60,9 @@ func composePublicKeysAPILink() string {
3760
return setting.AppURL + "api/v1/user/keys/"
3861
}
3962

40-
func listPublicKeys(ctx *context.APIContext, uid int64) {
41-
keys, err := models.ListPublicKeys(uid)
63+
func listPublicKeys(ctx *context.APIContext, user *models.User) {
64+
keys, err := models.ListPublicKeys(user.ID)
65+
4266
if err != nil {
4367
ctx.Error(500, "ListPublicKeys", err)
4468
return
@@ -48,6 +72,9 @@ func listPublicKeys(ctx *context.APIContext, uid int64) {
4872
apiKeys := make([]*api.PublicKey, len(keys))
4973
for i := range keys {
5074
apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
75+
if ctx.User.IsAdmin || ctx.User.ID == keys[i].OwnerID {
76+
apiKeys[i], _ = appendPrivateInformation(apiKeys[i], keys[i], user)
77+
}
5178
}
5279

5380
ctx.JSON(200, &apiKeys)
@@ -63,7 +90,7 @@ func ListMyPublicKeys(ctx *context.APIContext) {
6390
// responses:
6491
// "200":
6592
// "$ref": "#/responses/PublicKeyList"
66-
listPublicKeys(ctx, ctx.User.ID)
93+
listPublicKeys(ctx, ctx.User)
6794
}
6895

6996
// ListPublicKeys list the given user's public keys
@@ -86,7 +113,7 @@ func ListPublicKeys(ctx *context.APIContext) {
86113
if ctx.Written() {
87114
return
88115
}
89-
listPublicKeys(ctx, user.ID)
116+
listPublicKeys(ctx, user)
90117
}
91118

92119
// GetPublicKey get a public key
@@ -119,7 +146,11 @@ func GetPublicKey(ctx *context.APIContext) {
119146
}
120147

121148
apiLink := composePublicKeysAPILink()
122-
ctx.JSON(200, convert.ToPublicKey(apiLink, key))
149+
apiKey := convert.ToPublicKey(apiLink, key)
150+
if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID {
151+
apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User)
152+
}
153+
ctx.JSON(200, apiKey)
123154
}
124155

125156
// CreateUserPublicKey creates new public key to given user by ID.
@@ -136,7 +167,11 @@ func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid
136167
return
137168
}
138169
apiLink := composePublicKeysAPILink()
139-
ctx.JSON(201, convert.ToPublicKey(apiLink, key))
170+
apiKey := convert.ToPublicKey(apiLink, key)
171+
if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID {
172+
apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User)
173+
}
174+
ctx.JSON(201, apiKey)
140175
}
141176

142177
// CreatePublicKey create one public key for me

0 commit comments

Comments
 (0)