Last active
August 29, 2015 14:07
-
-
Save cynddl/600ad9d0cdd062ed82e1 to your computer and use it in GitHub Desktop.
Testing a simple CAS authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"net/url" | |
"io/ioutil" | |
"log" | |
"strings" | |
"github.com/Unknwon/macaron" | |
"github.com/macaron-contrib/session" | |
) | |
func main() { | |
server_root := "https://cas.ens-lyon.fr/cas" | |
local_login_url := "http://localhost:4000/login" | |
m := macaron.Classic() | |
m.Use(macaron.Renderer()) | |
m.Use(session.Sessioner()) | |
m.Get("/login", func(ctx *macaron.Context, s session.Store) { | |
ticket := ctx.Query("ticket") | |
if len(ticket) == 0 { | |
ctx.Redirect(server_root + "/login?service=" + local_login_url) | |
return | |
} else { | |
s.Set("ticket", ticket) | |
ctx.Redirect("/") | |
} | |
}) | |
m.Get("/", func(ctx *macaron.Context, s session.Store) string { | |
if s.Get("login") != nil { | |
return "Welcome, " + s.Get("login").(string) | |
} | |
// Retrieve the ticket | |
if s.Get("ticket") == nil { | |
ctx.Redirect("/login") | |
return "" | |
} | |
// Validate the ticket | |
ticket := s.Get("ticket").(string) | |
resp, err := http.Get(server_root + "/validate?ticket=" + ticket + "&service=" + local_login_url) | |
if err != nil { | |
log.Fatalf("ERROR: %s\n", err) | |
return "Unable to validate the ticket" | |
} | |
bs, _ := ioutil.ReadAll(resp.Body) | |
split := strings.Split(string(bs), "\n") | |
ticketValid, login := split[0] == "yes", split[1] | |
if ticketValid { | |
s.Set("login", login) | |
ctx.Redirect("/") | |
return "" | |
} | |
return "Invalid login" | |
}) | |
m.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment