2015-12-03 00:24:37 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
2015-12-04 17:16:42 -05:00
|
|
|
package user
|
2015-12-03 00:24:37 -05:00
|
|
|
|
|
|
|
|
import (
|
2019-08-08 23:53:43 -07:00
|
|
|
"net/http"
|
2018-05-27 08:53:48 +08:00
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
2020-02-22 09:05:26 +08:00
|
|
|
"gogs.io/gogs/internal/conf"
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
|
|
|
|
"gogs.io/gogs/internal/db"
|
2020-03-16 01:22:27 +08:00
|
|
|
"gogs.io/gogs/internal/route/api/v1/convert"
|
|
|
|
|
"gogs.io/gogs/internal/route/api/v1/repo"
|
2015-12-03 00:24:37 -05:00
|
|
|
)
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
func GetUserByParamsName(c *context.APIContext, name string) *db.User {
|
|
|
|
|
user, err := db.GetUserByName(c.Params(name))
|
2015-12-04 17:16:42 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.NotFoundOrError(err, "get user by name")
|
2015-12-04 17:16:42 -05:00
|
|
|
return nil
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
2015-12-04 17:16:42 -05:00
|
|
|
return user
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 10:42:09 +05:30
|
|
|
// GetUserByParams returns user whose name is presented in URL parameter.
|
2019-10-24 01:51:46 -07:00
|
|
|
func GetUserByParams(c *context.APIContext) *db.User {
|
2017-06-03 07:26:09 -04:00
|
|
|
return GetUserByParamsName(c, ":username")
|
2015-12-17 02:28:47 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 00:24:37 -05:00
|
|
|
func composePublicKeysAPILink() string {
|
2020-02-22 09:05:26 +08:00
|
|
|
return conf.Server.ExternalURL + "api/v1/user/keys/"
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func listPublicKeys(c *context.APIContext, uid int64) {
|
2019-10-24 01:51:46 -07:00
|
|
|
keys, err := db.ListPublicKeys(uid)
|
2015-12-03 00:24:37 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "list public keys")
|
2015-12-03 00:24:37 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiLink := composePublicKeysAPILink()
|
|
|
|
|
apiKeys := make([]*api.PublicKey, len(keys))
|
|
|
|
|
for i := range keys {
|
2020-03-16 01:22:27 +08:00
|
|
|
apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSONSuccess(&apiKeys)
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListMyPublicKeys(c *context.APIContext) {
|
|
|
|
|
listPublicKeys(c, c.User.ID)
|
2015-12-04 17:16:42 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListPublicKeys(c *context.APIContext) {
|
|
|
|
|
user := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-03 00:24:37 -05:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
listPublicKeys(c, user.ID)
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func GetPublicKey(c *context.APIContext) {
|
2019-10-24 01:51:46 -07:00
|
|
|
key, err := db.GetPublicKeyByID(c.ParamsInt64(":id"))
|
2015-12-03 00:24:37 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.NotFoundOrError(err, "get public key by ID")
|
2015-12-03 00:24:37 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiLink := composePublicKeysAPILink()
|
2020-03-16 01:22:27 +08:00
|
|
|
c.JSONSuccess(convert.ToPublicKey(apiLink, key))
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-05 17:13:13 -05:00
|
|
|
// CreateUserPublicKey creates new public key to given user by ID.
|
2017-06-03 07:26:09 -04:00
|
|
|
func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
|
2019-10-24 01:51:46 -07:00
|
|
|
content, err := db.CheckPublicKeyString(form.Key)
|
2015-12-03 00:24:37 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
repo.HandleCheckKeyStringError(c, err)
|
2015-12-03 00:24:37 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
key, err := db.AddPublicKey(uid, form.Title, content)
|
2015-12-03 00:24:37 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
repo.HandleAddKeyError(c, err)
|
2015-12-03 00:24:37 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
apiLink := composePublicKeysAPILink()
|
2020-03-16 01:22:27 +08:00
|
|
|
c.JSON(http.StatusCreated, convert.ToPublicKey(apiLink, key))
|
2015-12-04 17:16:42 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
|
|
|
|
|
CreateUserPublicKey(c, form, c.User.ID)
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func DeletePublicKey(c *context.APIContext) {
|
2019-10-24 01:51:46 -07:00
|
|
|
if err := db.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
|
|
|
|
|
if db.IsErrKeyAccessDenied(err) {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key."))
|
2015-12-03 00:24:37 -05:00
|
|
|
} else {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "delete public key")
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-08 23:53:43 -07:00
|
|
|
c.NoContent()
|
2015-12-03 00:24:37 -05:00
|
|
|
}
|