// Auth middleware adds auth from session and populates user info
Auth middleware adds auth from session and populates user info
// Auth middleware adds auth from session and populates user info
func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler {
	f := func(h http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			if a.basicDevUser(w, r) { 
				user := devUser
				r = rest.SetUserInfo(r, user)
				h.ServeHTTP(w, r)
				return
			}
			claims, err := a.JWTService.Get(r)
			if err != nil && reqAuth { 
				log.Printf("[DEBUG] failed auth, %s", err)
				http.Error(w, "Unauthorized", http.StatusUnauthorized)
				return
			}
			if err != nil { 
				h.ServeHTTP(w, r)
				return
			}
			if claims.User == nil && reqAuth {
				log.Print("[DEBUG] failed auth, no user info presented in the claim")
				http.Error(w, "Unauthorized", http.StatusUnauthorized)
				return
			}
			if claims.User != nil { 
				user := *claims.User
				user.Admin = isAdmin(user.ID, a.Admins) 
				if _, err := a.JWTService.Refresh(w, r); err != nil {
					log.Printf("[DEBUG] can't refresh jwt, %s", err)
				}
				r = rest.SetUserInfo(r, user)
			}
			h.ServeHTTP(w, r)
		}
		return http.HandlerFunc(fn)
	}
	return f
}
func ( a * Authenticator ) Auth ( reqAuth bool ) func ( http . Handler ) http . Handler { f := func ( h http . Handler ) http . Handler { fn := func ( w http . ResponseWriter , r * http . Request ) { if a . basicDevUser ( w , r ) { user := devUser r = rest . SetUserInfo ( r , user ) h . ServeHTTP ( w , r ) return } claims , err := a . JWTService . Get ( r ) if err != nil && reqAuth { log . Printf ( " " , err ) http . Error ( w , " " , http . StatusUnauthorized ) return } if err != nil { h . ServeHTTP ( w , r ) return } if claims . User == nil && reqAuth { log . Print ( " " ) http . Error ( w , " " , http . StatusUnauthorized ) return } if claims . User != nil { user := * claims . User user . Admin = isAdmin ( user . ID , a . Admins ) if _ , err := a . JWTService . Refresh ( w , r ) ; err != nil { log . Printf ( " " , err ) } r = rest . SetUserInfo ( r , user ) } h . ServeHTTP ( w , r ) } return http . HandlerFunc ( fn ) } return f }
Auth middleware adds auth from session and populates user info
Auth middleware adds auth from session and populates user info
// fail-back to dev user if enabled // in full auth lack of session causes Unauthorized // in anonymous mode just pass it to the next handler // if uinfo in token populate it to context // dbl-check for admin to reset admin flag even if token has it // refresh token if it close to expiration