created hwsauth module
This commit is contained in:
43
hwsauth/protectpage.go
Normal file
43
hwsauth/protectpage.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package hwsauth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Checks if the model is set in the context and shows 401 page if not logged in
|
||||
func (auth *Authenticator[T]) LoginReq(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
model := getAuthorizedModel[T](r.Context())
|
||||
if model == nil {
|
||||
auth.errorPage(http.StatusUnauthorized, w, r)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Checks if the model is set in the context and redirects them to the landing page if
|
||||
// they are logged in
|
||||
func (auth *Authenticator[T]) LogoutReq(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
model := getAuthorizedModel[T](r.Context())
|
||||
if model != nil {
|
||||
http.Redirect(w, r, auth.LandingPage, http.StatusFound)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (auth *Authenticator[T]) FreshReq(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
model := getAuthorizedModel[T](r.Context())
|
||||
isFresh := time.Now().Before(time.Unix(model.fresh, 0))
|
||||
if !isFresh {
|
||||
w.WriteHeader(444)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user