MCPcopy Create free account
hub / github.com/crewjam/saml / CreateSessionFromAssertion

Method CreateSessionFromAssertion

samlsp/middleware.go:355–412  ·  view source on GitHub ↗

CreateSessionFromAssertion is invoked by ServeHTTP when we have a new, valid SAML assertion.

(w http.ResponseWriter, r *http.Request, assertion *saml.Assertion, redirectURI string)

Source from the content-addressed store, hash-verified

353
354// CreateSessionFromAssertion is invoked by ServeHTTP when we have a new, valid SAML assertion.
355func (m *Middleware) CreateSessionFromAssertion(w http.ResponseWriter, r *http.Request, assertion *saml.Assertion, redirectURI string) {
356 var replay *SavedForm
357 if trackedRequestIndex := r.Form.Get("RelayState"); trackedRequestIndex != "" {
358 trackedRequest, err := m.RequestTracker.GetTrackedRequest(r, trackedRequestIndex)
359 if err != nil {
360 if errors.Is(err, http.ErrNoCookie) && m.ServiceProvider.AllowIDPInitiated {
361 // The RelayState of an unsolicited response comes from the
362 // IdP, or from whoever posted the form, so only follow it
363 // to a path on this site.
364 if uri := r.Form.Get("RelayState"); isLocalRedirect(uri) {
365 redirectURI = uri
366 }
367 } else {
368 m.OnError(w, r, err)
369 return
370 }
371 } else {
372 if err := m.RequestTracker.StopTrackingRequest(w, r, trackedRequestIndex); err != nil {
373 m.OnError(w, r, err)
374 return
375 }
376
377 redirectURI = trackedRequest.URI
378
379 if m.FormReplayStore != nil {
380 saved, ok, err := m.FormReplayStore.TakeForm(r, trackedRequestIndex)
381 if err != nil {
382 m.OnError(w, r, err)
383 return
384 }
385 // Replay the form only to the URI of the tracked request,
386 // whose cookie this browser holds, and only on this site.
387 if ok && saved.URI == trackedRequest.URI && isLocalRedirect(saved.URI) {
388 replay = saved
389 }
390 }
391 }
392 }
393
394 if err := m.Session.CreateSession(w, r, assertion); err != nil {
395 m.OnError(w, r, err)
396 return
397 }
398
399 if replay != nil {
400 form, err := replayForm(replay.URI, replay.Form, m.scriptNonce(r))
401 if err != nil {
402 m.OnError(w, r, err)
403 return
404 }
405 // The page holds the user's form data.
406 w.Header().Set("Cache-Control", "no-store")
407 m.writePostForm(w, replayScriptHash, form)
408 return
409 }
410
411 http.Redirect(w, r, redirectURI, http.StatusFound) //nolint:gosec // redirectURI is the tracked request URI, a local RelayState path, or the caller's default
412}

Calls 11

scriptNonceMethod · 0.95
writePostFormMethod · 0.95
isLocalRedirectFunction · 0.85
replayFormFunction · 0.85
GetMethod · 0.65
GetTrackedRequestMethod · 0.65
StopTrackingRequestMethod · 0.65
TakeFormMethod · 0.65
CreateSessionMethod · 0.65
HeaderMethod · 0.45
RedirectMethod · 0.45