intial import
This commit is contained in:
commit
2e90d32ce2
5 changed files with 264 additions and 0 deletions
35
auth.go
Normal file
35
auth.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func authenticateUser(db *sql.DB, username, password string) bool {
|
||||
pass, ok := getPasswordForUser(db, username)
|
||||
if ok {
|
||||
return subtle.ConstantTimeCompare([]byte(pass), []byte(password)) == 1
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func basicAuth(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, pass, ok := r.BasicAuth()
|
||||
if !ok || !authenticateUser(db, user, pass) {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="dyndns"`)
|
||||
w.WriteHeader(401)
|
||||
w.Write([]byte("badauth"))
|
||||
return
|
||||
}
|
||||
|
||||
userdata, err := getDataForUser(db, user)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
handleRequest(w, r, userdata)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue