本站源代码
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.

259 lines
6.7KB

  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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/setting"
  9. api "code.gitea.io/gitea/modules/structs"
  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 = convert.ToUser(defaultUser, true, true)
  21. } else {
  22. user, err := models.GetUserByID(key.OwnerID)
  23. if err != nil {
  24. return apiKey, err
  25. }
  26. apiKey.Owner = convert.ToUser(user, true, true)
  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.NotFound()
  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.NotFound()
  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.NotFound()
  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. }
上海开阖软件有限公司 沪ICP备12045867号-1