// Helper function to save a redirect to the page a user tried to visit before
// logging in.
972547
func (a Authorizer) goBack(rw http.ResponseWriter, req *http.Request) {
	redirectSession, _ := a.cookiejar.Get(req, "redirects")
	defer redirectSession.Save(req, rw)
	redirectSession.Flashes()
	redirectSession.AddFlash(req.URL.Path)
}
501547
func (c *ctx) Redirect(urlStr string) error {
	if c.code < 300 || c.code > 308 {
		return ErrInvalidRedirectStatusCode
	}

	http.Redirect(c, c.Request(), urlStr, c.code)
	return nil
}
145581
func (c *Client) Login(ctx context.Context, u *url.Userinfo) error {
	return c.SessionManager.Login(ctx, u)
}
17686
func (c *Client) Login(ctx context.Context, user *url.Userinfo) error {
	req := internal.URL(c, internal.SessionPath).Request(http.MethodPost)

	if user != nil {
		if password, ok := user.Password(); ok {
			req.SetBasicAuth(user.Username(), password)
		}
	}

	return c.Do(ctx, req, nil)
}
884040
func login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	username := r.FormValue("username")
	password := r.FormValue("password")
	linkingToken := r.FormValue("account_linking_token")
	rawRedirect := r.FormValue("redirect_uri")

	if !checkCredentials(username, password) {
		fmt.Fprint(w, templateLogin(loginPath, linkingToken, rawRedirect, true))
		return
	}

	redirectURL, err := url.Parse(rawRedirect)
	if err != nil {
		log.Println("failed to parse url:", err)
		return
	}

	q := redirectURL.Query()
	q.Set("authorization_code", "something")
	redirectURL.RawQuery = q.Encode()

	w.Header().Set("Location", redirectURL.String())
	w.WriteHeader(http.StatusFound)
}
186475
func (m UsersAPIAuthMethod) LoginURL(c context.Context, dest string) (string, error) {
	url, err := user.LoginURL(c, dest)
	if err != nil {
		return "", err
	}
	if !strings.HasPrefix(url, serviceLoginURL) {
		if !info.IsDevAppServer(c) {
			logging.Warningf(c, "Unexpected login URL: %q", url)
		}
		return url, nil
	}
	// Give the user a choice of existing accounts in their session or the option
	// to add an account, even if they are currently signed in to exactly one
	// account.
	return accountChooserURL + url[len(serviceLoginURL):], nil
}
949966
func (m *AuthMethod) LogoutURL(c context.Context, dest string) (string, error) {
	if m.SessionStore == nil {
		return "", ErrNotConfigured
	}
	return makeRedirectURL(logoutURL, dest)
}
59417
func (self *OpenIdProvider) Redirect(w http.ResponseWriter, r *http.Request) {

	// create the url params
	var params = make(url.Values)

	// construct the Redirect URL with default OpenId params
	for key, val := range openIdParams {
		params.Add(key, val)
	}

	// append the real and return_to parameters
	// they will be defaulted to the current Host / Path
	// TODO use url.New().String() instead string joins below
	params.Add("openid.realm", "http://"+r.Host)
	params.Add("openid.return_to", "http://"+r.Host+r.URL.Path)

	// create the redirect url
	redirectTo, _ := url.Parse(self.endpoint)
	redirectTo.RawQuery = params.Encode()

	// redirect to login
	http.Redirect(w, r, redirectTo.String(), http.StatusSeeOther)
}
1080806
func RedirectToHandler(w http.ResponseWriter, r *http.Request) {
	u := mux.Vars(r)["url"]
	w.Header().Set("Location", u)
	w.WriteHeader(http.StatusFound)
}
1340441
func PreventRedirects(_ *http.Request, _ []*http.Request) error {
	return errors.New("redirects forbidden")
}
88735
func (p *ProviderData) GetLoginURL(redirectURI, state string) string {
	var a url.URL
	a = *p.LoginURL
	params, _ := url.ParseQuery(a.RawQuery)
	params.Set("redirect_uri", redirectURI)
	params.Set("approval_prompt", p.ApprovalPrompt)
	params.Add("scope", p.Scope)
	params.Set("client_id", p.ClientID)
	params.Set("response_type", "code")
	params.Add("state", state)
	a.RawQuery = params.Encode()
	return a.String()
}
852532
func RedirectHandler(config oauth2.Config, successfulAuthRedirectURL, userInfoURL string) gin.HandlerFunc {
	return func(c *gin.Context) {
		session := sessions.Default(c)
		authError := c.Query("error")
		if authError != "" {
			session.Delete("state")
			err := errors.Unauthorizedf("OP was unable to successfully authenticate your request: %s", authError)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		serverState := c.Query("state")
		localState := session.Get("state")
		if localState == nil {
			err := errors.NotValidf("Couldn't find the local state or nonce to verify the response from the OP")
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		if localState.(string) != serverState {
			c.String(http.StatusForbidden, "State did not match")
			c.Abort()
			return
		}

		code := c.Query("code")
		token, err := config.Exchange(oauth2.NoContext, code)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		session.Set("token", token)
		client := config.Client(oauth2.NoContext, token)
		resp, err := client.Get(userInfoURL)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, errors.Annotate(err, "Couldn't connect to the user info endpoint"))
			return
		}
		defer resp.Body.Close()
		decoder := json.NewDecoder(resp.Body)
		userInfo := &heart.UserInfo{}
		err = decoder.Decode(userInfo)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, errors.Annotate(err, "Couldn't decode the token response"))
			return
		}
		session.Set("UserInfo", userInfo)
		session.Delete("state")
		err = session.Save()
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		c.Redirect(http.StatusFound, successfulAuthRedirectURL)
	}
}
130451
func (c *LoginController) Callback(ctx *app.CallbackLoginContext) error {
	state := ctx.Params.Get("state")
	code := ctx.Params.Get("code")
	redirectURL := rest.AbsoluteURL(ctx.RequestData, client.CallbackLoginPath(), nil)
	redirectTo, err := c.app.AuthenticationProviderService().LoginCallback(ctx, state, code, redirectURL)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, err)
	}
	ctx.ResponseData.Header().Set("Location", *redirectTo)
	return ctx.TemporaryRedirect()
}
302373
func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
	code := httpCode(handler, method, url, values)
	if code == -1 {
		return false
	}
	return code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
}
108109
func (ctxt *Context) Redirect(url string) {
	http.Redirect(ctxt, ctxt.Request, url, http.StatusFound)
}
1085193
func (s *Session) Save(w http.ResponseWriter) error {
	return s.store.Save(w, s)
}
731469
func (r *Response) Redirect(status int, url_ string) {
	r.ResponseWriter.Header().Set("Location", url_)
	r.Abort(status, "Redirecting to: "+url_)
}