mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
verifyJWT and jwtSubject accepted any HMAC algorithm, so a token signed with HS384 or HS512 and the session key was valid. Both now parse through parseSessionJWT, which accepts only HS256. New tokens carry iss "artex" and aud "artex-api". When a token has either claim, it must match. Tokens issued by earlier releases have neither claim and are still accepted, so nobody is signed out by the upgrade.
93 lines
3.5 KiB
Go
93 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
func testJWTClaims(iss string, aud []string, pwd int64) sessionClaims {
|
|
return sessionClaims{
|
|
Pwd: pwd,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
Issuer: iss,
|
|
Audience: aud,
|
|
Subject: "ARTEX",
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
}
|
|
|
|
func signTestJWT(t *testing.T, method jwt.SigningMethod, key any, c sessionClaims) string {
|
|
t.Helper()
|
|
tok, err := jwt.NewWithClaims(method, c).SignedString(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return tok
|
|
}
|
|
|
|
// New tokens carry iss and aud and are accepted.
|
|
func TestSignJWTSetsIssuerAndAudience(t *testing.T) {
|
|
key := []byte("0123456789abcdef0123456789abcdef")
|
|
tok, err := signJWTVersion(key, 3)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claims, ok := parseSessionJWT(tok, key)
|
|
if !ok {
|
|
t.Fatal("new token rejected")
|
|
}
|
|
if claims.Issuer != jwtIssuer || !slices.Equal(claims.Audience, jwt.ClaimStrings{jwtAudience}) {
|
|
t.Errorf("iss=%q aud=%v, want %q and [%q]", claims.Issuer, claims.Audience, jwtIssuer, jwtAudience)
|
|
}
|
|
if !verifyJWT(tok, key, 3) || jwtSubject(tok, key) != "ARTEX" {
|
|
t.Error("new token not accepted by verifyJWT or jwtSubject")
|
|
}
|
|
}
|
|
|
|
func TestVerifyJWTClaimsAndAlgorithm(t *testing.T) {
|
|
key := []byte("0123456789abcdef0123456789abcdef")
|
|
aud := []string{jwtAudience}
|
|
for _, tc := range []struct {
|
|
name string
|
|
tok string
|
|
accept bool
|
|
}{
|
|
// Tokens issued by earlier releases have no iss or aud and keep working.
|
|
{"legacy claims, HS256", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("", nil, 0)), true},
|
|
{"iss without aud", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims(jwtIssuer, nil, 0)), true},
|
|
{"aud without iss", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("", aud, 0)), true},
|
|
{"aud list with ours", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims(jwtIssuer, []string{"other", jwtAudience}, 0)), true},
|
|
{"wrong iss", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("someone-else", aud, 0)), false},
|
|
{"wrong aud", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims(jwtIssuer, []string{"other-api"}, 0)), false},
|
|
{"HS384", signTestJWT(t, jwt.SigningMethodHS384, key, testJWTClaims(jwtIssuer, aud, 0)), false},
|
|
{"HS512", signTestJWT(t, jwt.SigningMethodHS512, key, testJWTClaims(jwtIssuer, aud, 0)), false},
|
|
{"HS512 legacy claims", signTestJWT(t, jwt.SigningMethodHS512, key, testJWTClaims("", nil, 0)), false},
|
|
{"alg none", signTestJWT(t, jwt.SigningMethodNone, jwt.UnsafeAllowNoneSignatureType, testJWTClaims(jwtIssuer, aud, 0)), false},
|
|
{"other key", signTestJWT(t, jwt.SigningMethodHS256, []byte("another-key-another-key-another-k"), testJWTClaims(jwtIssuer, aud, 0)), false},
|
|
} {
|
|
if got := verifyJWT(tc.tok, key, 0); got != tc.accept {
|
|
t.Errorf("%s: verifyJWT=%v, want %v", tc.name, got, tc.accept)
|
|
}
|
|
if got := jwtSubject(tc.tok, key) == "ARTEX"; got != tc.accept {
|
|
t.Errorf("%s: jwtSubject accepted=%v, want %v", tc.name, got, tc.accept)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The password version check still applies to legacy tokens.
|
|
func TestVerifyJWTLegacyTokenPasswordVersion(t *testing.T) {
|
|
key := []byte("0123456789abcdef0123456789abcdef")
|
|
tok := signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("", nil, 2))
|
|
if !verifyJWT(tok, key, 2) {
|
|
t.Error("legacy token at the current password version rejected")
|
|
}
|
|
if verifyJWT(tok, key, 3) {
|
|
t.Error("legacy token at an old password version accepted")
|
|
}
|
|
}
|