-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
201 lines (167 loc) · 4.91 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
package main
import (
"context"
"crypto/x509"
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
"github.com/ucarion/saml"
)
type connection struct {
ID uuid.UUID `json:"id" db:"id"`
Issuer string `json:"issuer" db:"issuer"`
X509 []byte `json:"x509" db:"x509"`
RedirectURL string `json:"redirect_url" db:"redirect_url"`
}
type store struct {
DB *sqlx.DB
}
func (s *store) getConnection(ctx context.Context, id uuid.UUID) (connection, error) {
var c connection
err := s.DB.GetContext(ctx, &c, `select * from connections where id = $1`, id)
return c, err
}
func (s *store) createConnection(ctx context.Context, c connection) error {
_, err := s.DB.ExecContext(ctx, `insert into connections (id) values ($1)`, c.ID)
return err
}
func (s *store) updateConnection(ctx context.Context, c connection) error {
_, err := s.DB.ExecContext(ctx, `
update
connections
set
issuer = $1, x509 = $2, redirect_url = $3
where
id = $4
`, c.Issuer, c.X509, c.RedirectURL, c.ID)
return err
}
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("/connections/:id", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := uuid.Parse(p.ByName("id"))
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
c, err := store.getConnection(r.Context(), id)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
json.NewEncoder(w).Encode(c)
})
router.POST("/connections", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
c := connection{ID: uuid.New()}
err := store.createConnection(r.Context(), c)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
json.NewEncoder(w).Encode(c)
})
router.PATCH("/connections/:id/metadata", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()
id, err := uuid.Parse(p.ByName("id"))
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
var metadata saml.EntityDescriptor
if err := xml.NewDecoder(r.Body).Decode(&metadata); err != nil {
fmt.Fprintf(w, err.Error())
return
}
issuer, cert, redirectURL, err := metadata.GetEntityIDCertificateAndRedirectURL()
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
c := connection{ID: id, Issuer: issuer, X509: cert.Raw, RedirectURL: redirectURL.String()}
err = store.updateConnection(r.Context(), c)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
json.NewEncoder(w).Encode(c)
})
router.GET("/connections/:id/initiate", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := uuid.Parse(p.ByName("id"))
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
c, err := store.getConnection(r.Context(), id)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
redirectURL, err := url.Parse(c.RedirectURL)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
relayState := r.URL.Query().Get("relay_state")
query := redirectURL.Query()
query.Set(saml.ParamRelayState, relayState)
redirectURL.RawQuery = query.Encode()
http.Redirect(w, r, redirectURL.String(), http.StatusFound)
})
router.POST("/connections/:id/acs", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := uuid.Parse(p.ByName("id"))
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
c, err := store.getConnection(r.Context(), id)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, err.Error())
return
}
cert, err := x509.ParseCertificate(c.X509)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
rawSAMLResponse := r.FormValue(saml.ParamSAMLResponse)
samlResponse, err := saml.Verify(rawSAMLResponse, c.Issuer, cert, fmt.Sprintf("http://localhost:8080/connections/%s", id), time.Now())
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
relayState := r.FormValue(saml.ParamRelayState)
// In real life, at this point you would need to integrate samlResponse into
// your existing authentication / authorization system. There's no
// one-size-fits-all way of doing that, you'll need to do some critical
// thinking.
//
// See the README of github.com/ucarion/saml for some discussion around what
// SAML does and does not guarantee you, and for some suggestions around how
// to integrate SAML into your existing model.
//
// For now, just as a demo, let's write the saml response back out as JSON.
json.NewEncoder(w).Encode(map[string]interface{}{
"saml_response": samlResponse,
"relay_state": relayState,
})
})
if err := http.ListenAndServe("localhost:8080", router); err != nil {
panic(err)
}
}