From e3690c8743eec1bfdc8fc000e83e0ba66b46e6b5 Mon Sep 17 00:00:00 2001 From: Johan Lindh Date: Thu, 14 Nov 2024 13:51:57 +0100 Subject: [PATCH] add MakeAuth hook fn --- auth.go | 9 +++++++++ jaws.go | 1 + template.go | 5 +++++ template_test.go | 1 - testjaws_test.go | 20 ++++++++++++++++++++ with.go | 3 ++- 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 auth.go diff --git a/auth.go b/auth.go new file mode 100644 index 0000000..37bd3f5 --- /dev/null +++ b/auth.go @@ -0,0 +1,9 @@ +package jaws + +type Auth interface { + Data() map[string]any // returns authenticated user data, or nil + Email() string // returns authenticated user email, or an empty string + IsAdmin() bool // return true if admins are defined and current user is one +} + +type MakeAuthFn func(*Request) Auth diff --git a/jaws.go b/jaws.go index fda902b..2167d40 100644 --- a/jaws.go +++ b/jaws.go @@ -44,6 +44,7 @@ type Jaws struct { CookieName string // Name for session cookies, defaults to "jaws" Logger *slog.Logger // Optional logger to use Debug bool // Set to true to enable debug info in generated HTML code + MakeAuth MakeAuthFn // Optional function to create With.Auth for Templates doneCh <-chan struct{} bcastCh chan Message subCh chan subscription diff --git a/template.go b/template.go index f1012fa..fae8e8f 100644 --- a/template.go +++ b/template.go @@ -29,11 +29,16 @@ func (t Template) JawsRender(e *Element, wr io.Writer, params []any) error { e.Tag(tags...) e.handlers = append(e.handlers, handlers...) attrstr := template.HTMLAttr(strings.Join(attrs, " ")) // #nosec G203 + var auth Auth + if f := e.Request.Jaws.MakeAuth; f != nil { + auth = f(e.Request) + } return e.Request.Jaws.LookupTemplate(t.Name).Execute(wr, With{ Element: e, RequestWriter: e.Request.Writer(wr), Dot: t.Dot, Attrs: attrstr, + Auth: auth, }) } diff --git a/template_test.go b/template_test.go index dbd651a..29e2573 100644 --- a/template_test.go +++ b/template_test.go @@ -18,7 +18,6 @@ func TestTemplate_String(t *testing.T) { func TestTemplate_Calls_Dot_Updater(t *testing.T) { rq := newTestRequest() defer rq.Close() - dot := &testUi{} tmpl := NewTemplate("testtemplate", dot) tmpl.JawsUpdate(nil) diff --git a/testjaws_test.go b/testjaws_test.go index ec24801..4c00e4e 100644 --- a/testjaws_test.go +++ b/testjaws_test.go @@ -16,11 +16,31 @@ type testJaws struct { log bytes.Buffer } +type testAuth struct{} + +// Data implements Auth. +func (t testAuth) Data() map[string]any { + return nil +} + +// Email implements Auth. +func (t testAuth) Email() string { + return "" +} + +// IsAdmin implements Auth. +func (t testAuth) IsAdmin() bool { + return false +} + func newTestJaws() (tj *testJaws) { tj = &testJaws{ Jaws: New(), } tj.Jaws.Logger = slog.New(slog.NewTextHandler(&tj.log, nil)) + tj.Jaws.MakeAuth = func(r *Request) Auth { + return testAuth{} + } tj.testtmpl = template.Must(template.New("testtemplate").Parse(`{{with $.Dot}}
{{.}}
{{end}}`)) tj.AddTemplateLookuper(tj.testtmpl) tj.Jaws.updateTicker = time.NewTicker(time.Millisecond) diff --git a/with.go b/with.go index 855fa33..f39619d 100644 --- a/with.go +++ b/with.go @@ -5,10 +5,11 @@ import ( ) // With is passed as the data parameter when using RequestWriter.Template(), -// populated with all members set. +// populated with all required members set. type With struct { *Element // the Element being rendered using a template. RequestWriter // the RequestWriter Dot any // user data parameter Attrs template.HTMLAttr // HTML attributes string + Auth Auth // (optional) authentication information returned by MakeAuthFn }