-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
84 lines (69 loc) · 1.63 KB
/
user.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
package user
import (
"net/url"
"github.com/drone/routes/exp/context"
)
// Key used to store the user in the session
const userKey = "_user"
// User represents a user of the application.
type User struct {
Id string // the unique permanent ID of the user.
Name string // the human-readable ID of the user.
Email string
Photo string
FederatedIdentity string
FederatedProvider string
// additional, custom Attributes
Attrs map[string]string
}
// Decode will create a user from a URL Query string.
func Decode(v string) *User {
values, err := url.ParseQuery(v)
if err != nil {
return nil
}
attrs := map[string]string{}
for key, _ := range values {
attrs[key]=values.Get(key)
}
return &User {
Id : values.Get("id"),
Name : values.Get("name"),
Email : values.Get("email"),
Photo : values.Get("photo"),
Attrs : attrs,
}
}
// Encode will encode a user as a URL query string.
func (u *User) Encode() string {
values := url.Values{}
// add custom attributes
if u.Attrs != nil {
for key, val := range u.Attrs {
values.Set(key, val)
}
}
values.Set("id", u.Id)
values.Set("name", u.Name)
values.Set("email", u.Email)
values.Set("photo", u.Photo)
return values.Encode()
}
// Current returns the currently logged-in user, or nil if the user is not
// signed in.
func Current(c *context.Context) *User {
v := c.Values.Get(userKey)
if v == nil {
return nil
}
u, ok := v.(*User)
if !ok {
return nil
}
return u
}
// Set sets the currently logged-in user. This is typically used by middleware
// that handles user authentication.
func Set(c *context.Context, u *User) {
c.Values.Set(userKey, u)
}