diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 16:14:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 16:14:24 +0800 |
commit | 3ca5dc7e32b372d14ff80d96f14b8f6a805862f1 (patch) | |
tree | 50d193ed0dacf2888d57b193a9b0d36065aff205 /routers/web/user/setting | |
parent | 0a9fcf63a49799ad3b0f146c54879161bac61e10 (diff) | |
download | gitea-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/web/user/setting')
-rw-r--r-- | routers/web/user/setting/keys.go | 79 |
1 files changed, 40 insertions, 39 deletions
diff --git a/routers/web/user/setting/keys.go b/routers/web/user/setting/keys.go index 22a0fe4741..ec55462149 100644 --- a/routers/web/user/setting/keys.go +++ b/routers/web/user/setting/keys.go @@ -8,12 +8,13 @@ package setting import ( "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/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" + asymkey_service "code.gitea.io/gitea/services/asymkey" "code.gitea.io/gitea/services/forms" ) @@ -51,9 +52,9 @@ func KeysPost(ctx *context.Context) { } switch form.Type { case "principal": - content, err := models.CheckPrincipalKeyString(ctx.User, form.Content) + content, err := asymkey_model.CheckPrincipalKeyString(ctx.User, form.Content) if err != nil { - if models.IsErrSSHDisabled(err) { + if db.IsErrSSHDisabled(err) { ctx.Flash.Info(ctx.Tr("settings.ssh_disabled")) } else { ctx.Flash.Error(ctx.Tr("form.invalid_ssh_principal", err.Error())) @@ -61,10 +62,10 @@ func KeysPost(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/user/settings/keys") return } - if _, err = models.AddPrincipalKey(ctx.User.ID, content, 0); err != nil { + if _, err = asymkey_model.AddPrincipalKey(ctx.User.ID, content, 0); err != nil { ctx.Data["HasPrincipalError"] = true switch { - case models.IsErrKeyAlreadyExist(err), models.IsErrKeyNameAlreadyUsed(err): + case asymkey_model.IsErrKeyAlreadyExist(err), asymkey_model.IsErrKeyNameAlreadyUsed(err): loadKeysData(ctx) ctx.Data["Err_Content"] = true @@ -77,36 +78,36 @@ func KeysPost(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("settings.add_principal_success", form.Content)) ctx.Redirect(setting.AppSubURL + "/user/settings/keys") case "gpg": - 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(ctx.User.ID, form.Content, token, form.Signature) - if err != nil && models.IsErrGPGInvalidTokenSignature(err) { - keys, err = models.AddGPGKey(ctx.User.ID, form.Content, lastToken, form.Signature) + keys, err := asymkey_model.AddGPGKey(ctx.User.ID, form.Content, token, form.Signature) + if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) { + keys, err = asymkey_model.AddGPGKey(ctx.User.ID, form.Content, lastToken, form.Signature) } if err != nil { ctx.Data["HasGPGError"] = true switch { - case models.IsErrGPGKeyParsing(err): + case asymkey_model.IsErrGPGKeyParsing(err): ctx.Flash.Error(ctx.Tr("form.invalid_gpg_key", err.Error())) ctx.Redirect(setting.AppSubURL + "/user/settings/keys") - case models.IsErrGPGKeyIDAlreadyUsed(err): + case asymkey_model.IsErrGPGKeyIDAlreadyUsed(err): loadKeysData(ctx) ctx.Data["Err_Content"] = true ctx.RenderWithErr(ctx.Tr("settings.gpg_key_id_used"), tplSettingsKeys, &form) - case models.IsErrGPGInvalidTokenSignature(err): + case asymkey_model.IsErrGPGInvalidTokenSignature(err): loadKeysData(ctx) ctx.Data["Err_Content"] = true ctx.Data["Err_Signature"] = true - ctx.Data["KeyID"] = err.(models.ErrGPGInvalidTokenSignature).ID + ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGInvalidTokenSignature).ID ctx.RenderWithErr(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form) - case models.IsErrGPGNoEmailFound(err): + case asymkey_model.IsErrGPGNoEmailFound(err): loadKeysData(ctx) ctx.Data["Err_Content"] = true ctx.Data["Err_Signature"] = true - ctx.Data["KeyID"] = err.(models.ErrGPGNoEmailFound).ID + ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGNoEmailFound).ID ctx.RenderWithErr(ctx.Tr("settings.gpg_no_key_email_found"), tplSettingsKeys, &form) default: ctx.ServerError("AddPublicKey", err) @@ -124,21 +125,21 @@ func KeysPost(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("settings.add_gpg_key_success", keyIDs)) ctx.Redirect(setting.AppSubURL + "/user/settings/keys") case "verify_gpg": - 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) - keyID, err := models.VerifyGPGKey(ctx.User.ID, form.KeyID, token, form.Signature) - if err != nil && models.IsErrGPGInvalidTokenSignature(err) { - keyID, err = models.VerifyGPGKey(ctx.User.ID, form.KeyID, lastToken, form.Signature) + keyID, err := asymkey_model.VerifyGPGKey(ctx.User.ID, form.KeyID, token, form.Signature) + if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) { + keyID, err = asymkey_model.VerifyGPGKey(ctx.User.ID, form.KeyID, lastToken, form.Signature) } if err != nil { ctx.Data["HasGPGVerifyError"] = true switch { - case models.IsErrGPGInvalidTokenSignature(err): + case asymkey_model.IsErrGPGInvalidTokenSignature(err): loadKeysData(ctx) ctx.Data["VerifyingID"] = form.KeyID ctx.Data["Err_Signature"] = true - ctx.Data["KeyID"] = err.(models.ErrGPGInvalidTokenSignature).ID + ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGInvalidTokenSignature).ID ctx.RenderWithErr(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form) default: ctx.ServerError("VerifyGPG", err) @@ -147,11 +148,11 @@ func KeysPost(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("settings.verify_gpg_key_success", keyID)) ctx.Redirect(setting.AppSubURL + "/user/settings/keys") case "ssh": - content, err := models.CheckPublicKeyString(form.Content) + content, err := asymkey_model.CheckPublicKeyString(form.Content) if err != nil { - if models.IsErrSSHDisabled(err) { + if db.IsErrSSHDisabled(err) { ctx.Flash.Info(ctx.Tr("settings.ssh_disabled")) - } else if models.IsErrKeyUnableVerify(err) { + } else if asymkey_model.IsErrKeyUnableVerify(err) { ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) } else { ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) @@ -160,20 +161,20 @@ func KeysPost(ctx *context.Context) { return } - if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content, 0); err != nil { + if _, err = asymkey_model.AddPublicKey(ctx.User.ID, form.Title, content, 0); err != nil { ctx.Data["HasSSHError"] = true switch { - case models.IsErrKeyAlreadyExist(err): + case asymkey_model.IsErrKeyAlreadyExist(err): loadKeysData(ctx) ctx.Data["Err_Content"] = true ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), tplSettingsKeys, &form) - case models.IsErrKeyNameAlreadyUsed(err): + case asymkey_model.IsErrKeyNameAlreadyUsed(err): loadKeysData(ctx) ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsKeys, &form) - case models.IsErrKeyUnableVerify(err): + case asymkey_model.IsErrKeyUnableVerify(err): ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) ctx.Redirect(setting.AppSubURL + "/user/settings/keys") default: @@ -196,14 +197,14 @@ func DeleteKey(ctx *context.Context) { switch ctx.FormString("type") { case "gpg": - if err := models.DeleteGPGKey(ctx.User, ctx.FormInt64("id")); err != nil { + if err := asymkey_model.DeleteGPGKey(ctx.User, ctx.FormInt64("id")); err != nil { ctx.Flash.Error("DeleteGPGKey: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("settings.gpg_key_deletion_success")) } case "ssh": keyID := ctx.FormInt64("id") - external, err := models.PublicKeyIsExternallyManaged(keyID) + external, err := asymkey_model.PublicKeyIsExternallyManaged(keyID) if err != nil { ctx.ServerError("sshKeysExternalManaged", err) return @@ -213,13 +214,13 @@ func DeleteKey(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/user/settings/keys") return } - if err := models.DeletePublicKey(ctx.User, keyID); err != nil { + if err := asymkey_service.DeletePublicKey(ctx.User, keyID); err != nil { ctx.Flash.Error("DeletePublicKey: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success")) } case "principal": - if err := models.DeletePublicKey(ctx.User, ctx.FormInt64("id")); err != nil { + if err := asymkey_service.DeletePublicKey(ctx.User, ctx.FormInt64("id")); err != nil { ctx.Flash.Error("DeletePublicKey: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("settings.ssh_principal_deletion_success")) @@ -234,32 +235,32 @@ func DeleteKey(ctx *context.Context) { } func loadKeysData(ctx *context.Context) { - keys, err := models.ListPublicKeys(ctx.User.ID, db.ListOptions{}) + keys, err := asymkey_model.ListPublicKeys(ctx.User.ID, db.ListOptions{}) if err != nil { ctx.ServerError("ListPublicKeys", err) return } ctx.Data["Keys"] = keys - externalKeys, err := models.PublicKeysAreExternallyManaged(keys) + externalKeys, err := asymkey_model.PublicKeysAreExternallyManaged(keys) if err != nil { ctx.ServerError("ListPublicKeys", err) return } ctx.Data["ExternalKeys"] = externalKeys - gpgkeys, err := models.ListGPGKeys(ctx.User.ID, db.ListOptions{}) + gpgkeys, err := asymkey_model.ListGPGKeys(db.DefaultContext, ctx.User.ID, db.ListOptions{}) if err != nil { ctx.ServerError("ListGPGKeys", err) return } ctx.Data["GPGKeys"] = gpgkeys - tokenToSign := models.VerificationToken(ctx.User, 1) + tokenToSign := asymkey_model.VerificationToken(ctx.User, 1) // generate a new aes cipher using the csrfToken ctx.Data["TokenToSign"] = tokenToSign - principals, err := models.ListPrincipalKeys(ctx.User.ID, db.ListOptions{}) + principals, err := asymkey_model.ListPrincipalKeys(ctx.User.ID, db.ListOptions{}) if err != nil { ctx.ServerError("ListPrincipalKeys", err) return |