Replace request counter with basic auth/token system
This commit is contained in:
parent
c504f8c597
commit
ab01a459af
1 changed files with 17 additions and 14 deletions
31
auth/auth.go
31
auth/auth.go
|
|
@ -1,8 +1,8 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Authenticator struct {
|
type Authenticator struct {
|
||||||
|
|
@ -10,22 +10,25 @@ type Authenticator struct {
|
||||||
|
|
||||||
func (a *Authenticator) Authenticate(in http.HandlerFunc) http.HandlerFunc {
|
func (a *Authenticator) Authenticate(in http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
c := http.Cookie{
|
if r.URL.String() == "/loginok" {
|
||||||
Name: "RequestCounter",
|
c := http.Cookie{
|
||||||
}
|
Name: "token",
|
||||||
cookie, err := r.Cookie("RequestCounter")
|
Value: "good",
|
||||||
if err != nil {
|
|
||||||
cookie = &http.Cookie{
|
|
||||||
Value: "0",
|
|
||||||
}
|
}
|
||||||
|
http.SetCookie(w, &c)
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
count, err := strconv.Atoi(cookie.Value)
|
if r.URL.String() == "/login" {
|
||||||
if err != nil {
|
w.Header().Set("content-type", "text/html")
|
||||||
c.Value = "1"
|
fmt.Fprint(w, "<a href='/loginok'>Login</a>")
|
||||||
} else {
|
return
|
||||||
c.Value = strconv.Itoa(count + 1)
|
}
|
||||||
|
if _, err := r.Cookie("token"); err != nil {
|
||||||
|
fmt.Println("user not logged in")
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
http.SetCookie(w, &c)
|
|
||||||
in(w, r)
|
in(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue