Skip to content

Commit

Permalink
fix #6 log error when URL is not set and render index
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed Oct 2, 2018
1 parent f41664e commit ee0c30d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
42 changes: 24 additions & 18 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,10 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
session.Save(r, w)
sessstore.MaxAge(300)

var redirectURL = r.URL.Query().Get("url")
if redirectURL != "" {
http.Redirect(w, r, redirectURL, 302);

var requestedURL = r.URL.Query().Get("url")
if requestedURL != "" {
http.Redirect(w, r, requestedURL, 302);
} else {
renderIndex(w, "you have been logged out")
}
Expand All @@ -297,36 +298,41 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
log.Error(err)
}

// set the state varialbe in the session
// set the state variable in the session
var state = randString()
session.Values["state"] = state
log.Debugf("session state set to %s", session.Values["state"])

// increment the failure counter for this domain

// redirectURL comes from nginx in the query string
var redirectURL = r.URL.Query().Get("url")
if redirectURL != "" {
// TODO store the originally requested URL so we can redirec on the roundtrip
session.Values["requestedURL"] = redirectURL
// requestedURL comes from nginx in the query string via a 302 redirect
// it sets the ultimate destination
// https://lasso.yoursite.com/login?url=
var requestedURL = r.URL.Query().Get("url");
if (requestedURL == "") {
renderIndex(w, "no destination URL requested")
log.Error("no destination URL requested")
return
} else {
session.Values["requestedURL"] = requestedURL
log.Debugf("session requestedURL set to %s", session.Values["requestedURL"])
}

// stop them after three failures for this URL
var failcount = 0
if session.Values[redirectURL] != nil {
failcount = session.Values[redirectURL].(int)
log.Debugf("failcount for %s is %d", redirectURL, failcount)
if session.Values[requestedURL] != nil {
failcount = session.Values[requestedURL].(int)
log.Debugf("failcount for %s is %d", requestedURL, failcount)
}
failcount++
session.Values[redirectURL] = failcount
session.Values[requestedURL] = failcount

log.Debug("saving session")
session.Save(r, w)

if failcount > 2 {
var lassoError = r.URL.Query().Get("error")
renderIndex(w, "too many redirects for "+redirectURL+" - "+lassoError)
renderIndex(w, "too many redirects for "+requestedURL+" - "+lassoError)
} else {
// bounce to oauth provider for login
var lURL = loginURL(r, state)
Expand Down Expand Up @@ -412,16 +418,16 @@ func CallbackHandler(w http.ResponseWriter, r *http.Request) {
cookie.SetCookie(w, r, tokenstring)

// get the originally requested URL so we can send them on their way
redirectURL := session.Values["requestedURL"].(string)
if redirectURL != "" {
requestedURL := session.Values["requestedURL"].(string)
if requestedURL != "" {
// clear out the session value
session.Values["requestedURL"] = ""
session.Values[redirectURL] = 0
session.Values[requestedURL] = 0
session.Save(r, w)

// and redirect
context.WithValue(r.Context(), lctx.StatusCode, 302)
http.Redirect(w, r, redirectURL, 302)
http.Redirect(w, r, requestedURL, 302)
return
}
// otherwise serve an html page
Expand Down
5 changes: 5 additions & 0 deletions templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<link rel="icon" type="image/png" href="/static/img/favicon.ico" />
<link rel="stylesheet" href="/static/css/main.css" />
<title>Lasso: {{ .Msg }}.</title>
</head>
<body>

Expand All @@ -15,5 +16,9 @@
<li><a href="{{ .TestURL }}">{{ .TestURL }}</a></li>
</ul>

For support, please contact your network administrator or whomever setup nginx to use Lasso.
<p/>
For help with <a href="https://github.com/LassoProject/lasso">Lasso</a> or to file a bug report, please see the project page at <a href="https://github.com/LassoProject/lasso">https://github.com/LassoProject/lasso</a>
<p/>
</body>
</html>

0 comments on commit ee0c30d

Please sign in to comment.