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.

265 lines
6.9 KiB

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