/
profile.go
98 lines (83 loc) · 2.02 KB
/
profile.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
package data
import (
"database/sql"
"time"
)
// Profile stores a user's authentication methods, so they don't have to be
// queried again.
type Profile struct {
Me string
UpdatedAt time.Time
expiresAt time.Time
Methods []Method
}
func (p Profile) Expired() bool {
return time.Now().After(p.expiresAt)
}
// Method is a way a user can authenticate, it contains the name of a 3rd party
// provider and the expected profile URL with that provider.
type Method struct {
Provider string
Profile string
}
func (d *Database) CacheProfile(profile Profile) error {
_, err := d.db.Exec(`
DELETE FROM method WHERE Me = ?;
DELETE FROM profile WHERE Me = ?;
INSERT INTO profile(Me, CreatedAt) VALUES(?, ?);
`,
profile.Me,
profile.Me,
profile.Me,
profile.UpdatedAt)
tx, err := d.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(`INSERT INTO method(Me, Provider, Profile) VALUES(?, ?, ?)`)
if err != nil {
return err
}
for _, method := range profile.Methods {
_, err = stmt.Exec(profile.Me, method.Provider, method.Profile)
}
if err != nil {
terr := tx.Rollback()
if terr != nil {
return terr
}
return err
}
return tx.Commit()
}
func (d *Database) Profile(me string) (Profile, error) {
rows, err := d.db.Query(`
SELECT profile.Me, profile.CreatedAt, method.Provider, method.Profile
FROM method
LEFT JOIN profile ON method.Me = profile.Me
WHERE method.Me = ?
ORDER BY method.Provider`, me)
if err != nil {
return Profile{}, err
}
defer rows.Close()
var profile Profile
var ok bool
var updatedAt time.Time
for rows.Next() {
ok = true
var method Method
if err = rows.Scan(&profile.Me, &profile.UpdatedAt, &method.Provider, &method.Profile); err != nil {
return profile, err
}
if profile.UpdatedAt.After(updatedAt) {
updatedAt = profile.UpdatedAt
}
profile.Methods = append(profile.Methods, method)
}
profile.expiresAt = profile.UpdatedAt.Add(d.expiry.Profile)
if !ok {
return Profile{}, sql.ErrNoRows
}
return profile, rows.Err()
}