2017-03-16 04:27:35 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2019-12-20 20:07:12 +03:00
|
|
|
"net/http"
|
|
|
|
|
2017-03-16 04:27:35 +03:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-11-10 07:41:51 +03:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-08-23 19:40:30 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-01-24 22:00:29 +03:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2017-03-16 04:27:35 +03:00
|
|
|
)
|
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
func listGPGKeys(ctx *context.APIContext, uid int64, listOptions models.ListOptions) {
|
|
|
|
keys, err := models.ListGPGKeys(uid, listOptions)
|
2017-03-16 04:27:35 +03:00
|
|
|
if err != nil {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
|
2017-03-16 04:27:35 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeys := make([]*api.GPGKey, len(keys))
|
|
|
|
for i := range keys {
|
|
|
|
apiKeys[i] = convert.ToGPGKey(keys[i])
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.JSON(http.StatusOK, &apiKeys)
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//ListGPGKeys get the GPG key list of a user
|
|
|
|
func ListGPGKeys(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation GET /users/{username}/gpg_keys user userListGPGKeys
|
|
|
|
// ---
|
|
|
|
// summary: List the given user's GPG keys
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-24 22:00:29 +03:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, maximum page size is 50
|
|
|
|
// type: integer
|
2017-11-13 10:02:25 +03:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/GPGKeyList"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2017-03-16 04:27:35 +03:00
|
|
|
user := GetUserByParams(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2020-01-24 22:00:29 +03:00
|
|
|
listGPGKeys(ctx, user.ID, utils.GetListOptions(ctx))
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
|
2017-11-13 10:02:25 +03:00
|
|
|
//ListMyGPGKeys get the GPG key list of the authenticated user
|
2017-03-16 04:27:35 +03:00
|
|
|
func ListMyGPGKeys(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
|
|
|
|
// ---
|
|
|
|
// summary: List the authenticated user's GPG keys
|
2020-01-24 22:00:29 +03:00
|
|
|
// parameters:
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, maximum page size is 50
|
|
|
|
// type: integer
|
2017-11-13 10:02:25 +03:00
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/GPGKeyList"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
listGPGKeys(ctx, ctx.User.ID, utils.GetListOptions(ctx))
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//GetGPGKey get the GPG key based on a id
|
|
|
|
func GetGPGKey(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation GET /user/gpg_keys/{id} user userCurrentGetGPGKey
|
|
|
|
// ---
|
|
|
|
// summary: Get a GPG key
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of key to get
|
|
|
|
// type: integer
|
2018-10-21 06:40:42 +03:00
|
|
|
// format: int64
|
2017-11-13 10:02:25 +03:00
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/GPGKey"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2017-03-16 04:27:35 +03:00
|
|
|
key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrGPGKeyNotExist(err) {
|
2019-03-19 05:29:43 +03:00
|
|
|
ctx.NotFound()
|
2017-03-16 04:27:35 +03:00
|
|
|
} else {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err)
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUserGPGKey creates new GPG key to given user by ID.
|
|
|
|
func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
|
|
|
|
key, err := models.AddGPGKey(uid, form.ArmoredKey)
|
|
|
|
if err != nil {
|
|
|
|
HandleAddGPGKeyError(ctx, err)
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.JSON(http.StatusCreated, convert.ToGPGKey(key))
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:parameters userCurrentPostGPGKey
|
|
|
|
type swaggerUserCurrentPostGPGKey struct {
|
|
|
|
// in:body
|
|
|
|
Form api.CreateGPGKeyOption
|
|
|
|
}
|
2017-05-02 16:35:59 +03:00
|
|
|
|
2017-11-13 10:02:25 +03:00
|
|
|
//CreateGPGKey create a GPG key belonging to the authenticated user
|
|
|
|
func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
|
|
|
|
// swagger:operation POST /user/gpg_keys user userCurrentPostGPGKey
|
|
|
|
// ---
|
|
|
|
// summary: Create a GPG key
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/GPGKey"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2017-03-16 04:27:35 +03:00
|
|
|
CreateUserGPGKey(ctx, form, ctx.User.ID)
|
|
|
|
}
|
|
|
|
|
2017-11-13 10:02:25 +03:00
|
|
|
//DeleteGPGKey remove a GPG key belonging to the authenticated user
|
2017-03-16 04:27:35 +03:00
|
|
|
func DeleteGPGKey(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation DELETE /user/gpg_keys/{id} user userCurrentDeleteGPGKey
|
|
|
|
// ---
|
|
|
|
// summary: Remove a GPG key
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of key to delete
|
|
|
|
// type: integer
|
2018-10-21 06:40:42 +03:00
|
|
|
// format: int64
|
2017-11-13 10:02:25 +03:00
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2017-03-16 04:27:35 +03:00
|
|
|
if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
|
|
|
|
if models.IsErrGPGKeyAccessDenied(err) {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
|
2017-03-16 04:27:35 +03:00
|
|
|
} else {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err)
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleAddGPGKeyError handle add GPGKey error
|
|
|
|
func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
|
|
|
|
switch {
|
|
|
|
case models.IsErrGPGKeyAccessDenied(err):
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key")
|
2017-03-16 04:27:35 +03:00
|
|
|
case models.IsErrGPGKeyIDAlreadyUsed(err):
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists")
|
2017-03-16 04:27:35 +03:00
|
|
|
default:
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
|
2017-03-16 04:27:35 +03:00
|
|
|
}
|
|
|
|
}
|