2017-03-16 04:27:35 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2017-03-16 04:27:35 +03:00
package user
import (
2021-07-13 16:28:07 +03:00
"fmt"
2019-12-20 20:07:12 +03:00
"net/http"
2022-08-21 09:50:15 +03:00
"strings"
2019-12-20 20:07:12 +03:00
2021-12-10 11:14:24 +03:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
2021-09-24 14:32:56 +03:00
"code.gitea.io/gitea/models/db"
2017-03-16 04:27:35 +03:00
"code.gitea.io/gitea/modules/context"
2019-08-23 19:40:30 +03:00
api "code.gitea.io/gitea/modules/structs"
2021-01-26 18:36:53 +03:00
"code.gitea.io/gitea/modules/web"
2020-01-24 22:00:29 +03:00
"code.gitea.io/gitea/routers/api/v1/utils"
2022-12-29 05:57:15 +03:00
"code.gitea.io/gitea/services/convert"
2017-03-16 04:27:35 +03:00
)
2021-09-24 14:32:56 +03:00
func listGPGKeys ( ctx * context . APIContext , uid int64 , listOptions db . ListOptions ) {
2022-03-22 18:22:54 +03:00
keys , err := asymkey_model . ListGPGKeys ( ctx , 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 ] )
}
2023-09-25 20:24:35 +03:00
total , err := asymkey_model . CountUserGPGKeys ( ctx , uid )
2021-08-12 15:43:08 +03:00
if err != nil {
ctx . InternalServerError ( err )
return
}
ctx . SetTotalCountHeader ( total )
2019-12-20 20:07:12 +03:00
ctx . JSON ( http . StatusOK , & apiKeys )
2017-03-16 04:27:35 +03:00
}
2022-01-20 20:46:10 +03:00
// ListGPGKeys get the GPG key list of a user
2017-03-16 04:27:35 +03:00
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
2020-06-09 07:57:38 +03:00
// description: page size of results
2020-01-24 22:00:29 +03:00
// type: integer
2017-11-13 10:02:25 +03:00
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
2023-09-13 05:37:54 +03:00
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 20:07:12 +03:00
2022-03-26 12:04:22 +03:00
listGPGKeys ( ctx , ctx . ContextUser . ID , utils . GetListOptions ( ctx ) )
2017-03-16 04:27:35 +03:00
}
2022-01-20 20:46:10 +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
2020-06-09 07:57:38 +03:00
// description: page size of results
2020-01-24 22:00:29 +03:00
// 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
2022-03-22 10:03:22 +03:00
listGPGKeys ( ctx , ctx . Doer . ID , utils . GetListOptions ( ctx ) )
2017-03-16 04:27:35 +03:00
}
2022-01-20 20:46:10 +03:00
// GetGPGKey get the GPG key based on a id
2017-03-16 04:27:35 +03:00
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
2023-09-25 20:24:35 +03:00
key , err := asymkey_model . GetGPGKeyByID ( ctx , ctx . ParamsInt64 ( ":id" ) )
2017-03-16 04:27:35 +03:00
if err != nil {
2021-12-10 11:14:24 +03:00
if asymkey_model . 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 ) {
2022-03-22 10:03:22 +03:00
token := asymkey_model . VerificationToken ( ctx . Doer , 1 )
lastToken := asymkey_model . VerificationToken ( ctx . Doer , 0 )
2021-07-13 16:28:07 +03:00
2023-09-25 20:24:35 +03:00
keys , err := asymkey_model . AddGPGKey ( ctx , uid , form . ArmoredKey , token , form . Signature )
2021-12-10 11:14:24 +03:00
if err != nil && asymkey_model . IsErrGPGInvalidTokenSignature ( err ) {
2023-09-25 20:24:35 +03:00
keys , err = asymkey_model . AddGPGKey ( ctx , uid , form . ArmoredKey , lastToken , form . Signature )
2021-07-13 16:28:07 +03:00
}
2017-03-16 04:27:35 +03:00
if err != nil {
2021-07-13 16:28:07 +03:00
HandleAddGPGKeyError ( ctx , err , token )
2017-03-16 04:27:35 +03:00
return
}
2020-08-21 13:45:50 +03:00
ctx . JSON ( http . StatusCreated , convert . ToGPGKey ( keys [ 0 ] ) )
2017-03-16 04:27:35 +03:00
}
2021-07-13 16:28:07 +03: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 10:03:22 +03:00
token := asymkey_model . VerificationToken ( ctx . Doer , 1 )
2021-12-15 09:59:57 +03:00
ctx . PlainText ( http . StatusOK , token )
2021-07-13 16:28:07 +03:00
}
// 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 10:03:22 +03:00
token := asymkey_model . VerificationToken ( ctx . Doer , 1 )
lastToken := asymkey_model . VerificationToken ( ctx . Doer , 0 )
2021-07-13 16:28:07 +03:00
2022-08-21 09:50:15 +03:00
form . KeyID = strings . TrimLeft ( form . KeyID , "0" )
if form . KeyID == "" {
ctx . NotFound ( )
return
}
2022-03-22 10:03:22 +03:00
_ , err := asymkey_model . VerifyGPGKey ( ctx . Doer . ID , form . KeyID , token , form . Signature )
2021-12-10 11:14:24 +03:00
if err != nil && asymkey_model . IsErrGPGInvalidTokenSignature ( err ) {
2022-03-22 10:03:22 +03:00
_ , err = asymkey_model . VerifyGPGKey ( ctx . Doer . ID , form . KeyID , lastToken , form . Signature )
2021-07-13 16:28:07 +03:00
}
if err != nil {
2021-12-10 11:14:24 +03:00
if asymkey_model . IsErrGPGInvalidTokenSignature ( err ) {
2021-07-13 16:28:07 +03:00
ctx . Error ( http . StatusUnprocessableEntity , "GPGInvalidSignature" , fmt . Sprintf ( "The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s" , token ) )
return
}
ctx . Error ( http . StatusInternalServerError , "VerifyUserGPGKey" , err )
}
2023-09-25 20:24:35 +03:00
key , err := asymkey_model . GetGPGKeysByKeyID ( ctx , form . KeyID )
2021-07-13 16:28:07 +03:00
if err != nil {
2021-12-10 11:14:24 +03:00
if asymkey_model . IsErrGPGKeyNotExist ( err ) {
2021-07-13 16:28:07 +03:00
ctx . NotFound ( )
} else {
ctx . Error ( http . StatusInternalServerError , "GetGPGKeysByKeyID" , err )
}
return
}
ctx . JSON ( http . StatusOK , convert . ToGPGKey ( key [ 0 ] ) )
}
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
2022-01-20 20:46:10 +03:00
// CreateGPGKey create a GPG key belonging to the authenticated user
2021-01-26 18:36:53 +03:00
func CreateGPGKey ( ctx * context . APIContext ) {
2017-11-13 10:02:25 +03: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"
2020-05-29 00:25:54 +03:00
// "404":
// "$ref": "#/responses/notFound"
2017-11-13 10:02:25 +03:00
// "422":
// "$ref": "#/responses/validationError"
2019-12-20 20:07:12 +03:00
2021-01-26 18:36:53 +03:00
form := web . GetForm ( ctx ) . ( * api . CreateGPGKeyOption )
2022-03-22 10:03:22 +03:00
CreateUserGPGKey ( ctx , * form , ctx . Doer . ID )
2017-03-16 04:27:35 +03:00
}
2022-01-20 20:46:10 +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"
2020-05-29 00:25:54 +03:00
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 20:07:12 +03:00
2023-09-25 20:24:35 +03:00
if err := asymkey_model . DeleteGPGKey ( ctx , ctx . Doer , ctx . ParamsInt64 ( ":id" ) ) ; err != nil {
2021-12-10 11:14:24 +03:00
if asymkey_model . 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
2021-07-13 16:28:07 +03:00
func HandleAddGPGKeyError ( ctx * context . APIContext , err error , token string ) {
2017-03-16 04:27:35 +03:00
switch {
2021-12-10 11:14:24 +03:00
case asymkey_model . IsErrGPGKeyAccessDenied ( err ) :
2020-05-29 00:25:54 +03:00
ctx . Error ( http . StatusUnprocessableEntity , "GPGKeyAccessDenied" , "You do not have access to this GPG key" )
2021-12-10 11:14:24 +03:00
case asymkey_model . IsErrGPGKeyIDAlreadyUsed ( err ) :
2020-05-29 00:25:54 +03:00
ctx . Error ( http . StatusUnprocessableEntity , "GPGKeyIDAlreadyUsed" , "A key with the same id already exists" )
2021-12-10 11:14:24 +03:00
case asymkey_model . IsErrGPGKeyParsing ( err ) :
2020-05-29 00:25:54 +03:00
ctx . Error ( http . StatusUnprocessableEntity , "GPGKeyParsing" , err )
2021-12-10 11:14:24 +03:00
case asymkey_model . IsErrGPGNoEmailFound ( err ) :
2021-07-13 16:28:07 +03:00
ctx . Error ( http . StatusNotFound , "GPGNoEmailFound" , fmt . Sprintf ( "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: %s" , token ) )
2021-12-10 11:14:24 +03:00
case asymkey_model . IsErrGPGInvalidTokenSignature ( err ) :
2021-07-13 16:28:07 +03:00
ctx . Error ( http . StatusUnprocessableEntity , "GPGInvalidSignature" , fmt . Sprintf ( "The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s" , token ) )
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
}
}