generated from home-assistant/addons-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
263 lines (231 loc) · 7.04 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
ldap "github.com/go-ldap/ldap/v3"
log "github.com/sirupsen/logrus"
)
var configFile = flag.String("config", "/data/options.json", "The path to the server's config file")
// LDAPOptions holds the opotions for an `LDAPAuthenticator`.
type LDAPOptions struct {
ServerURL string
BindDNTemplate string
SearchBaseDN string
SearchFilterTemplate string
}
// LDAPUser holds metadata about a user from an LDAP server.
type LDAPUser struct {
DisplayName string
}
// LDAPAuthenticator is a client for authenticating users against an LDAP server.
type LDAPAuthenticator struct {
Options LDAPOptions
}
// Authenticate authenticates a user given their username and password.
//
// If authentication is successful, it also returns metadata about the
// authenticated user.
func (a *LDAPAuthenticator) Authenticate(username, password string) (bool, LDAPUser, error) {
if password == "" {
return false, LDAPUser{}, nil
}
conn, err := ldap.DialURL(a.Options.ServerURL)
if err != nil {
return false, LDAPUser{}, fmt.Errorf("could not dial server at %q: %w", a.Options.ServerURL, err)
}
defer conn.Close()
bindDN := fmt.Sprintf(a.Options.BindDNTemplate, username)
log.Debugf("Binding to DN: %q", bindDN)
if err := conn.Bind(bindDN, password); err != nil {
log.Debugf("Could not bind to user %q: %q", bindDN, err)
return false, LDAPUser{}, nil
}
searchFilter := fmt.Sprintf(a.Options.SearchFilterTemplate, username)
log.Debugf("Searching with filter: %q", searchFilter)
res, err := conn.Search(ldap.NewSearchRequest(
a.Options.SearchBaseDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
/* SizeLimit= */ 2,
/* TimeLimit= */ 1,
/* TypesOnly= */ false,
searchFilter,
/* Attribuutes= */ nil,
/* Controls= */ nil,
))
if err != nil {
log.Warnf("Search failed: %v", err)
return false, LDAPUser{}, nil
}
if len(res.Entries) == 0 {
log.Debugf("No matches found for filter: %q", searchFilter)
return false, LDAPUser{}, nil
}
if len(res.Entries) > 1 {
log.Debugf("More than one (%d) matches found for filter: %q", len(res.Entries), searchFilter)
return false, LDAPUser{}, nil
}
entry := res.Entries[0]
if log.IsLevelEnabled(log.DebugLevel) {
var entryStr strings.Builder
fmt.Fprintf(&entryStr, "DN: %q\n", entry.DN)
for _, attr := range entry.Attributes {
fmt.Fprintf(&entryStr, "%s: %q\n", attr.Name, attr.Values)
}
log.Debugf("User entry:\n%v", entryStr.String())
}
return true, LDAPUser{
DisplayName: entry.GetAttributeValue("cn"),
}, nil
}
var ErrUserAuthnFailed = errors.New("could not authenticate user")
type ServerOptions struct {
LDAPOptions LDAPOptions
}
type HASSAuthenticateRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
type ErrorResponse struct {
StatusCode int
PublicError string
Error error
}
type Server struct {
options ServerOptions
authenticator LDAPAuthenticator
router *gin.Engine
}
// NewServer creates a new Server.
func NewServer(options ServerOptions) *Server {
server := &Server{
options: options,
authenticator: LDAPAuthenticator{
Options: options.LDAPOptions,
},
router: gin.Default(),
}
if err := server.router.SetTrustedProxies(nil); err != nil {
panic("Not possible")
}
server.router.POST("/hass_authenticate", server.hassAuthenticate)
return server
}
func (s *Server) writeError(c *gin.Context, err ErrorResponse) {
if err.StatusCode >= 400 && err.StatusCode < 500 {
log.Infof(err.Error.Error())
} else {
log.Warnf(err.Error.Error())
}
if gin.Mode() == gin.DebugMode {
c.JSON(err.StatusCode, gin.H{"error": err.Error.Error()})
} else {
c.JSON(err.StatusCode, gin.H{"error": err.PublicError})
}
}
func (s *Server) hassAuthenticate(c *gin.Context) {
var req HASSAuthenticateRequest
if err := c.BindJSON(&req); err != nil {
s.writeError(c, ErrorResponse{
StatusCode: http.StatusBadRequest,
PublicError: err.Error(),
Error: err,
})
return
}
ok, user, err := s.authenticator.Authenticate(req.Username, req.Password)
if err != nil {
s.writeError(c, ErrorResponse{
StatusCode: http.StatusBadRequest,
PublicError: fmt.Sprintf("Could not authenticate user %q", req.Username),
Error: fmt.Errorf("when authenticating: %w", err),
})
return
}
if !ok {
s.writeError(c, ErrorResponse{
StatusCode: http.StatusBadRequest,
PublicError: fmt.Sprintf("Could not authenticate user: %q", req.Username),
Error: fmt.Errorf("when authenticating %q: %w", req.Username, ErrUserAuthnFailed),
})
return
}
if user.DisplayName != "" {
safeDN := strings.ReplaceAll(user.DisplayName, "\n", "")
fmt.Fprintf(c.Writer, "name = %v", safeDN)
}
}
// Serve starts the server listening to requests.
func (s *Server) Serve() error {
addr := ":80"
log.Infof("Starting server at: %q", addr)
return s.router.Run(addr)
}
// AddOnConfig is the struct representing the add-on's configuration.
type AddOnConfig struct {
LDAPServerURL string `json:"ldap_server_url"`
BindDNTemplate string `json:"bind_dn_template"`
SearchBaseDN string `json:"search_base_dn"`
SearchFilterTemplate string `json:"search_filter_template"`
DebugMode bool `json:"debug_mode"`
}
// parseAddOnConfig parses the `AddOnConfig` from a JSON file.
func parseAddOnConfig(configFile string) (AddOnConfig, error) {
jsonBytes, err := os.ReadFile(configFile)
if err != nil {
return AddOnConfig{}, fmt.Errorf("could not read config file: %q: %w", configFile, err)
}
var config AddOnConfig
err = json.Unmarshal(jsonBytes, &config)
if err != nil {
return AddOnConfig{}, fmt.Errorf("could not parse config file: %q: %w", configFile, err)
}
return config, nil
}
var ErrInvalidServerOptions = errors.New("invalid server options")
func toServerOptions(config AddOnConfig) (ServerOptions, error) {
if (config.SearchBaseDN == "") != (config.SearchFilterTemplate == "") {
return ServerOptions{}, fmt.Errorf("search_base_dn (%q) and search_filter (%q) must both be set or both be unset: %w", config.SearchBaseDN, config.SearchFilterTemplate, ErrInvalidServerOptions)
}
log.Infof("Loaded config: %+v", config)
return ServerOptions{
LDAPOptions: LDAPOptions{
ServerURL: config.LDAPServerURL,
BindDNTemplate: config.BindDNTemplate,
SearchBaseDN: config.SearchBaseDN,
SearchFilterTemplate: config.SearchFilterTemplate,
},
}, nil
}
func init() {
log.SetReportCaller(true)
log.SetLevel(log.InfoLevel)
}
func main() {
flag.Parse()
config, err := parseAddOnConfig(*configFile)
if err != nil {
log.Fatalf("Invalid config: %v", err)
}
if config.DebugMode {
gin.SetMode(gin.DebugMode)
log.SetLevel(log.DebugLevel)
} else {
gin.SetMode(gin.ReleaseMode)
}
options, err := toServerOptions(config)
if err != nil {
log.Fatalf("Invalid config: %v", err)
}
server := NewServer(options)
if err := server.Serve(); err != nil {
log.Fatalf("Error while serving: %v", err)
}
log.Info("Server terminated.")
}