mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-25 11:22:16 +00:00
Port "Use general token signing secret"
Port of https://github.com/go-gitea/gitea/pull/29205 Use a clearly defined "signing secret" for token signing. (cherry picked from commit 8be198cdef0a486f417663b1fd6878458d7e5d92)
This commit is contained in:
parent
cfd6420a0e
commit
62d3e5255f
13 changed files with 131 additions and 61 deletions
|
@ -6,7 +6,6 @@ package util
|
|||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
@ -246,13 +245,3 @@ func ToFloat64(number any) (float64, error) {
|
|||
func ToPointer[T any](val T) *T {
|
||||
return &val
|
||||
}
|
||||
|
||||
func Base64FixedDecode(encoding *base64.Encoding, src []byte, length int) ([]byte, error) {
|
||||
decoded := make([]byte, encoding.DecodedLen(len(src))+3)
|
||||
if n, err := encoding.Decode(decoded, src); err != nil {
|
||||
return nil, err
|
||||
} else if n != length {
|
||||
return nil, fmt.Errorf("invalid base64 decoded length: %d, expects: %d", n, length)
|
||||
}
|
||||
return decoded[:length], nil
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -234,16 +233,3 @@ func TestToPointer(t *testing.T) {
|
|||
val123 := 123
|
||||
assert.False(t, &val123 == ToPointer(val123))
|
||||
}
|
||||
|
||||
func TestBase64FixedDecode(t *testing.T) {
|
||||
_, err := Base64FixedDecode(base64.RawURLEncoding, []byte("abcd"), 32)
|
||||
assert.ErrorContains(t, err, "invalid base64 decoded length")
|
||||
_, err = Base64FixedDecode(base64.RawURLEncoding, []byte(strings.Repeat("a", 64)), 32)
|
||||
assert.ErrorContains(t, err, "invalid base64 decoded length")
|
||||
|
||||
str32 := strings.Repeat("x", 32)
|
||||
encoded32 := base64.RawURLEncoding.EncodeToString([]byte(str32))
|
||||
decoded32, err := Base64FixedDecode(base64.RawURLEncoding, []byte(encoded32), 32)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, str32, string(decoded32))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue