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"
|
2023-11-30 19:09:51 +01:00
|
|
|
edgehttp "forge.cadoles.com/arcad/edge/pkg/http"
|
2023-09-29 07:41:01 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/jwtutil"
|
2023-02-21 12:14:29 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module"
|
2023-03-20 16:40:08 +01:00
|
|
|
"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
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
key := getDummyKey()
|
2023-02-21 12:14:29 +01:00
|
|
|
|
|
|
|
server := app.NewServer(
|
|
|
|
module.ConsoleModuleFactory(),
|
2023-02-24 14:40:28 +01:00
|
|
|
ModuleFactory(
|
2023-03-20 16:40:08 +01:00
|
|
|
WithJWT(getDummyKeySet(key)),
|
2023-02-24 14:40:28 +01:00
|
|
|
),
|
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)
|
|
|
|
}
|
|
|
|
|
2023-10-19 20:05:59 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
token := jwt.New()
|
2023-02-21 12:14:29 +01:00
|
|
|
|
2023-03-20 16:40:08 +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))
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
req.Header.Add("Authorization", "Bearer "+string(rawToken))
|
2023-02-21 12:14:29 +01:00
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
ctx = edgehttp.WithContextHTTPRequest(context.Background(), req)
|
2023-02-21 12:14:29 +01:00
|
|
|
|
2023-03-01 13:04:40 +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
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
key := getDummyKey()
|
2023-02-21 12:14:29 +01:00
|
|
|
|
|
|
|
server := app.NewServer(
|
|
|
|
module.ConsoleModuleFactory(),
|
2023-03-20 16:40:08 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-10-19 20:05:59 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
ctx = edgehttp.WithContextHTTPRequest(context.Background(), req)
|
2023-02-21 12:14:29 +01:00
|
|
|
|
2023-03-01 13:04:40 +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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
func getDummyKey() jwk.Key {
|
2023-02-21 12:14:29 +01:00
|
|
|
secret := []byte("not_so_secret")
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
key, err := jwk.FromRaw(secret)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
2023-02-21 12:14:29 +01:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
if err := key.Set(jwk.AlgorithmKey, jwa.HS256); err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
2023-02-21 12:14:29 +01:00
|
|
|
}
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2023-09-29 07:41:01 +02:00
|
|
|
func getDummyKeySet(key jwk.Key) jwtutil.GetKeySetFunc {
|
2023-03-20 16:40:08 +01:00
|
|
|
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
|
|
|
}
|