-
Notifications
You must be signed in to change notification settings - Fork 37
/
module.go
131 lines (115 loc) · 3.13 KB
/
module.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
package tsauth
import (
"fmt"
"net/http"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/caddyauth"
"tailscale.com/client/tailscale"
"tailscale.com/tsnet"
"tailscale.com/util/strs"
)
func init() {
caddy.RegisterModule(TailscaleAuth{})
httpcaddyfile.RegisterHandlerDirective("tailscale_auth", parseCaddyfile)
}
type TailscaleAuth struct {
AuthKey string `json:"auth_key,omitempty"`
Hostname string `json:"hostname,omitempty"`
Verbose bool `json:"verbose,omitempty"`
server *tsnet.Server
client *tailscale.LocalClient
}
func (TailscaleAuth) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.authentication.providers.tailscale",
New: func() caddy.Module { return new(TailscaleAuth) },
}
}
func (ta *TailscaleAuth) Provision(ctx caddy.Context) error {
logger := ctx.Logger(ta).Sugar()
if ta.AuthKey != "" {
ta.server = &tsnet.Server{
Hostname: ta.Hostname,
Logf: func(format string, args ...any) {
if ta.Verbose {
logger.Infof(format, args...)
}
},
AuthKey: ta.AuthKey,
}
if err := ta.server.Start(); err != nil {
return err
}
var err error
ta.client, err = ta.server.LocalClient()
if err != nil {
return err
}
}
return nil
}
func (ta *TailscaleAuth) Cleanup() error {
if ta.server != nil {
ta.server.Close()
}
return nil
}
func (TailscaleAuth) Authenticate(w http.ResponseWriter, r *http.Request) (caddyauth.User, bool, error) {
user := caddyauth.User{}
info, err := tailscale.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
return user, false, err
}
if len(info.Node.Tags) != 0 {
return user, false, fmt.Errorf("node %s has tags", info.Node.Hostinfo.Hostname())
}
var tailnet string
if !info.Node.Hostinfo.ShareeNode() {
if s, found := strs.CutPrefix(info.Node.Name, info.Node.ComputedName+"."); found {
if s, found := strs.CutSuffix(s, ".beta.tailscale.net."); found {
tailnet = s
}
}
}
user.ID = info.UserProfile.LoginName
user.Metadata = map[string]string{
"tailscale_login": strings.Split(info.UserProfile.LoginName, "@")[0],
"tailscale_user": info.UserProfile.LoginName,
"tailscale_name": info.UserProfile.DisplayName,
"tailscale_profile_picture": info.UserProfile.ProfilePicURL,
"tailscale_tailnet": tailnet,
}
return user, true, nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var ta TailscaleAuth
repl := caddy.NewReplacer()
for h.Next() {
for nesting := h.Nesting(); h.NextBlock(nesting); {
switch h.Val() {
case "auth_key":
if !h.NextArg() {
return nil, h.ArgErr()
}
ta.AuthKey = h.Val()
ta.AuthKey = repl.ReplaceAll(ta.AuthKey, "")
case "hostname":
if !h.NextArg() {
return nil, h.ArgErr()
}
ta.Hostname = h.Val()
case "verbose":
ta.Verbose = true
}
}
}
return caddyauth.Authentication{
ProvidersRaw: caddy.ModuleMap{
"tailscale": caddyconfig.JSON(ta, nil),
},
}, nil
}