/
ssoadmin.go
418 lines (339 loc) · 12 KB
/
ssoadmin.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
package api
import (
"context"
"io"
"net/http"
"net/url"
"strings"
"unicode/utf8"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"github.com/go-chi/chi"
"github.com/gofrs/uuid"
"github.com/supabase/auth/internal/models"
"github.com/supabase/auth/internal/observability"
"github.com/supabase/auth/internal/storage"
"github.com/supabase/auth/internal/utilities"
)
// loadSSOProvider looks for an idp_id parameter in the URL route and loads the SSO provider
// with that ID (or resource ID) and adds it to the context.
func (a *API) loadSSOProvider(w http.ResponseWriter, r *http.Request) (context.Context, error) {
ctx := r.Context()
db := a.db.WithContext(ctx)
idpParam := chi.URLParam(r, "idp_id")
idpID, err := uuid.FromString(idpParam)
if err != nil {
// idpParam is not UUIDv4
return nil, notFoundError(ErrorCodeSSOProviderNotFound, "SSO Identity Provider not found")
}
// idpParam is a UUIDv4
provider, err := models.FindSSOProviderByID(db, idpID)
if err != nil {
if models.IsNotFoundError(err) {
return nil, notFoundError(ErrorCodeSSOProviderNotFound, "SSO Identity Provider not found")
} else {
return nil, internalServerError("Database error finding SSO Identity Provider").WithInternalError(err)
}
}
observability.LogEntrySetField(r, "sso_provider_id", provider.ID.String())
return withSSOProvider(r.Context(), provider), nil
}
// adminSSOProvidersList lists all SAML SSO Identity Providers in the system. Does
// not deal with pagination at this time.
func (a *API) adminSSOProvidersList(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
db := a.db.WithContext(ctx)
providers, err := models.FindAllSAMLProviders(db)
if err != nil {
return err
}
for i := range providers {
// remove metadata XML so that the returned JSON is not ginormous
providers[i].SAMLProvider.MetadataXML = ""
}
return sendJSON(w, http.StatusOK, map[string]interface{}{
"items": providers,
})
}
type CreateSSOProviderParams struct {
Type string `json:"type"`
MetadataURL string `json:"metadata_url"`
MetadataXML string `json:"metadata_xml"`
Domains []string `json:"domains"`
AttributeMapping models.SAMLAttributeMapping `json:"attribute_mapping"`
NameIDFormat string `json:"name_id_format"`
}
func (p *CreateSSOProviderParams) validate(forUpdate bool) error {
if !forUpdate && p.Type != "saml" {
return badRequestError(ErrorCodeValidationFailed, "Only 'saml' supported for SSO provider type")
} else if p.MetadataURL != "" && p.MetadataXML != "" {
return badRequestError(ErrorCodeValidationFailed, "Only one of metadata_xml or metadata_url needs to be set")
} else if !forUpdate && p.MetadataURL == "" && p.MetadataXML == "" {
return badRequestError(ErrorCodeValidationFailed, "Either metadata_xml or metadata_url must be set")
} else if p.MetadataURL != "" {
metadataURL, err := url.ParseRequestURI(p.MetadataURL)
if err != nil {
return badRequestError(ErrorCodeValidationFailed, "metadata_url is not a valid URL")
}
if metadataURL.Scheme != "https" {
return badRequestError(ErrorCodeValidationFailed, "metadata_url is not a HTTPS URL")
}
}
switch p.NameIDFormat {
case "",
string(saml.PersistentNameIDFormat),
string(saml.EmailAddressNameIDFormat),
string(saml.TransientNameIDFormat),
string(saml.UnspecifiedNameIDFormat):
// it's valid
default:
return badRequestError(ErrorCodeValidationFailed, "name_id_format must be unspecified or one of %v", strings.Join([]string{
string(saml.PersistentNameIDFormat),
string(saml.EmailAddressNameIDFormat),
string(saml.TransientNameIDFormat),
string(saml.UnspecifiedNameIDFormat),
}, ", "))
}
return nil
}
func (p *CreateSSOProviderParams) metadata(ctx context.Context) ([]byte, *saml.EntityDescriptor, error) {
var rawMetadata []byte
var err error
if p.MetadataXML != "" {
rawMetadata = []byte(p.MetadataXML)
} else if p.MetadataURL != "" {
rawMetadata, err = fetchSAMLMetadata(ctx, p.MetadataURL)
if err != nil {
return nil, nil, err
}
} else {
// impossible situation if you called validate() prior
return nil, nil, nil
}
metadata, err := parseSAMLMetadata(rawMetadata)
if err != nil {
return nil, nil, err
}
return rawMetadata, metadata, nil
}
func parseSAMLMetadata(rawMetadata []byte) (*saml.EntityDescriptor, error) {
if !utf8.Valid(rawMetadata) {
return nil, badRequestError(ErrorCodeValidationFailed, "SAML Metadata XML contains invalid UTF-8 characters, which are not supported at this time")
}
metadata, err := samlsp.ParseMetadata(rawMetadata)
if err != nil {
return nil, err
}
if metadata.EntityID == "" {
return nil, badRequestError(ErrorCodeValidationFailed, "SAML Metadata does not contain an EntityID")
}
if len(metadata.IDPSSODescriptors) < 1 {
return nil, badRequestError(ErrorCodeValidationFailed, "SAML Metadata does not contain any IDPSSODescriptor")
}
if len(metadata.IDPSSODescriptors) > 1 {
return nil, badRequestError(ErrorCodeValidationFailed, "SAML Metadata contains multiple IDPSSODescriptors")
}
return metadata, nil
}
func fetchSAMLMetadata(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, internalServerError("Unable to create a request to metadata_url").WithInternalError(err)
}
req = req.WithContext(ctx)
req.Header.Set("Accept", "application/xml;charset=UTF-8")
req.Header.Set("Accept-Charset", "UTF-8")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer utilities.SafeClose(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, badRequestError(ErrorCodeSAMLMetadataFetchFailed, "HTTP %v error fetching SAML Metadata from URL '%s'", resp.StatusCode, url)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
// adminSSOProvidersCreate creates a new SAML Identity Provider in the system.
func (a *API) adminSSOProvidersCreate(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
db := a.db.WithContext(ctx)
params := &CreateSSOProviderParams{}
if err := retrieveRequestParams(r, params); err != nil {
return err
}
if err := params.validate(false /* <- forUpdate */); err != nil {
return err
}
rawMetadata, metadata, err := params.metadata(ctx)
if err != nil {
return err
}
existingProvider, err := models.FindSAMLProviderByEntityID(db, metadata.EntityID)
if err != nil && !models.IsNotFoundError(err) {
return err
}
if existingProvider != nil {
return unprocessableEntityError(ErrorCodeSAMLIdPAlreadyExists, "SAML Identity Provider with this EntityID (%s) already exists", metadata.EntityID)
}
provider := &models.SSOProvider{
// TODO handle Name, Description, Attribute Mapping
SAMLProvider: models.SAMLProvider{
EntityID: metadata.EntityID,
MetadataXML: string(rawMetadata),
},
}
if params.MetadataURL != "" {
provider.SAMLProvider.MetadataURL = ¶ms.MetadataURL
}
if params.NameIDFormat != "" {
provider.SAMLProvider.NameIDFormat = ¶ms.NameIDFormat
}
provider.SAMLProvider.AttributeMapping = params.AttributeMapping
for _, domain := range params.Domains {
existingProvider, err := models.FindSSOProviderByDomain(db, domain)
if err != nil && !models.IsNotFoundError(err) {
return err
}
if existingProvider != nil {
return badRequestError(ErrorCodeSSODomainAlreadyExists, "SSO Domain '%s' is already assigned to an SSO identity provider (%s)", domain, existingProvider.ID.String())
}
provider.SSODomains = append(provider.SSODomains, models.SSODomain{
Domain: domain,
})
}
if err := db.Transaction(func(tx *storage.Connection) error {
if terr := tx.Eager().Create(provider); terr != nil {
return terr
}
return tx.Eager().Load(provider)
}); err != nil {
return err
}
return sendJSON(w, http.StatusCreated, provider)
}
// adminSSOProvidersGet returns an existing SAML Identity Provider in the system.
func (a *API) adminSSOProvidersGet(w http.ResponseWriter, r *http.Request) error {
provider := getSSOProvider(r.Context())
return sendJSON(w, http.StatusOK, provider)
}
// adminSSOProvidersUpdate updates a provider with the provided diff values.
func (a *API) adminSSOProvidersUpdate(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
db := a.db.WithContext(ctx)
params := &CreateSSOProviderParams{}
if err := retrieveRequestParams(r, params); err != nil {
return err
}
if err := params.validate(true /* <- forUpdate */); err != nil {
return err
}
modified := false
updateSAMLProvider := false
provider := getSSOProvider(ctx)
if params.MetadataXML != "" || params.MetadataURL != "" {
// metadata is being updated
rawMetadata, metadata, err := params.metadata(ctx)
if err != nil {
return err
}
if provider.SAMLProvider.EntityID != metadata.EntityID {
return badRequestError(ErrorCodeSAMLEntityIDMismatch, "SAML Metadata can be updated only if the EntityID matches for the provider; expected '%s' but got '%s'", provider.SAMLProvider.EntityID, metadata.EntityID)
}
if params.MetadataURL != "" {
provider.SAMLProvider.MetadataURL = ¶ms.MetadataURL
}
provider.SAMLProvider.MetadataXML = string(rawMetadata)
updateSAMLProvider = true
modified = true
}
// domains are being "updated" only when params.Domains is not nil, if
// it was nil (but not `[]`) then the caller is expecting not to modify
// the domains
updateDomains := params.Domains != nil
var createDomains, deleteDomains []models.SSODomain
keepDomains := make(map[string]bool)
for _, domain := range params.Domains {
existingProvider, err := models.FindSSOProviderByDomain(db, domain)
if err != nil && !models.IsNotFoundError(err) {
return err
}
if existingProvider != nil {
if existingProvider.ID == provider.ID {
keepDomains[domain] = true
} else {
return badRequestError(ErrorCodeSSODomainAlreadyExists, "SSO domain '%s' already assigned to another provider (%s)", domain, existingProvider.ID.String())
}
} else {
modified = true
createDomains = append(createDomains, models.SSODomain{
Domain: domain,
SSOProviderID: provider.ID,
})
}
}
if updateDomains {
for i, domain := range provider.SSODomains {
if !keepDomains[domain.Domain] {
modified = true
deleteDomains = append(deleteDomains, provider.SSODomains[i])
}
}
}
updateAttributeMapping := !provider.SAMLProvider.AttributeMapping.Equal(¶ms.AttributeMapping)
if updateAttributeMapping {
modified = true
provider.SAMLProvider.AttributeMapping = params.AttributeMapping
}
nameIDFormat := ""
if provider.SAMLProvider.NameIDFormat != nil {
nameIDFormat = *provider.SAMLProvider.NameIDFormat
}
if params.NameIDFormat != nameIDFormat {
modified = true
if params.NameIDFormat == "" {
provider.SAMLProvider.NameIDFormat = nil
} else {
provider.SAMLProvider.NameIDFormat = ¶ms.NameIDFormat
}
}
if modified {
if err := db.Transaction(func(tx *storage.Connection) error {
if terr := tx.Eager().Update(provider); terr != nil {
return terr
}
if updateDomains {
if terr := tx.Destroy(deleteDomains); terr != nil {
return terr
}
if terr := tx.Eager().Create(createDomains); terr != nil {
return terr
}
}
if updateAttributeMapping || updateSAMLProvider {
if terr := tx.Eager().Update(&provider.SAMLProvider); terr != nil {
return terr
}
}
return tx.Eager().Load(provider)
}); err != nil {
return unprocessableEntityError(ErrorCodeConflict, "Updating SSO provider failed, likely due to a conflict. Try again?").WithInternalError(err)
}
}
return sendJSON(w, http.StatusOK, provider)
}
// adminSSOProvidersDelete deletes a SAML identity provider.
func (a *API) adminSSOProvidersDelete(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
db := a.db.WithContext(ctx)
provider := getSSOProvider(ctx)
if err := db.Transaction(func(tx *storage.Connection) error {
return tx.Eager().Destroy(provider)
}); err != nil {
return err
}
return sendJSON(w, http.StatusOK, provider)
}