-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
67 lines (51 loc) · 1.47 KB
/
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
package main
import (
"log"
"os"
"strings"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type PAMAuthConfig struct {
MinUID int
MinGID int
ExcludeUsernames []string
}
var DefaultPAMAuthConfig = PAMAuthConfig{
MinUID: 1000,
MinGID: 1000,
ExcludeUsernames: []string{"root"},
}
func AuthMiddleware() echo.MiddlewareFunc {
return AuthMiddlewareWithConfig(DefaultPAMAuthConfig)
}
func BasicAuthValidator(minUID, minGID int, excludeUsernames []string) middleware.BasicAuthValidator {
return func(username, password string, c echo.Context) (bool, error) {
for _, excluded := range excludeUsernames {
if strings.Compare(username, excluded) == 0 {
return false, nil
}
}
fd, err := os.Open("/etc/passwd")
if err != nil {
log.Printf("Unable to open passwd file: %s", err.Error())
return false, err
}
defer fd.Close()
passwd := ParsePasswd(fd)
user, ok := passwd[username]
if ok && user.UID >= int64(minUID) && user.GID >= int64(minGID) {
c.Set("user", user)
return PAMAuth(username, password), nil
}
log.Printf("User {%s} not found", username)
return false, nil
}
}
func AuthMiddlewareWithConfig(config PAMAuthConfig) echo.MiddlewareFunc {
return middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{
Skipper: middleware.DefaultSkipper,
Validator: BasicAuthValidator(config.MinUID, config.MinGID, config.ExcludeUsernames),
Realm: middleware.DefaultBasicAuthConfig.Realm,
})
}