Skip to content

Commit

Permalink
Implementing api routing and all necessarry code in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed May 24, 2016
1 parent e9c1d02 commit 58827bf
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
16 changes: 16 additions & 0 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ func (err ErrKeyNameAlreadyUsed) Error() string {
return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name)
}

type ErrGPGKeyAccessDenied struct {
UserID int64
KeyID int64
Note string
}

func IsErrGPGKeyAccessDenied(err error) bool {
_, ok := err.(ErrGPGKeyAccessDenied)
return ok
}

func (err ErrGPGKeyAccessDenied) Error() string {
return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d, note: %s]",
err.UserID, err.KeyID, err.Note)
}

type ErrKeyAccessDenied struct {
UserID int64
KeyID int64
Expand Down
63 changes: 63 additions & 0 deletions models/gpg_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2014 The Gogs 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 models

import (
"time"

"github.com/gogits/gogs/modules/log"
)

//TODO maybe refector with ssh-key ?
//TODO database behind

// PublicGPGKey represents a GPG key.
type PublicGPGKey struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"INDEX NOT NULL"`
Name string `xorm:"NOT NULL"`
Fingerprint string `xorm:"NOT NULL"`
Content string `xorm:"TEXT NOT NULL"`

Created time.Time `xorm:"-"`
}

// ListPublicGPGKeys returns a list of public keys belongs to given user.
func ListPublicGPGKeys(uid int64) ([]*PublicGPGKey, error) {
keys := make([]*PublicGPGKey, 0, 5)
return keys, x.Where("owner_id=?", uid).Find(&keys)
}

// GetPublicGPGKeyByID returns public key by given ID.
func GetPublicGPGKeyByID(keyID int64) (*PublicGPGKey, error) {
key := new(PublicGPGKey)
has, err := x.Id(keyID).Get(key)
if err != nil {
return nil, err
} else if !has {
return nil, ErrKeyNotExist{keyID}
}
return key, nil
}

// CheckPublicGPGKeyString checks if the given public key string is a valid GPG key.
// The function returns the actual public key line on success.
func CheckPublicGPGKeyString(content string) (_ string, err error) {
//TODO Implement
return "", nil
}

// AddPublicGPGKey adds new public key to database.
func AddPublicGPGKey(ownerID int64, name, content string) (*PublicKey, error) {
log.Trace(content)
//TODO Implement
return nil, nil
}

// DeletePublicGPGKey deletes GPG key information in database.
func DeletePublicGPGKey(doer *User, id int64) (err error) {
//TODO Implement
return nil
}
2 changes: 1 addition & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func RegisterRoutes(m *macaron.Macaron) {
})
m.Group("/gpg_keys", func() {
m.Combo("").Get(user.ListMyPublicGPGKeys).
Post(bind(api.CreateGPGKeyOption{}), user.CreatePublicGPGKey)
Post(bind(api.CreateKeyOption{}), user.CreatePublicGPGKey) //TODO use specific api descriptor
m.Combo("/:id").Get(user.GetPublicGPGKey).
Delete(user.DeletePublicGPGKey)
})
Expand Down
10 changes: 10 additions & 0 deletions routers/api/v1/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
}
}

//TODO be more specific to GPG key with a api.PublicGPGKey ?
func ToPublicGPGKey(apiLink string, key *models.PublicGPGKey) *api.PublicKey {
return &api.PublicKey{
ID: key.ID,
Key: key.Content,
URL: apiLink + com.ToStr(key.ID),
Title: key.Name,
Created: key.Created,
}
}
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
config := map[string]string{
"url": w.URL,
Expand Down
8 changes: 8 additions & 0 deletions routers/api/v1/repo/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func GetDeployKey(ctx *context.APIContext) {
ctx.JSON(200, convert.ToDeployKey(apiLink, key))
}

func HandleCheckGPGKeyStringError(ctx *context.APIContext, err error) {
//TODO Implement
}

func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
//TODO Implement
}

func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
if models.IsErrKeyUnableVerify(err) {
ctx.Error(422, "", "Unable to verify key content")
Expand Down
7 changes: 4 additions & 3 deletions routers/api/v1/user/gpg_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func listPublicGPGKeys(ctx *context.APIContext, uid int64) {
}

apiLink := composePublicGPGKeysAPILink()
apiKeys := make([]*api.PublicGPGKey, len(keys))
apiKeys := make([]*api.PublicKey, len(keys))
for i := range keys {
apiKeys[i] = convert.ToPublicGPGKey(apiLink, keys[i])
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func GetPublicGPGKey(ctx *context.APIContext) {
}

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

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

Expand Down

0 comments on commit 58827bf

Please sign in to comment.