forked from cmars/affinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (128 loc) · 3.5 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"labix.org/v2/mgo"
"github.com/juju/affinity"
"github.com/juju/affinity/examples"
"github.com/juju/affinity/providers/usso"
"github.com/juju/affinity/rbac"
rbac_mongo "github.com/juju/affinity/storage/mongo"
)
const dataDir = "./"
var mgoAddr *string = flag.String("mongo", "localhost:27017", "Mongo DB URL")
var mgoDbName *string = flag.String("dbname", "demo", "Mongo DB name")
var certFile *string = flag.String("cert", "cert.pem", "SSL certificate")
var keyFile *string = flag.String("key", "key.pem", "SSL private key")
type DemoHandler struct {
Store rbac.Store
Scheme affinity.HandshakeScheme
}
func die(err error) {
log.Println(err)
os.Exit(1)
}
func main() {
flag.Parse()
// affinity only redirects to https:// URLs for OpenID.
// We'll create some self-signed certs for the demo if needed.
err := examples.BuildCerts(*keyFile, *certFile, "localhost:8443")
if err != nil {
die(err)
}
session, err := mgo.Dial(*mgoAddr)
if err != nil {
die(fmt.Errorf("Failed to connect to store:%v", err))
}
rbacStore, err := rbac_mongo.NewMongoStore(session, *mgoDbName)
if err != nil {
die(fmt.Errorf("Failed to find store:%v", err))
}
sessionStore := sessions.NewCookieStore(
securecookie.GenerateRandomKey(32),
securecookie.GenerateRandomKey(32),
)
demoContext := DemoHandler{
Store: rbacStore,
Scheme: usso.NewOpenIdWeb("openid-demo", sessionStore),
}
r := mux.NewRouter()
r.Handle("/", HomeHandler{&demoContext})
r.Handle("/login", LoginHandler{&demoContext})
r.Handle("/openidcallback", CallbackHandler{&demoContext})
// Send all incoming requests to mux.DefaultRouter.
go http.ListenAndServe(":8080", RedirectToTls{})
err = http.ListenAndServeTLS(":8443", *certFile, *keyFile, r)
if err != nil {
die(err)
}
}
type RedirectToTls struct{}
func (_ RedirectToTls) ServeHTTP(w http.ResponseWriter, req *http.Request) {
host, _, err := net.SplitHostPort(req.Host)
if err != nil {
host = req.Host
}
http.Redirect(w, req, fmt.Sprintf("https://%v:8443%v", host, req.URL.Path), 301)
}
func BadRequest(w http.ResponseWriter, err error) {
log.Println(err)
}
type HomeHandler struct {
*DemoHandler
}
func (h HomeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "text/html")
p := make(map[string]string)
user, err := h.Scheme.Authenticate(req)
if err != nil {
if err != affinity.ErrUnauthorized {
log.Println("Warning: authenticate error:", err)
BadRequest(w, err)
return
}
} else {
p["user"] = user.String()
}
if t, err := template.ParseFiles(dataDir + "index.html"); err == nil {
t.Execute(w, p)
} else {
BadRequest(w, err)
}
}
type LoginHandler struct {
*DemoHandler
}
func (h LoginHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
_, err := h.Scheme.Authenticate(req)
if err != nil {
if err != affinity.ErrUnauthorized {
log.Println("Warning: authenticate error:", err)
BadRequest(w, err)
return
}
err = h.Scheme.SignIn(w, req)
if err != nil {
log.Println("Warning: Sign in error:", err)
BadRequest(w, err)
}
// SignIn will have written a redirect if successful.
return
}
// What are we doing here if we're logged in?
http.Redirect(w, req, "/", 303)
}
type CallbackHandler struct {
*DemoHandler
}
func (h CallbackHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.Scheme.Authenticated(w, req)
}