-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
379 lines (314 loc) · 9.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/zxchris/discourse-sso-go/assets"
"github.com/zxchris/discourse-sso-go/static"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"gopkg.in/unrolled/render.v1"
"github.com/gorilla/pat"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/justinas/alice"
"github.com/justinas/nosurf"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"gopkg.in/authboss.v0"
aboauth2 "gopkg.in/authboss.v0/oauth2"
)
// globals
var ab = authboss.New()
var database = NewMemStorer()
type ProviderInfo struct {
Name string
}
type googleProfile struct {
Name string
Picture string
Link string
}
type config struct {
CookieKey string
SessionKey string
GoogleClientID string
GoogleClientSecret string
BindAddr string
HMAC256Secret []byte
AESKey []byte
}
type SSORequest struct {
Nonce string
ReturnURL string
}
var cfg config
var templates = template.Must(template.ParseFiles("assets/templates/providers.tmpl"))
type staticConfig struct {
// asset func(string) ([]byte, error)
// assetNames func() []string
}
func (s staticConfig) Asset() func(string) ([]byte, error) {
return assets.Asset
}
func (s staticConfig) AssetNames() func() []string {
return assets.AssetNames
}
var r = render.New(render.Options{
Asset: assets.Asset,
AssetNames: assets.AssetNames,
})
func main() {
cfg = config{
BindAddr: ":3100",
CookieKey: "NpEPi8pEjKVjLGJ6aYCS+VTCzi6BUuDzU0wrwXyf5uDPArtlofn2AG6aTMiPmN32909rsEWMNqJqhIVPGP3Exg==",
SessionKey: "AbfYwmmt8UCwUuad9qvfNA9UCuN1cVcKJN1ofbiky6xCyyBj20whe40rJa3Su0W1WLWcPpO1taqJdsEI/65+Jg==",
}
if v := os.Getenv("BIND_ADDR"); len(v) > 0 {
cfg.BindAddr = v
}
if v := os.Getenv("COOKIE_STORE_KEY"); len(v) > 0 {
cfg.CookieKey = v
}
if v := os.Getenv("SESSION_STORE_KEY"); len(v) > 0 {
cfg.SessionKey = v
}
if v := os.Getenv("GOOGLE_CLIENT_ID"); len(v) > 0 {
cfg.GoogleClientID = v
}
if v := os.Getenv("GOOGLE_CLIENT_SECRET"); len(v) > 0 {
cfg.GoogleClientSecret = v
}
if v := os.Getenv("HMAC_256_SECRET"); len(v) > 0 {
cfg.HMAC256Secret, _ = base64.StdEncoding.DecodeString(v)
}
if v := os.Getenv("AES_KEY"); len(v) > 0 {
cfg.AESKey, _ = base64.StdEncoding.DecodeString(v)
}
if len(cfg.AESKey) == 0 {
log.Fatal("No AES_KEY environment variable set\n")
}
cookieStoreKey, _ := base64.StdEncoding.DecodeString(cfg.CookieKey)
sessionStoreKey, _ := base64.StdEncoding.DecodeString(cfg.SessionKey)
cookieStore = securecookie.New(cookieStoreKey, nil)
sessionStore = sessions.NewCookieStore(sessionStoreKey)
// Configure Authboss
ab.RootURL = "http://localhost:3100"
ab.MountPath = "/auth"
ab.OAuth2Storer = database
ab.OAuth2Providers = map[string]authboss.OAuth2Provider{
"google": authboss.OAuth2Provider{
OAuth2Config: &oauth2.Config{
ClientID: cfg.GoogleClientID,
ClientSecret: cfg.GoogleClientSecret,
Scopes: []string{"profile", "email"},
Endpoint: google.Endpoint,
},
Callback: aboauth2.Google,
},
}
ab.XSRFName = "csrf_token"
ab.XSRFMaker = func(_ http.ResponseWriter, r *http.Request) string {
return nosurf.Token(r)
}
// Make sure login expires quickly, as we don't need or want to cache the
// login for long (user info is stored in memory and linked to by a cookie.
// If the server restarts, the cookie will be out of sync with the memory
// store (now empty) and generate "User is unknown" error).
// We're just acting as a proxy between Discourse and OAuth2, so holding onto
// the login state is not necessary.
ab.ExpireAfter = 5 // 5 Second expiry of login
ab.Mailer = authboss.LogMailer(os.Stdout)
ab.CookieStoreMaker = NewCookieStorer
ab.SessionStoreMaker = NewCookieStorer
if err := ab.Init(); err != nil {
log.Fatal(err)
}
// Routing
pat := pat.New()
pat.PathPrefix("/auth").Handler(ab.NewRouter())
pat.Path("/discourse").Methods("GET").HandlerFunc(discourseSSO)
//pat.PathPrefix("/images").Methods("GET").Handler(http.FileServer(http.Dir("/assets/images/")))
var myconf = staticConfig{}
static.Register(myconf, pat)
stack := alice.New(logger, ab.ExpireMiddleware).Then(pat)
log.Printf("Listening on %s", cfg.BindAddr)
err := http.ListenAndServe(cfg.BindAddr, stack)
if err != nil {
log.Fatal(err)
}
}
func discourseSSO(w http.ResponseWriter, req *http.Request) {
// If we get a decodable state parameter in the request, then
// we have completed the authorisation round-trip, and thus can
// inspect the user state and construct the Discourse return.
ssor := &SSORequest{}
state := req.FormValue("state")
if state != "" {
if err := DecodeState(state, ssor); err != nil {
state = ""
}
}
// Otherwise, create the encoded state and populate the OAuth2
// provider selection template.
if state == "" {
if verifyRequest(req) != true {
w.WriteHeader(400)
w.Write([]byte("Invalid request"))
return
}
ssor = decodeSSO(req)
if ssor == nil {
log.Printf("Cannot decode request")
return
}
}
//log.Println(ssor)
// If the user is not signed in, show the selection page.
// Once the authentication is complete, the process re-directs
// here with the original Discourse request encoded in an encrypted
// state parameter.
currentUserName := ""
currentEmail := ""
currentUID := ""
//currentPicture := ""
//isAdmin := false
_ = currentUserName
_ = currentEmail
_ = currentUID
userInter, err := ab.CurrentUser(w, req)
// State is set, and we have user info, so we can return login info to Discourse
if state != "" && userInter != nil && err == nil {
user := userInter.(*User)
if user.Name == "" && user.Oauth2Provider == "google" {
//log.Printf("NO USER\n")
req, err := http.NewRequest("GET", "https://www.googleapis.com/userinfo/v2/me", nil)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Failed to create profile request"))
return
}
req.Header.Set("Authorization", "Bearer "+user.Oauth2Token)
res, err := http.DefaultClient.Do(req)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Failed to read profile"))
return
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Failed to read profile"))
return
}
res.Body.Close()
var prof googleProfile
err = json.Unmarshal(b, &prof)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Failed to import profile"))
return
}
user.Name = prof.Name
user.GoogleName = prof.Name
user.GooglePicture = prof.Picture
user.GoogleLink = prof.Link
}
currentUID = userInter.(*User).Oauth2Uid + userInter.(*User).Oauth2Provider
currentUserName = userInter.(*User).Name
currentEmail = userInter.(*User).Email
//currentPicture = userInter.(*User).GooglePicture
//isAdmin = userInter.(*User).IsAdmin
u, _ := url.Parse("")
q := u.Query()
q.Set("email", currentEmail)
q.Set("external_id", currentUID)
q.Set("name", currentUserName)
q.Set("nonce", ssor.Nonce)
var payload = base64.URLEncoding.EncodeToString([]byte(q.Encode()))
//log.Printf("%s\n", payload)
u, _ = url.Parse(ssor.ReturnURL)
q = u.Query()
q.Set("sso", payload)
q.Set("sig", hex.EncodeToString(getSignature(payload)))
u.RawQuery = q.Encode()
//log.Println(u)
http.Redirect(w, req, u.String(), 302)
//w.WriteHeader(201)
//w.Write([]byte(u.String()))
} else {
// When generating the Federated Login link in the template, encode the
// Discourse SSO nonce and return URL into an encrypted state.
state, err := EncodeState(*ssor)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Cannot encode request state"))
return
}
r.HTML(w, http.StatusOK, "providers", map[string]string{
"State": state,
})
}
}
// returns ssoRequest and error state
func decodeSSO(req *http.Request) *SSORequest {
query, err := base64.URLEncoding.DecodeString(req.FormValue("sso"))
//log.Printf("Request sso content: %s", string(query))
if err != nil {
return nil
}
q, err := url.ParseQuery(string(query))
//log.Println(q)
ssor := &SSORequest{}
if n, ok := q["nonce"]; ok {
ssor.Nonce = n[0]
}
if n, ok := q["return"]; ok {
ssor.ReturnURL = n[0]
}
return ssor
}
func getSignature(payload string) []byte {
mac := hmac.New(sha256.New, cfg.HMAC256Secret)
mac.Write([]byte(payload))
return mac.Sum(nil)
}
func verifyRequest(req *http.Request) bool {
signature, err := hex.DecodeString(req.FormValue("sig"))
payload := req.FormValue("sso")
if err != nil || payload == "" {
return false
}
newsig := getSignature(payload)
return hmac.Equal(newsig, signature)
}
func logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("\n%s %s %s\n", r.Method, r.URL.Path, r.Proto)
session, err := sessionStore.Get(r, sessionCookieName)
if err == nil {
//fmt.Print("Session: ")
first := true
for k, v := range session.Values {
if first {
first = false
} else {
fmt.Print(", ")
}
//fmt.Printf("%s = %v", k, v)
_ = k
_ = v
}
fmt.Println()
}
h.ServeHTTP(w, r)
})
}
// end