/
auth.go
533 lines (448 loc) · 13.2 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"code.google.com/p/goauth2/oauth"
"github.com/dchest/uniuri" // give us random URIs
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
var config = &oauth.Config{
ClientId: "560046220732101",
ClientSecret: "18d10b619523227e65ecf5b38fc18f90",
//RedirectURL: // It is setted dynamicaly on LoginHandler based on server current url
Scope: "basic_info email",
AuthURL: "https://www.facebook.com/dialog/oauth",
TokenURL: "https://graph.facebook.com/oauth/access_token",
}
var transport = &oauth.Transport{
Config: config,
Transport: http.DefaultTransport,
}
// It's returns an User instance when required
type Auth interface {
Logged() bool
GetUser() (*User, error)
}
type UserAuth struct {
user *User
err error
}
func (u *UserAuth) Logged() bool {
if u.err != nil || u.user == nil {
return false
} else {
return true
}
}
func (u *UserAuth) GetUser() (*User, error) {
if u.err != nil || u.user == nil {
return nil, u.err
} else {
return u.user, nil
}
}
func AccessDeniedHandler(r render.Render, req *http.Request, err error) {
if req.Header.Get("Authorization") != "" {
// Client thiks he is logged, force him to logout
r.JSON(419, NewError(ErrorCodeDefault, fmt.Sprintf(
"Voce nao esta mais logado no sistema! %s.", err)))
return
}
// Client didn't send credentials, say him it's required
r.JSON(403, NewError(ErrorCodeDefault, fmt.Sprintf(
"Voce nao esta logado no sistema! %s.", err)))
}
func AuthMiddleware(c martini.Context, db DB, r render.Render, req *http.Request) {
header := req.Header.Get("Authorization")
if header == "" {
err := errors.New("Voce nao apresentou credenciais de autorizacao")
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() {
// If the nex handler aborted, empty return
// So he can't go on without user, let's alert user
AccessDeniedHandler(r, req, err)
}
return
}
// Len of our 41 (20:20) char encoded in base64 is 56
// (C++) long base64EncodedSize = 4 * (int)Math.Ceiling(originalSizeInBytes / 3.0);
if len(header) < 56 {
err := errors.New("Credenciais de authorizacao apresentadas sao invalidas.")
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
auth, err := decodeAuth(header)
if err != nil {
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
i := strings.Index(auth, ":")
if i == -1 {
err = errors.New("Credenciais de authorizacao estao corrompidas")
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
tokenid := auth[:i]
userid := auth[i+1:]
obj, err := db.Get(Token{}, tokenid)
if err != nil {
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
if obj == nil {
// This tokenid was not found
err = errors.New("Credenciais fornecidas nao foram encontradas")
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
token := obj.(*Token)
if token.UserId != userid {
err = errors.New("Usuario nao identificado pelas credenciais fornecidas")
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
obj, err = db.Get(User{}, userid)
if err != nil {
userAuth := &UserAuth{nil, err}
c.MapTo(userAuth, (*Auth)(nil))
c.Next() // Call the next handler
if !c.Written() { // Response the user
AccessDeniedHandler(r, req, err)
}
return
}
user := obj.(*User)
userAuth := &UserAuth{user: user, err: nil}
// Here the next handler has got the user
c.MapTo(userAuth, (*Auth)(nil))
}
// Here is our BasicAuth
func encodeAuth(token *Token) string {
auth := base64.StdEncoding.EncodeToString([]byte(token.TokenId + ":" + token.UserId))
return auth
}
func decodeAuth(auth string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(auth)
return string(decoded), err
}
func LoginHandler(w http.ResponseWriter, r *http.Request) {
// The state will inform if the response will be json or html
// Html is the default request and will do 2 things:
// Set the cookie "credentials" and return the visitor to the main page
state := r.URL.Query().Get("state")
if state == "" { // The default is a direct html request
state = "html"
}
// Set based on the server current url
transport.Config.RedirectURL = Env.Url + "logincallback/"
http.Redirect(w, r, transport.Config.AuthCodeURL(state), 302)
return
}
func renderHtmlOrJson(r render.Render, w http.ResponseWriter, req *http.Request, state string, err error, message string) {
if state == "json" {
r.JSON(http.StatusUnauthorized, NewError(ErrorCodeDefault, fmt.Sprintf(message+" %s.", err)))
} else {
cookieError := &http.Cookie{Name: "error", Value: fmt.Sprintf(message+" %s.", err), MaxAge: 60 * 30, Path: "/"}
http.SetCookie(w, cookieError)
http.Redirect(w, req, "/", 302)
}
}
func LoginCallbackHandler(db DB, r render.Render, w http.ResponseWriter, req *http.Request) {
// The state indicates if it is an browser request, or an default json request
state := req.URL.Query().Get("state")
code := req.URL.Query().Get("code")
// Token = oauth.Token, err = oauth.OAuthError
_, err := transport.Exchange(code)
if err != nil {
renderHtmlOrJson(r, w, req, state, err, "Desculpe, mas nao conseguimos autentica-lo.")
return
}
// Here we have our http client configured to make calls
client := transport.Client()
res, err := client.Get("https://graph.facebook.com/me")
if err != nil {
renderHtmlOrJson(r, w, req, state, err, "Desculpe, mas nao foi possivel pegar perfil do facebook.")
return
}
defer res.Body.Close()
data := new(interface{}) // Its a pointer to an interface
// The Decoder can handle a stream, unlike Unmarshal
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(data) // Passing a pointer
if err != nil {
renderHtmlOrJson(r, w, req, state, err, "Desculpe, mas a resposta do facebook nao foi um json valido.")
return
}
profile, err := extractProfile(transport.Token, data)
if err != nil {
renderHtmlOrJson(r, w, req, state, err, "Desculpe, mas nao conseguimos acessar seu perfil.")
return
}
user, err := getOrCreateUser(db, profile)
if err != nil {
renderHtmlOrJson(r, w, req, state, err, "Desculpe, mas nao conseguimos processar seu perfil")
return
}
// After answare the request, check if this user has an pic
err = checkOrGetPic(db, client, user)
if err != nil {
renderHtmlOrJson(r, w, req, state, err, "Desculpe, mas ocorreu um erro ao processar sua foto do perfil.")
return
}
token, err := newToken(db, user)
credentials := encodeAuth(token)
if state == "json" {
r.JSON(http.StatusOK, map[string]interface{}{"credentials": credentials})
return
}
// COOKIES CAN'T HAVE VALUES WITH COMMA OR SPACE !!!
cookieCredentials := &http.Cookie{Name: "credentials", Value: credentials, MaxAge: 60 * 60 * 24 * 30 * 12, Path: "/"}
cookieMessage := &http.Cookie{Name: "message", Value: "Logou-se!", MaxAge: 60 * 30, Path: "/"}
http.SetCookie(w, cookieCredentials)
http.SetCookie(w, cookieMessage)
http.Redirect(w, req, "/", 302)
}
func extractProfile(token *oauth.Token, data *interface{}) (*Profile, error) {
// Here we are taking the value in where this variable is pointing to,
// and we cast it to an map[string] interface
p := (*data).(map[string]interface{})
// Extracting updated_time from Facebook return
timeLayout := "2006-01-02T15:04:05+0000"
updateTime, err := time.Parse(timeLayout, p["updated_time"].(string))
if err != nil {
return nil, err
}
// The Facebook profile
profile := &Profile{
ProfileId: p["id"].(string),
UserId: "", // UserId is not inserted know, but is required
UserName: p["username"].(string),
Email: p["email"].(string),
FullName: p["name"].(string),
Gender: p["gender"].(string),
ProfileUrl: p["link"].(string),
Language: p["locale"].(string),
Verified: p["verified"].(bool),
FirstName: p["first_name"].(string),
LastName: p["last_name"].(string),
SourceUpdate: updateTime,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
Scope: config.Scope, // A package config variable
TokenExpiry: token.Expiry,
Creation: time.Now(),
LastUpdate: time.Now(),
}
return profile, nil
}
// Take a http client with a transport pre-configured with the user credentials
// and return the Pic saved in public/img/pic/ and stored in DB
func getPic(db DB, client *http.Client) (*Pic, error) {
// OK WE HAVE THE PROFILE, BUT WE DON'T HAVE USER'S PICTURE
// LETS TAKE IT
res, err := client.Get("https://graph.facebook.com/me/picture")
if err != nil {
return nil, err
}
defer res.Body.Close()
img, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
picId := uniuri.NewLen(20)
p, err := db.Get(Pic{}, picId)
for err == nil && p != nil {
// Shit, we generated an existing picId!
// Lotto, where are you?
picId := uniuri.NewLen(20)
p, err = db.Get(Pic{}, picId)
}
if err != nil {
return nil, err
}
fo, err := os.Create("public/pic/" + picId + ".png")
if err != nil {
return nil, err
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
fo.Write(img)
pic := &Pic{
PicId: picId,
Creation: time.Now(),
Deleted: false,
}
err = db.Insert(pic)
if err != nil {
return nil, err
}
return pic, nil
}
func checkOrGetPic(db DB, client *http.Client, user *User) error {
// Check if this user has a default pic yet
if user.PicId == "default" {
pic, err := getPic(db, client)
if err != nil {
return err
}
log.Printf("New pic %s saved from user %s", pic.PicId, user.UserId)
user.PicId = pic.PicId
count, err := db.Update(user)
if err != nil {
return err
}
if count == 0 {
user.PicId = "default"
return errors.New("Nenhum usuario foi atualizado com a foto salva")
}
}
return nil
}
func newUser(db DB, profile *Profile) (*User, error) {
userId := uniuri.NewLen(20)
userName := profile.UserName
var users []User
var increment = 0
query := "select * from user where userid=? or username=?"
_, err := db.Select(&users, query, userId, userName)
for err == nil && len(users) != 0 {
log.Printf("Trying to create another userid U[%v]: %#v \n", len(users), users)
u := users[0]
users = users[:0] // Clear my slice
// I will play in lottery if we see it happennig
// just one time in 10^32
if u.UserId == userId {
userId = uniuri.NewLen(20)
}
// If already exists an user with this userName
if u.UserName == userName {
increment += 1
userName = fmt.Sprintf("%s%d", profile.UserName, increment)
}
// Check if still existing an user like this
_, err = db.Select(&users, query, userId, userName)
}
// Checks if main loop didn't finish for an error
if err != nil {
return nil, err
}
user := &User{
UserId: userId,
UserName: userName,
PicId: "default", // Reference to default pic
FullName: profile.FullName,
LikeCount: 0,
Creation: time.Now(),
LastUpdate: time.Now(),
Deleted: false,
Admin: false,
}
err = db.Insert(user)
if err != nil {
return nil, err
}
return user, nil
}
func getOrCreateUser(db DB, profile *Profile) (*User, error) {
p, err := db.Get(Profile{}, profile.ProfileId)
if err != nil {
return nil, err
}
if p != nil {
profileSaved := p.(*Profile)
profile.UserId = profileSaved.UserId
profile.LastUpdate = time.Now()
_, err := db.Update(profile)
if err != nil {
return nil, err
}
u, err := db.Get(User{}, profile.UserId)
if err != nil {
return nil, err
}
user := u.(*User)
return user, nil
}
user, err := newUser(db, profile)
if err != nil {
return nil, err
}
// Now this new profile has an user account
profile.UserId = user.UserId
profile.Creation = time.Now()
profile.LastUpdate = time.Now()
err = db.Insert(profile)
if err != nil {
return nil, err
}
// !!! REMEMBER TO GET USERS PIC!
return user, nil
}
func newToken(db DB, user *User) (*Token, error) {
token := new(Token)
tokenId := uniuri.NewLen(20)
t, err := db.Get(Token{}, tokenId)
for err == nil && t != nil {
// Shit, we generated an existing token...
// just one time in 10^32
// Let's play the lottery!
tokenId := uniuri.NewLen(20)
t, err = db.Get(Token{}, tokenId)
}
if err != nil {
return nil, err
}
token.TokenId = tokenId
token.UserId = user.UserId
token.Creation = time.Now()
err = db.Insert(token)
if err != nil {
return nil, err
}
return token, nil
}