Skip to content

Commit 11bae50

Browse files
authored
Pad GPG Key ID with preceding zeroes (#20878)
1 parent 0ee96da commit 11bae50

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

models/asymkey/gpg_key.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
6363
}
6464
}
6565

66+
// PaddedKeyID show KeyID padded to 16 characters
67+
func (key *GPGKey) PaddedKeyID() string {
68+
if len(key.KeyID) > 15 {
69+
return key.KeyID
70+
}
71+
zeros := "0000000000000000"
72+
return zeros[0:16-len(key.KeyID)] + key.KeyID
73+
}
74+
6675
// ListGPGKeys returns a list of public keys belongs to given user.
6776
func ListGPGKeys(ctx context.Context, uid int64, listOptions db.ListOptions) ([]*GPGKey, error) {
6877
sess := db.GetEngine(ctx).Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid)

routers/api/v1/user/gpg_key.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package user
77
import (
88
"fmt"
99
"net/http"
10+
"strings"
1011

1112
asymkey_model "code.gitea.io/gitea/models/asymkey"
1213
"code.gitea.io/gitea/models/db"
@@ -177,6 +178,12 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
177178
token := asymkey_model.VerificationToken(ctx.Doer, 1)
178179
lastToken := asymkey_model.VerificationToken(ctx.Doer, 0)
179180

181+
form.KeyID = strings.TrimLeft(form.KeyID, "0")
182+
if form.KeyID == "" {
183+
ctx.NotFound()
184+
return
185+
}
186+
180187
_, err := asymkey_model.VerifyGPGKey(ctx.Doer.ID, form.KeyID, token, form.Signature)
181188
if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) {
182189
_, err = asymkey_model.VerifyGPGKey(ctx.Doer.ID, form.KeyID, lastToken, form.Signature)

templates/repo/commit_page.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@
222222
{{.Verification.SigningSSHKey.Fingerprint}}
223223
{{else}}
224224
<span class="ui text mr-3">{{.locale.Tr "repo.commits.gpg_key_id"}}:</span>
225-
{{.Verification.SigningKey.KeyID}}
225+
{{.Verification.SigningKey.PaddedKeyID}}
226226
{{end}}
227227
{{else}}
228228
{{svg "octicon-shield-lock" 16 "mr-3"}}
@@ -231,7 +231,7 @@
231231
{{.Verification.SigningSSHKey.Fingerprint}}
232232
{{else}}
233233
<span class="ui text mr-3 tooltip" data-content="{{.locale.Tr "gpg.default_key"}}">{{.locale.Tr "repo.commits.gpg_key_id"}}:</span>
234-
{{.Verification.SigningKey.KeyID}}
234+
{{.Verification.SigningKey.PaddedKeyID}}
235235
{{end}}
236236
{{end}}
237237
{{else if .Verification.Warning}}
@@ -241,14 +241,14 @@
241241
{{.Verification.SigningSSHKey.Fingerprint}}
242242
{{else}}
243243
<span class="ui text mr-3">{{.locale.Tr "repo.commits.gpg_key_id"}}:</span>
244-
{{.Verification.SigningKey.KeyID}}
244+
{{.Verification.SigningKey.PaddedKeyID}}
245245
{{end}}
246246
{{else}}
247247
{{if .Verification.SigningKey}}
248248
{{if ne .Verification.SigningKey.KeyID ""}}
249249
{{svg "octicon-shield" 16 "mr-3"}}
250250
<span class="ui text mr-3">{{.locale.Tr "repo.commits.gpg_key_id"}}:</span>
251-
{{.Verification.SigningKey.KeyID}}
251+
{{.Verification.SigningKey.PaddedKeyID}}
252252
{{end}}
253253
{{end}}
254254
{{if .Verification.SigningSSHKey}}

templates/user/settings/keys_gpg.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<input readonly="" value="{{.TokenToSign}}">
2323
<div class="help">
2424
<p>{{.locale.Tr "settings.gpg_token_help"}}</p>
25-
<p><code>{{$.locale.Tr "settings.gpg_token_code" .TokenToSign .KeyID}}</code></p>
25+
<p><code>{{$.locale.Tr "settings.gpg_token_code" .TokenToSign .PaddedKeyID}}</code></p>
2626
</div>
2727
</div>
2828
<div class="field">
@@ -64,8 +64,8 @@
6464
<span class="tooltip" data-content="{{$.locale.Tr "settings.gpg_key_matched_identities_long"}}">{{svg "octicon-mail"}} {{$.locale.Tr "settings.gpg_key_matched_identities"}} {{range .Emails}}<strong>{{.Email}} </strong>{{end}}</span>
6565
{{end}}
6666
<div class="print meta">
67-
<b>{{$.locale.Tr "settings.key_id"}}:</b> {{.KeyID}}
68-
<b>{{$.locale.Tr "settings.subkeys"}}:</b> {{range .SubsKey}} {{.KeyID}} {{end}}
67+
<b>{{$.locale.Tr "settings.key_id"}}:</b> {{.PaddedKeyID}}
68+
<b>{{$.locale.Tr "settings.subkeys"}}:</b> {{range .SubsKey}} {{.PaddedKeyID}} {{end}}
6969
</div>
7070
<div class="activity meta">
7171
<i>{{$.locale.Tr "settings.add_on"}} <span>{{.AddedUnix.FormatShort}}</span></i>
@@ -87,7 +87,7 @@
8787
<input readonly="" value="{{$.TokenToSign}}">
8888
<div class="help">
8989
<p>{{$.locale.Tr "settings.gpg_token_help"}}</p>
90-
<p><code>{{$.locale.Tr "settings.gpg_token_code" $.TokenToSign .KeyID}}</code></p>
90+
<p><code>{{$.locale.Tr "settings.gpg_token_code" $.TokenToSign .PaddedKeyID}}</code></p>
9191
</div>
9292
<br>
9393
</div>

0 commit comments

Comments
 (0)