edge/pkg/module/auth/module_test.go

144 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-02-21 12:14:29 +01:00
package auth
import (
"context"
"io/ioutil"
"net/http"
"testing"
"time"
"cdr.dev/slog"
"forge.cadoles.com/arcad/edge/pkg/app"
edgeHTTP "forge.cadoles.com/arcad/edge/pkg/http"
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()
logger.SetLevel(slog.LevelDebug)
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
)
data, err := ioutil.ReadFile("testdata/auth.js")
if err != nil {
t.Fatal(err)
}
if err := server.Load("testdata/auth.js", string(data)); err != nil {
t.Fatal(err)
}
if err := server.Start(); err != nil {
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 := context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, 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()
logger.SetLevel(slog.LevelDebug)
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
)
data, err := ioutil.ReadFile("testdata/auth_anonymous.js")
if err != nil {
t.Fatal(err)
}
if err := server.Load("testdata/auth_anonymous.js", string(data)); err != nil {
t.Fatal(err)
}
if err := server.Start(); err != nil {
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 := context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, 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) 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
}