summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/user/gpg_key.go
blob: 678816ccda71a31ad7114b463ce543c653e277cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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 (
	api "code.gitea.io/sdk/gitea"

	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/routers/api/v1/convert"
)

func composePublicGPGKeysAPILink() string {
	return setting.AppURL + "api/v1/user/gpg_keys/"
}

func listGPGKeys(ctx *context.APIContext, uid int64) {
	keys, err := models.ListGPGKeys(uid)
	if err != nil {
		ctx.Error(500, "ListGPGKeys", err)
		return
	}

	apiKeys := make([]*api.GPGKey, len(keys))
	for i := range keys {
		apiKeys[i] = convert.ToGPGKey(keys[i])
	}

	ctx.JSON(200, &apiKeys)
}

//ListGPGKeys get the GPG key list of a user
func ListGPGKeys(ctx *context.APIContext) {
	// swagger:route GET /users/{username}/gpg_keys userListGPGKeys
	//
	//     Produces:
	//     - application/json
	//
	//     Responses:
	//       200: GPGKeyList
	//       500: error

	user := GetUserByParams(ctx)
	if ctx.Written() {
		return
	}
	listGPGKeys(ctx, user.ID)
}

//ListMyGPGKeys get the GPG key list of the logged user
func ListMyGPGKeys(ctx *context.APIContext) {
	// swagger:route GET /user/gpg_keys userCurrentListGPGKeys
	//
	//     Produces:
	//     - application/json
	//
	//     Responses:
	//       200: GPGKeyList
	//       500: error

	listGPGKeys(ctx, ctx.User.ID)
}

//GetGPGKey get the GPG key based on a id
func GetGPGKey(ctx *context.APIContext) {
	// swagger:route GET /user/gpg_keys/{id} userCurrentGetGPGKey
	//
	//     Produces:
	//     - application/json
	//
	//     Responses:
	//       200: GPGKey
	//       404: notFound
	//       500: error

	key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
	if err != nil {
		if models.IsErrGPGKeyNotExist(err) {
			ctx.Status(404)
		} else {
			ctx.Error(500, "GetGPGKeyByID", err)
		}
		return
	}
	ctx.JSON(200, convert.ToGPGKey(key))
}

// 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
	}
	ctx.JSON(201, convert.ToGPGKey(key))
}

//CreateGPGKey associate a GPG key to the current user
func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
	// swagger:route POST /user/gpg_keys userCurrentPostGPGKey
	//
	//     Consumes:
	//     - application/json
	//
	//     Produces:
	//     - application/json
	//
	//     Responses:
	//       201: GPGKey
	//       422: validationError
	//       500: error

	CreateUserGPGKey(ctx, form, ctx.User.ID)
}

//DeleteGPGKey remove a GPG key associated to the current user
func DeleteGPGKey(ctx *context.APIContext) {
	// swagger:route DELETE /user/gpg_keys/{id} userCurrentDeleteGPGKey
	//
	//     Produces:
	//     - application/json
	//
	//     Responses:
	//       204: empty
	//       403: forbidden
	//       500: error

	if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
		if models.IsErrGPGKeyAccessDenied(err) {
			ctx.Error(403, "", "You do not have access to this key")
		} else {
			ctx.Error(500, "DeleteGPGKey", err)
		}
		return
	}

	ctx.Status(204)
}

// HandleAddGPGKeyError handle add GPGKey error
func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
	switch {
	case models.IsErrGPGKeyAccessDenied(err):
		ctx.Error(422, "", "You do not have access to this gpg key")
	case models.IsErrGPGKeyIDAlreadyUsed(err):
		ctx.Error(422, "", "A key with the same keyid is allready in database")
	default:
		ctx.Error(500, "AddGPGKey", err)
	}
}