-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
732 lines (606 loc) · 17.3 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package main
import (
"context"
"crypto/x509"
"database/sql"
"encoding/pem"
"encoding/xml"
"errors"
"fmt"
"html/template"
"net/http"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
"github.com/ucarion/saml"
"golang.org/x/crypto/bcrypt"
)
type account struct {
ID uuid.UUID `db:"id"`
SAMLIssuer *string `db:"saml_issuer"`
SAMLX509 []byte `db:"saml_x509"`
SAMLRedirectURL *string `db:"saml_redirect_url"`
}
type user struct {
ID uuid.UUID `db:"id"`
AccountID uuid.UUID `db:"account_id"`
SAMLID *string `db:"saml_id"`
DisplayName string `db:"display_name"`
PasswordHash []byte `db:"password_hash"`
}
type session struct {
ID uuid.UUID `db:"id"`
UserID uuid.UUID `db:"user_id"`
ExpiresAt time.Time `db:"expires_at"`
}
type todo struct {
ID uuid.UUID `db:"id"`
AuthorID uuid.UUID `db:"author_id"`
Body string `db:"body"`
}
type store struct {
DB *sqlx.DB
}
func (s *store) getAccount(ctx context.Context, id uuid.UUID) (account, error) {
var a account
err := s.DB.GetContext(ctx, &a, `
select
id, saml_issuer, saml_x509, saml_redirect_url
from
accounts
where
id = $1
`, id)
return a, err
}
func (s *store) createAccount(ctx context.Context, a account) error {
_, err := s.DB.ExecContext(ctx, `insert into accounts (id) values ($1)`, a.ID)
return err
}
func (s *store) updateAccount(ctx context.Context, a account) error {
_, err := s.DB.ExecContext(ctx, `
update
accounts
set
saml_issuer = $1,
saml_x509 = $2,
saml_redirect_url = $3
where
id = $4
`, a.SAMLIssuer, a.SAMLX509, a.SAMLRedirectURL, a.ID)
return err
}
func (s *store) listUsers(ctx context.Context, accountID uuid.UUID) ([]user, error) {
var users []user
err := s.DB.SelectContext(ctx, &users, `
select
id, account_id, display_name, password_hash
from
users
where
account_id = $1
`, accountID)
return users, err
}
func (s *store) getUser(ctx context.Context, id uuid.UUID) (user, error) {
var u user
err := s.DB.GetContext(ctx, &u, `
select
id, account_id, saml_id, display_name, password_hash
from
users
where
id = $1
`, id)
return u, err
}
func (s *store) getUserBySAMLID(ctx context.Context, accountID uuid.UUID, samlID string) (user, error) {
var u user
err := s.DB.GetContext(ctx, &u, `
select
id, account_id, saml_id, display_name, password_hash
from
users
where
account_id = $1 and saml_id = $2
`, accountID, samlID)
return u, err
}
func (s *store) createUser(ctx context.Context, u user) error {
_, err := s.DB.ExecContext(ctx, `
insert into users
(id, account_id, saml_id, display_name, password_hash)
values
($1, $2, $3, $4, $5)
`, u.ID, u.AccountID, u.SAMLID, u.DisplayName, u.PasswordHash)
return err
}
func (s *store) createSession(ctx context.Context, sess session) error {
_, err := s.DB.ExecContext(ctx, `
insert into sessions
(id, user_id, expires_at)
values
($1, $2, $3)
`, sess.ID, sess.UserID, sess.ExpiresAt)
return err
}
func (s *store) getSession(ctx context.Context, id uuid.UUID) (session, error) {
var sess session
err := s.DB.GetContext(ctx, &sess, `
select
id, user_id, expires_at
from
sessions
where
sessions.id = $1
`, id)
return sess, err
}
func (s *store) listTodos(ctx context.Context, accountID uuid.UUID) ([]todo, error) {
var todos []todo
err := s.DB.SelectContext(ctx, &todos, `
select
todos.id, todos.author_id, todos.body
from
todos
join
users on todos.author_id = users.id
where
users.account_id = $1
`, accountID)
return todos, err
}
func (s *store) createTodo(ctx context.Context, t todo) error {
_, err := s.DB.ExecContext(ctx, `
insert into todos
(id, author_id, body)
values
($1, $2, $3)
`, t.ID, t.AuthorID, t.Body)
return err
}
var indexTemplate = template.Must(template.New("index").Parse(`
<h1>
SAML TodoApp
</h1>
<h2>Sign up</h2>
<form action="/accounts" method="post">
<label>
Username
<input type="text" name="root_display_name" />
</label>
<label>
Password
<input type="password" name="root_password" />
</label>
<button>Create a new account</button>
</form>
<h2>Log in</h2>
<form action="/login" method="post">
<label>
User ID (not username)
<input type="text" name="id" />
</label>
<label>
Password
<input type="password" name="password" />
</label>
<button>Log into an existing user</button>
</form>
`))
type getAccountData struct {
ID string
CurrentUser user
SAMLACS string
SAMLRecipientID string
SAMLIssuerID string
SAMLIssuerX509 string
SAMLRedirectURL string
Users []user
Todos []todoWithAuthor
}
type todoWithAuthor struct {
Todo todo
User user
}
var getAccountTemplate = template.Must(template.New("get_account").Parse(`
<!DOCTYPE html>
<head>
<style>
table, th, td { border: 1px solid black; }
</style>
</head>
<body>
<h1>SAML TodoApp</h1>
<p>You are logged in as: {{ .CurrentUser.DisplayName }} (id = {{ .CurrentUser.ID }}) </p>
<h2>SAML Configuration</h2>
<a href="/accounts/{{ .ID }}/saml/initiate">Initiate SAML Login Flow</a>
<table>
<caption>
Data you need to put into your Identity Provider
</caption>
<tr>
<td>SAML Assertion Consumer Service ("ACS") URL</td>
<td><code>{{ .SAMLACS }}</code></td>
</tr>
<tr>
<td>SAML Recipient ID</td>
<td><code>{{ .SAMLRecipientID }}</code></td>
</tr>
</table>
<table>
<caption>
Data from your Identity Provider you need to give us
</caption>
<tr>
<td>SAML Issuer Entity ID</td>
<td><code>{{ .SAMLIssuerID }}</code></td>
</tr>
<tr>
<td>SAML Issuer x509 Certificate</td>
<td><code><pre>{{ .SAMLIssuerX509 }}</pre></code></td>
</tr>
<tr>
<td>SAML Redirect URL (aka "HTTP-Redirect Binding URL")</td>
<td><code><pre>{{ .SAMLRedirectURL }}</pre></code></td>
</tr>
</table>
<form action="/accounts/{{ .ID }}/metadata" method="post" enctype="multipart/form-data">
<input type="file" accept=".xml" name="metadata" />
<button>Upload Identity Provider SAML Metadata</button>
</form>
<h2>Users</h2>
<p>There are {{ len .Users }} users:</p>
<ul>
{{ range .Users }}
<li>
{{ .DisplayName }} (id = {{ .ID }})
</li>
{{ end }}
</ul>
<form action="/accounts/{{ .ID }}/users" method="post">
<label>
Display Name
<input type="text" name="display_name" />
</label>
<label>
Password
<input type="password" name="password" />
</label>
<button>Create a user</button>
</form>
<h2>Todos</h2>
There are {{ len .Todos }} todos:
<ul>
{{ range .Todos }}
<li>
{{ .User.DisplayName }}: {{ .Todo.Body }} (id = {{ .Todo.ID }})
</li>
{{ end }}
</ul>
<form action="/accounts/{{ .ID }}/todos" method="post">
<label>
Body
<input type="text" name="body" />
</label>
<button>Create a todo</button>
</form>
</body>
`))
func with500(f func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if err := f(w, r, p); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "internal server error: %s", err.Error())
}
}
}
func issueSession(ctx context.Context, s *store, w http.ResponseWriter, u user) error {
sess := session{ID: uuid.New(), UserID: u.ID, ExpiresAt: time.Now().Add(time.Hour * 24)}
if err := s.createSession(ctx, sess); err != nil {
return err
}
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Path: "/",
Expires: sess.ExpiresAt,
Value: sess.ID.String(),
})
return nil
}
var errUnauthorized = errors.New("unauthorized")
func authorize(s *store, w http.ResponseWriter, r *http.Request, accountID string) (user, error) {
sessionCookie, err := r.Cookie("session_token")
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return user{}, errUnauthorized
}
sessionID, err := uuid.Parse(sessionCookie.Value)
if err != nil {
return user{}, err
}
sess, err := s.getSession(r.Context(), sessionID)
if err != nil {
return user{}, err
}
u, err := s.getUser(r.Context(), sess.UserID)
if err != nil {
return user{}, err
}
if u.AccountID.String() != accountID {
w.WriteHeader(http.StatusForbidden)
return user{}, errUnauthorized
}
return u, nil
}
func main() {
db, err := sqlx.Open("postgres", "postgres://postgres:password@localhost?sslmode=disable")
if err != nil {
panic(err)
}
store := store{DB: db}
router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("content-type", "text/html")
indexTemplate.Execute(w, nil)
})
router.POST("/accounts", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
displayName := r.FormValue("root_display_name")
password := r.FormValue("root_password")
a := account{ID: uuid.New()}
if err := store.createAccount(r.Context(), a); err != nil {
return err
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
u := user{ID: uuid.New(), AccountID: a.ID, DisplayName: displayName, PasswordHash: passwordHash}
if err := store.createUser(r.Context(), u); err != nil {
return err
}
if err := issueSession(r.Context(), &store, w, u); err != nil {
return err
}
http.Redirect(w, r, fmt.Sprintf("/accounts/%s", a.ID.String()), http.StatusFound)
return nil
}))
router.POST("/login", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
userID := r.FormValue("id")
password := r.FormValue("password")
userUUID, err := uuid.Parse(userID)
if err != nil {
return err
}
user, err := store.getUser(r.Context(), userUUID)
if err != nil {
return err
}
if err := bcrypt.CompareHashAndPassword(user.PasswordHash, []byte(password)); err != nil {
return err
}
if err := issueSession(r.Context(), &store, w, user); err != nil {
return err
}
http.Redirect(w, r, fmt.Sprintf("/accounts/%s", user.AccountID.String()), http.StatusFound)
return nil
}))
router.GET("/accounts/:account_id", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
accountID := p.ByName("account_id")
currentUser, err := authorize(&store, w, r, accountID)
if err != nil {
return nil
}
accountUUID, err := uuid.Parse(accountID)
if err != nil {
return err
}
account, err := store.getAccount(r.Context(), accountUUID)
if err != nil {
return err
}
issuer := ""
if account.SAMLIssuer != nil {
issuer = *account.SAMLIssuer
}
issuerX509 := ""
if len(account.SAMLX509) != 0 {
issuerX509 = string(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: account.SAMLX509,
}))
}
redirectURL := ""
if account.SAMLRedirectURL != nil {
redirectURL = *account.SAMLRedirectURL
}
todos, err := store.listTodos(r.Context(), accountUUID)
if err != nil {
return err
}
users, err := store.listUsers(r.Context(), accountUUID)
if err != nil {
return err
}
todosWithAuthors := []todoWithAuthor{}
for _, todo := range todos {
for _, user := range users {
if user.ID == todo.AuthorID {
todosWithAuthors = append(todosWithAuthors, todoWithAuthor{Todo: todo, User: user})
}
}
}
w.Header().Add("content-type", "text/html")
getAccountTemplate.Execute(w, getAccountData{
ID: accountID,
CurrentUser: currentUser,
SAMLACS: fmt.Sprintf("http://localhost:8080/accounts/%s/saml/acs", accountID),
SAMLRecipientID: fmt.Sprintf("http://localhost:8080/accounts/%s/saml", accountID),
SAMLIssuerID: issuer,
SAMLIssuerX509: issuerX509,
SAMLRedirectURL: redirectURL,
Users: users,
Todos: todosWithAuthors,
})
return nil
}))
router.POST("/accounts/:account_id/metadata", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
accountID := p.ByName("account_id")
if _, err := authorize(&store, w, r, accountID); err != nil {
return nil
}
accountUUID, err := uuid.Parse(accountID)
if err != nil {
return err
}
file, _, err := r.FormFile("metadata")
if err != nil {
return err
}
defer file.Close()
var metadata saml.EntityDescriptor
if err := xml.NewDecoder(file).Decode(&metadata); err != nil {
return err
}
entityID, cert, redirectURL, err := metadata.GetEntityIDCertificateAndRedirectURL()
if err != nil {
return err
}
samlRedirectURL := redirectURL.String()
store.updateAccount(r.Context(), account{
ID: accountUUID,
SAMLIssuer: &entityID,
SAMLX509: cert.Raw,
SAMLRedirectURL: &samlRedirectURL,
})
http.Redirect(w, r, fmt.Sprintf("/accounts/%s", accountID), http.StatusFound)
return nil
}))
router.GET("/accounts/:account_id/saml/initiate", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
// This endpoint is intentionally not checking for authentication /
// authorization. Think of this endpoint as a customizable login page, where
// we redirect the user to a SAML identity provider of the account's
// choosing.
accountID := p.ByName("account_id")
accountUUID, err := uuid.Parse(accountID)
if err != nil {
return err
}
account, err := store.getAccount(r.Context(), accountUUID)
if err != nil {
return err
}
http.Redirect(w, r, *account.SAMLRedirectURL, http.StatusFound)
return nil
}))
router.POST("/accounts/:account_id/saml/acs", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
// This is the endpoint that users get redirected to from /saml/initiate
// above, or when log into our app directly from their Identity Provider.
accountID := p.ByName("account_id")
accountUUID, err := uuid.Parse(accountID)
if err != nil {
return err
}
account, err := store.getAccount(r.Context(), accountUUID)
if err != nil {
return err
}
cert, err := x509.ParseCertificate(account.SAMLX509)
if err != nil {
return err
}
// This is the destination ID we expect to see in the SAML assertion. We
// verify this to make sure that this SAML assertion is meant for us, and
// not some other SAML application in the identity provider.
expectedDestinationID := fmt.Sprintf("http://localhost:8080/accounts/%s/saml", accountID)
// Get the raw SAML response, and verify it.
rawSAMLResponse := r.FormValue(saml.ParamSAMLResponse)
samlResponse, err := saml.Verify(rawSAMLResponse, *account.SAMLIssuer, cert, expectedDestinationID, time.Now())
if err != nil {
return err
}
// samlUserID will contain the user ID from the identity provider.
//
// If a user with that saml_id already exists in our database, we'll log the
// user in as them. If no such user already exists, we'll create one first.
samlUserID := samlResponse.Assertion.Subject.NameID.Value
existingUser, err := store.getUserBySAMLID(r.Context(), accountUUID, samlUserID)
// loginUser will contain the user we should create a session for.
var loginUser user
if err == nil {
// A user with the given saml_id in this account already exists. Log into
// that user.
loginUser = existingUser
} else if err == sql.ErrNoRows {
// No such user already exists. Create one now.
//
// This practice of creating a user like this is often called
// "just-in-time" provisioning.
provisionedUser := user{
AccountID: accountUUID,
ID: uuid.New(),
SAMLID: &samlUserID,
DisplayName: samlUserID,
}
if err := store.createUser(r.Context(), provisionedUser); err != nil {
return err
}
loginUser = provisionedUser
} else {
return err
}
if err := issueSession(r.Context(), &store, w, loginUser); err != nil {
return err
}
http.Redirect(w, r, fmt.Sprintf("/accounts/%s", accountID), http.StatusFound)
return nil
}))
router.POST("/accounts/:account_id/users", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
accountID := p.ByName("account_id")
if _, err := authorize(&store, w, r, accountID); err != nil {
return err
}
accountUUID, err := uuid.Parse(accountID)
if err != nil {
return err
}
displayName := r.FormValue("display_name")
password := r.FormValue("password")
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
if err := store.createUser(r.Context(), user{
AccountID: accountUUID,
ID: uuid.New(),
DisplayName: displayName,
PasswordHash: passwordHash,
}); err != nil {
return err
}
http.Redirect(w, r, fmt.Sprintf("/accounts/%s", accountID), http.StatusFound)
return nil
}))
router.POST("/accounts/:account_id/todos", with500(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
accountID := p.ByName("account_id")
author, err := authorize(&store, w, r, accountID)
if err != nil {
return err
}
body := r.FormValue("body")
if err := store.createTodo(r.Context(), todo{
ID: uuid.New(),
AuthorID: author.ID,
Body: body,
}); err != nil {
return err
}
http.Redirect(w, r, fmt.Sprintf("/accounts/%s", accountID), http.StatusFound)
return nil
}))
if err := http.ListenAndServe("localhost:8080", router); err != nil {
panic(err)
}
}