You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

key.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/routers/api/v1/convert"
  11. "code.gitea.io/gitea/routers/api/v1/repo"
  12. )
  13. // appendPrivateInformation appends the owner and key type information to api.PublicKey
  14. func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defaultUser *models.User) (*api.PublicKey, error) {
  15. if key.Type == models.KeyTypeDeploy {
  16. apiKey.KeyType = "deploy"
  17. } else if key.Type == models.KeyTypeUser {
  18. apiKey.KeyType = "user"
  19. if defaultUser.ID == key.OwnerID {
  20. apiKey.Owner = defaultUser.APIFormat()
  21. } else {
  22. user, err := models.GetUserByID(key.OwnerID)
  23. if err != nil {
  24. return apiKey, err
  25. }
  26. apiKey.Owner = user.APIFormat()
  27. }
  28. } else {
  29. apiKey.KeyType = "unknown"
  30. }
  31. apiKey.ReadOnly = key.Mode == models.AccessModeRead
  32. return apiKey, nil
  33. }
  34. // GetUserByParamsName get user by name
  35. func GetUserByParamsName(ctx *context.APIContext, name string) *models.User {
  36. user, err := models.GetUserByName(ctx.Params(name))
  37. if err != nil {
  38. if models.IsErrUserNotExist(err) {
  39. ctx.Status(404)
  40. } else {
  41. ctx.Error(500, "GetUserByName", err)
  42. }
  43. return nil
  44. }
  45. return user
  46. }
  47. // GetUserByParams returns user whose name is presented in URL paramenter.
  48. func GetUserByParams(ctx *context.APIContext) *models.User {
  49. return GetUserByParamsName(ctx, ":username")
  50. }
  51. func composePublicKeysAPILink() string {
  52. return setting.AppURL + "api/v1/user/keys/"
  53. }
  54. func listPublicKeys(ctx *context.APIContext, user *models.User) {
  55. var keys []*models.PublicKey
  56. var err error
  57. fingerprint := ctx.Query("fingerprint")
  58. username := ctx.Params("username")
  59. if fingerprint != "" {
  60. // Querying not just listing
  61. if username != "" {
  62. // Restrict to provided uid
  63. keys, err = models.SearchPublicKey(user.ID, fingerprint)
  64. } else {
  65. // Unrestricted
  66. keys, err = models.SearchPublicKey(0, fingerprint)
  67. }
  68. } else {
  69. // Use ListPublicKeys
  70. keys, err = models.ListPublicKeys(user.ID)
  71. }
  72. if err != nil {
  73. ctx.Error(500, "ListPublicKeys", err)
  74. return
  75. }
  76. apiLink := composePublicKeysAPILink()
  77. apiKeys := make([]*api.PublicKey, len(keys))
  78. for i := range keys {
  79. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  80. if ctx.User.IsAdmin || ctx.User.ID == keys[i].OwnerID {
  81. apiKeys[i], _ = appendPrivateInformation(apiKeys[i], keys[i], user)
  82. }
  83. }
  84. ctx.JSON(200, &apiKeys)
  85. }
  86. // ListMyPublicKeys list all of the authenticated user's public keys
  87. func ListMyPublicKeys(ctx *context.APIContext) {
  88. // swagger:operation GET /user/keys user userCurrentListKeys
  89. // ---
  90. // summary: List the authenticated user's public keys
  91. // parameters:
  92. // - name: fingerprint
  93. // in: query
  94. // description: fingerprint of the key
  95. // type: string
  96. // produces:
  97. // - application/json
  98. // responses:
  99. // "200":
  100. // "$ref": "#/responses/PublicKeyList"
  101. listPublicKeys(ctx, ctx.User)
  102. }
  103. // ListPublicKeys list the given user's public keys
  104. func ListPublicKeys(ctx *context.APIContext) {
  105. // swagger:operation GET /users/{username}/keys user userListKeys
  106. // ---
  107. // summary: List the given user's public keys
  108. // produces:
  109. // - application/json
  110. // parameters:
  111. // - name: username
  112. // in: path
  113. // description: username of user
  114. // type: string
  115. // required: true
  116. // - name: fingerprint
  117. // in: query
  118. // description: fingerprint of the key
  119. // type: string
  120. // responses:
  121. // "200":
  122. // "$ref": "#/responses/PublicKeyList"
  123. user := GetUserByParams(ctx)
  124. if ctx.Written() {
  125. return
  126. }
  127. listPublicKeys(ctx, user)
  128. }
  129. // GetPublicKey get a public key
  130. func GetPublicKey(ctx *context.APIContext) {
  131. // swagger:operation GET /user/keys/{id} user userCurrentGetKey
  132. // ---
  133. // summary: Get a public key
  134. // produces:
  135. // - application/json
  136. // parameters:
  137. // - name: id
  138. // in: path
  139. // description: id of key to get
  140. // type: integer
  141. // format: int64
  142. // required: true
  143. // responses:
  144. // "200":
  145. // "$ref": "#/responses/PublicKey"
  146. // "404":
  147. // "$ref": "#/responses/notFound"
  148. key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
  149. if err != nil {
  150. if models.IsErrKeyNotExist(err) {
  151. ctx.Status(404)
  152. } else {
  153. ctx.Error(500, "GetPublicKeyByID", err)
  154. }
  155. return
  156. }
  157. apiLink := composePublicKeysAPILink()
  158. apiKey := convert.ToPublicKey(apiLink, key)
  159. if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID {
  160. apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User)
  161. }
  162. ctx.JSON(200, apiKey)
  163. }
  164. // CreateUserPublicKey creates new public key to given user by ID.
  165. func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
  166. content, err := models.CheckPublicKeyString(form.Key)
  167. if err != nil {
  168. repo.HandleCheckKeyStringError(ctx, err)
  169. return
  170. }
  171. key, err := models.AddPublicKey(uid, form.Title, content, 0)
  172. if err != nil {
  173. repo.HandleAddKeyError(ctx, err)
  174. return
  175. }
  176. apiLink := composePublicKeysAPILink()
  177. apiKey := convert.ToPublicKey(apiLink, key)
  178. if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID {
  179. apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User)
  180. }
  181. ctx.JSON(201, apiKey)
  182. }
  183. // CreatePublicKey create one public key for me
  184. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  185. // swagger:operation POST /user/keys user userCurrentPostKey
  186. // ---
  187. // summary: Create a public key
  188. // consumes:
  189. // - application/json
  190. // produces:
  191. // - application/json
  192. // parameters:
  193. // - name: body
  194. // in: body
  195. // schema:
  196. // "$ref": "#/definitions/CreateKeyOption"
  197. // responses:
  198. // "201":
  199. // "$ref": "#/responses/PublicKey"
  200. // "422":
  201. // "$ref": "#/responses/validationError"
  202. CreateUserPublicKey(ctx, form, ctx.User.ID)
  203. }
  204. // DeletePublicKey delete one public key
  205. func DeletePublicKey(ctx *context.APIContext) {
  206. // swagger:operation DELETE /user/keys/{id} user userCurrentDeleteKey
  207. // ---
  208. // summary: Delete a public key
  209. // produces:
  210. // - application/json
  211. // parameters:
  212. // - name: id
  213. // in: path
  214. // description: id of key to delete
  215. // type: integer
  216. // format: int64
  217. // required: true
  218. // responses:
  219. // "204":
  220. // "$ref": "#/responses/empty"
  221. // "403":
  222. // "$ref": "#/responses/forbidden"
  223. // "404":
  224. // "$ref": "#/responses/notFound"
  225. if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  226. if models.IsErrKeyNotExist(err) {
  227. ctx.Status(404)
  228. } else if models.IsErrKeyAccessDenied(err) {
  229. ctx.Error(403, "", "You do not have access to this key")
  230. } else {
  231. ctx.Error(500, "DeletePublicKey", err)
  232. }
  233. return
  234. }
  235. ctx.Status(204)
  236. }