aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api/v1/user
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-12-10 16:14:24 +0800
committerGitHub <noreply@github.com>2021-12-10 16:14:24 +0800
commit3ca5dc7e32b372d14ff80d96f14b8f6a805862f1 (patch)
tree50d193ed0dacf2888d57b193a9b0d36065aff205 /routers/api/v1/user
parent0a9fcf63a49799ad3b0f146c54879161bac61e10 (diff)
downloadgitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.tar.gz
gitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.zip
Move keys to models/asymkey (#17917)
* Move keys to models/keys * Rename models/keys -> models/asymkey * change the missed package name * Fix package alias * Fix test * Fix docs * Fix test * Fix test * merge
Diffstat (limited to 'routers/api/v1/user')
-rw-r--r--routers/api/v1/user/gpg_key.go52
-rw-r--r--routers/api/v1/user/key.go35
2 files changed, 44 insertions, 43 deletions
diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go
index 9066268bba..b0f9f1feb3 100644
--- a/routers/api/v1/user/gpg_key.go
+++ b/routers/api/v1/user/gpg_key.go
@@ -8,7 +8,7 @@ import (
"fmt"
"net/http"
- "code.gitea.io/gitea/models"
+ asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
@@ -18,7 +18,7 @@ import (
)
func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) {
- keys, err := models.ListGPGKeys(uid, listOptions)
+ keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, uid, listOptions)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
return
@@ -29,7 +29,7 @@ func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions)
apiKeys[i] = convert.ToGPGKey(keys[i])
}
- total, err := models.CountUserGPGKeys(uid)
+ total, err := asymkey_model.CountUserGPGKeys(uid)
if err != nil {
ctx.InternalServerError(err)
return
@@ -114,9 +114,9 @@ func GetGPGKey(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
- key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
+ key, err := asymkey_model.GetGPGKeyByID(ctx.ParamsInt64(":id"))
if err != nil {
- if models.IsErrGPGKeyNotExist(err) {
+ if asymkey_model.IsErrGPGKeyNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err)
@@ -128,12 +128,12 @@ func GetGPGKey(ctx *context.APIContext) {
// CreateUserGPGKey creates new GPG key to given user by ID.
func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
- token := models.VerificationToken(ctx.User, 1)
- lastToken := models.VerificationToken(ctx.User, 0)
+ token := asymkey_model.VerificationToken(ctx.User, 1)
+ lastToken := asymkey_model.VerificationToken(ctx.User, 0)
- keys, err := models.AddGPGKey(uid, form.ArmoredKey, token, form.Signature)
- if err != nil && models.IsErrGPGInvalidTokenSignature(err) {
- keys, err = models.AddGPGKey(uid, form.ArmoredKey, lastToken, form.Signature)
+ keys, err := asymkey_model.AddGPGKey(uid, form.ArmoredKey, token, form.Signature)
+ if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) {
+ keys, err = asymkey_model.AddGPGKey(uid, form.ArmoredKey, lastToken, form.Signature)
}
if err != nil {
HandleAddGPGKeyError(ctx, err, token)
@@ -156,7 +156,7 @@ func GetVerificationToken(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
- token := models.VerificationToken(ctx.User, 1)
+ token := asymkey_model.VerificationToken(ctx.User, 1)
ctx.PlainText(http.StatusOK, []byte(token))
}
@@ -178,25 +178,25 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.VerifyGPGKeyOption)
- token := models.VerificationToken(ctx.User, 1)
- lastToken := models.VerificationToken(ctx.User, 0)
+ token := asymkey_model.VerificationToken(ctx.User, 1)
+ lastToken := asymkey_model.VerificationToken(ctx.User, 0)
- _, err := models.VerifyGPGKey(ctx.User.ID, form.KeyID, token, form.Signature)
- if err != nil && models.IsErrGPGInvalidTokenSignature(err) {
- _, err = models.VerifyGPGKey(ctx.User.ID, form.KeyID, lastToken, form.Signature)
+ _, err := asymkey_model.VerifyGPGKey(ctx.User.ID, form.KeyID, token, form.Signature)
+ if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) {
+ _, err = asymkey_model.VerifyGPGKey(ctx.User.ID, form.KeyID, lastToken, form.Signature)
}
if err != nil {
- if models.IsErrGPGInvalidTokenSignature(err) {
+ if asymkey_model.IsErrGPGInvalidTokenSignature(err) {
ctx.Error(http.StatusUnprocessableEntity, "GPGInvalidSignature", fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token))
return
}
ctx.Error(http.StatusInternalServerError, "VerifyUserGPGKey", err)
}
- key, err := models.GetGPGKeysByKeyID(form.KeyID)
+ key, err := asymkey_model.GetGPGKeysByKeyID(form.KeyID)
if err != nil {
- if models.IsErrGPGKeyNotExist(err) {
+ if asymkey_model.IsErrGPGKeyNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetGPGKeysByKeyID", err)
@@ -255,8 +255,8 @@ func DeleteGPGKey(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
- if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
- if models.IsErrGPGKeyAccessDenied(err) {
+ if err := asymkey_model.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
+ if asymkey_model.IsErrGPGKeyAccessDenied(err) {
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
} else {
ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err)
@@ -270,15 +270,15 @@ func DeleteGPGKey(ctx *context.APIContext) {
// HandleAddGPGKeyError handle add GPGKey error
func HandleAddGPGKeyError(ctx *context.APIContext, err error, token string) {
switch {
- case models.IsErrGPGKeyAccessDenied(err):
+ case asymkey_model.IsErrGPGKeyAccessDenied(err):
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyAccessDenied", "You do not have access to this GPG key")
- case models.IsErrGPGKeyIDAlreadyUsed(err):
+ case asymkey_model.IsErrGPGKeyIDAlreadyUsed(err):
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyIDAlreadyUsed", "A key with the same id already exists")
- case models.IsErrGPGKeyParsing(err):
+ case asymkey_model.IsErrGPGKeyParsing(err):
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyParsing", err)
- case models.IsErrGPGNoEmailFound(err):
+ case asymkey_model.IsErrGPGNoEmailFound(err):
ctx.Error(http.StatusNotFound, "GPGNoEmailFound", fmt.Sprintf("None of the emails attached to the GPG key could be found. It may still be added if you provide a valid signature for the token: %s", token))
- case models.IsErrGPGInvalidTokenSignature(err):
+ case asymkey_model.IsErrGPGInvalidTokenSignature(err):
ctx.Error(http.StatusUnprocessableEntity, "GPGInvalidSignature", fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token))
default:
ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go
index 32291264bc..e8cc2035e5 100644
--- a/routers/api/v1/user/key.go
+++ b/routers/api/v1/user/key.go
@@ -7,7 +7,7 @@ package user
import (
"net/http"
- "code.gitea.io/gitea/models"
+ asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
@@ -17,13 +17,14 @@ import (
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/repo"
"code.gitea.io/gitea/routers/api/v1/utils"
+ asymkey_service "code.gitea.io/gitea/services/asymkey"
)
// appendPrivateInformation appends the owner and key type information to api.PublicKey
-func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defaultUser *user_model.User) (*api.PublicKey, error) {
- if key.Type == models.KeyTypeDeploy {
+func appendPrivateInformation(apiKey *api.PublicKey, key *asymkey_model.PublicKey, defaultUser *user_model.User) (*api.PublicKey, error) {
+ if key.Type == asymkey_model.KeyTypeDeploy {
apiKey.KeyType = "deploy"
- } else if key.Type == models.KeyTypeUser {
+ } else if key.Type == asymkey_model.KeyTypeUser {
apiKey.KeyType = "user"
if defaultUser.ID == key.OwnerID {
@@ -47,7 +48,7 @@ func composePublicKeysAPILink() string {
}
func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
- var keys []*models.PublicKey
+ var keys []*asymkey_model.PublicKey
var err error
var count int
@@ -58,14 +59,14 @@ func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
// Querying not just listing
if username != "" {
// Restrict to provided uid
- keys, err = models.SearchPublicKey(user.ID, fingerprint)
+ keys, err = asymkey_model.SearchPublicKey(user.ID, fingerprint)
} else {
// Unrestricted
- keys, err = models.SearchPublicKey(0, fingerprint)
+ keys, err = asymkey_model.SearchPublicKey(0, fingerprint)
}
count = len(keys)
} else {
- total, err2 := models.CountPublicKeys(user.ID)
+ total, err2 := asymkey_model.CountPublicKeys(user.ID)
if err2 != nil {
ctx.InternalServerError(err)
return
@@ -73,7 +74,7 @@ func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
count = int(total)
// Use ListPublicKeys
- keys, err = models.ListPublicKeys(user.ID, utils.GetListOptions(ctx))
+ keys, err = asymkey_model.ListPublicKeys(user.ID, utils.GetListOptions(ctx))
}
if err != nil {
@@ -177,9 +178,9 @@ func GetPublicKey(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
- key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
+ key, err := asymkey_model.GetPublicKeyByID(ctx.ParamsInt64(":id"))
if err != nil {
- if models.IsErrKeyNotExist(err) {
+ if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetPublicKeyByID", err)
@@ -197,13 +198,13 @@ func GetPublicKey(ctx *context.APIContext) {
// CreateUserPublicKey creates new public key to given user by ID.
func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
- content, err := models.CheckPublicKeyString(form.Key)
+ content, err := asymkey_model.CheckPublicKeyString(form.Key)
if err != nil {
repo.HandleCheckKeyStringError(ctx, err)
return
}
- key, err := models.AddPublicKey(uid, form.Title, content, 0)
+ key, err := asymkey_model.AddPublicKey(uid, form.Title, content, 0)
if err != nil {
repo.HandleAddKeyError(ctx, err)
return
@@ -263,7 +264,7 @@ func DeletePublicKey(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
id := ctx.ParamsInt64(":id")
- externallyManaged, err := models.PublicKeyIsExternallyManaged(id)
+ externallyManaged, err := asymkey_model.PublicKeyIsExternallyManaged(id)
if err != nil {
ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
}
@@ -271,10 +272,10 @@ func DeletePublicKey(ctx *context.APIContext) {
ctx.Error(http.StatusForbidden, "", "SSH Key is externally managed for this user")
}
- if err := models.DeletePublicKey(ctx.User, id); err != nil {
- if models.IsErrKeyNotExist(err) {
+ if err := asymkey_service.DeletePublicKey(ctx.User, id); err != nil {
+ if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
- } else if models.IsErrKeyAccessDenied(err) {
+ } else if asymkey_model.IsErrKeyAccessDenied(err) {
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
} else {
ctx.Error(http.StatusInternalServerError, "DeletePublicKey", err)