/
auth.go
339 lines (318 loc) · 9.3 KB
/
auth.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
package auth
import (
"golang.org/x/oauth2"
fboauth "golang.org/x/oauth2/facebook"
googleoauth "golang.org/x/oauth2/google"
"database/sql"
"encoding/json"
"fmt"
u "github.com/ChrisKaufmann/goutils"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/glog"
"github.com/msbranco/goconfig"
"io/ioutil"
"net/http"
"time"
)
var (
MyURL string
db *sql.DB
cookieName string = "auth"
environment string = "production"
stmtCookieIns *sql.Stmt
stmtInsertUser *sql.Stmt
stmtSessionExists *sql.Stmt
googleEnabled bool
facebookEnabled bool
GoogOauthCfg = &oauth2.Config{
ClientID: "",
ClientSecret: "",
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email",},
Endpoint: googleoauth.Endpoint,
}
FBOauthCfg = &oauth2.Config {
ClientID: "",
ClientSecret: "",
Scopes: []string{"email"},
Endpoint: fboauth.Endpoint,
}
oauthStateString = u.RandomString(32)
)
func CookieName(c string) {
cookieName = c
}
func Environment(e string) {
environment = e
}
func DB(d *sql.DB) {
db = d
var err error
userDB()
stmtCookieIns, err = u.Sth(db, "INSERT INTO sessions (user_id,session_hash) VALUES( ? ,? )")
if err != nil {
glog.Fatalf(" DB(): u.sth(stmtCookieIns) %s", err)
}
stmtInsertUser, err = u.Sth(db, "insert into users (email) values (?) ")
if err != nil {
glog.Fatalf(" DB(): u.sth(stmtInsertUser) %s", err)
}
stmtSessionExists, err = u.Sth(db, "select user_id from sessions where session_hash=?")
if err != nil {
glog.Fatalf(" DB(): u.sth(stmtSessionExists) %s", err)
}
}
func Config(config string) {
c, err := goconfig.ReadConfigFile(config)
googleEnabled = true
facebookEnabled = true
if err != nil {
glog.Fatalf("init(): readconfigfile(config)")
}
GoogOauthCfg.ClientID, err = c.GetString("Google", "ClientID")
if err != nil {
googleEnabled = false
glog.Errorf("init(): readconfigfile(Google.ClientID)")
}
GoogOauthCfg.ClientSecret, err = c.GetString("Google", "ClientSecret")
if err != nil {
googleEnabled = false
glog.Errorf("init(): readconfigfile(Google.ClientSecret)")
}
FBOauthCfg.ClientID, err = c.GetString("Facebook", "ClientID")
if err != nil {
facebookEnabled = false
glog.Errorf("init(): readconfigfile(Facebook.ClientID)")
}
FBOauthCfg.ClientSecret, err = c.GetString("Facebook", "ClientSecret")
if err != nil {
facebookEnabled = false
glog.Errorf("init(): readconfigfile(Facebook.ClientSecret)")
}
url, err := c.GetString("Web", "url")
MyURL = url
if err != nil {
glog.Fatalf("init(): readconfigfile(Web.url)")
}
GoogOauthCfg.RedirectURL = url + "oauth2callback"
FBOauthCfg.RedirectURL = url + "fboauth2callback"
}
func HandleLogout(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie(cookieName)
if err != nil {
//just means that the cookie doesn't exist or we couldn't read it
fmt.Printf("HandleLogout: No cookie to logut %s", err)
return
}
tokHash := cookie.Value
if !SessionExists(tokHash) {
fmt.Printf("HandleLogout: No matching sessions")
}
_, err = stmtLogoutSession.Exec(tokHash)
if err != nil {
glog.Errorf("HandleLougout: stmtLogoutSession.Exec(%s): %s", tokHash, err)
}
http.Redirect(w, r, "/", http.StatusFound)
}
// Start the authorization process
func HandleAuthorize(w http.ResponseWriter, r *http.Request) {
//Get the Google URL which shows the Authentication page to the user
url := GoogOauthCfg.AuthCodeURL("")
//redirect user to that page
http.Redirect(w, r, url, http.StatusFound)
}
func HandleAuthorizeFacebook(w http.ResponseWriter, r *http.Request) {
url := FBOauthCfg.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline)
http.Redirect(w, r, url, http.StatusFound)
}
//simulate a demo login, create the cookie, make sure the demo user exists, create the session
func DemoUser(w http.ResponseWriter, r *http.Request) {
demo_email := "demo@exmaple.com"
var us User
var err error
if !UserExists(demo_email) {
us, err = AddUser(demo_email)
if err != nil {
glog.Errorf("DemoUser(w,r)AddUser(%s): %s", demo_email, err)
return
}
} else {
us, err = GetUserByEmail(demo_email)
if err != nil {
glog.Errorf("DemoUser(w,r)GetUserByEmail(%s): %s", demo_email, err)
return
}
}
var authString = u.RandomString(64)
//set the cookie
err = us.AddSession(authString)
if err != nil {
glog.Errorf("DemoUser(w,r)AddUser(%s): %s", authString, err)
return
}
expire := time.Now().AddDate(1, 0, 0)
cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/main.html", http.StatusFound)
}
func LoginToken(w http.ResponseWriter, r *http.Request, lt string) (err error) {
us, err := GetUserByLoginToken(lt)
fmt.Printf("got user: %s", us)
if err != nil {
glog.Errorf("LoginToken(%s) No session by that token: %s", lt, err)
return err
}
var authString = u.RandomString(64)
fmt.Printf("AddSession(%s)", authString)
err = us.AddSession(authString)
if err != nil {
glog.Errorf("LoginToken():us.AddSession(%s): %s", authString, err)
return err
}
expire := time.Now().AddDate(1, 0, 0)
cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire, Path: "/"}
fmt.Printf("http.SetCookie(w,%s)expore:%s", cookie, expire)
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/main.html", http.StatusFound)
return err
}
// Function that handles the callback from the Google server
func HandleGoogleCallback(w http.ResponseWriter, r *http.Request) {
if googleEnabled == false {
return
}
authcode := r.FormValue("code")
tok, err := GoogOauthCfg.Exchange(oauth2.NoContext, authcode)
if err != nil {
fmt.Println("err is", err)
}
fmt.Println("token is ", tok)
response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + tok.AccessToken)
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
fmt.Printf("contents: %s", contents)
var f interface{}
err = json.Unmarshal(contents, &f)
if err != nil {
glog.Errorf("json.Unmarshal(%s,f): %s\n", contents, err)
}
m := f.(map[string]interface{})
email := m["email"].(string)
err = AddSession(w, r, email)
if err != nil {
glog.Errorf("AddSession(w,r,%s): %s\n", email, err)
}
return
}
func HandleFacebookOauth2Callback(w http.ResponseWriter, r *http.Request) {
if facebookEnabled == false {
print("facebookEnabled is false, returning\n")
return
}
authcode := r.FormValue("code")
type fbuser struct {
Id string `json:"id"`
Name string `json:"name"`
}
tok, err := FBOauthCfg.Exchange(oauth2.NoContext, authcode)
if err != nil {
glog.Errorf("FBOauthCfg.Exchange(oauth2.NoContext, %s): %s", authcode, err)
return
}
response, err := http.Get("https://graph.facebook.com/me?access_token=" + tok.AccessToken)
if err != nil {
glog.Errorf("http.Get(https://graph.facebook.com/me?access_token=%s): %s", tok.AccessToken, err)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
glog.Errorf("ioutil.ReadAll(%s): %s", response.Body, err)
return
}
var fbu fbuser
err = json.Unmarshal(body, &fbu)
if err != nil {
glog.Errorf("json.Unmarshal(%s, fbu): %s", body, err)
return
}
email := fmt.Sprintf("%s@facebook.com", fbu.Id)
err = AddSession(w,r,email)
if err != nil {
glog.Errorf("AddSession(w,r,%s): %s", email, err)
return
}
}
func AddSession(w http.ResponseWriter,r *http.Request, email string)( err error) {
var us User
if !UserExists(email) {
fmt.Printf("HandleOauth2Callback: creating new user %s", email)
us, err = AddUser(email)
if err != nil {
glog.Errorf("HandleOauth2Callback:UserExists()AddUser(%s): %s", email, err)
return err
}
} else {
us, err = GetUserByEmail(email)
if err != nil {
glog.Errorf("HandleOauth2Callback:UserExists()GetUserEmail(%s): %s", email, err)
return err
}
}
var authString = u.RandomString(64)
err = us.AddSession(authString)
if err != nil {
glog.Errorf("HandleOauth2Callback:stmtCookieIns.Exec(%s,%s): %s", us.ID, authString, err)
return err
}
//set the cookie
expire := time.Now().AddDate(1, 0, 0) // year expirey seems reasonable
cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/main", http.StatusFound)
return err
}
func LoggedIn(w http.ResponseWriter, r *http.Request) (bool, User) {
var falseuser User
if environment == "test" {
tue := "test@example.com"
fmt.Printf("Test login")
if !UserExists(tue) {
us, err := AddUser(tue)
if err != nil {
glog.Errorf("Couldn't add user: %s", err)
return false, falseuser
}
return true, us
} else {
us, err := GetUserByEmail(tue)
if err != nil {
glog.Errorf("Couldn't get user by email: %s", err)
return false, falseuser
}
return true, us
}
}
cookie, err := r.Cookie(cookieName)
switch {
case err == http.ErrNoCookie: // just means cookie doesn't exist or we couldn't read
fmt.Printf("Couldn't get cookie")
return false, falseuser
case err != nil:
glog.Errorf("Loggedin() r.Cookie(%s): %s", cookieName, err)
return false, falseuser
}
tokHash := cookie.Value
if !SessionExists(tokHash) {
fmt.Printf("SessionExists(%s) is false", tokHash)
return false, falseuser
}
us, err := GetUserBySession(tokHash)
if err != nil {
glog.Errorf("LoggedIn():GetUserBySession(%s): %s", tokHash, err)
return false, falseuser
}
if us.ID > 0 {
return true, us
}
fmt.Printf("UID: %s, returning false", us.ID)
return false, falseuser
}