@@ -14,6 +14,29 @@ import (
14
14
"code.gitea.io/gitea/routers/api/v1/repo"
15
15
)
16
16
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
+
17
40
// GetUserByParamsName get user by name
18
41
func GetUserByParamsName (ctx * context.APIContext , name string ) * models.User {
19
42
user , err := models .GetUserByName (ctx .Params (name ))
@@ -37,8 +60,9 @@ func composePublicKeysAPILink() string {
37
60
return setting .AppURL + "api/v1/user/keys/"
38
61
}
39
62
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
+
42
66
if err != nil {
43
67
ctx .Error (500 , "ListPublicKeys" , err )
44
68
return
@@ -48,6 +72,9 @@ func listPublicKeys(ctx *context.APIContext, uid int64) {
48
72
apiKeys := make ([]* api.PublicKey , len (keys ))
49
73
for i := range keys {
50
74
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
+ }
51
78
}
52
79
53
80
ctx .JSON (200 , & apiKeys )
@@ -63,7 +90,7 @@ func ListMyPublicKeys(ctx *context.APIContext) {
63
90
// responses:
64
91
// "200":
65
92
// "$ref": "#/responses/PublicKeyList"
66
- listPublicKeys (ctx , ctx .User . ID )
93
+ listPublicKeys (ctx , ctx .User )
67
94
}
68
95
69
96
// ListPublicKeys list the given user's public keys
@@ -86,7 +113,7 @@ func ListPublicKeys(ctx *context.APIContext) {
86
113
if ctx .Written () {
87
114
return
88
115
}
89
- listPublicKeys (ctx , user . ID )
116
+ listPublicKeys (ctx , user )
90
117
}
91
118
92
119
// GetPublicKey get a public key
@@ -119,7 +146,11 @@ func GetPublicKey(ctx *context.APIContext) {
119
146
}
120
147
121
148
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 )
123
154
}
124
155
125
156
// CreateUserPublicKey creates new public key to given user by ID.
@@ -136,7 +167,11 @@ func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid
136
167
return
137
168
}
138
169
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 )
140
175
}
141
176
142
177
// CreatePublicKey create one public key for me
0 commit comments