login.go (view raw)
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 |
//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"context"
"crypto/rand"
"crypto/sha512"
"crypto/subtle"
"database/sql"
"fmt"
"hash"
"io"
"log"
"net/http"
"reflect"
"regexp"
"strings"
"sync"
"time"
"golang.org/x/crypto/bcrypt"
)
type keytype struct{}
var thekey keytype
func LoginChecker(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userinfo, ok := checkauthcookie(r)
if ok {
ctx := context.WithValue(r.Context(), thekey, userinfo)
r = r.WithContext(ctx)
}
handler.ServeHTTP(w, r)
})
}
func LoginRequired(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok := GetUserInfo(r) != nil
if !ok {
loginredirect(w, r)
return
}
handler.ServeHTTP(w, r)
})
}
func GetUserInfo(r *http.Request) *UserInfo {
userinfo, ok := r.Context().Value(thekey).(*UserInfo)
if !ok {
return nil
}
return userinfo
}
func calculateCSRF(salt, action, auth string) string {
hasher := sha512.New512_256()
zero := []byte{0}
hasher.Write(zero)
hasher.Write([]byte(auth))
hasher.Write(zero)
hasher.Write([]byte(csrfkey))
hasher.Write(zero)
hasher.Write([]byte(salt))
hasher.Write(zero)
hasher.Write([]byte(action))
hasher.Write(zero)
hash := hexsum(hasher)
return salt + hash
}
func GetCSRF(action string, r *http.Request) string {
auth := getauthcookie(r)
if auth == "" {
return ""
}
hasher := sha512.New512_256()
io.CopyN(hasher, rand.Reader, 32)
salt := hexsum(hasher)
return calculateCSRF(salt, action, auth)
}
func CheckCSRF(action string, r *http.Request) bool {
auth := getauthcookie(r)
if auth == "" {
return false
}
csrf := r.FormValue("CSRF")
if len(csrf) != authlen*2 {
return false
}
salt := csrf[0:authlen]
rv := calculateCSRF(salt, action, auth)
ok := subtle.ConstantTimeCompare([]byte(rv), []byte(csrf)) == 1
return ok
}
func CSRFWrap(action string, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok := CheckCSRF(action, r)
if !ok {
http.Error(w, "invalid csrf", 403)
return
}
handler.ServeHTTP(w, r)
})
}
func loginredirect(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: "",
MaxAge: -1,
Secure: securecookies,
HttpOnly: true,
})
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
var authregex = regexp.MustCompile("^[[:alnum:]]+$")
var authlen = 32
var stmtUserName, stmtUserAuth, stmtSaveAuth, stmtDeleteAuth *sql.Stmt
var csrfkey string
var securecookies bool
func LoginInit(db *sql.DB) {
var err error
stmtUserName, err = db.Prepare("select userid, hash from users where username = ?")
if err != nil {
log.Fatal(err)
}
var userinfo UserInfo
t := reflect.TypeOf(userinfo)
var fields []string
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fields = append(fields, strings.ToLower(f.Name))
}
stmtUserAuth, err = db.Prepare(fmt.Sprintf("select %s from users where userid = (select userid from auth where hash = ?)", strings.Join(fields, ", ")))
if err != nil {
log.Fatal(err)
}
stmtSaveAuth, err = db.Prepare("insert into auth (userid, hash) values (?, ?)")
if err != nil {
log.Fatal(err)
}
stmtDeleteAuth, err = db.Prepare("delete from auth where userid = ?")
if err != nil {
log.Fatal(err)
}
debug := false
getconfig("debug", &debug)
securecookies = !debug
getconfig("csrfkey", &csrfkey)
}
var authinprogress = make(map[string]bool)
var authprogressmtx sync.Mutex
func rateandwait(username string) bool {
authprogressmtx.Lock()
defer authprogressmtx.Unlock()
if authinprogress[username] {
return false
}
authinprogress[username] = true
go func(name string) {
time.Sleep(1 * time.Second / 2)
authprogressmtx.Lock()
authinprogress[name] = false
authprogressmtx.Unlock()
}(username)
return true
}
func getauthcookie(r *http.Request) string {
cookie, err := r.Cookie("auth")
if err != nil {
return ""
}
auth := cookie.Value
if !(len(auth) == authlen && authregex.MatchString(auth)) {
log.Printf("login: bad auth: %s", auth)
return ""
}
return auth
}
func checkauthcookie(r *http.Request) (*UserInfo, bool) {
auth := getauthcookie(r)
if auth == "" {
return nil, false
}
hasher := sha512.New512_256()
hasher.Write([]byte(auth))
authhash := hexsum(hasher)
row := stmtUserAuth.QueryRow(authhash)
var userinfo UserInfo
v := reflect.ValueOf(&userinfo).Elem()
var ptrs []interface{}
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ptrs = append(ptrs, f.Addr().Interface())
}
err := row.Scan(ptrs...)
if err != nil {
if err == sql.ErrNoRows {
log.Printf("login: no auth found")
} else {
log.Printf("login: error scanning auth row: %s", err)
}
return nil, false
}
return &userinfo, true
}
func loaduser(username string) (int64, string, bool) {
row := stmtUserName.QueryRow(username)
var userid int64
var hash string
err := row.Scan(&userid, &hash)
if err != nil {
if err == sql.ErrNoRows {
log.Printf("login: no username found")
} else {
log.Printf("login: error loading username: %s", err)
}
return -1, "", false
}
return userid, hash, true
}
var userregex = regexp.MustCompile("^[[:alnum:]]+$")
var userlen = 32
var passlen = 128
func hexsum(h hash.Hash) string {
return fmt.Sprintf("%x", h.Sum(nil))[0:authlen]
}
func dologin(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username")
password := r.FormValue("password")
if len(username) == 0 || len(username) > userlen ||
!userregex.MatchString(username) || len(password) == 0 ||
len(password) > passlen {
log.Printf("login: invalid password attempt")
loginredirect(w, r)
return
}
userid, hash, ok := loaduser(username)
if !ok {
loginredirect(w, r)
return
}
if !rateandwait(username) {
loginredirect(w, r)
return
}
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
log.Printf("login: incorrect password")
loginredirect(w, r)
return
}
hasher := sha512.New512_256()
io.CopyN(hasher, rand.Reader, 32)
hash = hexsum(hasher)
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: hash,
MaxAge: 3600 * 24 * 30,
Secure: securecookies,
HttpOnly: true,
})
hasher.Reset()
hasher.Write([]byte(hash))
authhash := hexsum(hasher)
_, err = stmtSaveAuth.Exec(userid, authhash)
if err != nil {
log.Printf("error saving auth: %s", err)
}
log.Printf("login: successful login")
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func dologout(w http.ResponseWriter, r *http.Request) {
userinfo, ok := checkauthcookie(r)
if ok && CheckCSRF("logout", r) {
_, err := stmtDeleteAuth.Exec(userinfo.UserID)
if err != nil {
log.Printf("login: error deleting old auth: %s", err)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: "",
MaxAge: -1,
Secure: securecookies,
HttpOnly: true,
})
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
|