-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
user.go
147 lines (124 loc) · 3.32 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
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
package token
import (
"context"
"encoding/hex"
"fmt"
"hash"
"hash/crc64"
"io"
"net/http"
"regexp"
"github.com/pkg/errors"
)
var reValidSha = regexp.MustCompile("^[a-fA-F0-9]{40}$")
var reValidCrc64 = regexp.MustCompile("^[a-fA-F0-9]{16}$")
const adminAttr = "admin" // predefined attribute key for bool isAdmin status
// User is the basic part of oauth data provided by service
type User struct {
// set by service
Name string `json:"name"`
ID string `json:"id"`
Picture string `json:"picture"`
Audience string `json:"aud,omitempty"`
// set by client
IP string `json:"ip,omitempty"`
Email string `json:"email,omitempty"`
Attributes map[string]interface{} `json:"attrs,omitempty"`
}
// SetBoolAttr sets boolean attribute
func (u *User) SetBoolAttr(key string, val bool) {
if u.Attributes == nil {
u.Attributes = map[string]interface{}{}
}
u.Attributes[key] = val
}
// SetStrAttr sets string attribute
func (u *User) SetStrAttr(key, val string) {
if u.Attributes == nil {
u.Attributes = map[string]interface{}{}
}
u.Attributes[key] = val
}
// BoolAttr gets boolean attribute
func (u *User) BoolAttr(key string) bool {
r, ok := u.Attributes[key].(bool)
if !ok {
return false
}
return r
}
// StrAttr gets string attribute
func (u *User) StrAttr(key string) string {
r, ok := u.Attributes[key].(string)
if !ok {
return ""
}
return r
}
// SetAdmin is a shortcut to set "admin" attribute
func (u *User) SetAdmin(val bool) {
u.SetBoolAttr(adminAttr, val)
}
// IsAdmin is a shortcut to get admin attribute
func (u *User) IsAdmin() bool {
return u.BoolAttr(adminAttr)
}
// SliceAttr gets slice attribute
func (u *User) SliceAttr(key string) []string {
r, ok := u.Attributes[key].([]string)
if !ok {
return []string{}
}
return r
}
// SetSliceAttr sets slice attribute for given key
func (u *User) SetSliceAttr(key string, val []string) {
if u.Attributes == nil {
u.Attributes = map[string]interface{}{}
}
u.Attributes[key] = val
}
// HashID tries to hash val with hash.Hash and fallback to crc if needed
func HashID(h hash.Hash, val string) string {
if reValidSha.MatchString(val) {
return val // already hashed or empty
}
if _, err := io.WriteString(h, val); err != nil {
// fail back to crc64
if val == "" {
val = "!empty string!"
}
if reValidCrc64.MatchString(val) {
return val // already crced
}
return fmt.Sprintf("%x", crc64.Checksum([]byte(val), crc64.MakeTable(crc64.ECMA)))
}
return hex.EncodeToString(h.Sum(nil))
}
type contextKey string
// MustGetUserInfo gets user info and panics if can't extract it from the request.
// should be called from authenticated controllers only
func MustGetUserInfo(r *http.Request) User {
user, err := GetUserInfo(r)
if err != nil {
panic(err)
}
return user
}
// GetUserInfo returns user info from request context
func GetUserInfo(r *http.Request) (user User, err error) {
ctx := r.Context()
if ctx == nil {
return User{}, errors.New("no info about user")
}
if u, ok := ctx.Value(contextKey("user")).(User); ok {
return u, nil
}
return User{}, errors.New("user can't be parsed")
}
// SetUserInfo sets user into request context
func SetUserInfo(r *http.Request, user User) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, contextKey("user"), user)
return r.WithContext(ctx)
}