2017-03-16 06:57:35 +05:30
// 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 (
2021-07-13 18:58:07 +05:30
"fmt"
2019-12-20 22:37:12 +05:30
"net/http"
2017-03-16 06:57:35 +05:30
"code.gitea.io/gitea/models"
2021-09-24 17:02:56 +05:30
"code.gitea.io/gitea/models/db"
2017-03-16 06:57:35 +05:30
"code.gitea.io/gitea/modules/context"
2019-11-10 10:11:51 +05:30
"code.gitea.io/gitea/modules/convert"
2019-08-23 22:10:30 +05:30
api "code.gitea.io/gitea/modules/structs"
2021-01-26 21:06:53 +05:30
"code.gitea.io/gitea/modules/web"
2020-01-25 00:30:29 +05:30
"code.gitea.io/gitea/routers/api/v1/utils"
2017-03-16 06:57:35 +05:30
)
2021-09-24 17:02:56 +05:30
func listGPGKeys ( ctx * context . APIContext , uid int64 , listOptions db . ListOptions ) {
2020-01-25 00:30:29 +05:30
keys , err := models . ListGPGKeys ( uid , listOptions )
2017-03-16 06:57:35 +05:30
if err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "ListGPGKeys" , err )
2017-03-16 06:57:35 +05:30
return
}
apiKeys := make ( [ ] * api . GPGKey , len ( keys ) )
for i := range keys {
apiKeys [ i ] = convert . ToGPGKey ( keys [ i ] )
}
2021-08-12 18:13:08 +05:30
total , err := models . CountUserGPGKeys ( uid )
if err != nil {
ctx . InternalServerError ( err )
return
}
ctx . SetTotalCountHeader ( total )
2019-12-20 22:37:12 +05:30
ctx . JSON ( http . StatusOK , & apiKeys )
2017-03-16 06:57:35 +05:30
}
//ListGPGKeys get the GPG key list of a user
func ListGPGKeys ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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-25 00:30:29 +05:30
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
2020-06-09 10:27:38 +05:30
// description: page size of results
2020-01-25 00:30:29 +05:30
// type: integer
2017-11-13 12:32:25 +05:30
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
2019-12-20 22:37:12 +05:30
2017-03-16 06:57:35 +05:30
user := GetUserByParams ( ctx )
if ctx . Written ( ) {
return
}
2020-01-25 00:30:29 +05:30
listGPGKeys ( ctx , user . ID , utils . GetListOptions ( ctx ) )
2017-03-16 06:57:35 +05:30
}
2017-11-13 12:32:25 +05:30
//ListMyGPGKeys get the GPG key list of the authenticated user
2017-03-16 06:57:35 +05:30
func ListMyGPGKeys ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
// ---
// summary: List the authenticated user's GPG keys
2020-01-25 00:30:29 +05:30
// parameters:
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
2020-06-09 10:27:38 +05:30
// description: page size of results
2020-01-25 00:30:29 +05:30
// type: integer
2017-11-13 12:32:25 +05:30
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/GPGKeyList"
2019-12-20 22:37:12 +05:30
2020-01-25 00:30:29 +05:30
listGPGKeys ( ctx , ctx . User . ID , utils . GetListOptions ( ctx ) )
2017-03-16 06:57:35 +05:30
}
//GetGPGKey get the GPG key based on a id
func GetGPGKey ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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 09:10:42 +05:30
// format: int64
2017-11-13 12:32:25 +05:30
// required: true
// responses:
// "200":
// "$ref": "#/responses/GPGKey"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 22:37:12 +05:30
2017-03-16 06:57:35 +05:30
key , err := models . GetGPGKeyByID ( ctx . ParamsInt64 ( ":id" ) )
if err != nil {
if models . IsErrGPGKeyNotExist ( err ) {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2017-03-16 06:57:35 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetGPGKeyByID" , err )
2017-03-16 06:57:35 +05:30
}
return
}
2019-12-20 22:37:12 +05:30
ctx . JSON ( http . StatusOK , convert . ToGPGKey ( key ) )
2017-03-16 06:57:35 +05:30
}
// CreateUserGPGKey creates new GPG key to given user by ID.
func CreateUserGPGKey ( ctx * context . APIContext , form api . CreateGPGKeyOption , uid int64 ) {
2021-07-13 18:58:07 +05:30
token := models . VerificationToken ( ctx . User , 1 )
lastToken := models . VerificationToken ( ctx . User , 0 )
keys , err := models . AddGPGKey ( uid , form . ArmoredKey , token , form . Signature )
if err != nil && models . IsErrGPGInvalidTokenSignature ( err ) {
keys , err = models . AddGPGKey ( uid , form . ArmoredKey , lastToken , form . Signature )
}
2017-03-16 06:57:35 +05:30
if err != nil {
2021-07-13 18:58:07 +05:30
HandleAddGPGKeyError ( ctx , err , token )
2017-03-16 06:57:35 +05:30
return
}
2020-08-21 16:15:50 +05:30
ctx . JSON ( http . StatusCreated , convert . ToGPGKey ( keys [ 0 ] ) )
2017-03-16 06:57:35 +05:30
}
2021-07-13 18:58:07 +05:30
// 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"
token := models . VerificationToken ( ctx . User , 1 )
ctx . PlainText ( http . StatusOK , [ ] byte ( 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 )
token := models . VerificationToken ( ctx . User , 1 )
lastToken := models . VerificationToken ( ctx . User , 0 )
_ , err := models . VerifyGPGKey ( ctx . User . ID , form . KeyID , token , form . Signature )
if err != nil && models . IsErrGPGInvalidTokenSignature ( err ) {
_ , err = models . VerifyGPGKey ( ctx . User . ID , form . KeyID , lastToken , form . Signature )
}
if err != nil {
if models . IsErrGPGInvalidTokenSignature ( err ) {
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 )
}
key , err := models . GetGPGKeysByKeyID ( form . KeyID )
if err != nil {
if models . IsErrGPGKeyNotExist ( err ) {
ctx . NotFound ( )
} else {
ctx . Error ( http . StatusInternalServerError , "GetGPGKeysByKeyID" , err )
}
return
}
ctx . JSON ( http . StatusOK , convert . ToGPGKey ( key [ 0 ] ) )
}
2017-11-13 12:32:25 +05:30
// swagger:parameters userCurrentPostGPGKey
type swaggerUserCurrentPostGPGKey struct {
// in:body
Form api . CreateGPGKeyOption
}
2017-05-02 19:05:59 +05:30
2017-11-13 12:32:25 +05:30
//CreateGPGKey create a GPG key belonging to the authenticated user
2021-01-26 21:06:53 +05:30
func CreateGPGKey ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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 02:55:54 +05:30
// "404":
// "$ref": "#/responses/notFound"
2017-11-13 12:32:25 +05:30
// "422":
// "$ref": "#/responses/validationError"
2019-12-20 22:37:12 +05:30
2021-01-26 21:06:53 +05:30
form := web . GetForm ( ctx ) . ( * api . CreateGPGKeyOption )
CreateUserGPGKey ( ctx , * form , ctx . User . ID )
2017-03-16 06:57:35 +05:30
}
2017-11-13 12:32:25 +05:30
//DeleteGPGKey remove a GPG key belonging to the authenticated user
2017-03-16 06:57:35 +05:30
func DeleteGPGKey ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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 09:10:42 +05:30
// format: int64
2017-11-13 12:32:25 +05:30
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "403":
// "$ref": "#/responses/forbidden"
2020-05-29 02:55:54 +05:30
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 22:37:12 +05:30
2017-03-16 06:57:35 +05:30
if err := models . DeleteGPGKey ( ctx . User , ctx . ParamsInt64 ( ":id" ) ) ; err != nil {
if models . IsErrGPGKeyAccessDenied ( err ) {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusForbidden , "" , "You do not have access to this key" )
2017-03-16 06:57:35 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "DeleteGPGKey" , err )
2017-03-16 06:57:35 +05:30
}
return
}
2019-12-20 22:37:12 +05:30
ctx . Status ( http . StatusNoContent )
2017-03-16 06:57:35 +05:30
}
// HandleAddGPGKeyError handle add GPGKey error
2021-07-13 18:58:07 +05:30
func HandleAddGPGKeyError ( ctx * context . APIContext , err error , token string ) {
2017-03-16 06:57:35 +05:30
switch {
case models . IsErrGPGKeyAccessDenied ( err ) :
2020-05-29 02:55:54 +05:30
ctx . Error ( http . StatusUnprocessableEntity , "GPGKeyAccessDenied" , "You do not have access to this GPG key" )
2017-03-16 06:57:35 +05:30
case models . IsErrGPGKeyIDAlreadyUsed ( err ) :
2020-05-29 02:55:54 +05:30
ctx . Error ( http . StatusUnprocessableEntity , "GPGKeyIDAlreadyUsed" , "A key with the same id already exists" )
case models . IsErrGPGKeyParsing ( err ) :
ctx . Error ( http . StatusUnprocessableEntity , "GPGKeyParsing" , err )
case models . IsErrGPGNoEmailFound ( err ) :
2021-07-13 18:58:07 +05:30
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 ) )
case models . IsErrGPGInvalidTokenSignature ( err ) :
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 06:57:35 +05:30
default :
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "AddGPGKey" , err )
2017-03-16 06:57:35 +05:30
}
}