HandleStartAuthFlow is called to start the SAML authentication process.
(w http.ResponseWriter, r *http.Request)
| 227 | |
| 228 | // HandleStartAuthFlow is called to start the SAML authentication process. |
| 229 | func (m *Middleware) HandleStartAuthFlow(w http.ResponseWriter, r *http.Request) { |
| 230 | // If we try to redirect when the original request is the ACS URL we'll |
| 231 | // end up in a loop. This is a programming error, so we panic here. In |
| 232 | // general this means a 500 to the user, which is preferable to a |
| 233 | // redirect loop. |
| 234 | if r.URL.Path == m.ServiceProvider.AcsURL.Path { |
| 235 | panic("don't wrap Middleware with RequireAccount") |
| 236 | } |
| 237 | |
| 238 | var binding, bindingLocation string |
| 239 | if m.Binding != "" { |
| 240 | binding = m.Binding |
| 241 | bindingLocation = m.ServiceProvider.GetSSOBindingLocation(binding) |
| 242 | } else { |
| 243 | binding = saml.HTTPRedirectBinding |
| 244 | bindingLocation = m.ServiceProvider.GetSSOBindingLocation(binding) |
| 245 | if bindingLocation == "" { |
| 246 | binding = saml.HTTPPostBinding |
| 247 | bindingLocation = m.ServiceProvider.GetSSOBindingLocation(binding) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | authReq, err := m.ServiceProvider.MakeAuthenticationRequest(bindingLocation, binding, m.ResponseBinding) |
| 252 | if err != nil { |
| 253 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 254 | return |
| 255 | } |
| 256 | |
| 257 | // relayState is limited to 80 bytes but also must be integrity protected. |
| 258 | // this means that we cannot use a JWT because it is way to long. Instead |
| 259 | // we set a signed cookie that encodes the original URL which we'll check |
| 260 | // against the SAML response when we get it. |
| 261 | relayState, err := m.RequestTracker.TrackRequest(w, r, authReq.ID) |
| 262 | if err != nil { |
| 263 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 264 | return |
| 265 | } |
| 266 | |
| 267 | if m.FormReplayStore != nil && r.Method == http.MethodPost && isSameOriginRequest(r) { |
| 268 | if _, err := m.FormReplayStore.SaveForm(r, relayState); err != nil { |
| 269 | m.OnError(w, r, err) |
| 270 | return |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if binding == saml.HTTPRedirectBinding { |
| 275 | redirectURL, err := authReq.Redirect(relayState, &m.ServiceProvider) |
| 276 | if err != nil { |
| 277 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 278 | return |
| 279 | } |
| 280 | setNoCacheHeaders(w.Header()) |
| 281 | w.Header().Add("Location", redirectURL.String()) |
| 282 | w.WriteHeader(http.StatusFound) |
| 283 | return |
| 284 | } |
| 285 | if binding == saml.HTTPPostBinding { |
| 286 | m.writePostForm(w, postRequestScriptHash, authReq.PostWithNonce(relayState, m.scriptNonce(r))) |