forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
90 lines (75 loc) · 1.79 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package controllers
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/revel/revel"
)
const host = "" // set this to your host
type App struct {
*revel.Controller
}
type PersonaResponse struct {
Status string `json:"status"`
Email string `json:"email"`
Audience string `json:"audience"`
Expires int64 `json:"expires"`
Issuer string `json:"issuer"`
}
type LoginResult struct {
StatusCode int
Message string
}
func (r LoginResult) Apply(req *revel.Request, resp *revel.Response) {
resp.WriteHeader(r.StatusCode, "text/html")
resp.Out.Write([]byte(r.Message))
}
func (c App) Index() revel.Result {
email := c.Session["email"]
return c.Render(email)
}
func (c App) Login(assertion string) revel.Result {
assertion = strings.TrimSpace(assertion)
if assertion == "" {
return &LoginResult{
StatusCode: http.StatusBadRequest,
Message: "Assertion required.",
}
}
values := url.Values{"assertion": {assertion}, "audience": {host}}
resp, err := http.PostForm("https://verifier.login.persona.org/verify", values)
if err != nil {
return &LoginResult{
StatusCode: http.StatusBadRequest,
Message: "Authentication failed.",
}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &LoginResult{
StatusCode: http.StatusBadRequest,
Message: "Authentication failed.",
}
}
p := &PersonaResponse{}
err = json.Unmarshal(body, p)
if err != nil {
return &LoginResult{
StatusCode: http.StatusBadRequest,
Message: "Authentication failed.",
}
}
c.Session["email"] = p.Email
fmt.Println("Login successful: ", p.Email)
return &LoginResult{
StatusCode: http.StatusOK,
Message: "Login successful.",
}
}
func (c App) Logout() revel.Result {
delete(c.Session, "email")
return c.Redirect("/")
}