-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (141 loc) · 4.07 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
154
155
156
157
158
// Copyright (C) 2014 JT Olds
// See LICENSE for copying information
// This example shows how to set up a web service that allows users to log in
// via multiple OAuth2 providers
package main
import (
"encoding/hex"
"flag"
"fmt"
"net/http"
"net/url"
"gopkg.in/go-webhelp/whoauth2.v1"
"gopkg.in/webhelp.v1/whcompat"
"gopkg.in/webhelp.v1/wherr"
"gopkg.in/webhelp.v1/whlog"
"gopkg.in/webhelp.v1/whmux"
"gopkg.in/webhelp.v1/whredir"
"gopkg.in/webhelp.v1/whsess"
)
var (
listenAddr = flag.String("addr", ":8080", "address to listen on")
cookieSecret = flag.String("cookie_secret", "abcdef0123456789",
"the secret for securing cookie information")
githubClientId = flag.String("github_client_id", "", "")
githubClientSecret = flag.String("github_client_secret", "", "")
googleClientId = flag.String("google_client_id", "", "")
googleClientSecret = flag.String("google_client_secret", "", "")
facebookClientId = flag.String("facebook_client_id", "", "")
facebookClientSecret = flag.String("facebook_client_secret", "", "")
)
type SampleHandler struct {
Group *whoauth2.ProviderGroup
Restricted bool
}
func (s *SampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tokens, err := s.Group.Tokens(whcompat.Context(r))
if err != nil {
wherr.Handle(w, r, err)
return
}
w.Header().Set("Content-Type", "text/html")
if s.Restricted {
fmt.Fprintf(w, `<h3>Restricted</h3>`)
}
if len(tokens) > 0 {
fmt.Fprintf(w, `
<p>Logged in with:
<ul>
`)
for name := range tokens {
fmt.Fprintf(w, `
<li>%s (<a href="%s">logout</a>)</li>
`, name, s.Group.LogoutURL(name, "/"))
}
fmt.Fprintf(w, `
<li><a href="%s">logout all</a></li>
`, s.Group.LogoutAllURL("/"))
fmt.Fprintf(w, `
</ul></p>`)
} else {
fmt.Fprintf(w, `
<p>Not logged in</p>
`)
}
login_possible := false
for name := range s.Group.Providers() {
_, logged_in := tokens[name]
if !logged_in {
login_possible = true
break
}
}
if login_possible {
fmt.Fprintf(w, "<p>Log in with:<ul>")
}
for name, provider := range s.Group.Providers() {
_, logged_in := tokens[name]
if logged_in {
continue
}
fmt.Fprintf(w, `<li><a href="%s">%s</a></li>`,
provider.LoginURL(r.RequestURI, false), name)
}
fmt.Fprintf(w, "</ul></p>")
if !s.Restricted {
fmt.Fprintf(w, `
<p><a href="/restricted">Restricted</a></p>
`)
}
}
type LoginHandler struct {
Group *whoauth2.ProviderGroup
}
func (l *LoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<h3>Login required</h3>`)
fmt.Fprintf(w, "<p>Log in with:<ul>")
for name, provider := range l.Group.Providers() {
fmt.Fprintf(w, `<li><a href="%s">%s</a></li>`,
provider.LoginURL(r.FormValue("redirect_to"), false), name)
}
fmt.Fprintf(w, "</ul></p>")
}
func loginurl(redirect_to string) string {
return "/login?" + url.Values{"redirect_to": {redirect_to}}.Encode()
}
func main() {
flag.Parse()
secret, err := hex.DecodeString(*cookieSecret)
if err != nil {
panic(err)
}
store := whsess.NewCookieStore(secret)
group, err := whoauth2.NewProviderGroup(
"oauth", "/auth", whoauth2.RedirectURLs{},
whoauth2.Github(whoauth2.Config{
ClientID: *githubClientId,
ClientSecret: *githubClientSecret}),
whoauth2.Google(whoauth2.Config{
ClientID: *googleClientId,
ClientSecret: *googleClientSecret}),
whoauth2.Facebook(whoauth2.Config{
ClientID: *facebookClientId,
ClientSecret: *facebookClientSecret,
RedirectURL: "http://localhost:8080/auth/facebook/_cb"}))
if err != nil {
panic(err)
}
whlog.ListenAndServe(*listenAddr, whlog.LogRequests(whlog.Default,
whsess.HandlerWithStore(store,
whmux.Dir{
"": &SampleHandler{Group: group, Restricted: false},
"login": &LoginHandler{Group: group},
"logout": http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
whredir.Redirect(w, r, "/auth/all/logout")
}),
"restricted": group.LoginRequired(
&SampleHandler{Group: group, Restricted: true}, loginurl),
"auth": group})))
}