Skip to content

Commit 58827bf

Browse files
committed
Implementing api routing and all necessarry code in progress
1 parent e9c1d02 commit 58827bf

File tree

6 files changed

+102
-4
lines changed

6 files changed

+102
-4
lines changed

models/error.go

+16
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ func (err ErrKeyNameAlreadyUsed) Error() string {
201201
return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name)
202202
}
203203

204+
type ErrGPGKeyAccessDenied struct {
205+
UserID int64
206+
KeyID int64
207+
Note string
208+
}
209+
210+
func IsErrGPGKeyAccessDenied(err error) bool {
211+
_, ok := err.(ErrGPGKeyAccessDenied)
212+
return ok
213+
}
214+
215+
func (err ErrGPGKeyAccessDenied) Error() string {
216+
return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d, note: %s]",
217+
err.UserID, err.KeyID, err.Note)
218+
}
219+
204220
type ErrKeyAccessDenied struct {
205221
UserID int64
206222
KeyID int64

models/gpg_key.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"time"
9+
10+
"github.com/gogits/gogs/modules/log"
11+
)
12+
13+
//TODO maybe refector with ssh-key ?
14+
//TODO database behind
15+
16+
// PublicGPGKey represents a GPG key.
17+
type PublicGPGKey struct {
18+
ID int64 `xorm:"pk autoincr"`
19+
OwnerID int64 `xorm:"INDEX NOT NULL"`
20+
Name string `xorm:"NOT NULL"`
21+
Fingerprint string `xorm:"NOT NULL"`
22+
Content string `xorm:"TEXT NOT NULL"`
23+
24+
Created time.Time `xorm:"-"`
25+
}
26+
27+
// ListPublicGPGKeys returns a list of public keys belongs to given user.
28+
func ListPublicGPGKeys(uid int64) ([]*PublicGPGKey, error) {
29+
keys := make([]*PublicGPGKey, 0, 5)
30+
return keys, x.Where("owner_id=?", uid).Find(&keys)
31+
}
32+
33+
// GetPublicGPGKeyByID returns public key by given ID.
34+
func GetPublicGPGKeyByID(keyID int64) (*PublicGPGKey, error) {
35+
key := new(PublicGPGKey)
36+
has, err := x.Id(keyID).Get(key)
37+
if err != nil {
38+
return nil, err
39+
} else if !has {
40+
return nil, ErrKeyNotExist{keyID}
41+
}
42+
return key, nil
43+
}
44+
45+
// CheckPublicGPGKeyString checks if the given public key string is a valid GPG key.
46+
// The function returns the actual public key line on success.
47+
func CheckPublicGPGKeyString(content string) (_ string, err error) {
48+
//TODO Implement
49+
return "", nil
50+
}
51+
52+
// AddPublicGPGKey adds new public key to database.
53+
func AddPublicGPGKey(ownerID int64, name, content string) (*PublicKey, error) {
54+
log.Trace(content)
55+
//TODO Implement
56+
return nil, nil
57+
}
58+
59+
// DeletePublicGPGKey deletes GPG key information in database.
60+
func DeletePublicGPGKey(doer *User, id int64) (err error) {
61+
//TODO Implement
62+
return nil
63+
}

routers/api/v1/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func RegisterRoutes(m *macaron.Macaron) {
206206
})
207207
m.Group("/gpg_keys", func() {
208208
m.Combo("").Get(user.ListMyPublicGPGKeys).
209-
Post(bind(api.CreateGPGKeyOption{}), user.CreatePublicGPGKey)
209+
Post(bind(api.CreateKeyOption{}), user.CreatePublicGPGKey) //TODO use specific api descriptor
210210
m.Combo("/:id").Get(user.GetPublicGPGKey).
211211
Delete(user.DeletePublicGPGKey)
212212
})

routers/api/v1/convert/convert.go

+10
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
8484
}
8585
}
8686

87+
//TODO be more specific to GPG key with a api.PublicGPGKey ?
88+
func ToPublicGPGKey(apiLink string, key *models.PublicGPGKey) *api.PublicKey {
89+
return &api.PublicKey{
90+
ID: key.ID,
91+
Key: key.Content,
92+
URL: apiLink + com.ToStr(key.ID),
93+
Title: key.Name,
94+
Created: key.Created,
95+
}
96+
}
8797
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
8898
config := map[string]string{
8999
"url": w.URL,

routers/api/v1/repo/key.go

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ func GetDeployKey(ctx *context.APIContext) {
6161
ctx.JSON(200, convert.ToDeployKey(apiLink, key))
6262
}
6363

64+
func HandleCheckGPGKeyStringError(ctx *context.APIContext, err error) {
65+
//TODO Implement
66+
}
67+
68+
func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
69+
//TODO Implement
70+
}
71+
6472
func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
6573
if models.IsErrKeyUnableVerify(err) {
6674
ctx.Error(422, "", "Unable to verify key content")

routers/api/v1/user/gpg_keys.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func listPublicGPGKeys(ctx *context.APIContext, uid int64) {
2626
}
2727

2828
apiLink := composePublicGPGKeysAPILink()
29-
apiKeys := make([]*api.PublicGPGKey, len(keys))
29+
apiKeys := make([]*api.PublicKey, len(keys))
3030
for i := range keys {
3131
apiKeys[i] = convert.ToPublicGPGKey(apiLink, keys[i])
3232
}
@@ -56,7 +56,7 @@ func GetPublicGPGKey(ctx *context.APIContext) {
5656
}
5757

5858
// CreateUserPublicGPGKey creates new public GPG key to given user by ID.
59-
func CreateUserPublicGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
59+
func CreateUserPublicGPGKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
6060
content, err := models.CheckPublicGPGKeyString(form.Key)
6161
if err != nil {
6262
repo.HandleCheckGPGKeyStringError(ctx, err)
@@ -72,8 +72,9 @@ func CreateUserPublicGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption
7272
ctx.JSON(201, convert.ToPublicKey(apiLink, key))
7373
}
7474

75+
//TODO Update api
7576
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-GPG-Keys#create-a-public-key
76-
func CreatePublicGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
77+
func CreatePublicGPGKey(ctx *context.APIContext, form api.CreateKeyOption) {
7778
CreateUserPublicGPGKey(ctx, form, ctx.User.Id)
7879
}
7980

0 commit comments

Comments
 (0)