-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.go
66 lines (58 loc) · 1.42 KB
/
auth.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
package bnf
import (
"database/sql"
"errors"
"log"
"net/http"
"golang.org/x/crypto/bcrypt"
)
func basicAuth(r *http.Request) bool {
user, pass, ok := r.BasicAuth()
if !ok {
return false
}
hash := make([]byte, 0, 60)
err := db.QueryRow("SELECT password FROM auth WHERE username = ?", user).Scan(&hash)
if err != nil {
if err != sql.ErrNoRows {
log.Printf("reading database: %v", err)
}
return false
}
err = bcrypt.CompareHashAndPassword(hash, []byte(pass))
if err != nil && err != bcrypt.ErrMismatchedHashAndPassword {
log.Printf("checking password: %v", err)
}
return err == nil
}
func twitchAuthz(user string) (bool, bool) {
var admin bool
err := db.QueryRow("SELECT admin FROM twitch_authz WHERE user = ?", user).Scan(&admin)
if err != nil {
if err != sql.ErrNoRows {
log.Printf("reading database: %v", err)
}
return false, false
}
return true, admin
}
func twitchAuthzInsert(user string) error {
_, err := db.Exec("INSERT INTO twitch_authz(user, admin) VALUES(?, FALSE);", user)
if err != nil {
log.Printf("inserting authz: %v", err)
}
return err
}
func twitchAuthzDelete(user string) error {
result, err := db.Exec("DELETE FROM twitch_authz WHERE user = ? AND admin = FALSE;", user)
if err == nil {
rows, _ := result.RowsAffected()
if rows == 0 {
err = errors.New("no matching non-admin user found")
}
}
if err != nil {
log.Printf("deleting authz: %v", err)
}
return err
}