edge/pkg/module/auth/module_test.go

147 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-02-21 12:14:29 +01:00
package auth
import (
"context"
"net/http"
2023-11-28 16:35:49 +01:00
"os"
2023-02-21 12:14:29 +01:00
"testing"
"time"
"cdr.dev/slog"
"forge.cadoles.com/arcad/edge/pkg/app"
edgehttp "forge.cadoles.com/arcad/edge/pkg/http"
"forge.cadoles.com/arcad/edge/pkg/jwtutil"
2023-02-21 12:14:29 +01:00
"forge.cadoles.com/arcad/edge/pkg/module"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
2023-02-21 12:14:29 +01:00
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
func TestAuthModule(t *testing.T) {
t.Parallel()
2023-11-28 16:35:49 +01:00
if testing.Verbose() {
logger.SetLevel(slog.LevelDebug)
}
2023-02-21 12:14:29 +01:00
key := getDummyKey()
2023-02-21 12:14:29 +01:00
server := app.NewServer(
module.ConsoleModuleFactory(),
ModuleFactory(
WithJWT(getDummyKeySet(key)),
),
2023-02-21 12:14:29 +01:00
)
2023-11-28 16:35:49 +01:00
script := "testdata/auth.js"
2023-02-21 12:14:29 +01:00
2023-11-28 16:35:49 +01:00
data, err := os.ReadFile(script)
if err != nil {
2023-02-21 12:14:29 +01:00
t.Fatal(err)
}
ctx := context.Background()
2023-11-28 16:35:49 +01:00
if err := server.Start(ctx, script, string(data)); err != nil {
2023-02-21 12:14:29 +01:00
t.Fatalf("%+v", errors.WithStack(err))
}
defer server.Stop()
req, err := http.NewRequest("GET", "/foo", nil)
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
token := jwt.New()
2023-02-21 12:14:29 +01:00
if err := token.Set(jwt.SubjectKey, "jdoe"); err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
if err := token.Set(jwt.NotBeforeKey, time.Now()); err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
rawToken, err := jwt.Sign(token, jwt.WithKey(jwa.HS256, key))
2023-02-21 12:14:29 +01:00
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
req.Header.Add("Authorization", "Bearer "+string(rawToken))
2023-02-21 12:14:29 +01:00
ctx = edgehttp.WithContextHTTPRequest(context.Background(), req)
2023-02-21 12:14:29 +01:00
if _, err := server.ExecFuncByName(ctx, "testAuth", ctx); err != nil {
2023-02-21 12:14:29 +01:00
t.Fatalf("%+v", errors.WithStack(err))
}
}
func TestAuthAnonymousModule(t *testing.T) {
t.Parallel()
2023-11-28 16:35:49 +01:00
if testing.Verbose() {
logger.SetLevel(slog.LevelDebug)
}
2023-02-21 12:14:29 +01:00
key := getDummyKey()
2023-02-21 12:14:29 +01:00
server := app.NewServer(
module.ConsoleModuleFactory(),
ModuleFactory(WithJWT(getDummyKeySet(key))),
2023-02-21 12:14:29 +01:00
)
2023-11-28 16:35:49 +01:00
script := "testdata/auth_anonymous.js"
2023-02-21 12:14:29 +01:00
2023-11-28 16:35:49 +01:00
data, err := os.ReadFile("testdata/auth_anonymous.js")
if err != nil {
2023-02-21 12:14:29 +01:00
t.Fatal(err)
}
ctx := context.Background()
2023-11-28 16:35:49 +01:00
if err := server.Start(ctx, script, string(data)); err != nil {
2023-02-21 12:14:29 +01:00
t.Fatalf("%+v", errors.WithStack(err))
}
defer server.Stop()
req, err := http.NewRequest("GET", "/foo", nil)
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
ctx = edgehttp.WithContextHTTPRequest(context.Background(), req)
2023-02-21 12:14:29 +01:00
if _, err := server.ExecFuncByName(ctx, "testAuth", ctx); err != nil {
2023-02-21 12:14:29 +01:00
t.Fatalf("%+v", errors.WithStack(err))
}
}
func getDummyKey() jwk.Key {
2023-02-21 12:14:29 +01:00
secret := []byte("not_so_secret")
key, err := jwk.FromRaw(secret)
if err != nil {
panic(errors.WithStack(err))
}
2023-02-21 12:14:29 +01:00
if err := key.Set(jwk.AlgorithmKey, jwa.HS256); err != nil {
panic(errors.WithStack(err))
2023-02-21 12:14:29 +01:00
}
return key
}
func getDummyKeySet(key jwk.Key) jwtutil.GetKeySetFunc {
return func() (jwk.Set, error) {
set := jwk.NewSet()
if err := set.AddKey(key); err != nil {
return nil, errors.WithStack(err)
}
return set, nil
}
2023-02-21 12:14:29 +01:00
}