github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/service/user.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/evergreen-ci/evergreen"
     8  	"github.com/evergreen-ci/evergreen/model"
     9  	"github.com/evergreen-ci/evergreen/model/user"
    10  	"github.com/evergreen-ci/evergreen/util"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  func (uis *UIServer) loginPage(w http.ResponseWriter, r *http.Request) {
    15  	if uis.UserManager.IsRedirect() {
    16  		http.Redirect(w, r, "/login/redirect", http.StatusFound)
    17  	}
    18  	uis.WriteHTML(w, http.StatusOK, nil, "base", "login.html", "base_angular.html")
    19  }
    20  
    21  func (uis *UIServer) setLoginToken(token string, w http.ResponseWriter) {
    22  	authTokenCookie := &http.Cookie{
    23  		Name:     evergreen.AuthTokenCookie,
    24  		Value:    token,
    25  		HttpOnly: true,
    26  		Secure:   uis.Settings.Ui.SecureCookies,
    27  		Path:     "/",
    28  	}
    29  	http.SetCookie(w, authTokenCookie)
    30  }
    31  
    32  func clearSession(w http.ResponseWriter) {
    33  	authTokenCookie := &http.Cookie{
    34  		Name:   evergreen.AuthTokenCookie,
    35  		Value:  "",
    36  		MaxAge: -1,
    37  		Path:   "/",
    38  	}
    39  	http.SetCookie(w, authTokenCookie)
    40  }
    41  
    42  func (uis *UIServer) login(w http.ResponseWriter, r *http.Request) {
    43  	creds := struct {
    44  		Username string `json:"username"`
    45  		Password string `json:"password"`
    46  	}{}
    47  
    48  	if err := util.ReadJSONInto(util.NewRequestReader(r), &creds); err != nil {
    49  		http.Error(w, fmt.Sprintf("Invalid JSON: %v", err), http.StatusBadRequest)
    50  		return
    51  	}
    52  
    53  	if creds.Username == "" || creds.Password == "" {
    54  		http.Error(w, fmt.Sprintf("Username and password are required"), http.StatusBadRequest)
    55  		return
    56  	}
    57  
    58  	token, err := uis.UserManager.CreateUserToken(creds.Username, creds.Password)
    59  	if err != nil {
    60  		http.Error(w, "Invalid username/password", http.StatusUnauthorized)
    61  		return
    62  	}
    63  	uis.setLoginToken(token, w)
    64  	uis.WriteJSON(w, http.StatusOK, map[string]string{})
    65  }
    66  
    67  func (uis *UIServer) logout(w http.ResponseWriter, r *http.Request) {
    68  	clearSession(w)
    69  	loginURL := fmt.Sprintf("%v/login", uis.RootURL)
    70  	http.Redirect(w, r, loginURL, http.StatusFound)
    71  }
    72  
    73  func (uis *UIServer) newAPIKey(w http.ResponseWriter, r *http.Request) {
    74  	currentUser := MustHaveUser(r)
    75  	newKey := util.RandomString()
    76  	if err := model.SetUserAPIKey(currentUser.Id, newKey); err != nil {
    77  		uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "failed saving key"))
    78  		return
    79  	}
    80  	uis.WriteJSON(w, http.StatusOK, struct {
    81  		Key string `json:"key"`
    82  	}{newKey})
    83  }
    84  
    85  func (uis *UIServer) userSettingsPage(w http.ResponseWriter, r *http.Request) {
    86  	currentUser := MustHaveUser(r)
    87  	projCtx := MustHaveProjectContext(r)
    88  
    89  	settingsData := currentUser.Settings
    90  	flashes := PopFlashes(uis.CookieStore, r, w)
    91  
    92  	type confFile struct {
    93  		User    string `json:"user"`
    94  		APIKey  string `json:"api_key"`
    95  		APIHost string `json:"api_server_host"`
    96  		UIHost  string `json:"ui_server_host"`
    97  	}
    98  	exampleConf := confFile{currentUser.Id, currentUser.APIKey, uis.Settings.ApiUrl + "/api", uis.Settings.Ui.Url}
    99  
   100  	uis.WriteHTML(w, http.StatusOK, struct {
   101  		ProjectData projectContext
   102  		Data        user.UserSettings
   103  		User        *user.DBUser
   104  		Config      confFile
   105  		Binaries    []evergreen.ClientBinary
   106  		Flashes     []interface{}
   107  	}{projCtx, settingsData, currentUser, exampleConf,
   108  		uis.clientConfig.ClientBinaries, flashes},
   109  		"base", "settings.html", "base_angular.html", "menu.html")
   110  }
   111  
   112  func (uis *UIServer) userSettingsModify(w http.ResponseWriter, r *http.Request) {
   113  	currentUser := MustHaveUser(r)
   114  	userSettings := user.UserSettings{}
   115  
   116  	if err := util.ReadJSONInto(util.NewRequestReader(r), &userSettings); err != nil {
   117  		uis.LoggedError(w, r, http.StatusBadRequest, err)
   118  		return
   119  	}
   120  
   121  	if err := model.SaveUserSettings(currentUser.Username(), userSettings); err != nil {
   122  		uis.LoggedError(w, r, http.StatusInternalServerError,
   123  			errors.Wrap(err, "Error saving user settings"))
   124  		return
   125  	}
   126  
   127  	PushFlash(uis.CookieStore, r, w, NewSuccessFlash("Settings were saved."))
   128  	uis.WriteJSON(w, http.StatusOK, "Updated user settings successfully")
   129  }