summaryrefslogtreecommitdiff
path: root/api/auth/auth.go
blob: 78123a6ecb7dad54765d35b0aed626bd2288a942 (plain) (blame)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package auth

import (
	"errors"
	"log"
	"net"
	"net/http"
	"time"

	"penguins.lan/adapters/arp"
	"penguins.lan/api/types"
	"penguins.lan/database"
	"penguins.lan/utils"
)

const (
	sessionCookie = "penguin_session"
	sessionTTL    = 30 * 24 * time.Hour
)

func ClientIP(req *http.Request) string {
	host, _, err := net.SplitHostPort(req.RemoteAddr)
	if err != nil {
		return req.RemoteAddr
	}
	return host
}

// clientMac resolves the visitor's MAC from the DEV_MAC override or the ARP
// cache the scheduler keeps fresh. "" means we don't know it.
func clientMac(context *types.RequestContext, req *http.Request) string {
	if dev := context.Args.DevMAC; dev != "" {
		if mac, err := utils.ParseMac(dev); err == nil {
			return mac.Format()
		}
		return ""
	}
	ip := ClientIP(req)
	mac, err := database.MacForIP(context.DBConn, ip)
	if err != nil {
		log.Println("mac lookup failed:", err)
	}
	if mac != "" {
		return mac
	}
	return liveMac(ip)
}

// liveMac falls back to a direct ARP read for a brand-new connection the
// scheduler hasn't cached yet. Non-IPv4 peers (e.g. loopback) are skipped so we
// don't shell out for addresses that can never resolve.
func liveMac(ip string) string {
	if _, err := utils.ParseINet4(ip); err != nil {
		return ""
	}
	table, err := arp.SystemProvider{}.Fetch()
	if err != nil {
		log.Println("live arp lookup failed:", err)
		return ""
	}
	mac, _ := table.Lookup(ip)
	return mac
}

func setSessionCookie(resp http.ResponseWriter, id string) {
	http.SetCookie(resp, &http.Cookie{
		Name:     sessionCookie,
		Value:    id,
		Path:     "/",
		MaxAge:   int(sessionTTL.Seconds()),
		HttpOnly: true,
		SameSite: http.SameSiteLaxMode,
	})
}

func clearSessionCookie(resp http.ResponseWriter) {
	http.SetCookie(resp, &http.Cookie{Name: sessionCookie, Value: "", Path: "/", MaxAge: -1, HttpOnly: true})
}

func LoginUser(context *types.RequestContext, resp http.ResponseWriter, user *database.User) error {
	session, err := database.CreateSession(context.DBConn, utils.RandomId(), user.ID, sessionTTL)
	if err != nil {
		return err
	}
	setSessionCookie(resp, session.ID)
	context.User = user
	context.Nick = user.Username
	return nil
}

func ResolveSessionContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
		if cookie, err := req.Cookie(sessionCookie); err == nil && cookie.Value != "" {
			if user, err := database.GetSessionUser(context.DBConn, cookie.Value); err == nil {
				context.User = user
				context.Nick = user.Username
				_ = database.TouchUser(context.DBConn, user.ID)
				return success(context, req, resp)
			}
		}

		mac := clientMac(context, req)
		context.ClientMAC = mac

		if mac != "" {
			if user, err := database.GetUserByMAC(context.DBConn, mac); err == nil {
				if err := LoginUser(context, resp, user); err != nil {
					log.Println("mac auto-login failed:", err)
				} else {
					_ = database.TouchUser(context.DBConn, user.ID)
				}
			}
		}

		return success(context, req, resp)
	}
}

func RenameContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		if context.User == nil {
			return success(context, req, resp)
		}

		newName := utils.SanitizeName(req.FormValue("username"))
		if newName == "" || newName == context.User.Username {
			http.Redirect(resp, req, "/", http.StatusSeeOther)
			return success(context, req, resp)
		}

		renameError := func(message string) types.ContinuationChain {
			(*context.TemplateData)["Error"] = types.BannerMessages{Messages: []string{message}}
			return failure(context, req, resp)
		}

		switch existing, err := database.GetUserByUsername(context.DBConn, newName); {
		case err == nil && existing.ID != context.User.ID:
			return renameError("that handle's taken.")
		case err != nil && !errors.Is(err, database.ErrNotFound):
			log.Println("rename lookup failed:", err)
			return renameError("something broke, try again")
		}

		oldName := context.User.Username
		if err := database.RenameUser(context.DBConn, context.User.ID, newName); err != nil {
			log.Println("rename failed:", err)
			return renameError("couldn't rename, try again")
		}

		// Device records are synthesized from the current username and follow
		// the rename for free; stored records have the old name baked in, so
		// move them to the new subdomain to keep the user's setup working.
		zone := context.Args.DnsZone
		if err := database.RenameUserDNSDomain(context.DBConn, context.User.ID, oldName+"."+zone, newName+"."+zone); err != nil {
			log.Println("rename dns records failed:", err)
		}

		context.User.Username = newName
		context.Nick = newName
		http.Redirect(resp, req, "/", http.StatusSeeOther)
		return success(context, req, resp)
	}
}

func RequireUserContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		if context.User != nil {
			return success(context, req, resp)
		}
		return failure(context, req, resp)
	}
}

func GoPortalContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
		http.Redirect(resp, req, "/portal", http.StatusSeeOther)
		return success(context, req, resp)
	}
}

func UnauthorizedContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
		resp.WriteHeader(http.StatusUnauthorized)
		return success(context, req, resp)
	}
}

func LogoutContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
		if cookie, err := req.Cookie(sessionCookie); err == nil && cookie.Value != "" {
			_ = database.DeleteSession(context.DBConn, cookie.Value)
		}
		clearSessionCookie(resp)
		http.Redirect(resp, req, "/portal", http.StatusSeeOther)
		return success(context, req, resp)
	}
}

type portalView struct {
	SuggestedName string
	RequestedName string
	DetectedMAC   string
	Taken         bool
	LastSeen      string
}

func ShowPortalContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		if context.User != nil {
			http.Redirect(resp, req, "/", http.StatusSeeOther)
			return failure(context, req, resp)
		}
		(*context.TemplateData)["Portal"] = portalView{
			SuggestedName: utils.SuggestName(),
			DetectedMAC:   context.ClientMAC,
		}
		return success(context, req, resp)
	}
}

func SetupUserContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
	return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
		username := utils.SanitizeName(req.FormValue("username"))
		reclaim := req.FormValue("reclaim") == "1"

		mac := clientMac(context, req)
		context.ClientMAC = mac

		view := portalView{
			SuggestedName: utils.SuggestName(),
			RequestedName: username,
			DetectedMAC:   mac,
		}

		fail := func(message string) types.ContinuationChain {
			if message != "" {
				(*context.TemplateData)["Error"] = types.BannerMessages{Messages: []string{message}}
			}
			(*context.TemplateData)["Portal"] = view
			return failure(context, req, resp)
		}

		if username == "" {
			resp.WriteHeader(http.StatusBadRequest)
			return fail("pick a handle, any handle 🐧")
		}

		existing, err := database.GetUserByUsername(context.DBConn, username)
		switch {
		case errors.Is(err, database.ErrNotFound):
			user, err := database.CreateUser(context.DBConn, utils.RandomId(), username)
			if err != nil {
				log.Println("create user failed:", err)
				resp.WriteHeader(http.StatusInternalServerError)
				return fail("couldn't create your account, try again")
			}
			bindAndFinish(context, resp, user, mac)
			http.Redirect(resp, req, "/", http.StatusSeeOther)
			return success(context, req, resp)

		case err != nil:
			log.Println("lookup user failed:", err)
			resp.WriteHeader(http.StatusInternalServerError)
			return fail("something broke, try again")

		default:
			if !reclaim {
				view.Taken = true
				view.LastSeen = utils.FormatSince(existing.LastSeen)
				return fail("")
			}
			bindAndFinish(context, resp, existing, mac)
			http.Redirect(resp, req, "/", http.StatusSeeOther)
			return success(context, req, resp)
		}
	}
}

func bindAndFinish(context *types.RequestContext, resp http.ResponseWriter, user *database.User, mac string) {
	if mac != "" {
		if err := database.AssociateMAC(context.DBConn, mac, user.ID); err != nil {
			log.Println("associate mac failed:", err)
		}
		// First device a user binds becomes their primary automatically. The
		// conditional update is atomic, so this stays correct even if two
		// devices bind concurrently.
		if err := database.SetPrimaryMACIfUnset(context.DBConn, user.ID, mac); err != nil {
			log.Println("set primary mac failed:", err)
		}
	}
	_ = database.TouchUser(context.DBConn, user.ID)
	if err := LoginUser(context, resp, user); err != nil {
		log.Println("login failed:", err)
	}
}