Skip to content

Commit 9e5a98b

Browse files
refactor(web): auth
1 parent a8aacea commit 9e5a98b

File tree

9 files changed

+45
-35
lines changed

9 files changed

+45
-35
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ make dev-web
234234
235235
- Reload on config change
236236
- Add [Apprise](https://github.com/caronc/apprise) endpoint
237-
- HTTP and SMTP auth
238237
- CRUD endpoints and rules
239238
- SQLite full text search
240239
- Read mailbox files

internal/core/actor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package core
33
type Actor int
44

55
const (
6-
ActorAnonymous Actor = iota
6+
ActorUnknown Actor = iota
77
ActorSystem
88
)

internal/core/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (a App) newContext(ctx context.Context, actor Actor) Context {
3535
}
3636

3737
func (a App) Context(ctx context.Context) Context {
38-
return a.newContext(ctx, ActorAnonymous)
38+
return a.newContext(ctx, ActorUnknown)
3939
}
4040

4141
func (a App) SystemContext(ctx context.Context) Context {

internal/procs/auth.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import (
99

1010
var ErrorLogin = fmt.Errorf("login error")
1111

12-
func HTTPLogin(cc core.Context, username, password string) error {
13-
if cc.Config.AuthHTTP.Username == "" && cc.Config.AuthHTTP.Password == "" {
12+
func AuthHTTPAnonymous(cc core.Context) bool {
13+
return cc.Config.AuthHTTP.Username == "" && cc.Config.AuthHTTP.Password == ""
14+
}
15+
16+
func AuthHTTPLogin(cc core.Context, username, password string) error {
17+
if AuthHTTPAnonymous(cc) {
1418
return nil
1519
}
1620

web/controllers/auth.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"github.com/gofiber/fiber/v2/middleware/session"
99
)
1010

11-
type Flash struct {
11+
type LoginData struct {
1212
Flash string
1313
}
1414

1515
func Login(c *fiber.Ctx, cc core.Context) error {
16-
return c.Render("login", Flash{})
16+
return c.Render("login", LoginData{})
1717
}
1818

1919
func AuthLogin(c *fiber.Ctx, cc core.Context, store *session.Store) error {
@@ -22,65 +22,73 @@ func AuthLogin(c *fiber.Ctx, cc core.Context, store *session.Store) error {
2222
password := c.FormValue("password")
2323

2424
// Execute
25-
err := procs.HTTPLogin(cc, username, password)
25+
err := procs.AuthHTTPLogin(cc, username, password)
2626
if err != nil {
2727
if helpers.IsHTMXRequest(c) {
28-
return c.Render("login", Flash{Flash: err.Error()}, "form")
28+
return c.Render("login", LoginData{Flash: err.Error()}, "form")
2929
}
30-
return c.Render("login", Flash{Flash: err.Error()})
30+
return c.Render("login", LoginData{Flash: err.Error()})
3131
}
3232

3333
// Response
3434
sess, err := store.Get(c)
3535
if err != nil {
36-
panic(err)
36+
return helpers.Error(c, err)
3737
}
3838

3939
sess.Set("auth", true)
4040
if err := sess.Save(); err != nil {
41-
panic(err)
41+
return helpers.Error(c, err)
4242
}
4343

44-
return redirect(c, "/")
44+
return helpers.Redirect(c, "/")
4545
}
4646

4747
func AuthLogout(c *fiber.Ctx, cc core.Context, store *session.Store) error {
4848
sess, err := store.Get(c)
4949
if err != nil {
50-
panic(err)
50+
return helpers.Error(c, err)
5151
}
5252

5353
sess.Delete("auth")
5454
if err := sess.Save(); err != nil {
55-
panic(err)
55+
return helpers.Error(c, err)
5656
}
5757

58-
return redirect(c, "/login")
58+
return helpers.Redirect(c, "/login")
5959
}
6060

6161
func AuthRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
62+
if procs.AuthHTTPAnonymous(cc) {
63+
return c.Next()
64+
}
65+
6266
sess, err := store.Get(c)
6367
if err != nil {
6468
panic(err)
6569
}
6670

6771
auth := sess.Get("auth")
6872
if auth == nil {
69-
return redirect(c, "/login")
73+
return helpers.Redirect(c, "/login")
7074
}
7175

7276
return c.Next()
7377
}
7478

7579
func AuthSkip(c *fiber.Ctx, cc core.Context, store *session.Store) error {
80+
if procs.AuthHTTPAnonymous(cc) {
81+
return helpers.Redirect(c, "/")
82+
}
83+
7684
sess, err := store.Get(c)
7785
if err != nil {
78-
panic(err)
86+
return helpers.Error(c, err)
7987
}
8088

8189
auth := sess.Get("auth")
8290
if auth != nil {
83-
return redirect(c, "/")
91+
return helpers.Redirect(c, "/")
8492
}
8593

8694
return c.Next()

web/controllers/utils.go

-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package controllers
22

33
import (
4-
"github.com/ItsNotGoodName/smtpbridge/web/helpers"
54
"github.com/gofiber/fiber/v2"
65
)
76

@@ -13,11 +12,3 @@ func checkbox(c *fiber.Ctx, key string) bool {
1312

1413
return true
1514
}
16-
func redirect(c *fiber.Ctx, path string) error {
17-
if helpers.IsHTMXRequest(c) {
18-
c.Set("HX-Location", path)
19-
return nil
20-
} else {
21-
return c.Redirect(path)
22-
}
23-
}

web/helpers/helpers.go

-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/ItsNotGoodName/smtpbridge/internal/build"
99
"github.com/ItsNotGoodName/smtpbridge/web"
1010
"github.com/dustin/go-humanize"
11-
"github.com/gofiber/fiber/v2"
1211
)
1312

1413
var Map template.FuncMap = template.FuncMap{
@@ -42,8 +41,3 @@ var Map template.FuncMap = template.FuncMap{
4241
return template.URL(Query(queries, vals...))
4342
},
4443
}
45-
46-
func IsHTMXRequest(c *fiber.Ctx) bool {
47-
_, isHTMXRequest := c.GetReqHeaders()["Hx-Request"]
48-
return isHTMXRequest
49-
}

web/helpers/http.go

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import (
44
"github.com/gofiber/fiber/v2"
55
)
66

7+
func IsHTMXRequest(c *fiber.Ctx) bool {
8+
_, isHTMXRequest := c.GetReqHeaders()["Hx-Request"]
9+
return isHTMXRequest
10+
}
11+
12+
func Redirect(c *fiber.Ctx, path string) error {
13+
if IsHTMXRequest(c) {
14+
c.Set("HX-Location", path)
15+
return nil
16+
} else {
17+
return c.Redirect(path)
18+
}
19+
}
20+
721
func Error(c *fiber.Ctx, err error, codes ...int) error {
822
if IsHTMXRequest(c) {
923
c.Set("HX-Redirect", "/something-went-wrong")

web/views/login.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h2>Login</h2>
1919
<input type="password" id="password" name="password" placeholder="Password" />
2020
</label>
2121

22-
<button data-loading-path="/login" data-loading-disable data-loading-aria-busy>
22+
<button data-loading-path="/auth" data-loading-disable data-loading-aria-busy>
2323
Log in
2424
</button>
2525
{{with .Flash}}

0 commit comments

Comments
 (0)