Skip to content

Commit b76970f

Browse files
Gustedzeripath
andauthored
Fix key signature error page (#22229)
- When the GPG key contains an error, such as an invalid signature or an email address that does not match the user.A page will be shown that says you must provide a signature for the token. - This page had two errors: one had the wrong translation key and the other tried to use an undefined variable [`.PaddedKeyID`](https://github.com/go-gitea/gitea/blob/e81ccc406bf723a5a58d685e7782f281736affd4/models/asymkey/gpg_key.go#L65-L72), which is a function implemented on the `GPGKey` struct, given that we don't have that, we use [`KeyID`](https://github.com/go-gitea/gitea/blob/e81ccc406bf723a5a58d685e7782f281736affd4/routers/web/user/setting/keys.go#L102) which is [the fingerprint of the publickey](https://pkg.go.dev/golang.org/x/crypto/openpgp/packet#PublicKey.KeyIdString) and is a valid way for opengpg to refer to a key. Before: ![image](https://user-images.githubusercontent.com/25481501/209404800-0e7c39ce-861a-455b-b234-62498d750aa8.png) After: ![image](https://user-images.githubusercontent.com/25481501/209404821-c70f81c6-fd10-4197-ab58-61cb9fc873d8.png) Co-authored-by: zeripath <[email protected]>
1 parent a609cae commit b76970f

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

models/asymkey/gpg_key.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
6464

6565
// PaddedKeyID show KeyID padded to 16 characters
6666
func (key *GPGKey) PaddedKeyID() string {
67-
if len(key.KeyID) > 15 {
68-
return key.KeyID
67+
return PaddedKeyID(key.KeyID)
68+
}
69+
70+
// PaddedKeyID show KeyID padded to 16 characters
71+
func PaddedKeyID(keyID string) string {
72+
if len(keyID) > 15 {
73+
return keyID
6974
}
7075
zeros := "0000000000000000"
71-
return zeros[0:16-len(key.KeyID)] + key.KeyID
76+
return zeros[0:16-len(keyID)] + keyID
7277
}
7378

7479
// ListGPGKeys returns a list of public keys belongs to given user.

routers/web/user/setting/keys.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,18 @@ func KeysPost(ctx *context.Context) {
9999
loadKeysData(ctx)
100100
ctx.Data["Err_Content"] = true
101101
ctx.Data["Err_Signature"] = true
102-
ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
102+
keyID := err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
103+
ctx.Data["KeyID"] = keyID
104+
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
103105
ctx.RenderWithErr(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form)
104106
case asymkey_model.IsErrGPGNoEmailFound(err):
105107
loadKeysData(ctx)
106108

107109
ctx.Data["Err_Content"] = true
108110
ctx.Data["Err_Signature"] = true
109-
ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGNoEmailFound).ID
111+
keyID := err.(asymkey_model.ErrGPGNoEmailFound).ID
112+
ctx.Data["KeyID"] = keyID
113+
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
110114
ctx.RenderWithErr(ctx.Tr("settings.gpg_no_key_email_found"), tplSettingsKeys, &form)
111115
default:
112116
ctx.ServerError("AddPublicKey", err)
@@ -138,7 +142,9 @@ func KeysPost(ctx *context.Context) {
138142
loadKeysData(ctx)
139143
ctx.Data["VerifyingID"] = form.KeyID
140144
ctx.Data["Err_Signature"] = true
141-
ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
145+
keyID := err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
146+
ctx.Data["KeyID"] = keyID
147+
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
142148
ctx.RenderWithErr(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form)
143149
default:
144150
ctx.ServerError("VerifyGPG", err)

templates/user/settings/keys_gpg.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<p>{{.locale.Tr "settings.gpg_token_required"}}</p>
1919
</div>
2020
<div class="field">
21-
<label for="token">{{.locale.Tr "setting.gpg_token"}}
21+
<label for="token">{{.locale.Tr "settings.gpg_token"}}
2222
<input readonly="" value="{{.TokenToSign}}">
2323
<div class="help">
2424
<p>{{.locale.Tr "settings.gpg_token_help"}}</p>

0 commit comments

Comments
 (0)