Merge branch '54mir/authentication' of ssh://github.com/molecula/featurebase into 54mir/authentication

This commit is contained in:
Hoang Pham 2021-12-20 12:30:27 -06:00
commit fa96d39cd0
3 changed files with 28 additions and 12 deletions

View file

@ -131,13 +131,14 @@ func (a *Auth) Redirect(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
token, err := a.getToken(code)
if err != nil {
errors.Wrap(err, "getting token")
http.Redirect(w, r, "/login", http.StatusUnauthorized)
http.Error(w, "Bad Request: 400", http.StatusBadRequest)
return
}
cv, err := a.newCookieValue(token)
if err != nil {
http.Error(w, "authenticating", http.StatusBadRequest)
if err != nil || cv == nil {
http.Error(w, "Bad Request: 400", http.StatusBadRequest)
return
}
a.setCookie(w, cv)
@ -181,8 +182,6 @@ func (a *Auth) newCookieValue(token *oauth2.Token) (*CookieValue, error) {
}
// not needed at this point in the logic and makes the encoded cookie too large
token.AccessToken = ""
// mannually setting expiry for testing ... REMOVE
token.Expiry = time.Now().Add(time.Second * time.Duration(30))
return &CookieValue{
UserID: claims["oid"].(string),
UserName: claims["name"].(string),

View file

@ -3369,7 +3369,9 @@ func (h *Handler) handlePostRestore(w http.ResponseWriter, r *http.Request) {
func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) {
if h.auth == nil {
http.Error(w, "Trying to login but authentication is off.", http.StatusBadRequest)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("Auth Off")) //nolint:errcheck
return
}
@ -3378,15 +3380,23 @@ func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) {
func (h *Handler) handleRedirect(w http.ResponseWriter, r *http.Request) {
if h.auth == nil {
http.Error(w, "Authentication is off.", http.StatusBadRequest)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("Auth Off")) //nolint:errcheck
return
}
h.auth.Redirect(w, r)
}
func (h *Handler) handleCheckAuthentication(w http.ResponseWriter, r *http.Request) {
if !validHeaderAcceptJSON(r.Header) {
http.Error(w, "JSON only acceptable response", http.StatusNotAcceptable)
return
}
if h.auth == nil {
http.Error(w, "Trying to authenticate but authentication is off.", http.StatusBadRequest)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("Auth Off")) //nolint:errcheck
return
}
groups, err := h.auth.Authenticate(w, r)
@ -3402,8 +3412,14 @@ func (h *Handler) handleCheckAuthentication(w http.ResponseWriter, r *http.Reque
}
func (h *Handler) handleUserInfo(w http.ResponseWriter, r *http.Request) {
if !validHeaderAcceptJSON(r.Header) {
http.Error(w, "JSON only acceptable response", http.StatusNotAcceptable)
return
}
if h.auth == nil {
http.Error(w, "Authentication is off.", http.StatusBadRequest)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("Auth Off")) //nolint:errcheck
return
}
if err := json.NewEncoder(w).Encode(h.auth.GetUserInfo(r)); err != nil {
@ -3413,7 +3429,9 @@ func (h *Handler) handleUserInfo(w http.ResponseWriter, r *http.Request) {
func (h *Handler) handleLogout(w http.ResponseWriter, r *http.Request) {
if h.auth == nil {
http.Error(w, "Trying to log out but authentication is off.", http.StatusBadRequest)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("Auth Off")) //nolint:errcheck
return
}
h.auth.Logout(w, r)

View file

@ -532,7 +532,6 @@ func (m *Command) SetupServer() error {
}
m.logger.Infof("Before Handler %+v", m.auth)
m.Handler, err = http.NewHandler(
http.OptHandlerAllowedOrigins(m.Config.Handler.AllowedOrigins),
http.OptHandlerAPI(m.API),