-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (69 loc) · 1.78 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"github.com/trymoose/authentikhome/pkg/homeassistant"
"github.com/trymoose/authentikhome/pkg/ldap"
"golang.org/x/exp/slog"
"gopkg.in/yaml.v3"
"os"
"time"
)
func main() {
defer RecoverAndExit()
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})))
args := ReadArgs()
cfg := ReadConfig[struct {
BaseDN string `yaml:"base"`
BindUser string `yaml:"username"`
BindPass string `yaml:"password"`
Group *string `yaml:"group"`
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
Secure bool `yaml:"secure"`
}](args.ConfigFile)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
conn := Must((&ldap.Dialer{Secure: cfg.Secure}).Dial(
ctx,
"tcp",
fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
))
defer Defer(conn.Close)
user := Must(conn.Login(
cfg.BaseDN,
cfg.BindUser,
cfg.BindPass,
args.Username,
args.Password,
cfg.Group,
))
if !user.Active {
return
}
meta := homeassistant.Meta{Name: user.Name, Group: homeassistant.GroupUsers}
if user.Admin {
meta.Group = homeassistant.GroupAdmin
}
fmt.Print(string(Must(meta.MarshalText())))
Login()
}
func ReadArgs() (args struct {
Username, Password string
ConfigFile string
}) {
args.Username = Must(homeassistant.EnvKeyUsername.Value())
args.Password = Must(homeassistant.EnvKeyPassword.Value())
args.ConfigFile = "./config.yml"
flag.StringVar(&args.ConfigFile, "config", args.ConfigFile, "ldap config file to read")
flag.BoolVar(&printStack, "debug", printStack, "print to terminal")
flag.Parse()
return
}
func ReadConfig[T any](filename string) (cfg T) {
f := Must(os.Open(filename))
defer Defer(f.Close)
Check(yaml.NewDecoder(f).Decode(&cfg))
return
}