diff options
author | Antoine GIRARD <sapk@users.noreply.github.com> | 2017-03-16 02:27:35 +0100 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-03-16 09:27:35 +0800 |
commit | ca1c3f1926eff992a2458f26cb24ed2f35265b05 (patch) | |
tree | a0c859357539aebf4c3ce521fc8b5d1e6a870364 /routers/api/v1 | |
parent | 43c5469f81851c084fa6ac84d8379ae949c3a05c (diff) | |
download | gitea-ca1c3f1926eff992a2458f26cb24ed2f35265b05.tar.gz gitea-ca1c3f1926eff992a2458f26cb24ed2f35265b05.zip |
Implement GPG api (#710)
* Implement GPG API
* Better handle error
* Apply review recommendation + simplify database operations
* Remove useless comments
Diffstat (limited to 'routers/api/v1')
-rw-r--r-- | routers/api/v1/api.go | 8 | ||||
-rw-r--r-- | routers/api/v1/convert/convert.go | 45 | ||||
-rw-r--r-- | routers/api/v1/user/gpg_key.go | 102 |
3 files changed, 155 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ca4592c607..2a1f5e196c 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -254,6 +254,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/users", func() { m.Group("/:username", func() { m.Get("/keys", user.ListPublicKeys) + m.Get("/gpg_keys", user.ListGPGKeys) m.Get("/followers", user.ListFollowers) m.Group("/following", func() { @@ -286,6 +287,13 @@ func RegisterRoutes(m *macaron.Macaron) { Delete(user.DeletePublicKey) }) + m.Group("/gpg_keys", func() { + m.Combo("").Get(user.ListMyGPGKeys). + Post(bind(api.CreateGPGKeyOption{}), user.CreateGPGKey) + m.Combo("/:id").Get(user.GetGPGKey). + Delete(user.DeleteGPGKey) + }) + m.Combo("/repos").Get(user.ListMyRepos). Post(bind(api.CreateRepoOption{}), repo.Create) diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go index 9729e65452..7485ac9e2a 100644 --- a/routers/api/v1/convert/convert.go +++ b/routers/api/v1/convert/convert.go @@ -73,6 +73,51 @@ func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey { } } +// ToGPGKey converts models.GPGKey to api.GPGKey +func ToGPGKey(key *models.GPGKey) *api.GPGKey { + subkeys := make([]*api.GPGKey, len(key.SubsKey)) + for id, k := range key.SubsKey { + subkeys[id] = &api.GPGKey{ + ID: k.ID, + PrimaryKeyID: k.PrimaryKeyID, + KeyID: k.KeyID, + PublicKey: k.Content, + Created: k.Created, + Expires: k.Expired, + CanSign: k.CanSign, + CanEncryptComms: k.CanEncryptComms, + CanEncryptStorage: k.CanEncryptStorage, + CanCertify: k.CanSign, + } + } + emails := make([]*api.GPGKeyEmail, len(key.Emails)) + for i, e := range key.Emails { + emails[i] = ToGPGKeyEmail(e) + } + return &api.GPGKey{ + ID: key.ID, + PrimaryKeyID: key.PrimaryKeyID, + KeyID: key.KeyID, + PublicKey: key.Content, + Created: key.Created, + Expires: key.Expired, + Emails: emails, + SubsKey: subkeys, + CanSign: key.CanSign, + CanEncryptComms: key.CanEncryptComms, + CanEncryptStorage: key.CanEncryptStorage, + CanCertify: key.CanSign, + } +} + +// ToGPGKeyEmail convert models.EmailAddress to api.GPGKeyEmail +func ToGPGKeyEmail(email *models.EmailAddress) *api.GPGKeyEmail { + return &api.GPGKeyEmail{ + Email: email.Email, + Verified: email.IsActivated, + } +} + // ToHook convert models.Webhook to api.Hook func ToHook(repoLink string, w *models.Webhook) *api.Hook { config := map[string]string{ diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go new file mode 100644 index 0000000000..bb4ae42431 --- /dev/null +++ b/routers/api/v1/user/gpg_key.go @@ -0,0 +1,102 @@ +// 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) { + 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) { + listGPGKeys(ctx, ctx.User.ID) +} + +//GetGPGKey get the GPG key based on a id +func GetGPGKey(ctx *context.APIContext) { + 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) { + CreateUserGPGKey(ctx, form, ctx.User.ID) +} + +//DeleteGPGKey remove a GPG key associated to the current user +func DeleteGPGKey(ctx *context.APIContext) { + 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) + } +} |