forked from TrueCloudLab/distribution
e0281dc609
gofumpt (https://github.com/mvdan/gofumpt) provides a supserset of `gofmt` / `go fmt`, and addresses various formatting issues that linters may be checking for. We can consider enabling the `gofumpt` linter to verify the formatting in CI, although not every developer may have it installed, so for now this runs it once to get formatting in shape. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"encoding/base64"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/distribution/distribution/v3/registry/auth"
|
|
"github.com/docker/libtrust"
|
|
)
|
|
|
|
func TestCreateJWTSuccessWithEmptyACL(t *testing.T) {
|
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pk, err := libtrust.FromCryptoPrivateKey(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tokenIssuer := TokenIssuer{
|
|
Expiration: time.Duration(100),
|
|
Issuer: "localhost",
|
|
SigningKey: pk,
|
|
}
|
|
|
|
grantedAccessList := make([]auth.Access, 0)
|
|
token, err := tokenIssuer.CreateJWT("test", "test", grantedAccessList)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tokens := strings.Split(token, ".")
|
|
|
|
if len(token) == 0 {
|
|
t.Fatal("token not generated.")
|
|
}
|
|
|
|
json, err := decodeJWT(tokens[1])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !strings.Contains(json, "test") {
|
|
t.Fatal("Valid token was not generated.")
|
|
}
|
|
}
|
|
|
|
func decodeJWT(rawToken string) (string, error) {
|
|
data, err := joseBase64Decode(rawToken)
|
|
if err != nil {
|
|
return "", errors.New("Error in Decoding base64 String")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func joseBase64Decode(s string) (string, error) {
|
|
switch len(s) % 4 {
|
|
case 0:
|
|
case 2:
|
|
s += "=="
|
|
case 3:
|
|
s += "="
|
|
default:
|
|
{
|
|
return "", errors.New("Invalid base64 String")
|
|
}
|
|
}
|
|
data, err := base64.StdEncoding.DecodeString(s)
|
|
if err != nil {
|
|
return "", err // errors.New("Error in Decoding base64 String")
|
|
}
|
|
return string(data), nil
|
|
}
|