-
Notifications
You must be signed in to change notification settings - Fork 1
/
header_auth.go
171 lines (146 loc) · 4.67 KB
/
header_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package auth
import (
"fmt"
"strings"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tokens"
"github.com/pocketbase/pocketbase/tools/security"
)
/*
Header auth enables you to authenticate users and admins via HTTP headers.
This is useful when you want to authenticate users via a proxy server.
For example, you can use [caddy](https://caddyserver.com/) to authenticate
users via SAML and pass the user's email to the backend via a header.
*/
func InstallHeaderAuth(app core.App, router *echo.Echo, config HeaderAuthConfig) {
if !config.IsValid() {
return
}
router.Pre(
authViaHeader(app, config), // authenticate admin & user requests
// these are needed because echo doesn't allow us to overwrite endpoints
overwriteAdminEmailAuth(app), // overwrite admin email auth endpoint: /api/admins/auth-via-email
overwriteUserEmailAuth(app), // overwrite user email auth endpoint: /api/users/auth-via-email
)
}
/*
AuthenticateUser is a helper function that authenticates a user via email.
If the user doesn't exist, it will optionally create a new user (HeaderAuthConfig.AutoCreateUser).
*/
func AuthenticateUser(app core.App, c echo.Context, config HeaderAuthConfig) *models.Record {
email := config.GetEmailFromHeader(c.Request().Header)
users, _ := app.Dao().FindCollectionByNameOrId("users")
user, err := app.Dao().FindAuthRecordByEmail("users", email)
if err != nil {
if config.AutoCreateUser && email != "" {
name := config.GetNameFromHeader(c.Request().Header)
fields := config.GetFieldsFromHeader(c.Request().Header)
// create user
user = models.NewRecord(users)
user.SetEmail(email)
user.SetVerified(true)
user.Set("name", name)
// set user data
for field, value := range fields {
user.Set(field, value)
}
// check for username in fields
if user.Username() == "" {
baseUsername := users.Name + security.RandomStringWithAlphabet(5, "123456789")
user.SetUsername(app.Dao().SuggestUniqueAuthRecordUsername(users.Id, baseUsername))
}
user.RefreshTokenKey()
if err := app.Dao().Save(user); err != nil {
fmt.Println("Error creating user,", email, err)
return nil
}
fmt.Println("User", user.GetString("email"), "created")
} else {
return nil
}
}
return user
}
func authViaHeader(app core.App, config HeaderAuthConfig) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// if request starts with /api/*
if strings.HasPrefix(c.Request().RequestURI, "/api/") {
email := config.GetEmailFromHeader(c.Request().Header)
if email != "" {
if config.AdminLogin {
// check for admin
admin, _ := app.Dao().FindAdminByEmail(email)
if admin != nil {
c.Set(apis.ContextAdminKey, admin)
}
}
// check for user
if user := AuthenticateUser(app, c, config); user != nil {
c.Set(apis.ContextAuthRecordKey, user)
}
}
}
return next(c)
}
}
}
func overwriteAdminEmailAuth(app core.App) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Method == "POST" && c.Request().RequestURI == "/api/admins/auth-with-password" {
admin, _ := c.Get(apis.ContextAdminKey).(*models.Admin)
if admin == nil {
return next(c)
}
token, tokenErr := tokens.NewAdminAuthToken(app, admin)
if tokenErr != nil {
return next(c)
}
event := &core.AdminAuthEvent{
HttpContext: c,
Admin: admin,
Token: token,
}
return app.OnAdminAuthRequest().Trigger(event, func(e *core.AdminAuthEvent) error {
return e.HttpContext.JSON(200, map[string]any{
"token": e.Token,
"admin": e.Admin,
})
})
}
return next(c)
}
}
}
func overwriteUserEmailAuth(app core.App) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Method == "POST" && c.Request().RequestURI == "/api/collections/users/auth-with-password" {
user, _ := c.Get("user").(*models.Record)
if user == nil {
return next(c)
}
token, tokenErr := tokens.NewRecordAuthToken(app, user)
if tokenErr != nil {
return next(c)
}
event := &core.RecordAuthEvent{
HttpContext: c,
Record: user,
Token: token,
}
return app.OnRecordAuthRequest().Trigger(event, func(e *core.RecordAuthEvent) error {
return e.HttpContext.JSON(200, map[string]any{
"token": e.Token,
"record": e.Record,
})
})
}
return next(c)
}
}
}