| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package middleware
- import (
- "net/http"
- "strconv"
- "github.com/go-chi/chi"
- sessionstore "github.com/porter-dev/porter/internal/auth"
- )
- type Auth struct {
- store *sessionstore.PGStore
- cookieName string
- }
- func NewAuth(
- store *sessionstore.PGStore,
- cookieName string,
- ) *Auth {
- return &Auth{store, cookieName}
- }
- // BasicAuthenticate just checks that a user is logged in
- func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if auth.isLoggedIn(r) {
- next.ServeHTTP(w, r)
- } else {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- return
- })
- }
- // DoesUserIDMatch checks the id URL parameter and verifies that it matches
- // the one stored in the session
- func (auth *Auth) DoesUserIDMatch(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
- if err == nil && auth.doesSessionMatchID(r, uint(id)) {
- next.ServeHTTP(w, r)
- } else {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- return
- })
- }
- // Helpers
- func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
- session, _ := auth.store.Get(r, auth.cookieName)
- if sessID, ok := session.Values["user_id"].(uint); !ok || sessID != id {
- return false
- }
- return true
- }
- func (auth *Auth) isLoggedIn(r *http.Request) bool {
- session, _ := auth.store.Get(r, auth.cookieName)
- if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
- return false
- }
- return true
- }
|