Skip to content

Commit 03f06d5

Browse files
authored
Use padded keyid (#22288)
- Followup for #22231 to follow the frontport.
1 parent 8cd6be1 commit 03f06d5

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

models/asymkey/gpg_key.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
6565

6666
// PaddedKeyID show KeyID padded to 16 characters
6767
func (key *GPGKey) PaddedKeyID() string {
68-
if len(key.KeyID) > 15 {
69-
return key.KeyID
68+
return PaddedKeyID(key.KeyID)
69+
}
70+
71+
// PaddedKeyID show KeyID padded to 16 characters
72+
func PaddedKeyID(keyID string) string {
73+
if len(keyID) > 15 {
74+
return keyID
7075
}
76+
7177
zeros := "0000000000000000"
72-
return zeros[0:16-len(key.KeyID)] + key.KeyID
78+
return zeros[0:16-len(keyID)] + keyID
7379
}
7480

7581
// 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
@@ -100,14 +100,18 @@ func KeysPost(ctx *context.Context) {
100100
loadKeysData(ctx)
101101
ctx.Data["Err_Content"] = true
102102
ctx.Data["Err_Signature"] = true
103-
ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
103+
keyID := err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
104+
ctx.Data["KeyID"] = keyID
105+
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
104106
ctx.RenderWithErr(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form)
105107
case asymkey_model.IsErrGPGNoEmailFound(err):
106108
loadKeysData(ctx)
107109

108110
ctx.Data["Err_Content"] = true
109111
ctx.Data["Err_Signature"] = true
110-
ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGNoEmailFound).ID
112+
keyID := err.(asymkey_model.ErrGPGNoEmailFound).ID
113+
ctx.Data["KeyID"] = keyID
114+
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
111115
ctx.RenderWithErr(ctx.Tr("settings.gpg_no_key_email_found"), tplSettingsKeys, &form)
112116
default:
113117
ctx.ServerError("AddPublicKey", err)
@@ -139,7 +143,9 @@ func KeysPost(ctx *context.Context) {
139143
loadKeysData(ctx)
140144
ctx.Data["VerifyingID"] = form.KeyID
141145
ctx.Data["Err_Signature"] = true
142-
ctx.Data["KeyID"] = err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
146+
keyID := err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
147+
ctx.Data["KeyID"] = keyID
148+
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
143149
ctx.RenderWithErr(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form)
144150
default:
145151
ctx.ServerError("VerifyGPG", err)

0 commit comments

Comments
 (0)