Skip to content

Commit

Permalink
add MakeAuth hook fn
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Nov 14, 2024
1 parent 874a694 commit e3690c8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions jaws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
1 change: 0 additions & 1 deletion template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions testjaws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}<div id="{{$.Jid}}" {{$.Attrs}}>{{.}}</div>{{end}}`))
tj.AddTemplateLookuper(tj.testtmpl)
tj.Jaws.updateTicker = time.NewTicker(time.Millisecond)
Expand Down
3 changes: 2 additions & 1 deletion with.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e3690c8

Please sign in to comment.