/
userpass.go
152 lines (130 loc) · 3.14 KB
/
userpass.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
package auth
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
"github.com/pborman/uuid"
)
//AuthUserpass is the identifier returned by Nop.Userpass
const AuthUserpass = "Userpass"
type sessions struct {
table map[string]time.Time
lock sync.RWMutex
timeout time.Duration
refresh bool
}
func (s *sessions) new() string {
u := uuid.NewUUID()
s.lock.Lock()
s.table[u.String()] = time.Now().Add(s.timeout)
s.lock.Unlock()
return u.String()
}
func (s *sessions) validate(sessionID string) bool {
s.lock.RLock()
expiry, found := s.table[sessionID]
s.lock.RUnlock()
if found {
if time.Now().Before(expiry) {
if s.refresh {
s.lock.Lock()
s.table[sessionID] = time.Now().Add(s.timeout)
s.lock.Unlock()
}
return true
}
s.lock.Lock()
delete(s.table, sessionID)
s.lock.Unlock()
}
return false
}
func (s *sessions) expiry(sessionID string) time.Time {
s.lock.RLock()
ret := s.table[sessionID]
s.lock.RUnlock()
return ret
}
type Userpass struct {
username string
password string
sessions sessions
}
type UserpassConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Timeout int `yaml:"timeout"`
Refresh bool `yaml:"refresh"`
}
func NewUserpass(conf UserpassConfig) (*Userpass, error) {
if conf.Username == "" {
return nil, fmt.Errorf("No username provided in userpass auth config")
}
if conf.Password == "" {
return nil, fmt.Errorf("No password provided in userpass auth config")
}
if conf.Timeout == 0 {
conf.Timeout = 30
}
return &Userpass{
username: conf.Username,
password: conf.Password,
sessions: sessions{
table: map[string]time.Time{},
timeout: time.Duration(conf.Timeout) * time.Minute,
refresh: conf.Refresh,
},
}, nil
}
func (u *Userpass) LoginHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
return
}
provided := struct {
Username string `json:"username"`
Password string `json:"password"`
}{}
err = json.Unmarshal(body, &provided)
if err != nil {
w.WriteHeader(400)
return
}
if provided.Username != u.username || provided.Password != u.password {
w.WriteHeader(401)
return
}
token := u.sessions.new()
w.Header().Set("Content-Type", "application/json")
http.SetCookie(w, &http.Cookie{Name: "doomsday-token", Value: token, Path: "/", Expires: u.sessions.expiry(token)})
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf(`{"token":"%s"}`+"\n", token)))
}
}
func (u *Userpass) TokenHandler() TokenFunc {
return func(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sessionID := r.Header.Get("X-Doomsday-Token")
if sessionID == "" {
cookie, err := r.Cookie("doomsday-token")
if err == nil {
sessionID = cookie.Value
}
}
if u.sessions.validate(sessionID) {
http.SetCookie(w, &http.Cookie{Name: "doomsday-token", Value: sessionID, Path: "/", Expires: u.sessions.expiry(sessionID)})
fn(w, r)
} else {
w.WriteHeader(401)
}
}
}
}
func (_ *Userpass) Identifier() AuthType {
return AuthUserpass
}