Files

312 lines
9.0 KiB
Go
Raw Permalink Normal View History

2017-03-16 02:27:35 +01:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2017-03-16 02:27:35 +01:00
package user
import (
2025-04-01 12:14:01 +02:00
"errors"
2019-12-20 18:07:12 +01:00
"net/http"
"strings"
2019-12-20 18:07:12 +01:00
2021-12-10 16:14:24 +08:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
2019-08-23 09:40:30 -07:00
api "code.gitea.io/gitea/modules/structs"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/web"
2020-01-24 19:00:29 +00:00
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
2017-03-16 02:27:35 +01:00
)
func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) {
2024-01-15 10:19:25 +08:00
keys, total, err := db.FindAndCount[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
ListOptions: listOptions,
OwnerID: uid,
})
2017-03-16 02:27:35 +01:00
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2017-03-16 02:27:35 +01:00
return
}
2024-01-15 10:19:25 +08:00
if err := asymkey_model.GPGKeyList(keys).LoadSubKeys(ctx); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2024-01-15 10:19:25 +08:00
return
}
2017-03-16 02:27:35 +01:00
apiKeys := make([]*api.GPGKey, len(keys))
for i := range keys {
apiKeys[i] = convert.ToGPGKey(keys[i])
}
2021-08-12 14:43:08 +02:00
ctx.SetTotalCountHeader(total)
2019-12-20 18:07:12 +01:00
ctx.JSON(http.StatusOK, &apiKeys)
2017-03-16 02:27:35 +01:00
}
2022-01-20 18:46:10 +01:00
// ListGPGKeys get the GPG key list of a user
2017-03-16 02:27:35 +01:00
func ListGPGKeys(ctx *context.APIContext) {
2017-11-12 23:02:25 -08: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 the user whose GPG key list is to be obtained
2017-11-12 23:02:25 -08:00
// type: string
// required: true
2020-01-24 19:00:29 +00: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
2020-01-24 19:00:29 +00:00
// type: integer
2017-11-12 23:02:25 -08:00
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
listGPGKeys(ctx, ctx.ContextUser.ID, utils.GetListOptions(ctx))
2017-03-16 02:27:35 +01:00
}
2022-01-20 18:46:10 +01:00
// ListMyGPGKeys get the GPG key list of the authenticated user
2017-03-16 02:27:35 +01:00
func ListMyGPGKeys(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
// ---
// summary: List the authenticated user's GPG keys
2020-01-24 19:00:29 +00: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
2020-01-24 19:00:29 +00:00
// type: integer
2017-11-12 23:02:25 -08:00
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
2019-12-20 18:07:12 +01:00
2022-03-22 08:03:22 +01:00
listGPGKeys(ctx, ctx.Doer.ID, utils.GetListOptions(ctx))
2017-03-16 02:27:35 +01:00
}
2022-01-20 18:46:10 +01:00
// GetGPGKey get the GPG key based on a id
2017-03-16 02:27:35 +01:00
func GetGPGKey(ctx *context.APIContext) {
2017-11-12 23:02:25 -08: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
// format: int64
2017-11-12 23:02:25 -08:00
// required: true
// responses:
// "200":
// "$ref": "#/responses/GPGKey"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
2024-12-24 21:47:45 +08:00
key, err := asymkey_model.GetGPGKeyForUserByID(ctx, ctx.Doer.ID, ctx.PathParamInt64("id"))
2017-03-16 02:27:35 +01:00
if err != nil {
2021-12-10 16:14:24 +08:00
if asymkey_model.IsErrGPGKeyNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2017-03-16 02:27:35 +01:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2017-03-16 02:27:35 +01:00
}
return
}
2024-01-15 10:19:25 +08:00
if err := key.LoadSubKeys(ctx); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2024-01-15 10:19:25 +08:00
return
}
2019-12-20 18:07:12 +01:00
ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
2017-03-16 02:27:35 +01:00
}
// CreateUserGPGKey creates new GPG key to given user by ID.
func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) {
2025-04-01 12:14:01 +02:00
ctx.APIErrorNotFound("Not Found", errors.New("gpg keys setting is not allowed to be visited"))
return
}
2022-03-22 08:03:22 +01:00
token := asymkey_model.VerificationToken(ctx.Doer, 1)
lastToken := asymkey_model.VerificationToken(ctx.Doer, 0)
keys, err := asymkey_model.AddGPGKey(ctx, uid, form.ArmoredKey, token, form.Signature)
2021-12-10 16:14:24 +08:00
if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) {
keys, err = asymkey_model.AddGPGKey(ctx, uid, form.ArmoredKey, lastToken, form.Signature)
}
2017-03-16 02:27:35 +01:00
if err != nil {
HandleAddGPGKeyError(ctx, err, token)
2017-03-16 02:27:35 +01:00
return
}
ctx.JSON(http.StatusCreated, convert.ToGPGKey(keys[0]))
2017-03-16 02:27:35 +01:00
}
// GetVerificationToken returns the current token to be signed for this user
func GetVerificationToken(ctx *context.APIContext) {
// swagger:operation GET /user/gpg_key_token user getVerificationToken
// ---
// summary: Get a Token to verify
// produces:
// - text/plain
// parameters:
// responses:
// "200":
// "$ref": "#/responses/string"
// "404":
// "$ref": "#/responses/notFound"
2022-03-22 08:03:22 +01:00
token := asymkey_model.VerificationToken(ctx.Doer, 1)
2021-12-15 14:59:57 +08:00
ctx.PlainText(http.StatusOK, token)
}
// VerifyUserGPGKey creates new GPG key to given user by ID.
func VerifyUserGPGKey(ctx *context.APIContext) {
// swagger:operation POST /user/gpg_key_verify user userVerifyGPGKey
// ---
// summary: Verify a GPG key
// consumes:
// - application/json
// produces:
// - application/json
// responses:
// "201":
// "$ref": "#/responses/GPGKey"
// "404":
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.VerifyGPGKeyOption)
2022-03-22 08:03:22 +01:00
token := asymkey_model.VerificationToken(ctx.Doer, 1)
lastToken := asymkey_model.VerificationToken(ctx.Doer, 0)
form.KeyID = strings.TrimLeft(form.KeyID, "0")
if form.KeyID == "" {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
return
}
_, err := asymkey_model.VerifyGPGKey(ctx, ctx.Doer.ID, form.KeyID, token, form.Signature)
2021-12-10 16:14:24 +08:00
if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) {
_, err = asymkey_model.VerifyGPGKey(ctx, ctx.Doer.ID, form.KeyID, lastToken, form.Signature)
}
if err != nil {
2021-12-10 16:14:24 +08:00
if asymkey_model.IsErrGPGInvalidTokenSignature(err) {
2025-04-01 12:14:01 +02:00
ctx.APIError(http.StatusUnprocessableEntity, "The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: "+token)
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
}
2024-01-15 10:19:25 +08:00
keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
KeyID: form.KeyID,
IncludeSubKeys: true,
})
if err != nil {
2021-12-10 16:14:24 +08:00
if asymkey_model.IsErrGPGKeyNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
}
return
}
2024-01-15 10:19:25 +08:00
ctx.JSON(http.StatusOK, convert.ToGPGKey(keys[0]))
}
2017-11-12 23:02:25 -08:00
// swagger:parameters userCurrentPostGPGKey
type swaggerUserCurrentPostGPGKey struct {
// in:body
Form api.CreateGPGKeyOption
}
2017-05-02 15:35:59 +02:00
2022-01-20 18:46:10 +01:00
// CreateGPGKey create a GPG key belonging to the authenticated user
2021-01-26 23:36:53 +08:00
func CreateGPGKey(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation POST /user/gpg_keys user userCurrentPostGPGKey
// ---
// summary: Create a GPG key
// consumes:
// - application/json
// produces:
// - application/json
// responses:
// "201":
// "$ref": "#/responses/GPGKey"
// "404":
// "$ref": "#/responses/notFound"
2017-11-12 23:02:25 -08:00
// "422":
// "$ref": "#/responses/validationError"
2019-12-20 18:07:12 +01:00
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.CreateGPGKeyOption)
2022-03-22 08:03:22 +01:00
CreateUserGPGKey(ctx, *form, ctx.Doer.ID)
2017-03-16 02:27:35 +01:00
}
2022-01-20 18:46:10 +01:00
// DeleteGPGKey remove a GPG key belonging to the authenticated user
2017-03-16 02:27:35 +01:00
func DeleteGPGKey(ctx *context.APIContext) {
2017-11-12 23:02:25 -08: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
// format: int64
2017-11-12 23:02:25 -08:00
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) {
2025-04-01 12:14:01 +02:00
ctx.APIErrorNotFound("Not Found", errors.New("gpg keys setting is not allowed to be visited"))
return
}
2024-12-24 21:47:45 +08:00
if err := asymkey_model.DeleteGPGKey(ctx, ctx.Doer, ctx.PathParamInt64("id")); err != nil {
2021-12-10 16:14:24 +08:00
if asymkey_model.IsErrGPGKeyAccessDenied(err) {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusForbidden, "You do not have access to this key")
2017-03-16 02:27:35 +01:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2017-03-16 02:27:35 +01:00
}
return
}
2019-12-20 18:07:12 +01:00
ctx.Status(http.StatusNoContent)
2017-03-16 02:27:35 +01:00
}
// HandleAddGPGKeyError handle add GPGKey error
func HandleAddGPGKeyError(ctx *context.APIContext, err error, token string) {
2017-03-16 02:27:35 +01:00
switch {
2021-12-10 16:14:24 +08:00
case asymkey_model.IsErrGPGKeyAccessDenied(err):
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusUnprocessableEntity, "You do not have access to this GPG key")
2021-12-10 16:14:24 +08:00
case asymkey_model.IsErrGPGKeyIDAlreadyUsed(err):
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusUnprocessableEntity, "A key with the same id already exists")
2021-12-10 16:14:24 +08:00
case asymkey_model.IsErrGPGKeyParsing(err):
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusUnprocessableEntity, err)
2021-12-10 16:14:24 +08:00
case asymkey_model.IsErrGPGNoEmailFound(err):
2025-04-01 12:14:01 +02:00
ctx.APIError(http.StatusNotFound, "None of the emails attached to the GPG key could be found. It may still be added if you provide a valid signature for the token: "+token)
2021-12-10 16:14:24 +08:00
case asymkey_model.IsErrGPGInvalidTokenSignature(err):
2025-04-01 12:14:01 +02:00
ctx.APIError(http.StatusUnprocessableEntity, "The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: "+token)
2017-03-16 02:27:35 +01:00
default:
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2017-03-16 02:27:35 +01:00
}
}