github.com/miguelangel-nubla/oauth2commander@v0.3.7/oauth2server/middleware.go (about)

     1  package oauth2server
     2  
     3  import "net/http"
     4  
     5  func (app *Oauth2Commander) AuthMiddleware(requiredGroups []string, next http.HandlerFunc) http.HandlerFunc {
     6  	return func(w http.ResponseWriter, r *http.Request) {
     7  		userInfo := app.auth(r)
     8  		if userInfo == nil {
     9  			app.oauth2Start(w, r, r.URL.String())
    10  			return
    11  		}
    12  
    13  		if !app.requireGroups(userInfo, requiredGroups) {
    14  			http.Error(w, "Unauthorized: insufficient group permissions", http.StatusUnauthorized)
    15  			return
    16  		}
    17  		next.ServeHTTP(w, r)
    18  	}
    19  }
    20  
    21  func (a *Oauth2Commander) SessionMiddleware(handler http.Handler) http.Handler {
    22  	return a.sessionManager.LoadAndSave(handler)
    23  }