From 0568006a2789037c58e8660514e7df345383c9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 28 Oct 2020 14:35:41 +0100 Subject: [PATCH 01/13] Auth WIP --- server/model/user.go | 15 ++ server/services/auth/authentication.go | 14 ++ server/services/auth/password.go | 126 +++++++++++++++ server/services/auth/password_test.go | 166 ++++++++++++++++++++ server/services/auth/request_parser.go | 67 ++++++++ server/services/auth/request_parser_test.go | 48 ++++++ server/services/store/sqlstore/user.go | 48 ++++++ 7 files changed, 484 insertions(+) create mode 100644 server/model/user.go create mode 100644 server/services/auth/authentication.go create mode 100644 server/services/auth/password.go create mode 100644 server/services/auth/password_test.go create mode 100644 server/services/auth/request_parser.go create mode 100644 server/services/auth/request_parser_test.go create mode 100644 server/services/store/sqlstore/user.go diff --git a/server/model/user.go b/server/model/user.go new file mode 100644 index 000000000..0b9f1273b --- /dev/null +++ b/server/model/user.go @@ -0,0 +1,15 @@ +package model + +type User struct { + ID string `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + Password string `json:"-"` + MfaSecret string `json:"-"` + AuthService string `json:"-"` + AuthData string `json:"-"` + Props map[string]interface{} `json:"props"` + CreateAt int64 `json:"create_at,omitempty"` + UpdateAt int64 `json:"update_at,omitempty"` + DeleteAt int64 `json:"delete_at"` +} diff --git a/server/services/auth/authentication.go b/server/services/auth/authentication.go new file mode 100644 index 000000000..519c4f7e3 --- /dev/null +++ b/server/services/auth/authentication.go @@ -0,0 +1,14 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package auth + +type AuthService struct { + passwordSettings PasswordSettings +} + +func New(passwordSettings PasswordSettings) *AuthService { + return &AuthService{ + passwordSettings: passwordSettings, + } +} diff --git a/server/services/auth/password.go b/server/services/auth/password.go new file mode 100644 index 000000000..e5925ad06 --- /dev/null +++ b/server/services/auth/password.go @@ -0,0 +1,126 @@ +package auth + +import ( + "fmt" + "math/rand" + "strings" + "time" + + "golang.org/x/crypto/bcrypt" +) + +var passwordRandomSource = rand.NewSource(time.Now().Unix()) + +const ( + PasswordMaximumLength = 64 + PasswordSpecialChars = "!\"\\#$%&'()*+,-./:;<=>?@[]^_`|~" + PasswordNumbers = "0123456789" + PasswordUpperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + PasswordLowerCaseLetters = "abcdefghijklmnopqrstuvwxyz" + PasswordAllChars = PasswordSpecialChars + PasswordNumbers + PasswordUpperCaseLetters + PasswordLowerCaseLetters + + InvalidLowercasePassword = "lowercase" + InvalidMinLengthPassword = "min-length" + InvalidMaxLengthPassword = "max-length" + InvalidNumberPassword = "number" + InvalidUppercasePassword = "uppercase" + InvalidSymbolPassword = "symbol" +) + +// HashPassword generates a hash using the bcrypt.GenerateFromPassword +func HashPassword(password string) string { + hash, err := bcrypt.GenerateFromPassword([]byte(password), 10) + if err != nil { + panic(err) + } + + return string(hash) +} + +// ComparePassword compares the hash +func ComparePassword(hash string, password string) bool { + + if len(password) == 0 || len(hash) == 0 { + return false + } + + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) + return err == nil +} + +func GeneratePassword(minimumLength int) string { + r := rand.New(passwordRandomSource) + + // Make sure we are guaranteed at least one of each type to meet any possible password complexity requirements. + password := string([]rune(PasswordUpperCaseLetters)[r.Intn(len(PasswordUpperCaseLetters))]) + + string([]rune(PasswordNumbers)[r.Intn(len(PasswordNumbers))]) + + string([]rune(PasswordLowerCaseLetters)[r.Intn(len(PasswordLowerCaseLetters))]) + + string([]rune(PasswordSpecialChars)[r.Intn(len(PasswordSpecialChars))]) + + for len(password) < minimumLength { + i := r.Intn(len(PasswordAllChars)) + password = password + string([]rune(PasswordAllChars)[i]) + } + + return password +} + +type InvalidPasswordError struct { + FailingCriterias []string +} + +func (ipe *InvalidPasswordError) Error() string { + return fmt.Sprintf("invalid password, failing criterias: %s", strings.Join(ipe.FailingCriterias, ", ")) +} + +type PasswordSettings struct { + MinimumLength int + Lowercase bool + Number bool + Uppercase bool + Symbol bool +} + +func (as *AuthService) IsPasswordValid(password string) error { + err := &InvalidPasswordError{ + FailingCriterias: []string{}, + } + + if len(password) < as.passwordSettings.MinimumLength { + err.FailingCriterias = append(err.FailingCriterias, InvalidMinLengthPassword) + } + + if len(password) > PasswordMaximumLength { + err.FailingCriterias = append(err.FailingCriterias, InvalidMaxLengthPassword) + } + + if as.passwordSettings.Lowercase { + if !strings.ContainsAny(password, PasswordLowerCaseLetters) { + err.FailingCriterias = append(err.FailingCriterias, InvalidLowercasePassword) + } + } + + if as.passwordSettings.Uppercase { + if !strings.ContainsAny(password, PasswordUpperCaseLetters) { + err.FailingCriterias = append(err.FailingCriterias, InvalidUppercasePassword) + } + } + + if as.passwordSettings.Number { + if !strings.ContainsAny(password, PasswordNumbers) { + err.FailingCriterias = append(err.FailingCriterias, InvalidNumberPassword) + } + } + + if as.passwordSettings.Symbol { + if !strings.ContainsAny(password, PasswordSpecialChars) { + err.FailingCriterias = append(err.FailingCriterias, InvalidSymbolPassword) + } + } + + if len(err.FailingCriterias) > 0 { + return err + } + + return nil +} diff --git a/server/services/auth/password_test.go b/server/services/auth/password_test.go new file mode 100644 index 000000000..eb5af8e7f --- /dev/null +++ b/server/services/auth/password_test.go @@ -0,0 +1,166 @@ +package auth + +import ( + "math/rand" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPasswordHash(t *testing.T) { + hash := HashPassword("Test") + + assert.True(t, ComparePassword(hash, "Test"), "Passwords don't match") + assert.False(t, ComparePassword(hash, "Test2"), "Passwords should not have matched") +} + +func TestGeneratePassword(t *testing.T) { + passwordRandomSource = rand.NewSource(12345) + + t.Run("Should be the minimum length or 4, whichever is less", func(t *testing.T) { + password1 := GeneratePassword(5) + assert.Len(t, password1, 5) + password2 := GeneratePassword(10) + assert.Len(t, password2, 10) + password3 := GeneratePassword(1) + assert.Len(t, password3, 4) + }) + + t.Run("Should contain at least one of symbols, upper case, lower case and numbers", func(t *testing.T) { + password := GeneratePassword(4) + require.Len(t, password, 4) + assert.Contains(t, []rune(PasswordUpperCaseLetters), []rune(password)[0]) + assert.Contains(t, []rune(PasswordNumbers), []rune(password)[1]) + assert.Contains(t, []rune(PasswordLowerCaseLetters), []rune(password)[2]) + assert.Contains(t, []rune(PasswordSpecialChars), []rune(password)[3]) + }) +} + +func TestIsPasswordValidWithSettings(t *testing.T) { + for name, tc := range map[string]struct { + Password string + Settings PasswordSettings + ExpectedFailingCriterias []string + }{ + "Short": { + Password: strings.Repeat("x", 3), + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: false, + Uppercase: false, + Number: false, + Symbol: false, + }, + }, + "Long": { + Password: strings.Repeat("x", PasswordMaximumLength), + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: false, + Uppercase: false, + Number: false, + Symbol: false, + }, + }, + "TooShort": { + Password: strings.Repeat("x", 2), + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: false, + Uppercase: false, + Number: false, + Symbol: false, + }, + ExpectedFailingCriterias: []string{"min-length"}, + }, + "TooLong": { + Password: strings.Repeat("x", PasswordMaximumLength+1), + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: false, + Uppercase: false, + Number: false, + Symbol: false, + }, + ExpectedFailingCriterias: []string{"max-length"}, + }, + "MissingLower": { + Password: "AAAAAAAAAAASD123!@#", + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: true, + Uppercase: false, + Number: false, + Symbol: false, + }, + ExpectedFailingCriterias: []string{"lowercase"}, + }, + "MissingUpper": { + Password: "aaaaaaaaaaaaasd123!@#", + Settings: PasswordSettings{ + MinimumLength: 3, + Uppercase: true, + Lowercase: false, + Number: false, + Symbol: false, + }, + ExpectedFailingCriterias: []string{"uppercase"}, + }, + "MissingNumber": { + Password: "asasdasdsadASD!@#", + Settings: PasswordSettings{ + MinimumLength: 3, + Number: true, + Lowercase: false, + Uppercase: false, + Symbol: false, + }, + ExpectedFailingCriterias: []string{"number"}, + }, + "MissingSymbol": { + Password: "asdasdasdasdasdASD123", + Settings: PasswordSettings{ + MinimumLength: 3, + Symbol: true, + Lowercase: false, + Uppercase: false, + Number: false, + }, + ExpectedFailingCriterias: []string{"symbol"}, + }, + "MissingMultiple": { + Password: "asdasdasdasdasdasd", + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: true, + Uppercase: true, + Number: true, + Symbol: true, + }, + ExpectedFailingCriterias: []string{"uppercase", "number", "symbol"}, + }, + "Everything": { + Password: "asdASD!@#123", + Settings: PasswordSettings{ + MinimumLength: 3, + Lowercase: true, + Uppercase: true, + Number: true, + Symbol: true, + }, + }, + } { + t.Run(name, func(t *testing.T) { + as := New(tc.Settings) + err := as.IsPasswordValid(tc.Password) + if len(tc.ExpectedFailingCriterias) == 0 { + assert.NoError(t, err) + } else { + require.Error(t, err) + assert.Equal(t, tc.ExpectedFailingCriterias, err.(*InvalidPasswordError).FailingCriterias) + } + }) + } +} diff --git a/server/services/auth/request_parser.go b/server/services/auth/request_parser.go new file mode 100644 index 000000000..a0f073da1 --- /dev/null +++ b/server/services/auth/request_parser.go @@ -0,0 +1,67 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package auth + +import ( + "net/http" + "strings" +) + +const ( + HEADER_TOKEN = "token" + HEADER_AUTH = "Authorization" + HEADER_BEARER = "BEARER" + SESSION_COOKIE_TOKEN = "MMAUTHTOKEN" +) + +type TokenLocation int + +const ( + TokenLocationNotFound TokenLocation = iota + TokenLocationHeader + TokenLocationCookie + TokenLocationQueryString +) + +func (tl TokenLocation) String() string { + switch tl { + case TokenLocationNotFound: + return "Not Found" + case TokenLocationHeader: + return "Header" + case TokenLocationCookie: + return "Cookie" + case TokenLocationQueryString: + return "QueryString" + default: + return "Unknown" + } +} + +func ParseAuthTokenFromRequest(r *http.Request) (string, TokenLocation) { + authHeader := r.Header.Get(HEADER_AUTH) + + // Attempt to parse the token from the cookie + if cookie, err := r.Cookie(SESSION_COOKIE_TOKEN); err == nil { + return cookie.Value, TokenLocationCookie + } + + // Parse the token from the header + if len(authHeader) > 6 && strings.ToUpper(authHeader[0:6]) == HEADER_BEARER { + // Default session token + return authHeader[7:], TokenLocationHeader + } + + if len(authHeader) > 5 && strings.ToLower(authHeader[0:5]) == HEADER_TOKEN { + // OAuth token + return authHeader[6:], TokenLocationHeader + } + + // Attempt to parse token out of the query string + if token := r.URL.Query().Get("access_token"); token != "" { + return token, TokenLocationQueryString + } + + return "", TokenLocationNotFound +} diff --git a/server/services/auth/request_parser_test.go b/server/services/auth/request_parser_test.go new file mode 100644 index 000000000..fceeed8f4 --- /dev/null +++ b/server/services/auth/request_parser_test.go @@ -0,0 +1,48 @@ +package auth + +import ( + "net/http" + "net/http/httptest" + "strconv" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestParseAuthTokenFromRequest(t *testing.T) { + cases := []struct { + header string + cookie string + query string + expectedToken string + expectedLocation TokenLocation + }{ + {"", "", "", "", TokenLocationNotFound}, + {"token mytoken", "", "", "mytoken", TokenLocationHeader}, + {"BEARER mytoken", "", "", "mytoken", TokenLocationHeader}, + {"", "mytoken", "", "mytoken", TokenLocationCookie}, + {"", "", "mytoken", "mytoken", TokenLocationQueryString}, + } + + for testnum, tc := range cases { + pathname := "/test/here" + if tc.query != "" { + pathname += "?access_token=" + tc.query + } + req := httptest.NewRequest("GET", pathname, nil) + if tc.header != "" { + req.Header.Add(HEADER_AUTH, tc.header) + } + if tc.cookie != "" { + req.AddCookie(&http.Cookie{ + Name: SESSION_COOKIE_TOKEN, + Value: tc.cookie, + }) + } + + token, location := ParseAuthTokenFromRequest(req) + + require.Equal(t, tc.expectedToken, token, "Wrong token on test "+strconv.Itoa(testnum)) + require.Equal(t, tc.expectedLocation, location, "Wrong location on test "+strconv.Itoa(testnum)) + } +} diff --git a/server/services/store/sqlstore/user.go b/server/services/store/sqlstore/user.go new file mode 100644 index 000000000..ee380d5e4 --- /dev/null +++ b/server/services/store/sqlstore/user.go @@ -0,0 +1,48 @@ +package sqlstore + +import ( + "time" + + "github.com/mattermost/mattermost-octo-tasks/server/model" + + sq "github.com/Masterminds/squirrel" +) + +func (s *SQLStore) getUserByCondition(condition sq.Eq) (*model.User, error) { + query := s.getQueryBuilder(). + Select("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at"). + From("users"). + Where(condition) + row := query.QueryRow() + user := model.User{} + + err := row.Scan(&user.ID, &user.Username, &user.Email, &user.Password, &user.MfaSecret, &user.AuthService, &user.AuthData, &user.Props, &user.CreateAt, &user.UpdateAt, &user.DeleteAt) + if err != nil { + return nil, err + } + + return &user, nil +} + +func (s *SQLStore) GetUserById(userID string) (*model.User, error) { + return s.getUserByCondition(sq.Eq{"id": userID}) +} + +func (s *SQLStore) GetUserByEmail(email string) (*model.User, error) { + return s.getUserByCondition(sq.Eq{"email": email}) +} + +func (s *SQLStore) GetUserByUsername(username string) (*model.User, error) { + return s.getUserByCondition(sq.Eq{"username": username}) +} + +func (s *SQLStore) CreateUser(user *model.User) error { + now := time.Now().Unix() + + query := s.getQueryBuilder().Insert("users"). + Columns("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at"). + Values(user.ID, user.Username, user.Email, user.Password, user.MfaSecret, user.AuthService, user.AuthData, user.Props, now, now, 0) +} + +func (s *SQLStore) UpdateUser(user *model.User) error { +} From 35ebd44d242be54af5658b015745784458e4b225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 6 Nov 2020 16:46:35 +0100 Subject: [PATCH 02/13] More work on authentication --- config.json | 3 +- server/api/api.go | 4 + server/api/auth.go | 104 +++++++++++++++++++++++++ server/app/auth.go | 83 ++++++++++++++++++++ server/go.mod | 3 + server/go.sum | 1 + server/services/auth/password.go | 12 +-- server/services/auth/token.go | 20 +++++ server/services/config/config.go | 1 + server/services/store/sqlstore/user.go | 34 +++++++- server/services/store/store.go | 5 ++ 11 files changed, 261 insertions(+), 9 deletions(-) create mode 100644 server/api/auth.go create mode 100644 server/app/auth.go create mode 100644 server/services/auth/token.go diff --git a/config.json b/config.json index c15fb9379..ab3d8b48a 100644 --- a/config.json +++ b/config.json @@ -8,5 +8,6 @@ "webpath": "./webapp/pack", "filespath": "./files", "telemetry": true, - "webhook_update": [] + "webhook_update": [], + "secret": "this-is-a-secret-string" } diff --git a/server/api/api.go b/server/api/api.go index a33e61837..e80ae55db 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -36,6 +36,9 @@ func (a *API) RegisterRoutes(r *mux.Router) { r.HandleFunc("/api/v1/blocks/{blockID}", a.handleDeleteBlock).Methods("DELETE") r.HandleFunc("/api/v1/blocks/{blockID}/subtree", a.handleGetSubTree).Methods("GET") + r.HandleFunc("/api/v1/login", a.handleLogin).Methods("POST") + r.HandleFunc("/api/v1/register", a.handleRegister).Methods("POST") + r.HandleFunc("/api/v1/files", a.handleUploadFile).Methods("POST") r.HandleFunc("/files/{filename}", a.handleServeFile).Methods("GET") @@ -293,6 +296,7 @@ func jsonBytesResponse(w http.ResponseWriter, code int, json []byte) { func errorResponse(w http.ResponseWriter, code int, message map[string]string) { log.Printf("%d ERROR", code) + log.Printf("%v ERROR", message) data, err := json.Marshal(message) if err != nil { data = []byte("{}") diff --git a/server/api/auth.go b/server/api/auth.go new file mode 100644 index 000000000..0f99b099f --- /dev/null +++ b/server/api/auth.go @@ -0,0 +1,104 @@ +package api + +import ( + "encoding/json" + "errors" + "io/ioutil" + "log" + "net/http" + "strings" +) + +type LoginData struct { + Type string `json:"type"` + Username string `json:"username"` + Email string `json:"email"` + Password string `json:"password"` + MfaToken string `json:"mfa_token"` +} + +type RegisterData struct { + Username string `json:"username"` + Email string `json:"email"` + Password string `json:"password"` +} + +func (rd *RegisterData) IsValid() error { + if rd.Username == "" { + return errors.New("Username is required") + } + if rd.Email == "" { + return errors.New("Email is required") + } + if !strings.Contains(rd.Email, "@") { + return errors.New("Invalid email format") + } + if !strings.Contains(rd.Password, "") { + return errors.New("Password is required") + } + return nil +} + +func (a *API) handleLogin(w http.ResponseWriter, r *http.Request) { + requestBody, err := ioutil.ReadAll(r.Body) + if err != nil { + errorResponse(w, http.StatusInternalServerError, nil) + + return + } + + var loginData LoginData + err = json.Unmarshal(requestBody, &loginData) + if err != nil { + errorResponse(w, http.StatusInternalServerError, nil) + return + } + + if loginData.Type == "normal" { + jwtToken, err := a.app().Login(loginData.Username, loginData.Email, loginData.Password, loginData.MfaToken) + if err != nil { + errorResponse(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + json, err := json.Marshal(jwtToken) + if err != nil { + log.Printf(`ERROR json.Marshal: %v`, r) + errorResponse(w, http.StatusInternalServerError, nil) + return + } + + jsonBytesResponse(w, http.StatusOK, json) + } + + errorResponse(w, http.StatusInternalServerError, map[string]string{"error": "Unknown login type"}) + return +} + +func (a *API) handleRegister(w http.ResponseWriter, r *http.Request) { + requestBody, err := ioutil.ReadAll(r.Body) + if err != nil { + errorResponse(w, http.StatusInternalServerError, nil) + + return + } + + var registerData RegisterData + err = json.Unmarshal(requestBody, ®isterData) + if err != nil { + errorResponse(w, http.StatusInternalServerError, nil) + return + } + + if err = registerData.IsValid(); err != nil { + errorResponse(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + + err = a.app().RegisterUser(registerData.Username, registerData.Email, registerData.Password) + if err != nil { + errorResponse(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + jsonBytesResponse(w, http.StatusOK, nil) + return +} diff --git a/server/app/auth.go b/server/app/auth.go new file mode 100644 index 000000000..a12466747 --- /dev/null +++ b/server/app/auth.go @@ -0,0 +1,83 @@ +package app + +import ( + "github.com/google/uuid" + "github.com/mattermost/mattermost-octo-tasks/server/model" + "github.com/mattermost/mattermost-octo-tasks/server/services/auth" + + "github.com/pkg/errors" +) + +func (a *App) Login(username string, email string, password string, mfaToken string) (string, error) { + var user *model.User + if username != "" { + var err error + user, err = a.store.GetUserByUsername(username) + if err != nil { + return "", errors.Wrap(err, "invalid username or password") + } + } + + if user == nil && email != "" { + var err error + user, err = a.store.GetUserByEmail(email) + if err != nil { + return "", errors.Wrap(err, "invalid username or password") + } + } + if user == nil { + return "", errors.New("invalid username or password") + } + + if !auth.ComparePassword(user.Password, password) { + return "", errors.New("invalid username or password") + } + + // TODO: MFA verification + return auth.CreateToken(user.ID, a.config.Secret) +} + +func (a *App) RegisterUser(username string, email string, password string) error { + var user *model.User + if username != "" { + var err error + user, err = a.store.GetUserByUsername(username) + if err == nil && user != nil { + return errors.Wrap(err, "The username already exists") + } + } + + if user == nil && email != "" { + var err error + user, err = a.store.GetUserByEmail(email) + if err == nil && user != nil { + return errors.Wrap(err, "The email already exists") + } + } + + // TODO: Move this into the config + passwordSettings := auth.PasswordSettings{ + MinimumLength: 6, + } + + err := auth.IsPasswordValid(password, passwordSettings) + if err != nil { + return errors.Wrap(err, "Invalid password") + } + + err = a.store.CreateUser(&model.User{ + ID: uuid.New().String(), + Username: username, + Email: email, + Password: auth.HashPassword(password), + MfaSecret: "", + AuthService: "", + AuthData: "", + Props: map[string]interface{}{}, + }) + if err != nil { + return errors.Wrap(err, "Unable to create the new user") + } + + return nil +} diff --git a/server/go.mod b/server/go.mod index 01bce9264..60b864b45 100644 --- a/server/go.mod +++ b/server/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/Masterminds/squirrel v1.4.0 + github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-ldap/ldap v3.0.3+incompatible // indirect github.com/golang-migrate/migrate v3.5.4+incompatible github.com/golang-migrate/migrate/v4 v4.13.0 @@ -20,10 +21,12 @@ require ( github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/nicksnyder/go-i18n v1.10.1 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/pkg/errors v0.9.1 github.com/rudderlabs/analytics-go v3.2.1+incompatible github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.6.1 go.uber.org/zap v1.15.0 + golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de golang.org/x/tools v0.0.0-20201017001424-6003fad69a88 // indirect gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect diff --git a/server/go.sum b/server/go.sum index 658403bf3..e734aa074 100644 --- a/server/go.sum +++ b/server/go.sum @@ -186,6 +186,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20200620013148-b91950f658ec/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/dgoogauth v0.0.0-20190221195224-5a805980a5f3 h1:AqeKSZIG/NIC75MNQlPy/LM3LxfpLwahICJBHwSMFNc= github.com/dgryski/dgoogauth v0.0.0-20190221195224-5a805980a5f3/go.mod h1:hEfFauPHz7+NnjR/yHJGhrKo1Za+zStgwUETx3yzqgY= diff --git a/server/services/auth/password.go b/server/services/auth/password.go index e5925ad06..b9b31ee68 100644 --- a/server/services/auth/password.go +++ b/server/services/auth/password.go @@ -81,12 +81,12 @@ type PasswordSettings struct { Symbol bool } -func (as *AuthService) IsPasswordValid(password string) error { +func IsPasswordValid(password string, settings PasswordSettings) error { err := &InvalidPasswordError{ FailingCriterias: []string{}, } - if len(password) < as.passwordSettings.MinimumLength { + if len(password) < settings.MinimumLength { err.FailingCriterias = append(err.FailingCriterias, InvalidMinLengthPassword) } @@ -94,25 +94,25 @@ func (as *AuthService) IsPasswordValid(password string) error { err.FailingCriterias = append(err.FailingCriterias, InvalidMaxLengthPassword) } - if as.passwordSettings.Lowercase { + if settings.Lowercase { if !strings.ContainsAny(password, PasswordLowerCaseLetters) { err.FailingCriterias = append(err.FailingCriterias, InvalidLowercasePassword) } } - if as.passwordSettings.Uppercase { + if settings.Uppercase { if !strings.ContainsAny(password, PasswordUpperCaseLetters) { err.FailingCriterias = append(err.FailingCriterias, InvalidUppercasePassword) } } - if as.passwordSettings.Number { + if settings.Number { if !strings.ContainsAny(password, PasswordNumbers) { err.FailingCriterias = append(err.FailingCriterias, InvalidNumberPassword) } } - if as.passwordSettings.Symbol { + if settings.Symbol { if !strings.ContainsAny(password, PasswordSpecialChars) { err.FailingCriterias = append(err.FailingCriterias, InvalidSymbolPassword) } diff --git a/server/services/auth/token.go b/server/services/auth/token.go new file mode 100644 index 000000000..c62856936 --- /dev/null +++ b/server/services/auth/token.go @@ -0,0 +1,20 @@ +package auth + +import ( + "time" + + "github.com/dgrijalva/jwt-go" +) + +func CreateToken(userID string, appSecret string) (string, error) { + claims := jwt.MapClaims{} + claims["authorized"] = true + claims["user_id"] = userID + claims["exp"] = time.Now().Add(time.Minute * 15).Unix() + at := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + token, err := at.SignedString([]byte(appSecret)) + if err != nil { + return "", err + } + return token, nil +} diff --git a/server/services/config/config.go b/server/services/config/config.go index b63bf30a2..e33db199b 100644 --- a/server/services/config/config.go +++ b/server/services/config/config.go @@ -22,6 +22,7 @@ type Configuration struct { FilesPath string `json:"filespath" mapstructure:"filespath"` Telemetry bool `json:"telemetry" mapstructure:"telemetry"` WebhookUpdate []string `json:"webhook_update" mapstructure:"webhook_update"` + Secret string `json:"secret" mapstructure:"secret"` } // ReadConfigFile read the configuration from the filesystem. diff --git a/server/services/store/sqlstore/user.go b/server/services/store/sqlstore/user.go index ee380d5e4..5b82e10b4 100644 --- a/server/services/store/sqlstore/user.go +++ b/server/services/store/sqlstore/user.go @@ -1,6 +1,7 @@ package sqlstore import ( + "encoding/json" "time" "github.com/mattermost/mattermost-octo-tasks/server/model" @@ -16,7 +17,13 @@ func (s *SQLStore) getUserByCondition(condition sq.Eq) (*model.User, error) { row := query.QueryRow() user := model.User{} - err := row.Scan(&user.ID, &user.Username, &user.Email, &user.Password, &user.MfaSecret, &user.AuthService, &user.AuthData, &user.Props, &user.CreateAt, &user.UpdateAt, &user.DeleteAt) + var propsBytes []byte + err := row.Scan(&user.ID, &user.Username, &user.Email, &user.Password, &user.MfaSecret, &user.AuthService, &user.AuthData, &propsBytes, &user.CreateAt, &user.UpdateAt, &user.DeleteAt) + if err != nil { + return nil, err + } + + err = json.Unmarshal(propsBytes, &user.Props) if err != nil { return nil, err } @@ -39,10 +46,33 @@ func (s *SQLStore) GetUserByUsername(username string) (*model.User, error) { func (s *SQLStore) CreateUser(user *model.User) error { now := time.Now().Unix() + propsBytes, err := json.Marshal(user.Props) + if err != nil { + return err + } + query := s.getQueryBuilder().Insert("users"). Columns("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at"). - Values(user.ID, user.Username, user.Email, user.Password, user.MfaSecret, user.AuthService, user.AuthData, user.Props, now, now, 0) + Values(user.ID, user.Username, user.Email, user.Password, user.MfaSecret, user.AuthService, user.AuthData, propsBytes, now, now, 0) + + _, err = query.Exec() + return err } func (s *SQLStore) UpdateUser(user *model.User) error { + now := time.Now().Unix() + + propsBytes, err := json.Marshal(user.Props) + if err != nil { + return err + } + + query := s.getQueryBuilder().Update("users"). + Set("username", user.Username). + Set("email", user.Email). + Set("props", propsBytes). + Set("update_at", now) + + _, err = query.Exec() + return err } diff --git a/server/services/store/store.go b/server/services/store/store.go index 6d16a5552..00a560319 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -16,4 +16,9 @@ type Store interface { Shutdown() error GetSystemSettings() (map[string]string, error) SetSystemSetting(key string, value string) error + GetUserById(userID string) (*model.User, error) + GetUserByEmail(email string) (*model.User, error) + GetUserByUsername(username string) (*model.User, error) + CreateUser(user *model.User) error + UpdateUser(user *model.User) error } From a1008fee1c2d89d160e0cd06753afe917e30a09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 17 Nov 2020 15:43:56 +0100 Subject: [PATCH 03/13] Working on auth --- server/api/api.go | 2 +- server/services/auth/authentication.go | 14 ---- server/services/store/mockstore/mockstore.go | 76 ++++++++++++++++++- .../sqlstore/migrations/postgres/bindata.go | 24 ++++++ .../sqlstore/migrations/sqlite/bindata.go | 24 ++++++ 5 files changed, 123 insertions(+), 17 deletions(-) delete mode 100644 server/services/auth/authentication.go diff --git a/server/api/api.go b/server/api/api.go index e80ae55db..7f7d13b89 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -296,7 +296,7 @@ func jsonBytesResponse(w http.ResponseWriter, code int, json []byte) { func errorResponse(w http.ResponseWriter, code int, message map[string]string) { log.Printf("%d ERROR", code) - log.Printf("%v ERROR", message) + w.Header().Set("Content-Type", "application/json") data, err := json.Marshal(message) if err != nil { data = []byte("{}") diff --git a/server/services/auth/authentication.go b/server/services/auth/authentication.go deleted file mode 100644 index 519c4f7e3..000000000 --- a/server/services/auth/authentication.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. - -package auth - -type AuthService struct { - passwordSettings PasswordSettings -} - -func New(passwordSettings PasswordSettings) *AuthService { - return &AuthService{ - passwordSettings: passwordSettings, - } -} diff --git a/server/services/store/mockstore/mockstore.go b/server/services/store/mockstore/mockstore.go index fc0d53e46..c413b2265 100644 --- a/server/services/store/mockstore/mockstore.go +++ b/server/services/store/mockstore/mockstore.go @@ -5,10 +5,9 @@ package mockstore import ( - reflect "reflect" - gomock "github.com/golang/mock/gomock" model "github.com/mattermost/mattermost-octo-tasks/server/model" + reflect "reflect" ) // MockStore is a mock of Store interface @@ -34,6 +33,20 @@ func (m *MockStore) EXPECT() *MockStoreMockRecorder { return m.recorder } +// CreateUser mocks base method +func (m *MockStore) CreateUser(arg0 *model.User) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateUser", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateUser indicates an expected call of CreateUser +func (mr *MockStoreMockRecorder) CreateUser(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*MockStore)(nil).CreateUser), arg0) +} + // DeleteBlock mocks base method func (m *MockStore) DeleteBlock(arg0 string) error { m.ctrl.T.Helper() @@ -153,6 +166,51 @@ func (mr *MockStoreMockRecorder) GetSystemSettings() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSystemSettings", reflect.TypeOf((*MockStore)(nil).GetSystemSettings)) } +// GetUserByEmail mocks base method +func (m *MockStore) GetUserByEmail(arg0 string) (*model.User, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUserByEmail", arg0) + ret0, _ := ret[0].(*model.User) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetUserByEmail indicates an expected call of GetUserByEmail +func (mr *MockStoreMockRecorder) GetUserByEmail(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserByEmail", reflect.TypeOf((*MockStore)(nil).GetUserByEmail), arg0) +} + +// GetUserById mocks base method +func (m *MockStore) GetUserById(arg0 string) (*model.User, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUserById", arg0) + ret0, _ := ret[0].(*model.User) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetUserById indicates an expected call of GetUserById +func (mr *MockStoreMockRecorder) GetUserById(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserById", reflect.TypeOf((*MockStore)(nil).GetUserById), arg0) +} + +// GetUserByUsername mocks base method +func (m *MockStore) GetUserByUsername(arg0 string) (*model.User, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUserByUsername", arg0) + ret0, _ := ret[0].(*model.User) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetUserByUsername indicates an expected call of GetUserByUsername +func (mr *MockStoreMockRecorder) GetUserByUsername(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserByUsername", reflect.TypeOf((*MockStore)(nil).GetUserByUsername), arg0) +} + // InsertBlock mocks base method func (m *MockStore) InsertBlock(arg0 model.Block) error { m.ctrl.T.Helper() @@ -194,3 +252,17 @@ func (mr *MockStoreMockRecorder) Shutdown() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Shutdown", reflect.TypeOf((*MockStore)(nil).Shutdown)) } + +// UpdateUser mocks base method +func (m *MockStore) UpdateUser(arg0 *model.User) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateUser", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateUser indicates an expected call of UpdateUser +func (mr *MockStoreMockRecorder) UpdateUser(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateUser", reflect.TypeOf((*MockStore)(nil).UpdateUser), arg0) +} diff --git a/server/services/store/sqlstore/migrations/postgres/bindata.go b/server/services/store/sqlstore/migrations/postgres/bindata.go index 2d3d117bb..6eb4dcf8c 100644 --- a/server/services/store/sqlstore/migrations/postgres/bindata.go +++ b/server/services/store/sqlstore/migrations/postgres/bindata.go @@ -61,6 +61,24 @@ func _000002_system_settings_table_up_sql() ([]byte, error) { ) } +var __000003_users_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x02\x04\x00\x00\xff\xff\xcf\x0c\x8a\x87\x12\x00\x00\x00") + +func _000003_users_table_down_sql() ([]byte, error) { + return bindata_read( + __000003_users_table_down_sql, + "000003_users_table.down.sql", + ) +} + +var __000003_users_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\xcd\xcd\x4e\x85\x30\x10\x86\xe1\x75\x7b\x15\xb3\x84\x84\xc5\xf1\x24\xac\x5c\x15\xac\x5a\x7f\x80\x94\x6a\x64\x45\x26\x74\x8c\x4d\x40\x48\x5b\xf4\xf6\x0d\x31\xd1\x08\xce\xf2\x79\x93\xf9\x4a\x2d\x85\x91\x60\x44\xf1\x20\x41\x5d\x43\x55\x1b\x90\x2f\xaa\x35\x2d\xac\x81\x7c\x80\x84\x33\x67\xe1\x59\xe8\xf2\x56\xe8\xe4\xe2\x74\x4a\x33\xce\xb6\xf4\x8e\x13\xed\x9d\x26\x74\xe3\x0f\x9e\xf3\x7c\xc3\x05\x43\xf8\x9c\xfd\xfe\x09\x00\xc0\xf4\x8a\x7d\xa0\xc1\x53\xfc\x27\xe2\x1a\xdf\xfa\x40\xfe\xc3\x0d\xbf\x43\xe7\x3f\xd5\x62\xc4\xfd\x5c\xe3\xe7\x25\xc0\xf7\xdd\xb5\x75\x95\x71\x56\x7a\xc2\x48\x22\x6e\x54\xa8\x1b\x55\x99\x8c\xb3\xa7\xc5\x1e\xf1\x8a\x46\x3a\x60\xa3\xd5\xa3\xd0\x1d\xdc\xcb\x0e\x12\x67\x53\x9e\x5e\xf2\xaf\x00\x00\x00\xff\xff\xa3\x41\x36\x1b\x38\x01\x00\x00") + +func _000003_users_table_up_sql() ([]byte, error) { + return bindata_read( + __000003_users_table_up_sql, + "000003_users_table.up.sql", + ) +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -87,6 +105,8 @@ var _bindata = map[string]func() ([]byte, error){ "000001_init.up.sql": _000001_init_up_sql, "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, + "000003_users_table.down.sql": _000003_users_table_down_sql, + "000003_users_table.up.sql": _000003_users_table_up_sql, } // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. @@ -136,4 +156,8 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ }}, "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ }}, + "000003_users_table.down.sql": &_bintree_t{_000003_users_table_down_sql, map[string]*_bintree_t{ + }}, + "000003_users_table.up.sql": &_bintree_t{_000003_users_table_up_sql, map[string]*_bintree_t{ + }}, }} diff --git a/server/services/store/sqlstore/migrations/sqlite/bindata.go b/server/services/store/sqlstore/migrations/sqlite/bindata.go index 6895c5201..92ea4445f 100644 --- a/server/services/store/sqlstore/migrations/sqlite/bindata.go +++ b/server/services/store/sqlstore/migrations/sqlite/bindata.go @@ -61,6 +61,24 @@ func _000002_system_settings_table_up_sql() ([]byte, error) { ) } +var __000003_users_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x02\x04\x00\x00\xff\xff\xcf\x0c\x8a\x87\x12\x00\x00\x00") + +func _000003_users_table_down_sql() ([]byte, error) { + return bindata_read( + __000003_users_table_down_sql, + "000003_users_table.down.sql", + ) +} + +var __000003_users_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\xcd\xd1\x4b\x85\x30\x14\xc7\xf1\x67\xf7\x57\x9c\x47\x85\xfb\x70\x13\x7c\xea\x69\xda\xaa\x51\x99\xcc\x15\xfa\x24\x07\x77\xa2\x81\xa6\x6c\xb3\xfe\xfd\x18\x41\x91\xde\xf3\xf8\xf9\xc2\xf9\x55\x4a\x70\x2d\x40\xf3\xf2\x51\x80\xbc\x85\xfa\x59\x83\xe8\x64\xab\x5b\xd8\x3c\x39\x0f\x29\x4b\xac\x81\x57\xae\xaa\x7b\xae\xd2\xab\xf3\x39\x3b\xb1\x24\xa6\x0f\x9c\x69\xef\x34\xa3\x9d\x7e\x31\x2f\x8a\x88\x2b\x7a\xff\xb5\xb8\xfd\x13\x00\x80\xf9\x0d\x07\x4f\xa3\xa3\x70\x21\xe2\x16\xde\x07\x4f\xee\xd3\x8e\x7f\x43\xf9\xbf\x6a\x30\xe0\x7e\xae\x71\xcb\xea\xe1\xe7\xb4\xe8\xf4\x89\x25\x95\x23\x0c\xc4\x43\xa4\x52\xde\xc9\x3a\xe2\xcb\x6a\x8e\x78\x43\x13\x1d\xb0\x51\xf2\x89\xab\x1e\x1e\x44\x0f\xa9\x35\x19\xcb\xae\xd9\x77\x00\x00\x00\xff\xff\x8b\xb1\x5a\xe4\x38\x01\x00\x00") + +func _000003_users_table_up_sql() ([]byte, error) { + return bindata_read( + __000003_users_table_up_sql, + "000003_users_table.up.sql", + ) +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -87,6 +105,8 @@ var _bindata = map[string]func() ([]byte, error){ "000001_init.up.sql": _000001_init_up_sql, "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, + "000003_users_table.down.sql": _000003_users_table_down_sql, + "000003_users_table.up.sql": _000003_users_table_up_sql, } // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. @@ -136,4 +156,8 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ }}, "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ }}, + "000003_users_table.down.sql": &_bintree_t{_000003_users_table_down_sql, map[string]*_bintree_t{ + }}, + "000003_users_table.up.sql": &_bintree_t{_000003_users_table_up_sql, map[string]*_bintree_t{ + }}, }} From ea81d0493737c5273758ed26e58322f501be069b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 17 Nov 2020 19:44:13 +0100 Subject: [PATCH 04/13] Adding migration files for users tables --- .../postgres_files/000003_users_table.down.sql | 1 + .../postgres_files/000003_users_table.up.sql | 14 ++++++++++++++ .../sqlite_files/000003_users_table.down.sql | 1 + .../sqlite_files/000003_users_table.up.sql | 14 ++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql create mode 100644 server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql create mode 100644 server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql create mode 100644 server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql b/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql new file mode 100644 index 000000000..cc1f647d2 --- /dev/null +++ b/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql @@ -0,0 +1 @@ +DROP TABLE users; diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql b/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql new file mode 100644 index 000000000..68f6cde60 --- /dev/null +++ b/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS users ( + id VARCHAR(100), + username VARCHAR(100), + email VARCHAR(255), + password VARCHAR(100), + mfa_secret VARCHAR(100), + auth_service VARCHAR(20), + auth_data VARCHAR(255), + Props JSON, + CreateAt BIGINT, + UpdateAt BIGINT, + DeleteAt BIGINT, + PRIMARY KEY (id) +); diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql b/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql new file mode 100644 index 000000000..cc1f647d2 --- /dev/null +++ b/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql @@ -0,0 +1 @@ +DROP TABLE users; diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql b/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql new file mode 100644 index 000000000..1ada8c31e --- /dev/null +++ b/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS users ( + id VARCHAR(100), + username VARCHAR(100), + email VARCHAR(255), + password VARCHAR(100), + mfa_secret VARCHAR(100), + auth_service VARCHAR(20), + auth_data VARCHAR(255), + Props TEXT, + CreateAt BIGINT, + UpdateAt BIGINT, + DeleteAt BIGINT, + PRIMARY KEY (id) +); From 7382f9c55c3f63114602a3ade6d7c6a925e9e584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 2 Dec 2020 21:12:14 +0100 Subject: [PATCH 05/13] Adding sessions code --- server/api/api.go | 2 +- server/api/auth.go | 17 ++++ server/app/auth.go | 21 ++++- server/model/user.go | 7 ++ .../sqlstore/migrations/postgres/bindata.go | 24 +++--- .../postgres_files/000003_auth_table.down.sql | 2 + .../postgres_files/000003_auth_table.up.sql | 24 ++++++ .../000003_users_table.down.sql | 1 - .../postgres_files/000003_users_table.up.sql | 14 ---- .../sqlstore/migrations/sqlite/bindata.go | 24 +++--- .../sqlite_files/000003_auth_table.down.sql | 2 + .../sqlite_files/000003_auth_table.up.sql | 24 ++++++ .../sqlite_files/000003_users_table.down.sql | 1 - .../sqlite_files/000003_users_table.up.sql | 14 ---- server/services/store/sqlstore/session.go | 82 +++++++++++++++++++ server/services/store/store.go | 5 ++ 16 files changed, 208 insertions(+), 56 deletions(-) create mode 100644 server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.down.sql create mode 100644 server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.up.sql delete mode 100644 server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql delete mode 100644 server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql create mode 100644 server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.down.sql create mode 100644 server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.up.sql delete mode 100644 server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql delete mode 100644 server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql create mode 100644 server/services/store/sqlstore/session.go diff --git a/server/api/api.go b/server/api/api.go index bead83372..39a2f4454 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -32,7 +32,7 @@ func (a *API) app() *app.App { } func (a *API) RegisterRoutes(r *mux.Router) { - r.HandleFunc("/api/v1/blocks", a.handleGetBlocks).Methods("GET") + r.HandleFunc("/api/v1/blocks", a.sessionRequired(a.handleGetBlocks)).Methods("GET") r.HandleFunc("/api/v1/blocks", a.handlePostBlocks).Methods("POST") r.HandleFunc("/api/v1/blocks/{blockID}", a.handleDeleteBlock).Methods("DELETE") r.HandleFunc("/api/v1/blocks/{blockID}/subtree", a.handleGetSubTree).Methods("GET") diff --git a/server/api/auth.go b/server/api/auth.go index 0f99b099f..3b839cd96 100644 --- a/server/api/auth.go +++ b/server/api/auth.go @@ -1,12 +1,15 @@ package api import ( + "context" "encoding/json" "errors" "io/ioutil" "log" "net/http" "strings" + + "github.com/mattermost/mattermost-octo-tasks/server/services/auth" ) type LoginData struct { @@ -68,6 +71,7 @@ func (a *API) handleLogin(w http.ResponseWriter, r *http.Request) { } jsonBytesResponse(w, http.StatusOK, json) + return } errorResponse(w, http.StatusInternalServerError, map[string]string{"error": "Unknown login type"}) @@ -102,3 +106,16 @@ func (a *API) handleRegister(w http.ResponseWriter, r *http.Request) { jsonBytesResponse(w, http.StatusOK, nil) return } + +func (a *API) sessionRequired(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + token, _ := auth.ParseAuthTokenFromRequest(r) + session, err := a.app().GetSession(token) + if err != nil { + errorResponse(w, http.StatusUnauthorized, map[string]string{"error": err.Error()}) + return + } + ctx := context.WithValue(r.Context(), "session", session) + handler(w, r.WithContext(ctx)) + } +} diff --git a/server/app/auth.go b/server/app/auth.go index a12466747..7266b89c4 100644 --- a/server/app/auth.go +++ b/server/app/auth.go @@ -8,6 +8,14 @@ import ( "github.com/pkg/errors" ) +func (a *App) GetSession(token string) (*model.Session, error) { + session, err := a.store.GetSession(token) + if err != nil { + return nil, errors.Wrap(err, "unable to get the session for the token") + } + return session, nil +} + func (a *App) Login(username string, email string, password string, mfaToken string) (string, error) { var user *model.User if username != "" { @@ -33,8 +41,19 @@ func (a *App) Login(username string, email string, password string, mfaToken str return "", errors.New("invalid username or password") } + session := model.Session{ + ID: uuid.New().String(), + Token: uuid.New().String(), + UserID: user.ID, + Props: map[string]interface{}{}, + } + err := a.store.CreateSession(&session) + if err != nil { + return "", errors.Wrap(err, "unable to create session") + } + // TODO: MFA verification - return auth.CreateToken(user.ID, a.config.Secret) + return session.Token, nil } func (a *App) RegisterUser(username string, email string, password string) error { diff --git a/server/model/user.go b/server/model/user.go index 0b9f1273b..55847de13 100644 --- a/server/model/user.go +++ b/server/model/user.go @@ -13,3 +13,10 @@ type User struct { UpdateAt int64 `json:"update_at,omitempty"` DeleteAt int64 `json:"delete_at"` } + +type Session struct { + ID string `json:"id"` + Token string `json:"token"` + UserID string `json:"user_id"` + Props map[string]interface{} `json:"props"` +} diff --git a/server/services/store/sqlstore/migrations/postgres/bindata.go b/server/services/store/sqlstore/migrations/postgres/bindata.go index 6eb4dcf8c..462cae231 100644 --- a/server/services/store/sqlstore/migrations/postgres/bindata.go +++ b/server/services/store/sqlstore/migrations/postgres/bindata.go @@ -61,21 +61,21 @@ func _000002_system_settings_table_up_sql() ([]byte, error) { ) } -var __000003_users_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x02\x04\x00\x00\xff\xff\xcf\x0c\x8a\x87\x12\x00\x00\x00") +var __000003_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") -func _000003_users_table_down_sql() ([]byte, error) { +func _000003_auth_table_down_sql() ([]byte, error) { return bindata_read( - __000003_users_table_down_sql, - "000003_users_table.down.sql", + __000003_auth_table_down_sql, + "000003_auth_table.down.sql", ) } -var __000003_users_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\xcd\xcd\x4e\x85\x30\x10\x86\xe1\x75\x7b\x15\xb3\x84\x84\xc5\xf1\x24\xac\x5c\x15\xac\x5a\x7f\x80\x94\x6a\x64\x45\x26\x74\x8c\x4d\x40\x48\x5b\xf4\xf6\x0d\x31\xd1\x08\xce\xf2\x79\x93\xf9\x4a\x2d\x85\x91\x60\x44\xf1\x20\x41\x5d\x43\x55\x1b\x90\x2f\xaa\x35\x2d\xac\x81\x7c\x80\x84\x33\x67\xe1\x59\xe8\xf2\x56\xe8\xe4\xe2\x74\x4a\x33\xce\xb6\xf4\x8e\x13\xed\x9d\x26\x74\xe3\x0f\x9e\xf3\x7c\xc3\x05\x43\xf8\x9c\xfd\xfe\x09\x00\xc0\xf4\x8a\x7d\xa0\xc1\x53\xfc\x27\xe2\x1a\xdf\xfa\x40\xfe\xc3\x0d\xbf\x43\xe7\x3f\xd5\x62\xc4\xfd\x5c\xe3\xe7\x25\xc0\xf7\xdd\xb5\x75\x95\x71\x56\x7a\xc2\x48\x22\x6e\x54\xa8\x1b\x55\x99\x8c\xb3\xa7\xc5\x1e\xf1\x8a\x46\x3a\x60\xa3\xd5\xa3\xd0\x1d\xdc\xcb\x0e\x12\x67\x53\x9e\x5e\xf2\xaf\x00\x00\x00\xff\xff\xa3\x41\x36\x1b\x38\x01\x00\x00") +var __000003_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x90\x4f\x4b\x03\x31\x10\xc5\xcf\x9b\x4f\x31\xc7\x5d\xe8\xa1\x16\x7a\xf2\x94\x96\xa8\xf1\xcf\x56\xb2\x41\xec\x69\x19\x36\x23\x06\xbb\x7f\xc8\xa4\xfa\xf5\x25\x08\x8a\xdd\xe8\xa5\x73\xfc\xbd\x61\xde\xbc\xb7\x35\x4a\x5a\x05\x56\x6e\xee\x15\xe8\x2b\xa8\x77\x16\xd4\xb3\x6e\x6c\x03\x47\xa6\xc0\x50\x8a\xc2\x3b\x78\x92\x66\x7b\x23\x4d\x79\xb1\x5c\x56\x0b\x51\x24\x69\xc0\x9e\x4e\x39\xf5\xe8\x0f\xdf\x70\xb5\x5e\x27\x38\x21\xf3\xc7\x18\x4e\x8f\x00\x00\xf4\x2f\xd8\x32\x75\x81\x62\x46\xc4\x63\x7c\x6d\x99\xc2\xbb\xef\x7e\x8c\x56\xbf\x54\x87\x11\x67\x76\x61\x9c\x18\xbe\xe6\xb6\xd9\xd5\x0b\x51\x74\x81\x30\x52\x8b\x31\xb1\x8d\xbe\xd6\xb5\x4d\x21\x26\x97\xa1\x8e\x0e\x34\xa7\x8f\x46\x3f\x48\xb3\x87\x3b\xb5\x87\xd2\xbb\x4a\x54\x97\x42\xfc\xd3\x1d\x13\xb3\x1f\x87\x3f\xea\x8b\xe3\x1b\x0d\xb9\x4e\xdb\xf9\xee\x99\x71\x72\x8f\x7f\x06\x00\x00\xff\xff\x35\x8e\x4b\xc6\xf4\x01\x00\x00") -func _000003_users_table_up_sql() ([]byte, error) { +func _000003_auth_table_up_sql() ([]byte, error) { return bindata_read( - __000003_users_table_up_sql, - "000003_users_table.up.sql", + __000003_auth_table_up_sql, + "000003_auth_table.up.sql", ) } @@ -105,8 +105,8 @@ var _bindata = map[string]func() ([]byte, error){ "000001_init.up.sql": _000001_init_up_sql, "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, - "000003_users_table.down.sql": _000003_users_table_down_sql, - "000003_users_table.up.sql": _000003_users_table_up_sql, + "000003_auth_table.down.sql": _000003_auth_table_down_sql, + "000003_auth_table.up.sql": _000003_auth_table_up_sql, } // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. @@ -156,8 +156,8 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ }}, "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ }}, - "000003_users_table.down.sql": &_bintree_t{_000003_users_table_down_sql, map[string]*_bintree_t{ + "000003_auth_table.down.sql": &_bintree_t{_000003_auth_table_down_sql, map[string]*_bintree_t{ }}, - "000003_users_table.up.sql": &_bintree_t{_000003_users_table_up_sql, map[string]*_bintree_t{ + "000003_auth_table.up.sql": &_bintree_t{_000003_auth_table_up_sql, map[string]*_bintree_t{ }}, }} diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.down.sql b/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.down.sql new file mode 100644 index 000000000..10b4ee14d --- /dev/null +++ b/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.down.sql @@ -0,0 +1,2 @@ +DROP TABLE users; +DROP TABLE sessions; diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.up.sql b/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.up.sql new file mode 100644 index 000000000..aa91258cb --- /dev/null +++ b/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.up.sql @@ -0,0 +1,24 @@ +CREATE TABLE IF NOT EXISTS users ( + id VARCHAR(100), + username VARCHAR(100), + email VARCHAR(255), + password VARCHAR(100), + mfa_secret VARCHAR(100), + auth_service VARCHAR(20), + auth_data VARCHAR(255), + props JSON, + create_at BIGINT, + update_at BIGINT, + delete_at BIGINT, + PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS sessions ( + id VARCHAR(100), + token VARCHAR(100), + user_id VARCHAR(100), + props JSON, + create_at BIGINT, + update_at BIGINT, + PRIMARY KEY (id) +); diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql b/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql deleted file mode 100644 index cc1f647d2..000000000 --- a/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE users; diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql b/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql deleted file mode 100644 index 68f6cde60..000000000 --- a/server/services/store/sqlstore/migrations/postgres_files/000003_users_table.up.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE IF NOT EXISTS users ( - id VARCHAR(100), - username VARCHAR(100), - email VARCHAR(255), - password VARCHAR(100), - mfa_secret VARCHAR(100), - auth_service VARCHAR(20), - auth_data VARCHAR(255), - Props JSON, - CreateAt BIGINT, - UpdateAt BIGINT, - DeleteAt BIGINT, - PRIMARY KEY (id) -); diff --git a/server/services/store/sqlstore/migrations/sqlite/bindata.go b/server/services/store/sqlstore/migrations/sqlite/bindata.go index 92ea4445f..d9b404107 100644 --- a/server/services/store/sqlstore/migrations/sqlite/bindata.go +++ b/server/services/store/sqlstore/migrations/sqlite/bindata.go @@ -61,21 +61,21 @@ func _000002_system_settings_table_up_sql() ([]byte, error) { ) } -var __000003_users_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x02\x04\x00\x00\xff\xff\xcf\x0c\x8a\x87\x12\x00\x00\x00") +var __000003_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") -func _000003_users_table_down_sql() ([]byte, error) { +func _000003_auth_table_down_sql() ([]byte, error) { return bindata_read( - __000003_users_table_down_sql, - "000003_users_table.down.sql", + __000003_auth_table_down_sql, + "000003_auth_table.down.sql", ) } -var __000003_users_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\xcd\xd1\x4b\x85\x30\x14\xc7\xf1\x67\xf7\x57\x9c\x47\x85\xfb\x70\x13\x7c\xea\x69\xda\xaa\x51\x99\xcc\x15\xfa\x24\x07\x77\xa2\x81\xa6\x6c\xb3\xfe\xfd\x18\x41\x91\xde\xf3\xf8\xf9\xc2\xf9\x55\x4a\x70\x2d\x40\xf3\xf2\x51\x80\xbc\x85\xfa\x59\x83\xe8\x64\xab\x5b\xd8\x3c\x39\x0f\x29\x4b\xac\x81\x57\xae\xaa\x7b\xae\xd2\xab\xf3\x39\x3b\xb1\x24\xa6\x0f\x9c\x69\xef\x34\xa3\x9d\x7e\x31\x2f\x8a\x88\x2b\x7a\xff\xb5\xb8\xfd\x13\x00\x80\xf9\x0d\x07\x4f\xa3\xa3\x70\x21\xe2\x16\xde\x07\x4f\xee\xd3\x8e\x7f\x43\xf9\xbf\x6a\x30\xe0\x7e\xae\x71\xcb\xea\xe1\xe7\xb4\xe8\xf4\x89\x25\x95\x23\x0c\xc4\x43\xa4\x52\xde\xc9\x3a\xe2\xcb\x6a\x8e\x78\x43\x13\x1d\xb0\x51\xf2\x89\xab\x1e\x1e\x44\x0f\xa9\x35\x19\xcb\xae\xd9\x77\x00\x00\x00\xff\xff\x8b\xb1\x5a\xe4\x38\x01\x00\x00") +var __000003_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\xd0\xc1\x4a\x03\x31\x10\x06\xe0\xf3\xe6\x29\xe6\xb8\x0b\x3d\xd4\x42\x4f\x9e\xd2\x12\x35\xa8\x55\xd2\x20\xed\x69\x19\x36\x23\x06\xbb\x9b\x25\x93\xd5\xd7\x97\x20\x28\x76\xa3\x17\x73\xfc\xfe\x90\xc9\xfc\x5b\xa3\xa4\x55\x60\xe5\xe6\x4e\x81\xbe\x82\xdd\x83\x05\x75\xd0\x7b\xbb\x87\x89\x29\x32\xd4\xa2\xf2\x0e\x9e\xa4\xd9\xde\x48\x53\x5f\x2c\x97\xcd\x42\x54\x39\x1a\xb0\xa7\x73\xa7\x1e\xfd\xe9\x0b\x57\xeb\x75\xc6\x11\x99\xdf\x43\x3c\x7f\x04\x00\xa0\x7f\xc6\x96\xa9\x8b\x94\x0a\x21\x4e\xe9\xa5\x65\x8a\x6f\xbe\xfb\x1e\xb4\xfa\x91\x3a\x4c\x38\x1b\x17\xc3\xc8\xf0\x79\xac\x3a\xd8\x85\xa8\xba\x48\x98\xa8\xc5\x94\x6d\xa3\xaf\xf5\x2e\xeb\x34\xba\x82\x3a\x3a\xd1\x5c\x1f\x8d\xbe\x97\xe6\x08\xb7\xea\x08\xb5\x77\x8d\x68\x2e\x85\xf8\xa3\x3b\x26\x66\x1f\x86\x5f\xea\x4b\xe1\x95\x86\x52\xa7\xed\xfc\xee\x3f\xd7\x29\x7d\xfc\x23\x00\x00\xff\xff\xa1\xd6\x24\xe6\xf4\x01\x00\x00") -func _000003_users_table_up_sql() ([]byte, error) { +func _000003_auth_table_up_sql() ([]byte, error) { return bindata_read( - __000003_users_table_up_sql, - "000003_users_table.up.sql", + __000003_auth_table_up_sql, + "000003_auth_table.up.sql", ) } @@ -105,8 +105,8 @@ var _bindata = map[string]func() ([]byte, error){ "000001_init.up.sql": _000001_init_up_sql, "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, - "000003_users_table.down.sql": _000003_users_table_down_sql, - "000003_users_table.up.sql": _000003_users_table_up_sql, + "000003_auth_table.down.sql": _000003_auth_table_down_sql, + "000003_auth_table.up.sql": _000003_auth_table_up_sql, } // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. @@ -156,8 +156,8 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ }}, "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ }}, - "000003_users_table.down.sql": &_bintree_t{_000003_users_table_down_sql, map[string]*_bintree_t{ + "000003_auth_table.down.sql": &_bintree_t{_000003_auth_table_down_sql, map[string]*_bintree_t{ }}, - "000003_users_table.up.sql": &_bintree_t{_000003_users_table_up_sql, map[string]*_bintree_t{ + "000003_auth_table.up.sql": &_bintree_t{_000003_auth_table_up_sql, map[string]*_bintree_t{ }}, }} diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.down.sql b/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.down.sql new file mode 100644 index 000000000..10b4ee14d --- /dev/null +++ b/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.down.sql @@ -0,0 +1,2 @@ +DROP TABLE users; +DROP TABLE sessions; diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.up.sql b/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.up.sql new file mode 100644 index 000000000..814322eea --- /dev/null +++ b/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.up.sql @@ -0,0 +1,24 @@ +CREATE TABLE IF NOT EXISTS users ( + id VARCHAR(100), + username VARCHAR(100), + email VARCHAR(255), + password VARCHAR(100), + mfa_secret VARCHAR(100), + auth_service VARCHAR(20), + auth_data VARCHAR(255), + props TEXT, + create_at BIGINT, + update_at BIGINT, + delete_at BIGINT, + PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS sessions ( + id VARCHAR(100), + token VARCHAR(100), + user_id VARCHAR(100), + props TEXT, + create_at BIGINT, + update_at BIGINT, + PRIMARY KEY (id) +); diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql b/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql deleted file mode 100644 index cc1f647d2..000000000 --- a/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE users; diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql b/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql deleted file mode 100644 index 1ada8c31e..000000000 --- a/server/services/store/sqlstore/migrations/sqlite_files/000003_users_table.up.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE IF NOT EXISTS users ( - id VARCHAR(100), - username VARCHAR(100), - email VARCHAR(255), - password VARCHAR(100), - mfa_secret VARCHAR(100), - auth_service VARCHAR(20), - auth_data VARCHAR(255), - Props TEXT, - CreateAt BIGINT, - UpdateAt BIGINT, - DeleteAt BIGINT, - PRIMARY KEY (id) -); diff --git a/server/services/store/sqlstore/session.go b/server/services/store/sqlstore/session.go new file mode 100644 index 000000000..30a7e28ed --- /dev/null +++ b/server/services/store/sqlstore/session.go @@ -0,0 +1,82 @@ +package sqlstore + +import ( + "encoding/json" + "time" + + sq "github.com/Masterminds/squirrel" + "github.com/mattermost/mattermost-octo-tasks/server/model" +) + +func (s *SQLStore) GetSession(token string) (*model.Session, error) { + query := s.getQueryBuilder(). + Select("id", "token", "user_id", "props"). + From("sessions"). + Where(sq.Eq{"token": token}) + + row := query.QueryRow() + session := model.Session{} + + var propsBytes []byte + err := row.Scan(&session.ID, &session.Token, &session.UserID, &propsBytes) + if err != nil { + return nil, err + } + + err = json.Unmarshal(propsBytes, &session.Props) + if err != nil { + return nil, err + } + + return &session, nil +} + +func (s *SQLStore) CreateSession(session *model.Session) error { + now := time.Now().Unix() + + propsBytes, err := json.Marshal(session.Props) + if err != nil { + return err + } + + query := s.getQueryBuilder().Insert("sessions"). + Columns("id", "token", "user_id", "props", "create_at", "update_at"). + Values(session.ID, session.Token, session.UserID, propsBytes, now, now) + + _, err = query.Exec() + return err +} + +func (s *SQLStore) RefreshSession(session *model.Session) error { + now := time.Now().Unix() + + query := s.getQueryBuilder().Update("sessions"). + Set("update_at", now) + + _, err := query.Exec() + return err +} + +func (s *SQLStore) UpdateSession(session *model.Session) error { + now := time.Now().Unix() + + propsBytes, err := json.Marshal(session.Props) + if err != nil { + return err + } + + query := s.getQueryBuilder().Update("sessions"). + Set("update_at", now). + Set("props", propsBytes) + + _, err = query.Exec() + return err +} + +func (s *SQLStore) DeleteSession(sessionId string) error { + query := s.getQueryBuilder().Delete("sessions"). + Where("id", sessionId) + + _, err := query.Exec() + return err +} diff --git a/server/services/store/store.go b/server/services/store/store.go index 4611e9bec..c89839dcb 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -22,4 +22,9 @@ type Store interface { GetUserByUsername(username string) (*model.User, error) CreateUser(user *model.User) error UpdateUser(user *model.User) error + GetSession(token string) (*model.Session, error) + CreateSession(session *model.Session) error + RefreshSession(session *model.Session) error + UpdateSession(session *model.Session) error + DeleteSession(sessionId string) error } From 99cefc5356986d18843ef165f60fd3511c84b73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 4 Dec 2020 11:28:35 +0100 Subject: [PATCH 06/13] More work on auth --- linux/main.go | 2 +- mac/Tasks/AppDelegate.swift | 2 +- server/api/api.go | 5 +++-- server/api/auth.go | 16 ++++++++++++++++ server/main/main.go | 6 +++++- server/model/user.go | 10 ++++++---- server/server/server.go | 4 ++-- win/main.go | 2 +- 8 files changed, 35 insertions(+), 12 deletions(-) diff --git a/linux/main.go b/linux/main.go index 8c7d4403a..58c34b460 100644 --- a/linux/main.go +++ b/linux/main.go @@ -11,7 +11,7 @@ import ( ) func runOctoTasks(ctx context.Context) { - cmd := exec.CommandContext(ctx, "./octoserver", "--monitorpid", strconv.FormatInt(int64(os.Getpid()), 10)) + cmd := exec.CommandContext(ctx, "./octoserver", "--monitorpid", strconv.FormatInt(int64(os.Getpid()), 10), "--single-user") cmd.Stdout = os.Stdout err := cmd.Run() if err != nil { diff --git a/mac/Tasks/AppDelegate.swift b/mac/Tasks/AppDelegate.swift index 41b0e1022..a3e0e97ab 100644 --- a/mac/Tasks/AppDelegate.swift +++ b/mac/Tasks/AppDelegate.swift @@ -80,7 +80,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSLog("pid: \(pid)") let serverProcess = Process() serverProcess.currentDirectoryPath = cwdUrl.path - serverProcess.arguments = ["-monitorpid", "\(pid)", "-port", "\(serverPort)"] + serverProcess.arguments = ["-monitorpid", "\(pid)", "-port", "\(serverPort)", "--single-user"] serverProcess.launchPath = executablePath serverProcess.launch() self.serverProcess = serverProcess diff --git a/server/api/api.go b/server/api/api.go index 39a2f4454..c709b1357 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -21,10 +21,11 @@ import ( type API struct { appBuilder func() *app.App + singleUser bool } -func NewAPI(appBuilder func() *app.App) *API { - return &API{appBuilder: appBuilder} +func NewAPI(appBuilder func() *app.App, singleUser bool) *API { + return &API{appBuilder: appBuilder, singleUser: singleUser} } func (a *API) app() *app.App { diff --git a/server/api/auth.go b/server/api/auth.go index 3b839cd96..0bf59c1a1 100644 --- a/server/api/auth.go +++ b/server/api/auth.go @@ -8,7 +8,9 @@ import ( "log" "net/http" "strings" + "time" + "github.com/mattermost/mattermost-octo-tasks/server/model" "github.com/mattermost/mattermost-octo-tasks/server/services/auth" ) @@ -109,6 +111,20 @@ func (a *API) handleRegister(w http.ResponseWriter, r *http.Request) { func (a *API) sessionRequired(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { + if a.singleUser { + now := time.Now().Unix() + session := &model.Session{ + ID: "single-user", + Token: "single-user", + UserID: "single-user", + CreateAt: now, + UpdateAt: now, + } + ctx := context.WithValue(r.Context(), "session", session) + handler(w, r.WithContext(ctx)) + return + } + token, _ := auth.ParseAuthTokenFromRequest(r) session, err := a.app().GetSession(token) if err != nil { diff --git a/server/main/main.go b/server/main/main.go index ac3680a75..17fe05848 100644 --- a/server/main/main.go +++ b/server/main/main.go @@ -56,6 +56,10 @@ func main() { // Command line args pMonitorPid := flag.Int("monitorpid", -1, "a process ID") pPort := flag.Int("port", config.Port, "the port number") + singleUser := false + if pSingleUser := flag.Bool("single-user", false, "single user mode"); pSingleUser != nil { + singleUser = *pSingleUser + } flag.Parse() if pMonitorPid != nil && *pMonitorPid > 0 { @@ -68,7 +72,7 @@ func main() { config.Port = *pPort } - server, err := server.New(config) + server, err := server.New(config, singleUser) if err != nil { log.Fatal("ListenAndServeTLS: ", err) } diff --git a/server/model/user.go b/server/model/user.go index 55847de13..2ffa93a8d 100644 --- a/server/model/user.go +++ b/server/model/user.go @@ -15,8 +15,10 @@ type User struct { } type Session struct { - ID string `json:"id"` - Token string `json:"token"` - UserID string `json:"user_id"` - Props map[string]interface{} `json:"props"` + ID string `json:"id"` + Token string `json:"token"` + UserID string `json:"user_id"` + Props map[string]interface{} `json:"props"` + CreateAt int64 `json:"create_at,omitempty"` + UpdateAt int64 `json:"update_at,omitempty"` } diff --git a/server/server/server.go b/server/server/server.go index c5dbe754d..12f42ecea 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -35,7 +35,7 @@ type Server struct { logger *zap.Logger } -func New(cfg *config.Configuration) (*Server, error) { +func New(cfg *config.Configuration, singleUser bool) (*Server, error) { logger, err := zap.NewProduction() if err != nil { return nil, err @@ -63,7 +63,7 @@ func New(cfg *config.Configuration) (*Server, error) { webhookClient := webhook.NewClient(cfg) appBuilder := func() *app.App { return app.New(cfg, store, wsServer, filesBackend, webhookClient) } - api := api.NewAPI(appBuilder) + api := api.NewAPI(appBuilder, singleUser) webServer := web.NewServer(cfg.WebPath, cfg.Port, cfg.UseSSL) webServer.AddRoutes(wsServer) diff --git a/win/main.go b/win/main.go index 2b16ea192..13ca35a99 100644 --- a/win/main.go +++ b/win/main.go @@ -12,7 +12,7 @@ import ( func runOctoTasks(ctx context.Context) *exec.Cmd { // cmd := exec.CommandContext(ctx, "bin/octoserver.exe", "--monitorpid", strconv.FormatInt(int64(os.Getpid()), 10)) - cmd := exec.CommandContext(ctx, "bin/octoserver.exe") + cmd := exec.CommandContext(ctx, "bin/octoserver.exe --single-user") // cmd := exec.CommandContext(ctx, "cmd.exe", "/C", "start", "./bin/octoserver.exe", "--monitorpid", strconv.FormatInt(int64(os.Getpid()), 10)) // cmd := exec.CommandContext(ctx, "cmd.exe", "/C", "start", "./bin/octoserver.exe") From f491241c1ab08443618878e21ba1506b11fc9e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 4 Dec 2020 16:03:09 +0100 Subject: [PATCH 07/13] Preliminary auth implementation --- Makefile | 17 ++++++ server/api/auth.go | 5 +- server/app/auth.go | 3 ++ server/main/main.go | 6 ++- server/modd.conf | 2 +- server/services/auth/password.go | 1 - server/services/auth/request_parser.go | 2 +- webapp/src/app.tsx | 9 ++++ webapp/src/octoClient.ts | 70 ++++++++++++++++++------ webapp/src/pages/loginPage.tsx | 23 +++++--- webapp/src/pages/registerPage.scss | 27 ++++++++++ webapp/src/pages/registerPage.tsx | 74 ++++++++++++++++++++++++++ 12 files changed, 209 insertions(+), 30 deletions(-) create mode 100644 webapp/src/pages/registerPage.scss create mode 100644 webapp/src/pages/registerPage.tsx diff --git a/Makefile b/Makefile index 59ac52199..094a00085 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,20 @@ server-linux: server-win: cd server; env GOOS=windows GOARCH=amd64 go build -o ../bin/octoserver.exe ./main +server-single-user: + cd server; go build -o ../bin/octoserver ./main --single-user + +server-mac-single-user: + mkdir -p bin/mac + cd server; env GOOS=darwin GOARCH=amd64 go build -o ../bin/mac/octoserver ./main --single-user + +server-linux-single-user: + mkdir -p bin/linux + cd server; env GOOS=linux GOARCH=amd64 go build -o ../bin/linux/octoserver ./main --single-user + +server-win-single-user: + cd server; env GOOS=windows GOARCH=amd64 go build -o ../bin/octoserver.exe ./main --single-user + generate: cd server; go get -modfile=go.tools.mod github.com/golang/mock/mockgen cd server; go get -modfile=go.tools.mod github.com/jteeuwen/go-bindata @@ -45,6 +59,9 @@ server-doc: watch-server: cd server; modd +watch-server-single-user: + cd server; env OCTOSERVER_ARGS=--single-user modd + webapp: cd webapp; npm run pack diff --git a/server/api/auth.go b/server/api/auth.go index 0bf59c1a1..d214c5ab4 100644 --- a/server/api/auth.go +++ b/server/api/auth.go @@ -60,12 +60,12 @@ func (a *API) handleLogin(w http.ResponseWriter, r *http.Request) { } if loginData.Type == "normal" { - jwtToken, err := a.app().Login(loginData.Username, loginData.Email, loginData.Password, loginData.MfaToken) + token, err := a.app().Login(loginData.Username, loginData.Email, loginData.Password, loginData.MfaToken) if err != nil { errorResponse(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } - json, err := json.Marshal(jwtToken) + json, err := json.Marshal(map[string]string{"token": token}) if err != nil { log.Printf(`ERROR json.Marshal: %v`, r) errorResponse(w, http.StatusInternalServerError, nil) @@ -111,6 +111,7 @@ func (a *API) handleRegister(w http.ResponseWriter, r *http.Request) { func (a *API) sessionRequired(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { + log.Printf(`Single User: %v`, a.singleUser) if a.singleUser { now := time.Now().Unix() session := &model.Session{ diff --git a/server/app/auth.go b/server/app/auth.go index 7266b89c4..3bfec91d4 100644 --- a/server/app/auth.go +++ b/server/app/auth.go @@ -1,6 +1,8 @@ package app import ( + "log" + "github.com/google/uuid" "github.com/mattermost/mattermost-octo-tasks/server/model" "github.com/mattermost/mattermost-octo-tasks/server/services/auth" @@ -38,6 +40,7 @@ func (a *App) Login(username string, email string, password string, mfaToken str } if !auth.ComparePassword(user.Password, password) { + log.Printf("Not valid passowrd. %s (%s)\n", password, user.Password) return "", errors.New("invalid username or password") } diff --git a/server/main/main.go b/server/main/main.go index 17fe05848..6f187a81b 100644 --- a/server/main/main.go +++ b/server/main/main.go @@ -56,11 +56,13 @@ func main() { // Command line args pMonitorPid := flag.Int("monitorpid", -1, "a process ID") pPort := flag.Int("port", config.Port, "the port number") + pSingleUser := flag.Bool("single-user", false, "single user mode") + flag.Parse() + singleUser := false - if pSingleUser := flag.Bool("single-user", false, "single user mode"); pSingleUser != nil { + if pSingleUser != nil { singleUser = *pSingleUser } - flag.Parse() if pMonitorPid != nil && *pMonitorPid > 0 { monitorPid(*pMonitorPid) diff --git a/server/modd.conf b/server/modd.conf index 96e2b7722..7611502b2 100644 --- a/server/modd.conf +++ b/server/modd.conf @@ -1,4 +1,4 @@ **/*.go !**/*_test.go { prep: go build -o ../bin/octoserver ./main - daemon +sigterm: cd .. && ./bin/octoserver + daemon +sigterm: cd .. && ./bin/octoserver $OCTOSERVER_ARGS } diff --git a/server/services/auth/password.go b/server/services/auth/password.go index b9b31ee68..2915d8fe8 100644 --- a/server/services/auth/password.go +++ b/server/services/auth/password.go @@ -39,7 +39,6 @@ func HashPassword(password string) string { // ComparePassword compares the hash func ComparePassword(hash string, password string) bool { - if len(password) == 0 || len(hash) == 0 { return false } diff --git a/server/services/auth/request_parser.go b/server/services/auth/request_parser.go index a0f073da1..161f87624 100644 --- a/server/services/auth/request_parser.go +++ b/server/services/auth/request_parser.go @@ -12,7 +12,7 @@ const ( HEADER_TOKEN = "token" HEADER_AUTH = "Authorization" HEADER_BEARER = "BEARER" - SESSION_COOKIE_TOKEN = "MMAUTHTOKEN" + SESSION_COOKIE_TOKEN = "OCTOTASKSAUTHTOKEN" ) type TokenLocation int diff --git a/webapp/src/app.tsx b/webapp/src/app.tsx index 077bf7bcc..27f4c86cd 100644 --- a/webapp/src/app.tsx +++ b/webapp/src/app.tsx @@ -7,13 +7,17 @@ import { BrowserRouter as Router, Switch, Route, + Redirect, } from 'react-router-dom' +import client from './octoClient' + import {getCurrentLanguage, getMessages, storeLanguage} from './i18n' import {FlashMessages} from './components/flashMessages' import LoginPage from './pages/loginPage' +import RegisterPage from './pages/registerPage' import BoardPage from './pages/boardPage' export default function App(): JSX.Element { @@ -35,10 +39,15 @@ export default function App(): JSX.Element { + + + + {!client.token && } + {!client.token && } diff --git a/webapp/src/octoClient.ts b/webapp/src/octoClient.ts index 0d7c0a628..8819383f1 100644 --- a/webapp/src/octoClient.ts +++ b/webapp/src/octoClient.ts @@ -8,15 +8,59 @@ import {Utils} from './utils' // class OctoClient { serverUrl: string + token?: string - constructor(serverUrl?: string) { + constructor(serverUrl?: string, token?: string) { this.serverUrl = serverUrl || window.location.origin + this.token = token Utils.log(`OctoClient serverUrl: ${this.serverUrl}`) } + async login(username: string, password: string): Promise { + const path = '/api/v1/login' + const body = JSON.stringify({username, password, type: 'normal'}) + const response = await fetch(this.serverUrl + path, { + method: 'POST', + headers: this.headers(), + body, + }) + if (response.status === 200) { + const responseJson = (await response.json() || {}) as {token?: string} + this.token = responseJson.token + if (responseJson.token !== '') { + localStorage.setItem('sessionId', this.token || '') + return true + } + return false + } + return false + } + + async register(email: string, username: string, password: string): Promise { + const path = '/api/v1/register' + const body = JSON.stringify({email, username, password}) + const response = await fetch(this.serverUrl + path, { + method: 'POST', + headers: this.headers(), + body, + }) + if (response.status === 200) { + return true + } + return false + } + + headers() { + return { + Accept: 'application/json', + 'Content-Type': 'application/json', + Authorization: this.token ? 'Bearer ' + this.token : '', + } + } + async getSubtree(rootId?: string, levels = 2): Promise { const path = `/api/v1/blocks/${rootId}/subtree?l=${levels}` - const response = await fetch(this.serverUrl + path) + const response = await fetch(this.serverUrl + path, {headers: this.headers()}) const blocks = (await response.json() || []) as IMutableBlock[] this.fixBlocks(blocks) return blocks @@ -24,7 +68,7 @@ class OctoClient { async exportFullArchive(): Promise { const path = '/api/v1/blocks/export' - const response = await fetch(this.serverUrl + path) + const response = await fetch(this.serverUrl + path, {headers: this.headers()}) const blocks = (await response.json() || []) as IMutableBlock[] this.fixBlocks(blocks) return blocks @@ -38,10 +82,7 @@ class OctoClient { const body = JSON.stringify(blocks) return fetch(this.serverUrl + '/api/v1/blocks/import', { method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers: this.headers(), body, }) } @@ -62,7 +103,7 @@ class OctoClient { } private async getBlocksWithPath(path: string): Promise { - const response = await fetch(this.serverUrl + path) + const response = await fetch(this.serverUrl + path, {headers: this.headers()}) const blocks = (await response.json() || []) as IMutableBlock[] this.fixBlocks(blocks) return blocks @@ -98,10 +139,7 @@ class OctoClient { Utils.log(`deleteBlock: ${blockId}`) return fetch(this.serverUrl + `/api/v1/blocks/${encodeURIComponent(blockId)}`, { method: 'DELETE', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers: this.headers(), }) } @@ -117,10 +155,7 @@ class OctoClient { const body = JSON.stringify(blocks) return fetch(this.serverUrl + '/api/v1/blocks', { method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers: this.headers(), body, }) } @@ -138,6 +173,7 @@ class OctoClient { // TIPTIP: Leave out Content-Type here, it will be automatically set by the browser headers: { Accept: 'application/json', + Authorization: this.token ? 'Bearer ' + this.token : '', }, body: formData, }) @@ -161,6 +197,6 @@ class OctoClient { } } -const client = new OctoClient() +const client = new OctoClient(undefined, localStorage.getItem('sessionId') || '') export default client diff --git a/webapp/src/pages/loginPage.tsx b/webapp/src/pages/loginPage.tsx index 97e81554f..a2376e401 100644 --- a/webapp/src/pages/loginPage.tsx +++ b/webapp/src/pages/loginPage.tsx @@ -2,26 +2,34 @@ // See LICENSE.txt for license information. import React from 'react' -import {Utils} from '../utils' +import { + withRouter, + RouteComponentProps, + Link, +} from 'react-router-dom' + import Button from '../widgets/buttons/button' +import client from '../octoClient' import './loginPage.scss' -type Props = { -} +type Props = RouteComponentProps type State = { username: string password: string } -export default class LoginPage extends React.PureComponent { +class LoginPage extends React.PureComponent { state = { username: '', password: '', } - private handleLogin = (): void => { - Utils.log('Logging in') + private handleLogin = async (): Promise => { + const logged = await client.login(this.state.username, this.state.password) + if (logged) { + this.props.history.push('/') + } } render(): React.ReactNode { @@ -45,7 +53,10 @@ export default class LoginPage extends React.PureComponent { /> + {'or create an account if you don\'t have one'} ) } } + +export default withRouter(LoginPage) diff --git a/webapp/src/pages/registerPage.scss b/webapp/src/pages/registerPage.scss new file mode 100644 index 000000000..bb013d303 --- /dev/null +++ b/webapp/src/pages/registerPage.scss @@ -0,0 +1,27 @@ +.RegisterPage { + border: 1px solid #cccccc; + border-radius: 15px; + width: 450px; + height: 400px; + margin: auto; + display: flex; + align-items: center; + justify-content: center; + flex-direction: column; + .email, .username, .password { + margin-bottom: 5px; + label { + display: inline-block; + width: 140px; + } + input { + display: inline-block; + width: 250px; + border: 1px solid #cccccc; + border-radius: 4px; + } + } + .Button { + margin-top: 10px; + } +} diff --git a/webapp/src/pages/registerPage.tsx b/webapp/src/pages/registerPage.tsx new file mode 100644 index 000000000..79adbc8b5 --- /dev/null +++ b/webapp/src/pages/registerPage.tsx @@ -0,0 +1,74 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. +import React from 'react' + +import { + withRouter, + RouteComponentProps, + Link, +} from 'react-router-dom' + +import Button from '../widgets/buttons/button' +import client from '../octoClient' +import './registerPage.scss' + +type Props = RouteComponentProps + +type State = { + email: string + username: string + password: string +} + +class RegisterPage extends React.PureComponent { + state = { + email: '', + username: '', + password: '', + } + + private handleRegister = async (): Promise => { + const registered = await client.register(this.state.email, this.state.username, this.state.password) + if (registered) { + const logged = await client.login(this.state.username, this.state.password) + if (logged) { + this.props.history.push('/') + } + } + } + + render(): React.ReactNode { + return ( +
+
+ + this.setState({email: e.target.value})} + /> +
+
+ + this.setState({username: e.target.value})} + /> +
+
+ + this.setState({password: e.target.value})} + /> +
+ + {'or login if you already have an account'} +
+ ) + } +} +export default withRouter(RegisterPage) From bbddba2d2b282dd0ae10d4435a5aa65358026893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 7 Dec 2020 16:00:13 +0100 Subject: [PATCH 08/13] Addressing review concerns --- server/services/auth/token.go | 20 -------- .../sqlstore/migrations/postgres/bindata.go | 48 +++++++++---------- ...le.down.sql => 000004_auth_table.down.sql} | 0 ..._table.up.sql => 000004_auth_table.up.sql} | 0 .../sqlstore/migrations/sqlite/bindata.go | 48 +++++++++---------- ...le.down.sql => 000004_auth_table.down.sql} | 0 ..._table.up.sql => 000004_auth_table.up.sql} | 0 7 files changed, 48 insertions(+), 68 deletions(-) delete mode 100644 server/services/auth/token.go rename server/services/store/sqlstore/migrations/postgres_files/{000003_auth_table.down.sql => 000004_auth_table.down.sql} (100%) rename server/services/store/sqlstore/migrations/postgres_files/{000003_auth_table.up.sql => 000004_auth_table.up.sql} (100%) rename server/services/store/sqlstore/migrations/sqlite_files/{000003_auth_table.down.sql => 000004_auth_table.down.sql} (100%) rename server/services/store/sqlstore/migrations/sqlite_files/{000003_auth_table.up.sql => 000004_auth_table.up.sql} (100%) diff --git a/server/services/auth/token.go b/server/services/auth/token.go deleted file mode 100644 index c62856936..000000000 --- a/server/services/auth/token.go +++ /dev/null @@ -1,20 +0,0 @@ -package auth - -import ( - "time" - - "github.com/dgrijalva/jwt-go" -) - -func CreateToken(userID string, appSecret string) (string, error) { - claims := jwt.MapClaims{} - claims["authorized"] = true - claims["user_id"] = userID - claims["exp"] = time.Now().Add(time.Minute * 15).Unix() - at := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - token, err := at.SignedString([]byte(appSecret)) - if err != nil { - return "", err - } - return token, nil -} diff --git a/server/services/store/sqlstore/migrations/postgres/bindata.go b/server/services/store/sqlstore/migrations/postgres/bindata.go index c99c36f98..0547ecc59 100644 --- a/server/services/store/sqlstore/migrations/postgres/bindata.go +++ b/server/services/store/sqlstore/migrations/postgres/bindata.go @@ -61,24 +61,6 @@ func _000002_system_settings_table_up_sql() ([]byte, error) { ) } -var __000003_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") - -func _000003_auth_table_down_sql() ([]byte, error) { - return bindata_read( - __000003_auth_table_down_sql, - "000003_auth_table.down.sql", - ) -} - -var __000003_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x90\x4f\x4b\x03\x31\x10\xc5\xcf\x9b\x4f\x31\xc7\x5d\xe8\xa1\x16\x7a\xf2\x94\x96\xa8\xf1\xcf\x56\xb2\x41\xec\x69\x19\x36\x23\x06\xbb\x7f\xc8\xa4\xfa\xf5\x25\x08\x8a\xdd\xe8\xa5\x73\xfc\xbd\x61\xde\xbc\xb7\x35\x4a\x5a\x05\x56\x6e\xee\x15\xe8\x2b\xa8\x77\x16\xd4\xb3\x6e\x6c\x03\x47\xa6\xc0\x50\x8a\xc2\x3b\x78\x92\x66\x7b\x23\x4d\x79\xb1\x5c\x56\x0b\x51\x24\x69\xc0\x9e\x4e\x39\xf5\xe8\x0f\xdf\x70\xb5\x5e\x27\x38\x21\xf3\xc7\x18\x4e\x8f\x00\x00\xf4\x2f\xd8\x32\x75\x81\x62\x46\xc4\x63\x7c\x6d\x99\xc2\xbb\xef\x7e\x8c\x56\xbf\x54\x87\x11\x67\x76\x61\x9c\x18\xbe\xe6\xb6\xd9\xd5\x0b\x51\x74\x81\x30\x52\x8b\x31\xb1\x8d\xbe\xd6\xb5\x4d\x21\x26\x97\xa1\x8e\x0e\x34\xa7\x8f\x46\x3f\x48\xb3\x87\x3b\xb5\x87\xd2\xbb\x4a\x54\x97\x42\xfc\xd3\x1d\x13\xb3\x1f\x87\x3f\xea\x8b\xe3\x1b\x0d\xb9\x4e\xdb\xf9\xee\x99\x71\x72\x8f\x7f\x06\x00\x00\xff\xff\x35\x8e\x4b\xc6\xf4\x01\x00\x00") - -func _000003_auth_table_up_sql() ([]byte, error) { - return bindata_read( - __000003_auth_table_up_sql, - "000003_auth_table.up.sql", - ) -} - var __000003_blocks_rootid_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x09\xf2\x0f\x50\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\xb1\xe6\x02\x04\x00\x00\xff\xff\x94\x1c\x55\xb9\x28\x00\x00\x00") func _000003_blocks_rootid_down_sql() ([]byte, error) { @@ -97,6 +79,24 @@ func _000003_blocks_rootid_up_sql() ([]byte, error) { ) } +var __000004_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") + +func _000004_auth_table_down_sql() ([]byte, error) { + return bindata_read( + __000004_auth_table_down_sql, + "000004_auth_table.down.sql", + ) +} + +var __000004_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x90\x4f\x4b\x03\x31\x10\xc5\xcf\x9b\x4f\x31\xc7\x5d\xe8\xa1\x16\x7a\xf2\x94\x96\xa8\xf1\xcf\x56\xb2\x41\xec\x69\x19\x36\x23\x06\xbb\x7f\xc8\xa4\xfa\xf5\x25\x08\x8a\xdd\xe8\xa5\x73\xfc\xbd\x61\xde\xbc\xb7\x35\x4a\x5a\x05\x56\x6e\xee\x15\xe8\x2b\xa8\x77\x16\xd4\xb3\x6e\x6c\x03\x47\xa6\xc0\x50\x8a\xc2\x3b\x78\x92\x66\x7b\x23\x4d\x79\xb1\x5c\x56\x0b\x51\x24\x69\xc0\x9e\x4e\x39\xf5\xe8\x0f\xdf\x70\xb5\x5e\x27\x38\x21\xf3\xc7\x18\x4e\x8f\x00\x00\xf4\x2f\xd8\x32\x75\x81\x62\x46\xc4\x63\x7c\x6d\x99\xc2\xbb\xef\x7e\x8c\x56\xbf\x54\x87\x11\x67\x76\x61\x9c\x18\xbe\xe6\xb6\xd9\xd5\x0b\x51\x74\x81\x30\x52\x8b\x31\xb1\x8d\xbe\xd6\xb5\x4d\x21\x26\x97\xa1\x8e\x0e\x34\xa7\x8f\x46\x3f\x48\xb3\x87\x3b\xb5\x87\xd2\xbb\x4a\x54\x97\x42\xfc\xd3\x1d\x13\xb3\x1f\x87\x3f\xea\x8b\xe3\x1b\x0d\xb9\x4e\xdb\xf9\xee\x99\x71\x72\x8f\x7f\x06\x00\x00\xff\xff\x35\x8e\x4b\xc6\xf4\x01\x00\x00") + +func _000004_auth_table_up_sql() ([]byte, error) { + return bindata_read( + __000004_auth_table_up_sql, + "000004_auth_table.up.sql", + ) +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -123,10 +123,10 @@ var _bindata = map[string]func() ([]byte, error){ "000001_init.up.sql": _000001_init_up_sql, "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, - "000003_auth_table.down.sql": _000003_auth_table_down_sql, - "000003_auth_table.up.sql": _000003_auth_table_up_sql, "000003_blocks_rootid.down.sql": _000003_blocks_rootid_down_sql, "000003_blocks_rootid.up.sql": _000003_blocks_rootid_up_sql, + "000004_auth_table.down.sql": _000004_auth_table_down_sql, + "000004_auth_table.up.sql": _000004_auth_table_up_sql, } // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. @@ -176,12 +176,12 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ }}, "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ }}, - "000003_auth_table.down.sql": &_bintree_t{_000003_auth_table_down_sql, map[string]*_bintree_t{ - }}, - "000003_auth_table.up.sql": &_bintree_t{_000003_auth_table_up_sql, map[string]*_bintree_t{ - }}, "000003_blocks_rootid.down.sql": &_bintree_t{_000003_blocks_rootid_down_sql, map[string]*_bintree_t{ }}, "000003_blocks_rootid.up.sql": &_bintree_t{_000003_blocks_rootid_up_sql, map[string]*_bintree_t{ }}, + "000004_auth_table.down.sql": &_bintree_t{_000004_auth_table_down_sql, map[string]*_bintree_t{ + }}, + "000004_auth_table.up.sql": &_bintree_t{_000004_auth_table_up_sql, map[string]*_bintree_t{ + }}, }} diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.down.sql b/server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.down.sql similarity index 100% rename from server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.down.sql rename to server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.down.sql diff --git a/server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.up.sql b/server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.up.sql similarity index 100% rename from server/services/store/sqlstore/migrations/postgres_files/000003_auth_table.up.sql rename to server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.up.sql diff --git a/server/services/store/sqlstore/migrations/sqlite/bindata.go b/server/services/store/sqlstore/migrations/sqlite/bindata.go index 1c4b71d13..082031fb8 100644 --- a/server/services/store/sqlstore/migrations/sqlite/bindata.go +++ b/server/services/store/sqlstore/migrations/sqlite/bindata.go @@ -61,24 +61,6 @@ func _000002_system_settings_table_up_sql() ([]byte, error) { ) } -var __000003_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") - -func _000003_auth_table_down_sql() ([]byte, error) { - return bindata_read( - __000003_auth_table_down_sql, - "000003_auth_table.down.sql", - ) -} - -var __000003_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\xd0\xc1\x4a\x03\x31\x10\x06\xe0\xf3\xe6\x29\xe6\xb8\x0b\x3d\xd4\x42\x4f\x9e\xd2\x12\x35\xa8\x55\xd2\x20\xed\x69\x19\x36\x23\x06\xbb\x9b\x25\x93\xd5\xd7\x97\x20\x28\x76\xa3\x17\x73\xfc\xfe\x90\xc9\xfc\x5b\xa3\xa4\x55\x60\xe5\xe6\x4e\x81\xbe\x82\xdd\x83\x05\x75\xd0\x7b\xbb\x87\x89\x29\x32\xd4\xa2\xf2\x0e\x9e\xa4\xd9\xde\x48\x53\x5f\x2c\x97\xcd\x42\x54\x39\x1a\xb0\xa7\x73\xa7\x1e\xfd\xe9\x0b\x57\xeb\x75\xc6\x11\x99\xdf\x43\x3c\x7f\x04\x00\xa0\x7f\xc6\x96\xa9\x8b\x94\x0a\x21\x4e\xe9\xa5\x65\x8a\x6f\xbe\xfb\x1e\xb4\xfa\x91\x3a\x4c\x38\x1b\x17\xc3\xc8\xf0\x79\xac\x3a\xd8\x85\xa8\xba\x48\x98\xa8\xc5\x94\x6d\xa3\xaf\xf5\x2e\xeb\x34\xba\x82\x3a\x3a\xd1\x5c\x1f\x8d\xbe\x97\xe6\x08\xb7\xea\x08\xb5\x77\x8d\x68\x2e\x85\xf8\xa3\x3b\x26\x66\x1f\x86\x5f\xea\x4b\xe1\x95\x86\x52\xa7\xed\xfc\xee\x3f\xd7\x29\x7d\xfc\x23\x00\x00\xff\xff\xa1\xd6\x24\xe6\xf4\x01\x00\x00") - -func _000003_auth_table_up_sql() ([]byte, error) { - return bindata_read( - __000003_auth_table_up_sql, - "000003_auth_table.up.sql", - ) -} - var __000003_blocks_rootid_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x09\xf2\x0f\x50\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\xb1\xe6\x02\x04\x00\x00\xff\xff\x94\x1c\x55\xb9\x28\x00\x00\x00") func _000003_blocks_rootid_down_sql() ([]byte, error) { @@ -97,6 +79,24 @@ func _000003_blocks_rootid_up_sql() ([]byte, error) { ) } +var __000004_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") + +func _000004_auth_table_down_sql() ([]byte, error) { + return bindata_read( + __000004_auth_table_down_sql, + "000004_auth_table.down.sql", + ) +} + +var __000004_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\xd0\xc1\x4a\x03\x31\x10\x06\xe0\xf3\xe6\x29\xe6\xb8\x0b\x3d\xd4\x42\x4f\x9e\xd2\x12\x35\xa8\x55\xd2\x20\xed\x69\x19\x36\x23\x06\xbb\x9b\x25\x93\xd5\xd7\x97\x20\x28\x76\xa3\x17\x73\xfc\xfe\x90\xc9\xfc\x5b\xa3\xa4\x55\x60\xe5\xe6\x4e\x81\xbe\x82\xdd\x83\x05\x75\xd0\x7b\xbb\x87\x89\x29\x32\xd4\xa2\xf2\x0e\x9e\xa4\xd9\xde\x48\x53\x5f\x2c\x97\xcd\x42\x54\x39\x1a\xb0\xa7\x73\xa7\x1e\xfd\xe9\x0b\x57\xeb\x75\xc6\x11\x99\xdf\x43\x3c\x7f\x04\x00\xa0\x7f\xc6\x96\xa9\x8b\x94\x0a\x21\x4e\xe9\xa5\x65\x8a\x6f\xbe\xfb\x1e\xb4\xfa\x91\x3a\x4c\x38\x1b\x17\xc3\xc8\xf0\x79\xac\x3a\xd8\x85\xa8\xba\x48\x98\xa8\xc5\x94\x6d\xa3\xaf\xf5\x2e\xeb\x34\xba\x82\x3a\x3a\xd1\x5c\x1f\x8d\xbe\x97\xe6\x08\xb7\xea\x08\xb5\x77\x8d\x68\x2e\x85\xf8\xa3\x3b\x26\x66\x1f\x86\x5f\xea\x4b\xe1\x95\x86\x52\xa7\xed\xfc\xee\x3f\xd7\x29\x7d\xfc\x23\x00\x00\xff\xff\xa1\xd6\x24\xe6\xf4\x01\x00\x00") + +func _000004_auth_table_up_sql() ([]byte, error) { + return bindata_read( + __000004_auth_table_up_sql, + "000004_auth_table.up.sql", + ) +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -123,10 +123,10 @@ var _bindata = map[string]func() ([]byte, error){ "000001_init.up.sql": _000001_init_up_sql, "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, - "000003_auth_table.down.sql": _000003_auth_table_down_sql, - "000003_auth_table.up.sql": _000003_auth_table_up_sql, "000003_blocks_rootid.down.sql": _000003_blocks_rootid_down_sql, "000003_blocks_rootid.up.sql": _000003_blocks_rootid_up_sql, + "000004_auth_table.down.sql": _000004_auth_table_down_sql, + "000004_auth_table.up.sql": _000004_auth_table_up_sql, } // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. @@ -176,12 +176,12 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ }}, "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ }}, - "000003_auth_table.down.sql": &_bintree_t{_000003_auth_table_down_sql, map[string]*_bintree_t{ - }}, - "000003_auth_table.up.sql": &_bintree_t{_000003_auth_table_up_sql, map[string]*_bintree_t{ - }}, "000003_blocks_rootid.down.sql": &_bintree_t{_000003_blocks_rootid_down_sql, map[string]*_bintree_t{ }}, "000003_blocks_rootid.up.sql": &_bintree_t{_000003_blocks_rootid_up_sql, map[string]*_bintree_t{ }}, + "000004_auth_table.down.sql": &_bintree_t{_000004_auth_table_down_sql, map[string]*_bintree_t{ + }}, + "000004_auth_table.up.sql": &_bintree_t{_000004_auth_table_up_sql, map[string]*_bintree_t{ + }}, }} diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.down.sql b/server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.down.sql similarity index 100% rename from server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.down.sql rename to server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.down.sql diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.up.sql b/server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.up.sql similarity index 100% rename from server/services/store/sqlstore/migrations/sqlite_files/000003_auth_table.up.sql rename to server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.up.sql From e08f9a9c9683d079a6c43d995fc8d31d33c9950b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 7 Dec 2020 17:04:35 +0100 Subject: [PATCH 09/13] Improving sessions handling --- server/app/auth.go | 9 ++++++++- server/server/server.go | 24 ++++++++++++++++------- server/services/config/config.go | 24 +++++++++++++---------- server/services/store/sqlstore/session.go | 13 ++++++++++-- server/services/store/store.go | 3 ++- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/server/app/auth.go b/server/app/auth.go index 3bfec91d4..f44f87ed0 100644 --- a/server/app/auth.go +++ b/server/app/auth.go @@ -2,6 +2,7 @@ package app import ( "log" + "time" "github.com/google/uuid" "github.com/mattermost/mattermost-octo-tasks/server/model" @@ -10,14 +11,19 @@ import ( "github.com/pkg/errors" ) +// GetSession Get a user active session and refresh the session if is needed func (a *App) GetSession(token string) (*model.Session, error) { - session, err := a.store.GetSession(token) + session, err := a.store.GetSession(token, a.config.SessionExpireTime) if err != nil { return nil, errors.Wrap(err, "unable to get the session for the token") } + if session.UpdateAt < (time.Now().Unix() - a.config.SessionRefreshTime) { + a.store.RefreshSession(session) + } return session, nil } +// Login create a new user session if the authentication data is valid func (a *App) Login(username string, email string, password string, mfaToken string) (string, error) { var user *model.User if username != "" { @@ -59,6 +65,7 @@ func (a *App) Login(username string, email string, password string, mfaToken str return session.Token, nil } +// RegisterUser create a new user if the provided data is valid func (a *App) RegisterUser(username string, email string, password string) error { var user *model.User if username != "" { diff --git a/server/server/server.go b/server/server/server.go index 12f42ecea..f5202e4fd 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" "runtime" + "time" "github.com/google/uuid" "go.uber.org/zap" @@ -13,6 +14,7 @@ import ( "github.com/mattermost/mattermost-octo-tasks/server/api" "github.com/mattermost/mattermost-octo-tasks/server/app" "github.com/mattermost/mattermost-octo-tasks/server/services/config" + "github.com/mattermost/mattermost-octo-tasks/server/services/scheduler" "github.com/mattermost/mattermost-octo-tasks/server/services/store" "github.com/mattermost/mattermost-octo-tasks/server/services/store/sqlstore" "github.com/mattermost/mattermost-octo-tasks/server/services/telemetry" @@ -26,13 +28,14 @@ import ( const currentVersion = "0.0.1" type Server struct { - config *config.Configuration - wsServer *ws.Server - webServer *web.Server - store store.Store - filesBackend filesstore.FileBackend - telemetry *telemetry.Service - logger *zap.Logger + config *config.Configuration + wsServer *ws.Server + webServer *web.Server + store store.Store + filesBackend filesstore.FileBackend + telemetry *telemetry.Service + logger *zap.Logger + cleanUpSessionsTask *scheduler.ScheduledTask } func New(cfg *config.Configuration, singleUser bool) (*Server, error) { @@ -130,6 +133,11 @@ func (s *Server) Start() error { if err := s.webServer.Start(); err != nil { return err } + s.cleanUpSessionsTask = scheduler.CreateRecurringTask("cleanUpSessions", func() { + if err := s.store.CleanUpSessions(s.config.SessionExpireTime); err != nil { + s.logger.Error("Unable to clean up the sessions", zap.Error(err)) + } + }, 10*time.Minute) return nil } @@ -139,6 +147,8 @@ func (s *Server) Shutdown() error { return err } + s.cleanUpSessionsTask.Cancel() + return s.store.Shutdown() } diff --git a/server/services/config/config.go b/server/services/config/config.go index e33db199b..95a2adfba 100644 --- a/server/services/config/config.go +++ b/server/services/config/config.go @@ -13,16 +13,18 @@ const ( // Configuration is the app configuration stored in a json file. type Configuration struct { - ServerRoot string `json:"serverRoot" mapstructure:"serverRoot"` - Port int `json:"port" mapstructure:"port"` - DBType string `json:"dbtype" mapstructure:"dbtype"` - DBConfigString string `json:"dbconfig" mapstructure:"dbconfig"` - UseSSL bool `json:"useSSL" mapstructure:"useSSL"` - WebPath string `json:"webpath" mapstructure:"webpath"` - FilesPath string `json:"filespath" mapstructure:"filespath"` - Telemetry bool `json:"telemetry" mapstructure:"telemetry"` - WebhookUpdate []string `json:"webhook_update" mapstructure:"webhook_update"` - Secret string `json:"secret" mapstructure:"secret"` + ServerRoot string `json:"serverRoot" mapstructure:"serverRoot"` + Port int `json:"port" mapstructure:"port"` + DBType string `json:"dbtype" mapstructure:"dbtype"` + DBConfigString string `json:"dbconfig" mapstructure:"dbconfig"` + UseSSL bool `json:"useSSL" mapstructure:"useSSL"` + WebPath string `json:"webpath" mapstructure:"webpath"` + FilesPath string `json:"filespath" mapstructure:"filespath"` + Telemetry bool `json:"telemetry" mapstructure:"telemetry"` + WebhookUpdate []string `json:"webhook_update" mapstructure:"webhook_update"` + Secret string `json:"secret" mapstructure:"secret"` + SessionExpireTime int64 `json:"session_expire_time" mapstructure:"session_expire_time"` + SessionRefreshTime int64 `json:"session_refresh_time" mapstructure:"session_refresh_time"` } // ReadConfigFile read the configuration from the filesystem. @@ -37,6 +39,8 @@ func ReadConfigFile() (*Configuration, error) { viper.SetDefault("WebPath", "./pack") viper.SetDefault("FilesPath", "./files") viper.SetDefault("WebhookUpdate", nil) + viper.SetDefault("SessionExpireTime", 60*60*24*30) // 30 days session lifetime + viper.SetDefault("SessionRefreshTime", 60*60*5) // 5 minutes session refresh err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file diff --git a/server/services/store/sqlstore/session.go b/server/services/store/sqlstore/session.go index 30a7e28ed..86872b0a4 100644 --- a/server/services/store/sqlstore/session.go +++ b/server/services/store/sqlstore/session.go @@ -8,11 +8,12 @@ import ( "github.com/mattermost/mattermost-octo-tasks/server/model" ) -func (s *SQLStore) GetSession(token string) (*model.Session, error) { +func (s *SQLStore) GetSession(token string, expireTime int64) (*model.Session, error) { query := s.getQueryBuilder(). Select("id", "token", "user_id", "props"). From("sessions"). - Where(sq.Eq{"token": token}) + Where(sq.Eq{"token": token}). + Where(sq.Gt{"update_at": time.Now().Unix() - expireTime}) row := query.QueryRow() session := model.Session{} @@ -80,3 +81,11 @@ func (s *SQLStore) DeleteSession(sessionId string) error { _, err := query.Exec() return err } + +func (s *SQLStore) CleanUpSessions(expireTime int64) error { + query := s.getQueryBuilder().Delete("sessions"). + Where(sq.Lt{"update_at": time.Now().Unix() - expireTime}) + + _, err := query.Exec() + return err +} diff --git a/server/services/store/store.go b/server/services/store/store.go index c89839dcb..aa445a2e4 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -22,9 +22,10 @@ type Store interface { GetUserByUsername(username string) (*model.User, error) CreateUser(user *model.User) error UpdateUser(user *model.User) error - GetSession(token string) (*model.Session, error) + GetSession(token string, expireTime int64) (*model.Session, error) CreateSession(session *model.Session) error RefreshSession(session *model.Session) error UpdateSession(session *model.Session) error DeleteSession(sessionId string) error + CleanUpSessions(expireTime int64) error } From e5941d644087863927d8fbdf845cbe2df7d57603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 7 Dec 2020 20:40:16 +0100 Subject: [PATCH 10/13] More work on auth --- server/api/api.go | 77 +++++++++++++++++-- server/app/auth.go | 9 +++ server/services/store/sqlstore/user.go | 1 + webapp/src/app.tsx | 100 ++++++++++++++++--------- webapp/src/octoClient.ts | 8 ++ webapp/src/user.tsx | 17 +++++ 6 files changed, 168 insertions(+), 44 deletions(-) create mode 100644 webapp/src/user.tsx diff --git a/server/api/api.go b/server/api/api.go index 5f070d74d..d9d83b476 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/gorilla/mux" "github.com/mattermost/mattermost-octo-tasks/server/app" @@ -34,18 +35,21 @@ func (a *API) app() *app.App { func (a *API) RegisterRoutes(r *mux.Router) { r.HandleFunc("/api/v1/blocks", a.sessionRequired(a.handleGetBlocks)).Methods("GET") - r.HandleFunc("/api/v1/blocks", a.handlePostBlocks).Methods("POST") - r.HandleFunc("/api/v1/blocks/{blockID}", a.handleDeleteBlock).Methods("DELETE") - r.HandleFunc("/api/v1/blocks/{blockID}/subtree", a.handleGetSubTree).Methods("GET") + r.HandleFunc("/api/v1/blocks", a.sessionRequired(a.handlePostBlocks)).Methods("POST") + r.HandleFunc("/api/v1/blocks/{blockID}", a.sessionRequired(a.handleDeleteBlock)).Methods("DELETE") + r.HandleFunc("/api/v1/blocks/{blockID}/subtree", a.sessionRequired(a.handleGetSubTree)).Methods("GET") + + r.HandleFunc("/api/v1/users/me", a.sessionRequired(a.handleGetMe)).Methods("GET") + r.HandleFunc("/api/v1/users/{userID}", a.sessionRequired(a.handleGetUser)).Methods("GET") r.HandleFunc("/api/v1/login", a.handleLogin).Methods("POST") r.HandleFunc("/api/v1/register", a.handleRegister).Methods("POST") - r.HandleFunc("/api/v1/files", a.handleUploadFile).Methods("POST") - r.HandleFunc("/files/{filename}", a.handleServeFile).Methods("GET") + r.HandleFunc("/api/v1/files", a.sessionRequired(a.handleUploadFile)).Methods("POST") + r.HandleFunc("/files/{filename}", a.sessionRequired(a.handleServeFile)).Methods("GET") - r.HandleFunc("/api/v1/blocks/export", a.handleExport).Methods("GET") - r.HandleFunc("/api/v1/blocks/import", a.handleImport).Methods("POST") + r.HandleFunc("/api/v1/blocks/export", a.sessionRequired(a.handleExport)).Methods("GET") + r.HandleFunc("/api/v1/blocks/import", a.sessionRequired(a.handleImport)).Methods("POST") } func (a *API) handleGetBlocks(w http.ResponseWriter, r *http.Request) { @@ -137,6 +141,65 @@ func (a *API) handlePostBlocks(w http.ResponseWriter, r *http.Request) { jsonStringResponse(w, http.StatusOK, "{}") } +func (a *API) handleGetUser(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + userID := vars["userID"] + + user, err := a.app().GetUser(userID) + if err != nil { + log.Printf(`ERROR: %v`, r) + errorResponse(w, http.StatusInternalServerError, nil) + + return + } + + userData, err := json.Marshal(user) + if err != nil { + log.Printf(`ERROR: %v`, r) + errorResponse(w, http.StatusInternalServerError, nil) + + return + } + + jsonStringResponse(w, http.StatusOK, string(userData)) +} + +func (a *API) handleGetMe(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + session := ctx.Value("session").(*model.Session) + var user *model.User + var err error + + if session.UserID == "single-user" { + now := time.Now().Unix() + user = &model.User{ + ID: "single-user", + Username: "single-user", + Email: "single-user", + CreateAt: now, + UpdateAt: now, + } + } else { + user, err = a.app().GetUser(session.UserID) + if err != nil { + log.Printf(`ERROR: %v`, r) + errorResponse(w, http.StatusInternalServerError, nil) + + return + } + } + + userData, err := json.Marshal(user) + if err != nil { + log.Printf(`ERROR: %v`, r) + errorResponse(w, http.StatusInternalServerError, nil) + + return + } + + jsonStringResponse(w, http.StatusOK, string(userData)) +} + func (a *API) handleDeleteBlock(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) blockID := vars["blockID"] diff --git a/server/app/auth.go b/server/app/auth.go index f44f87ed0..3e5dcd1c2 100644 --- a/server/app/auth.go +++ b/server/app/auth.go @@ -23,6 +23,15 @@ func (a *App) GetSession(token string) (*model.Session, error) { return session, nil } +// GetUser Get an existing active user by id +func (a *App) GetUser(ID string) (*model.User, error) { + user, err := a.store.GetUserById(ID) + if err != nil { + return nil, errors.Wrap(err, "unable to get the session for the token") + } + return user, nil +} + // Login create a new user session if the authentication data is valid func (a *App) Login(username string, email string, password string, mfaToken string) (string, error) { var user *model.User diff --git a/server/services/store/sqlstore/user.go b/server/services/store/sqlstore/user.go index 5b82e10b4..48bb56e07 100644 --- a/server/services/store/sqlstore/user.go +++ b/server/services/store/sqlstore/user.go @@ -13,6 +13,7 @@ func (s *SQLStore) getUserByCondition(condition sq.Eq) (*model.User, error) { query := s.getQueryBuilder(). Select("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at"). From("users"). + Where(sq.Eq{"delete_at": 0}). Where(condition) row := query.QueryRow() user := model.User{} diff --git a/webapp/src/app.tsx b/webapp/src/app.tsx index 27f4c86cd..e01f895a1 100644 --- a/webapp/src/app.tsx +++ b/webapp/src/app.tsx @@ -1,6 +1,6 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React, {useState} from 'react' +import React from 'react' import {IntlProvider} from 'react-intl' import { @@ -11,6 +11,7 @@ import { } from 'react-router-dom' import client from './octoClient' +import {IUser, UserContext} from './user' import {getCurrentLanguage, getMessages, storeLanguage} from './i18n' @@ -20,40 +21,65 @@ import LoginPage from './pages/loginPage' import RegisterPage from './pages/registerPage' import BoardPage from './pages/boardPage' -export default function App(): JSX.Element { - const [language, setLanguage] = useState(getCurrentLanguage()) - const setAndStoreLanguage = (lang: string) => { - storeLanguage(lang) - setLanguage(lang) - } - return ( - - - -
-
- - - - - - - - - {!client.token && } - - - - {!client.token && } - - - -
-
-
-
- ) +type State = { + language: string, + user: IUser|null, + initialLoad: boolean, +} + +export default class App extends React.PureComponent { + constructor(props: unknown) { + super(props) + this.state = { + language: getCurrentLanguage(), + user: null, + initialLoad: false, + } + } + + public componentDidMount(): void { + client.getMe().then((user: IUser|null) => { + this.setState({user, initialLoad: true}) + }) + } + + setAndStoreLanguage = (lang: string): void => { + storeLanguage(lang) + this.setState({language: lang}) + } + + public render(): JSX.Element { + return ( + + + + +
+
+ + + + + + + + + {this.state.initialLoad && !this.state.user && } + + + + {this.state.initialLoad && !this.state.user && } + + + +
+
+
+
+
+ ) + } } diff --git a/webapp/src/octoClient.ts b/webapp/src/octoClient.ts index c394d0ea0..8d6a18f31 100644 --- a/webapp/src/octoClient.ts +++ b/webapp/src/octoClient.ts @@ -1,6 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import {IBlock, IMutableBlock} from './blocks/block' +import {IUser} from './user' import {Utils} from './utils' // @@ -58,6 +59,13 @@ class OctoClient { } } + async getMe(): Promise { + const path = '/api/v1/users/me' + const response = await fetch(this.serverUrl + path, {headers: this.headers()}) + const user = (await response.json()) as IUser || null + return user + } + async getSubtree(rootId?: string, levels = 2): Promise { const path = `/api/v1/blocks/${rootId}/subtree?l=${levels}` const response = await fetch(this.serverUrl + path, {headers: this.headers()}) diff --git a/webapp/src/user.tsx b/webapp/src/user.tsx new file mode 100644 index 000000000..4b80a1a2a --- /dev/null +++ b/webapp/src/user.tsx @@ -0,0 +1,17 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import React from 'react' + +const UserContext = React.createContext(null as IUser|null) + +interface IUser { + id: string, + username: string, + email: string, + props: Record, + createAt: number, + updateAt: number, +} + +export {IUser, UserContext} From dabfd2ea01041aa259c5c01e1a10c13ac1478575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 11 Jan 2021 15:16:39 +0100 Subject: [PATCH 11/13] Adding config for session expire and refresh --- config.json | 4 +++- webapp/src/octoClient.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index 174c25b4c..0ad2c57c4 100644 --- a/config.json +++ b/config.json @@ -10,5 +10,7 @@ "filespath": "./files", "telemetry": true, "webhook_update": [], - "secret": "this-is-a-secret-string" + "secret": "this-is-a-secret-string", + "session_expire_time": 2592000, + "session_refresh_time": 18000 } diff --git a/webapp/src/octoClient.ts b/webapp/src/octoClient.ts index 8d6a18f31..e52205359 100644 --- a/webapp/src/octoClient.ts +++ b/webapp/src/octoClient.ts @@ -51,7 +51,7 @@ class OctoClient { return false } - headers() { + private headers() { return { Accept: 'application/json', 'Content-Type': 'application/json', From 20a223a091efb493fb6fad5d56062f578275a95e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 11 Jan 2021 15:33:34 +0100 Subject: [PATCH 12/13] Fixing tests --- server/integrationtests/clienttestlib.go | 2 +- server/server/server.go | 4 +- server/services/auth/password_test.go | 3 +- server/services/store/mockstore/mockstore.go | 22 +- .../store/sqlstore/initializations/bindata.go | 95 +++-- .../sqlstore/migrations/postgres/bindata.go | 398 ++++++++++++++---- .../postgres_files/000004_auth_table.up.sql | 6 +- .../sqlstore/migrations/sqlite/bindata.go | 398 ++++++++++++++---- .../sqlite_files/000004_auth_table.up.sql | 6 +- 9 files changed, 743 insertions(+), 191 deletions(-) diff --git a/server/integrationtests/clienttestlib.go b/server/integrationtests/clienttestlib.go index b501acf31..51fb81b5f 100644 --- a/server/integrationtests/clienttestlib.go +++ b/server/integrationtests/clienttestlib.go @@ -28,7 +28,7 @@ func getTestConfig() *config.Configuration { func SetupTestHelper() *TestHelper { th := &TestHelper{} - srv, err := server.New(getTestConfig()) + srv, err := server.New(getTestConfig(), true) if err != nil { panic(err) } diff --git a/server/server/server.go b/server/server/server.go index f5202e4fd..621edf4dc 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -147,7 +147,9 @@ func (s *Server) Shutdown() error { return err } - s.cleanUpSessionsTask.Cancel() + if s.cleanUpSessionsTask != nil { + s.cleanUpSessionsTask.Cancel() + } return s.store.Shutdown() } diff --git a/server/services/auth/password_test.go b/server/services/auth/password_test.go index eb5af8e7f..cdde59e29 100644 --- a/server/services/auth/password_test.go +++ b/server/services/auth/password_test.go @@ -153,8 +153,7 @@ func TestIsPasswordValidWithSettings(t *testing.T) { }, } { t.Run(name, func(t *testing.T) { - as := New(tc.Settings) - err := as.IsPasswordValid(tc.Password) + err := IsPasswordValid(tc.Password, tc.Settings) if len(tc.ExpectedFailingCriterias) == 0 { assert.NoError(t, err) } else { diff --git a/server/services/store/mockstore/mockstore.go b/server/services/store/mockstore/mockstore.go index 22a72ce6b..68c54dd64 100644 --- a/server/services/store/mockstore/mockstore.go +++ b/server/services/store/mockstore/mockstore.go @@ -33,6 +33,20 @@ func (m *MockStore) EXPECT() *MockStoreMockRecorder { return m.recorder } +// CleanUpSessions mocks base method +func (m *MockStore) CleanUpSessions(arg0 int64) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CleanUpSessions", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// CleanUpSessions indicates an expected call of CleanUpSessions +func (mr *MockStoreMockRecorder) CleanUpSessions(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CleanUpSessions", reflect.TypeOf((*MockStore)(nil).CleanUpSessions), arg0) +} + // CreateSession mocks base method func (m *MockStore) CreateSession(arg0 *model.Session) error { m.ctrl.T.Helper() @@ -165,18 +179,18 @@ func (mr *MockStoreMockRecorder) GetParentID(arg0 interface{}) *gomock.Call { } // GetSession mocks base method -func (m *MockStore) GetSession(arg0 string) (*model.Session, error) { +func (m *MockStore) GetSession(arg0 string, arg1 int64) (*model.Session, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetSession", arg0) + ret := m.ctrl.Call(m, "GetSession", arg0, arg1) ret0, _ := ret[0].(*model.Session) ret1, _ := ret[1].(error) return ret0, ret1 } // GetSession indicates an expected call of GetSession -func (mr *MockStoreMockRecorder) GetSession(arg0 interface{}) *gomock.Call { +func (mr *MockStoreMockRecorder) GetSession(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSession", reflect.TypeOf((*MockStore)(nil).GetSession), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSession", reflect.TypeOf((*MockStore)(nil).GetSession), arg0, arg1) } // GetSubTree2 mocks base method diff --git a/server/services/store/sqlstore/initializations/bindata.go b/server/services/store/sqlstore/initializations/bindata.go index d4b4e7a5d..e22e6a8de 100644 --- a/server/services/store/sqlstore/initializations/bindata.go +++ b/server/services/store/sqlstore/initializations/bindata.go @@ -1,11 +1,13 @@ // Code generated by go-bindata. DO NOT EDIT. // sources: -// templates/templates.json +// templates/templates.json (22.926kB) + package initializations import ( "bytes" "compress/gzip" + "crypto/sha256" "fmt" "io" "io/ioutil" @@ -18,7 +20,7 @@ import ( func bindataRead(data []byte, name string) ([]byte, error) { gz, err := gzip.NewReader(bytes.NewBuffer(data)) if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %w", name, err) } var buf bytes.Buffer @@ -26,7 +28,7 @@ func bindataRead(data []byte, name string) ([]byte, error) { clErr := gz.Close() if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %w", name, err) } if clErr != nil { return nil, err @@ -36,8 +38,9 @@ func bindataRead(data []byte, name string) ([]byte, error) { } type asset struct { - bytes []byte - info os.FileInfo + bytes []byte + info os.FileInfo + digest [sha256.Size]byte } type bindataFileInfo struct { @@ -81,8 +84,8 @@ func templatesJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "templates.json", size: 22926, mode: os.FileMode(420), modTime: time.Unix(1609798890, 0)} - a := &asset{bytes: bytes, info: info} + info := bindataFileInfo{name: "templates.json", size: 22926, mode: os.FileMode(0644), modTime: time.Unix(1610369786, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x3, 0x15, 0x49, 0xe, 0xb2, 0xe7, 0x7, 0xd0, 0x6e, 0x35, 0x6f, 0xd0, 0x76, 0xe7, 0x1d, 0x9d, 0xc7, 0xa0, 0x55, 0x1, 0x25, 0x51, 0x9e, 0xd5, 0xf0, 0x81, 0x4c, 0x91, 0xd7, 0x33, 0x37, 0x3b}} return a, nil } @@ -90,8 +93,8 @@ func templatesJson() (*asset, error) { // It returns an error if the asset could not be found or // could not be loaded. func Asset(name string) ([]byte, error) { - cannonicalName := strings.Replace(name, "\\", "/", -1) - if f, ok := _bindata[cannonicalName]; ok { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { a, err := f() if err != nil { return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) @@ -101,6 +104,12 @@ func Asset(name string) ([]byte, error) { return nil, fmt.Errorf("Asset %s not found", name) } +// AssetString returns the asset contents as a string (instead of a []byte). +func AssetString(name string) (string, error) { + data, err := Asset(name) + return string(data), err +} + // MustAsset is like Asset but panics when Asset would return an error. // It simplifies safe initialization of global variables. func MustAsset(name string) []byte { @@ -112,12 +121,18 @@ func MustAsset(name string) []byte { return a } +// MustAssetString is like AssetString but panics when Asset would return an +// error. It simplifies safe initialization of global variables. +func MustAssetString(name string) string { + return string(MustAsset(name)) +} + // AssetInfo loads and returns the asset info for the given name. // It returns an error if the asset could not be found or // could not be loaded. func AssetInfo(name string) (os.FileInfo, error) { - cannonicalName := strings.Replace(name, "\\", "/", -1) - if f, ok := _bindata[cannonicalName]; ok { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { a, err := f() if err != nil { return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) @@ -127,6 +142,33 @@ func AssetInfo(name string) (os.FileInfo, error) { return nil, fmt.Errorf("AssetInfo %s not found", name) } +// AssetDigest returns the digest of the file with the given name. It returns an +// error if the asset could not be found or the digest could not be loaded. +func AssetDigest(name string) ([sha256.Size]byte, error) { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s can't read by error: %v", name, err) + } + return a.digest, nil + } + return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s not found", name) +} + +// Digests returns a map of all known files and their checksums. +func Digests() (map[string][sha256.Size]byte, error) { + mp := make(map[string][sha256.Size]byte, len(_bindata)) + for name := range _bindata { + a, err := _bindata[name]() + if err != nil { + return nil, err + } + mp[name] = a.digest + } + return mp, nil +} + // AssetNames returns the names of the assets. func AssetNames() []string { names := make([]string, 0, len(_bindata)) @@ -141,6 +183,9 @@ var _bindata = map[string]func() (*asset, error){ "templates.json": templatesJson, } +// AssetDebug is true if the assets were built with the debug flag enabled. +const AssetDebug = false + // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. // For example if you run go-bindata on data/... and data contains the @@ -150,15 +195,15 @@ var _bindata = map[string]func() (*asset, error){ // img/ // a.png // b.png -// then AssetDir("data") would return []string{"foo.txt", "img"} -// AssetDir("data/img") would return []string{"a.png", "b.png"} -// AssetDir("foo.txt") and AssetDir("notexist") would return an error +// then AssetDir("data") would return []string{"foo.txt", "img"}, +// AssetDir("data/img") would return []string{"a.png", "b.png"}, +// AssetDir("foo.txt") and AssetDir("notexist") would return an error, and // AssetDir("") will return []string{"data"}. func AssetDir(name string) ([]string, error) { node := _bintree if len(name) != 0 { - cannonicalName := strings.Replace(name, "\\", "/", -1) - pathList := strings.Split(cannonicalName, "/") + canonicalName := strings.Replace(name, "\\", "/", -1) + pathList := strings.Split(canonicalName, "/") for _, p := range pathList { node = node.Children[p] if node == nil { @@ -180,11 +225,12 @@ type bintree struct { Func func() (*asset, error) Children map[string]*bintree } + var _bintree = &bintree{nil, map[string]*bintree{ - "templates.json": &bintree{templatesJson, map[string]*bintree{}}, + "templates.json": {templatesJson, map[string]*bintree{}}, }} -// RestoreAsset restores an asset under the given directory +// RestoreAsset restores an asset under the given directory. func RestoreAsset(dir, name string) error { data, err := Asset(name) if err != nil { @@ -202,14 +248,10 @@ func RestoreAsset(dir, name string) error { if err != nil { return err } - err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) - if err != nil { - return err - } - return nil + return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) } -// RestoreAssets restores an asset under the given directory recursively +// RestoreAssets restores an asset under the given directory recursively. func RestoreAssets(dir, name string) error { children, err := AssetDir(name) // File @@ -227,7 +269,6 @@ func RestoreAssets(dir, name string) error { } func _filePath(dir, name string) string { - cannonicalName := strings.Replace(name, "\\", "/", -1) - return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) + canonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(canonicalName, "/")...)...) } - diff --git a/server/services/store/sqlstore/migrations/postgres/bindata.go b/server/services/store/sqlstore/migrations/postgres/bindata.go index 0547ecc59..0f10afc4b 100644 --- a/server/services/store/sqlstore/migrations/postgres/bindata.go +++ b/server/services/store/sqlstore/migrations/postgres/bindata.go @@ -1,113 +1,321 @@ +// Code generated by go-bindata. DO NOT EDIT. +// sources: +// postgres_files/000001_init.down.sql (19B) +// postgres_files/000001_init.up.sql (268B) +// postgres_files/000002_system_settings_table.down.sql (28B) +// postgres_files/000002_system_settings_table.up.sql (97B) +// postgres_files/000003_blocks_rootid.down.sql (40B) +// postgres_files/000003_blocks_rootid.up.sql (51B) +// postgres_files/000004_auth_table.down.sql (39B) +// postgres_files/000004_auth_table.up.sql (491B) + package postgres import ( "bytes" "compress/gzip" + "crypto/sha256" "fmt" "io" + "io/ioutil" + "os" + "path/filepath" "strings" + "time" ) -func bindata_read(data []byte, name string) ([]byte, error) { +func bindataRead(data []byte, name string) ([]byte, error) { gz, err := gzip.NewReader(bytes.NewBuffer(data)) if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %w", name, err) } var buf bytes.Buffer _, err = io.Copy(&buf, gz) - gz.Close() + clErr := gz.Close() if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %w", name, err) + } + if clErr != nil { + return nil, err } return buf.Bytes(), nil } -var __000001_init_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xb6\xe6\x02\x04\x00\x00\xff\xff\x45\xbe\x01\x0f\x13\x00\x00\x00") +type asset struct { + bytes []byte + info os.FileInfo + digest [sha256.Size]byte +} -func _000001_init_down_sql() ([]byte, error) { - return bindata_read( - __000001_init_down_sql, +type bindataFileInfo struct { + name string + size int64 + mode os.FileMode + modTime time.Time +} + +func (fi bindataFileInfo) Name() string { + return fi.name +} +func (fi bindataFileInfo) Size() int64 { + return fi.size +} +func (fi bindataFileInfo) Mode() os.FileMode { + return fi.mode +} +func (fi bindataFileInfo) ModTime() time.Time { + return fi.modTime +} +func (fi bindataFileInfo) IsDir() bool { + return false +} +func (fi bindataFileInfo) Sys() interface{} { + return nil +} + +var __000001_initDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xb6\xe6\x02\x04\x00\x00\xff\xff\x45\xbe\x01\x0f\x13\x00\x00\x00") + +func _000001_initDownSqlBytes() ([]byte, error) { + return bindataRead( + __000001_initDownSql, "000001_init.down.sql", ) } -var __000001_init_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x8f\x31\x4f\xc3\x30\x10\x85\xe7\xf8\x57\xbc\x31\x91\xb2\x21\xb1\x30\xb9\xe5\x0a\x86\xc4\xa9\x9c\x2b\xb4\x2c\x55\x88\x0f\x61\x11\x4a\x14\x9b\x81\x7f\x8f\xda\x21\x43\xba\xdd\x7d\xba\xef\x9e\xde\xda\x91\x66\x02\xeb\x55\x45\x30\x1b\xd8\x86\x41\x7b\xd3\x72\x8b\xf7\xe1\xa7\xff\x8a\xc8\x55\x16\x3c\x5e\xb4\x5b\x3f\x6a\x97\xdf\xdc\x16\xa5\xca\xc2\x29\xca\x94\x8e\x5d\x02\x9b\x9a\x5a\xd6\xf5\x96\xdf\x2e\xae\xdd\x55\x15\xee\x69\xa3\x77\x15\xc3\x36\xaf\xf9\xf9\x7c\xec\x26\x39\xa5\xe3\xd5\x9b\xd8\x7f\xca\x77\x87\x95\x79\x30\x96\x4b\x95\xa5\xbf\x51\xc0\xb4\xbf\xcc\x21\x0d\xf3\xf2\x11\x64\xf0\x11\x4f\x6d\x63\x4b\x95\xf5\x93\x74\x49\xce\xe9\xb3\xf9\x3b\xfa\x25\xf2\x32\xc8\x02\x6d\x9d\xa9\xb5\x3b\xe0\x99\x0e\xc8\x83\x2f\x31\xf7\x28\x54\x71\xa7\xfe\x03\x00\x00\xff\xff\xa3\xc9\xa2\x70\x0c\x01\x00\x00") +func _000001_initDownSql() (*asset, error) { + bytes, err := _000001_initDownSqlBytes() + if err != nil { + return nil, err + } -func _000001_init_up_sql() ([]byte, error) { - return bindata_read( - __000001_init_up_sql, + info := bindataFileInfo{name: "000001_init.down.sql", size: 19, mode: os.FileMode(0644), modTime: time.Unix(1602958996, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe4, 0x9a, 0x67, 0x49, 0xbf, 0xf7, 0xaa, 0x0, 0xe2, 0x48, 0xe4, 0xe2, 0xdc, 0x1, 0x8b, 0xa3, 0x24, 0x4, 0xcc, 0xbd, 0x3d, 0xbd, 0xf8, 0xec, 0xcf, 0x54, 0x9e, 0xc6, 0x83, 0x69, 0x7c, 0xb0}} + return a, nil +} + +var __000001_initUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x8f\x31\x4f\xc3\x30\x10\x85\xe7\xf8\x57\xbc\x31\x91\xb2\x21\xb1\x30\xb9\xe5\x0a\x86\xc4\xa9\x9c\x2b\xb4\x2c\x55\x88\x0f\x61\x11\x4a\x14\x9b\x81\x7f\x8f\xda\x21\x43\xba\xdd\x7d\xba\xef\x9e\xde\xda\x91\x66\x02\xeb\x55\x45\x30\x1b\xd8\x86\x41\x7b\xd3\x72\x8b\xf7\xe1\xa7\xff\x8a\xc8\x55\x16\x3c\x5e\xb4\x5b\x3f\x6a\x97\xdf\xdc\x16\xa5\xca\xc2\x29\xca\x94\x8e\x5d\x02\x9b\x9a\x5a\xd6\xf5\x96\xdf\x2e\xae\xdd\x55\x15\xee\x69\xa3\x77\x15\xc3\x36\xaf\xf9\xf9\x7c\xec\x26\x39\xa5\xe3\xd5\x9b\xd8\x7f\xca\x77\x87\x95\x79\x30\x96\x4b\x95\xa5\xbf\x51\xc0\xb4\xbf\xcc\x21\x0d\xf3\xf2\x11\x64\xf0\x11\x4f\x6d\x63\x4b\x95\xf5\x93\x74\x49\xce\xe9\xb3\xf9\x3b\xfa\x25\xf2\x32\xc8\x02\x6d\x9d\xa9\xb5\x3b\xe0\x99\x0e\xc8\x83\x2f\x31\xf7\x28\x54\x71\xa7\xfe\x03\x00\x00\xff\xff\xa3\xc9\xa2\x70\x0c\x01\x00\x00") + +func _000001_initUpSqlBytes() ([]byte, error) { + return bindataRead( + __000001_initUpSql, "000001_init.up.sql", ) } -var __000002_system_settings_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\xb6\xe6\x02\x04\x00\x00\xff\xff\x8b\x60\xbf\x1e\x1c\x00\x00\x00") +func _000001_initUpSql() (*asset, error) { + bytes, err := _000001_initUpSqlBytes() + if err != nil { + return nil, err + } -func _000002_system_settings_table_down_sql() ([]byte, error) { - return bindata_read( - __000002_system_settings_table_down_sql, + info := bindataFileInfo{name: "000001_init.up.sql", size: 268, mode: os.FileMode(0644), modTime: time.Unix(1603214842, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1d, 0xaf, 0xd8, 0x41, 0x91, 0x97, 0x98, 0xd, 0x84, 0x1f, 0x4, 0xf8, 0xc8, 0x17, 0xa3, 0x20, 0x3f, 0x5b, 0x65, 0xee, 0x26, 0xae, 0x17, 0x8, 0xe7, 0xe, 0x25, 0xca, 0x87, 0xaa, 0xd4, 0xd4}} + return a, nil +} + +var __000002_system_settings_tableDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\xb6\xe6\x02\x04\x00\x00\xff\xff\x8b\x60\xbf\x1e\x1c\x00\x00\x00") + +func _000002_system_settings_tableDownSqlBytes() ([]byte, error) { + return bindataRead( + __000002_system_settings_tableDownSql, "000002_system_settings_table.down.sql", ) } -var __000002_system_settings_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x0e\x72\x75\x0c\x71\x55\x08\x71\x74\xf2\x71\x55\xf0\x74\x53\xf0\xf3\x0f\x51\x70\x8d\xf0\x0c\x0e\x09\x56\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\x56\xd0\xe0\xe2\xcc\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x34\x30\xd0\xd4\xe1\xe2\x2c\x4b\xcc\x29\x4d\x55\x08\x71\x8d\x08\xd1\xe1\xe2\x0c\x08\xf2\xf4\x75\x0c\x8a\x54\xf0\x76\x8d\x54\xd0\xc8\x4c\xd1\xe4\xd2\xb4\xe6\x02\x04\x00\x00\xff\xff\x17\x95\xca\x5b\x61\x00\x00\x00") +func _000002_system_settings_tableDownSql() (*asset, error) { + bytes, err := _000002_system_settings_tableDownSqlBytes() + if err != nil { + return nil, err + } -func _000002_system_settings_table_up_sql() ([]byte, error) { - return bindata_read( - __000002_system_settings_table_up_sql, + info := bindataFileInfo{name: "000002_system_settings_table.down.sql", size: 28, mode: os.FileMode(0644), modTime: time.Unix(1603112131, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe4, 0xfd, 0xe6, 0x5d, 0xd1, 0xb2, 0xe9, 0x49, 0x14, 0x3b, 0xec, 0xb, 0x5f, 0x9d, 0x1d, 0x56, 0x13, 0x70, 0x76, 0x78, 0x7e, 0xd7, 0xd2, 0x57, 0x1e, 0xe7, 0x11, 0xb, 0xf9, 0xfb, 0x67, 0xb9}} + return a, nil +} + +var __000002_system_settings_tableUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x0e\x72\x75\x0c\x71\x55\x08\x71\x74\xf2\x71\x55\xf0\x74\x53\xf0\xf3\x0f\x51\x70\x8d\xf0\x0c\x0e\x09\x56\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\x56\xd0\xe0\xe2\xcc\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x34\x30\xd0\xd4\xe1\xe2\x2c\x4b\xcc\x29\x4d\x55\x08\x71\x8d\x08\xd1\xe1\xe2\x0c\x08\xf2\xf4\x75\x0c\x8a\x54\xf0\x76\x8d\x54\xd0\xc8\x4c\xd1\xe4\xd2\xb4\xe6\x02\x04\x00\x00\xff\xff\x17\x95\xca\x5b\x61\x00\x00\x00") + +func _000002_system_settings_tableUpSqlBytes() ([]byte, error) { + return bindataRead( + __000002_system_settings_tableUpSql, "000002_system_settings_table.up.sql", ) } -var __000003_blocks_rootid_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x09\xf2\x0f\x50\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\xb1\xe6\x02\x04\x00\x00\xff\xff\x94\x1c\x55\xb9\x28\x00\x00\x00") +func _000002_system_settings_tableUpSql() (*asset, error) { + bytes, err := _000002_system_settings_tableUpSqlBytes() + if err != nil { + return nil, err + } -func _000003_blocks_rootid_down_sql() ([]byte, error) { - return bindata_read( - __000003_blocks_rootid_down_sql, + info := bindataFileInfo{name: "000002_system_settings_table.up.sql", size: 97, mode: os.FileMode(0644), modTime: time.Unix(1603112131, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x5, 0xac, 0x61, 0xab, 0xf5, 0x91, 0x2f, 0xfd, 0xa8, 0x27, 0xa4, 0x63, 0xd1, 0x2d, 0xf4, 0x79, 0x2a, 0x9d, 0x78, 0x94, 0xb7, 0x7a, 0xaf, 0xcb, 0x9d, 0xae, 0x10, 0x89, 0xb3, 0xeb, 0x1e, 0x5d}} + return a, nil +} + +var __000003_blocks_rootidDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x09\xf2\x0f\x50\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\xb1\xe6\x02\x04\x00\x00\xff\xff\x94\x1c\x55\xb9\x28\x00\x00\x00") + +func _000003_blocks_rootidDownSqlBytes() ([]byte, error) { + return bindataRead( + __000003_blocks_rootidDownSql, "000003_blocks_rootid.down.sql", ) } -var __000003_blocks_rootid_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x74\x71\x51\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x36\xd3\xb4\xe6\x02\x04\x00\x00\xff\xff\xce\x60\x70\x4e\x33\x00\x00\x00") +func _000003_blocks_rootidDownSql() (*asset, error) { + bytes, err := _000003_blocks_rootidDownSqlBytes() + if err != nil { + return nil, err + } -func _000003_blocks_rootid_up_sql() ([]byte, error) { - return bindata_read( - __000003_blocks_rootid_up_sql, + info := bindataFileInfo{name: "000003_blocks_rootid.down.sql", size: 40, mode: os.FileMode(0644), modTime: time.Unix(1607094225, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1a, 0xb4, 0xdd, 0x99, 0x6e, 0xb8, 0x1a, 0xfd, 0x54, 0x43, 0x8f, 0x9, 0x3a, 0xd1, 0xe4, 0x32, 0x20, 0x3b, 0x43, 0xf4, 0x61, 0x17, 0x9b, 0xff, 0x85, 0xb0, 0x9a, 0x48, 0x9f, 0xfd, 0xbb, 0xcc}} + return a, nil +} + +var __000003_blocks_rootidUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x74\x71\x51\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x36\xd3\xb4\xe6\x02\x04\x00\x00\xff\xff\xce\x60\x70\x4e\x33\x00\x00\x00") + +func _000003_blocks_rootidUpSqlBytes() ([]byte, error) { + return bindataRead( + __000003_blocks_rootidUpSql, "000003_blocks_rootid.up.sql", ) } -var __000004_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") +func _000003_blocks_rootidUpSql() (*asset, error) { + bytes, err := _000003_blocks_rootidUpSqlBytes() + if err != nil { + return nil, err + } -func _000004_auth_table_down_sql() ([]byte, error) { - return bindata_read( - __000004_auth_table_down_sql, + info := bindataFileInfo{name: "000003_blocks_rootid.up.sql", size: 51, mode: os.FileMode(0644), modTime: time.Unix(1607094225, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x20, 0x3e, 0xe3, 0x25, 0xb9, 0x60, 0x1a, 0xc2, 0xbf, 0x51, 0x2e, 0xf0, 0xbb, 0x9e, 0x61, 0xb9, 0x16, 0x19, 0xc0, 0x6, 0x7a, 0x81, 0x1c, 0xce, 0xa0, 0xea, 0x91, 0xb2, 0xf, 0xb9, 0xba, 0x91}} + return a, nil +} + +var __000004_auth_tableDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") + +func _000004_auth_tableDownSqlBytes() ([]byte, error) { + return bindataRead( + __000004_auth_tableDownSql, "000004_auth_table.down.sql", ) } -var __000004_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x90\x4f\x4b\x03\x31\x10\xc5\xcf\x9b\x4f\x31\xc7\x5d\xe8\xa1\x16\x7a\xf2\x94\x96\xa8\xf1\xcf\x56\xb2\x41\xec\x69\x19\x36\x23\x06\xbb\x7f\xc8\xa4\xfa\xf5\x25\x08\x8a\xdd\xe8\xa5\x73\xfc\xbd\x61\xde\xbc\xb7\x35\x4a\x5a\x05\x56\x6e\xee\x15\xe8\x2b\xa8\x77\x16\xd4\xb3\x6e\x6c\x03\x47\xa6\xc0\x50\x8a\xc2\x3b\x78\x92\x66\x7b\x23\x4d\x79\xb1\x5c\x56\x0b\x51\x24\x69\xc0\x9e\x4e\x39\xf5\xe8\x0f\xdf\x70\xb5\x5e\x27\x38\x21\xf3\xc7\x18\x4e\x8f\x00\x00\xf4\x2f\xd8\x32\x75\x81\x62\x46\xc4\x63\x7c\x6d\x99\xc2\xbb\xef\x7e\x8c\x56\xbf\x54\x87\x11\x67\x76\x61\x9c\x18\xbe\xe6\xb6\xd9\xd5\x0b\x51\x74\x81\x30\x52\x8b\x31\xb1\x8d\xbe\xd6\xb5\x4d\x21\x26\x97\xa1\x8e\x0e\x34\xa7\x8f\x46\x3f\x48\xb3\x87\x3b\xb5\x87\xd2\xbb\x4a\x54\x97\x42\xfc\xd3\x1d\x13\xb3\x1f\x87\x3f\xea\x8b\xe3\x1b\x0d\xb9\x4e\xdb\xf9\xee\x99\x71\x72\x8f\x7f\x06\x00\x00\xff\xff\x35\x8e\x4b\xc6\xf4\x01\x00\x00") +func _000004_auth_tableDownSql() (*asset, error) { + bytes, err := _000004_auth_tableDownSqlBytes() + if err != nil { + return nil, err + } -func _000004_auth_table_up_sql() ([]byte, error) { - return bindata_read( - __000004_auth_table_up_sql, + info := bindataFileInfo{name: "000004_auth_table.down.sql", size: 39, mode: os.FileMode(0644), modTime: time.Unix(1606923754, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xb6, 0x5b, 0x33, 0x33, 0xd4, 0x68, 0x4, 0xb4, 0xfd, 0x61, 0x61, 0x33, 0x1b, 0xc6, 0x44, 0x5e, 0x57, 0xd0, 0xe7, 0x21, 0x36, 0xff, 0x10, 0x1a, 0xc2, 0xa2, 0xe7, 0x3a, 0xbd, 0x2e, 0xba, 0x66}} + return a, nil +} + +var __000004_auth_tableUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x90\x4f\x4b\x03\x31\x10\xc5\xcf\x9b\x4f\x31\xc7\x5d\xe8\xa1\x16\x7a\xf2\x94\x96\xa8\xf1\xcf\x56\xb2\x41\xec\x69\x19\x36\x23\x06\xbb\x7f\xc8\xa4\xfa\xf5\x25\x08\x16\x9a\xd5\x4b\xe7\xf8\x7b\xc3\xbc\x37\x6f\x6b\x94\xb4\x0a\xac\xdc\x3c\x2a\xd0\x37\x50\xef\x2c\xa8\x57\xdd\xd8\x06\x8e\x4c\x81\xa1\x14\x85\x77\xf0\x22\xcd\xf6\x4e\x9a\xf2\x6a\xb9\xac\x16\xa2\x48\xd2\x80\x3d\x9d\x73\xea\xd1\x1f\x7e\xe1\x6a\xbd\x4e\x70\x42\xe6\xaf\x31\x64\x47\xfa\x37\x6c\x99\xba\x40\xf1\x5c\xc1\x63\x7c\x6f\x99\xc2\xa7\xef\x4e\x16\xab\x93\xe4\x30\x62\xe6\x12\xc6\x89\xe1\x67\xee\x9b\x5d\xbd\x10\x45\x17\x08\x23\xb5\x18\x13\xdb\xe8\x5b\x5d\xdb\x94\x7d\x72\x33\xd4\xd1\x81\x72\xfa\x6c\xf4\x93\x34\x7b\x78\x50\x7b\x28\xbd\xab\x44\x75\x2d\xc4\x3f\x95\x31\x31\xfb\x71\xf8\xa3\xb5\x38\x7e\xd0\x30\x57\x65\x9b\xef\x5e\xf8\xce\x5c\xf0\xef\x00\x00\x00\xff\xff\x16\x8f\x58\x39\xeb\x01\x00\x00") + +func _000004_auth_tableUpSqlBytes() ([]byte, error) { + return bindataRead( + __000004_auth_tableUpSql, "000004_auth_table.up.sql", ) } +func _000004_auth_tableUpSql() (*asset, error) { + bytes, err := _000004_auth_tableUpSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "000004_auth_table.up.sql", size: 491, mode: os.FileMode(0644), modTime: time.Unix(1610375570, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x51, 0xbe, 0x84, 0x23, 0xcb, 0x3b, 0x88, 0xbf, 0x9e, 0xd, 0x32, 0x46, 0x47, 0xe1, 0x6e, 0xb2, 0x2e, 0x36, 0x6b, 0xde, 0x38, 0x7f, 0x0, 0x27, 0xd8, 0x10, 0x7e, 0xf2, 0xa4, 0x6, 0x73, 0xd}} + return a, nil +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. func Asset(name string) ([]byte, error) { - cannonicalName := strings.Replace(name, "\\", "/", -1) - if f, ok := _bindata[cannonicalName]; ok { - return f() + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) + } + return a.bytes, nil } return nil, fmt.Errorf("Asset %s not found", name) } +// AssetString returns the asset contents as a string (instead of a []byte). +func AssetString(name string) (string, error) { + data, err := Asset(name) + return string(data), err +} + +// MustAsset is like Asset but panics when Asset would return an error. +// It simplifies safe initialization of global variables. +func MustAsset(name string) []byte { + a, err := Asset(name) + if err != nil { + panic("asset: Asset(" + name + "): " + err.Error()) + } + + return a +} + +// MustAssetString is like AssetString but panics when Asset would return an +// error. It simplifies safe initialization of global variables. +func MustAssetString(name string) string { + return string(MustAsset(name)) +} + +// AssetInfo loads and returns the asset info for the given name. +// It returns an error if the asset could not be found or +// could not be loaded. +func AssetInfo(name string) (os.FileInfo, error) { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) + } + return a.info, nil + } + return nil, fmt.Errorf("AssetInfo %s not found", name) +} + +// AssetDigest returns the digest of the file with the given name. It returns an +// error if the asset could not be found or the digest could not be loaded. +func AssetDigest(name string) ([sha256.Size]byte, error) { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s can't read by error: %v", name, err) + } + return a.digest, nil + } + return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s not found", name) +} + +// Digests returns a map of all known files and their checksums. +func Digests() (map[string][sha256.Size]byte, error) { + mp := make(map[string][sha256.Size]byte, len(_bindata)) + for name := range _bindata { + a, err := _bindata[name]() + if err != nil { + return nil, err + } + mp[name] = a.digest + } + return mp, nil +} + // AssetNames returns the names of the assets. func AssetNames() []string { names := make([]string, 0, len(_bindata)) @@ -118,16 +326,20 @@ func AssetNames() []string { } // _bindata is a table, holding each asset generator, mapped to its name. -var _bindata = map[string]func() ([]byte, error){ - "000001_init.down.sql": _000001_init_down_sql, - "000001_init.up.sql": _000001_init_up_sql, - "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, - "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, - "000003_blocks_rootid.down.sql": _000003_blocks_rootid_down_sql, - "000003_blocks_rootid.up.sql": _000003_blocks_rootid_up_sql, - "000004_auth_table.down.sql": _000004_auth_table_down_sql, - "000004_auth_table.up.sql": _000004_auth_table_up_sql, +var _bindata = map[string]func() (*asset, error){ + "000001_init.down.sql": _000001_initDownSql, + "000001_init.up.sql": _000001_initUpSql, + "000002_system_settings_table.down.sql": _000002_system_settings_tableDownSql, + "000002_system_settings_table.up.sql": _000002_system_settings_tableUpSql, + "000003_blocks_rootid.down.sql": _000003_blocks_rootidDownSql, + "000003_blocks_rootid.up.sql": _000003_blocks_rootidUpSql, + "000004_auth_table.down.sql": _000004_auth_tableDownSql, + "000004_auth_table.up.sql": _000004_auth_tableUpSql, } + +// AssetDebug is true if the assets were built with the debug flag enabled. +const AssetDebug = false + // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. // For example if you run go-bindata on data/... and data contains the @@ -137,15 +349,15 @@ var _bindata = map[string]func() ([]byte, error){ // img/ // a.png // b.png -// then AssetDir("data") would return []string{"foo.txt", "img"} -// AssetDir("data/img") would return []string{"a.png", "b.png"} -// AssetDir("foo.txt") and AssetDir("notexist") would return an error +// then AssetDir("data") would return []string{"foo.txt", "img"}, +// AssetDir("data/img") would return []string{"a.png", "b.png"}, +// AssetDir("foo.txt") and AssetDir("notexist") would return an error, and // AssetDir("") will return []string{"data"}. func AssetDir(name string) ([]string, error) { node := _bintree if len(name) != 0 { - cannonicalName := strings.Replace(name, "\\", "/", -1) - pathList := strings.Split(cannonicalName, "/") + canonicalName := strings.Replace(name, "\\", "/", -1) + pathList := strings.Split(canonicalName, "/") for _, p := range pathList { node = node.Children[p] if node == nil { @@ -157,31 +369,67 @@ func AssetDir(name string) ([]string, error) { return nil, fmt.Errorf("Asset %s not found", name) } rv := make([]string, 0, len(node.Children)) - for name := range node.Children { - rv = append(rv, name) + for childName := range node.Children { + rv = append(rv, childName) } return rv, nil } -type _bintree_t struct { - Func func() ([]byte, error) - Children map[string]*_bintree_t +type bintree struct { + Func func() (*asset, error) + Children map[string]*bintree } -var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ - "000001_init.down.sql": &_bintree_t{_000001_init_down_sql, map[string]*_bintree_t{ - }}, - "000001_init.up.sql": &_bintree_t{_000001_init_up_sql, map[string]*_bintree_t{ - }}, - "000002_system_settings_table.down.sql": &_bintree_t{_000002_system_settings_table_down_sql, map[string]*_bintree_t{ - }}, - "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ - }}, - "000003_blocks_rootid.down.sql": &_bintree_t{_000003_blocks_rootid_down_sql, map[string]*_bintree_t{ - }}, - "000003_blocks_rootid.up.sql": &_bintree_t{_000003_blocks_rootid_up_sql, map[string]*_bintree_t{ - }}, - "000004_auth_table.down.sql": &_bintree_t{_000004_auth_table_down_sql, map[string]*_bintree_t{ - }}, - "000004_auth_table.up.sql": &_bintree_t{_000004_auth_table_up_sql, map[string]*_bintree_t{ - }}, + +var _bintree = &bintree{nil, map[string]*bintree{ + "000001_init.down.sql": {_000001_initDownSql, map[string]*bintree{}}, + "000001_init.up.sql": {_000001_initUpSql, map[string]*bintree{}}, + "000002_system_settings_table.down.sql": {_000002_system_settings_tableDownSql, map[string]*bintree{}}, + "000002_system_settings_table.up.sql": {_000002_system_settings_tableUpSql, map[string]*bintree{}}, + "000003_blocks_rootid.down.sql": {_000003_blocks_rootidDownSql, map[string]*bintree{}}, + "000003_blocks_rootid.up.sql": {_000003_blocks_rootidUpSql, map[string]*bintree{}}, + "000004_auth_table.down.sql": {_000004_auth_tableDownSql, map[string]*bintree{}}, + "000004_auth_table.up.sql": {_000004_auth_tableUpSql, map[string]*bintree{}}, }} + +// RestoreAsset restores an asset under the given directory. +func RestoreAsset(dir, name string) error { + data, err := Asset(name) + if err != nil { + return err + } + info, err := AssetInfo(name) + if err != nil { + return err + } + err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755)) + if err != nil { + return err + } + err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + if err != nil { + return err + } + return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) +} + +// RestoreAssets restores an asset under the given directory recursively. +func RestoreAssets(dir, name string) error { + children, err := AssetDir(name) + // File + if err != nil { + return RestoreAsset(dir, name) + } + // Dir + for _, child := range children { + err = RestoreAssets(dir, filepath.Join(name, child)) + if err != nil { + return err + } + } + return nil +} + +func _filePath(dir, name string) string { + canonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(canonicalName, "/")...)...) +} diff --git a/server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.up.sql b/server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.up.sql index aa91258cb..7126d1c48 100644 --- a/server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.up.sql +++ b/server/services/store/sqlstore/migrations/postgres_files/000004_auth_table.up.sql @@ -3,9 +3,9 @@ CREATE TABLE IF NOT EXISTS users ( username VARCHAR(100), email VARCHAR(255), password VARCHAR(100), - mfa_secret VARCHAR(100), - auth_service VARCHAR(20), - auth_data VARCHAR(255), + mfa_secret VARCHAR(100), + auth_service VARCHAR(20), + auth_data VARCHAR(255), props JSON, create_at BIGINT, update_at BIGINT, diff --git a/server/services/store/sqlstore/migrations/sqlite/bindata.go b/server/services/store/sqlstore/migrations/sqlite/bindata.go index 082031fb8..58e8d8fbc 100644 --- a/server/services/store/sqlstore/migrations/sqlite/bindata.go +++ b/server/services/store/sqlstore/migrations/sqlite/bindata.go @@ -1,113 +1,321 @@ +// Code generated by go-bindata. DO NOT EDIT. +// sources: +// sqlite_files/000001_init.down.sql (19B) +// sqlite_files/000001_init.up.sql (297B) +// sqlite_files/000002_system_settings_table.down.sql (28B) +// sqlite_files/000002_system_settings_table.up.sql (96B) +// sqlite_files/000003_blocks_rootid.down.sql (40B) +// sqlite_files/000003_blocks_rootid.up.sql (51B) +// sqlite_files/000004_auth_table.down.sql (39B) +// sqlite_files/000004_auth_table.up.sql (491B) + package sqlite import ( "bytes" "compress/gzip" + "crypto/sha256" "fmt" "io" + "io/ioutil" + "os" + "path/filepath" "strings" + "time" ) -func bindata_read(data []byte, name string) ([]byte, error) { +func bindataRead(data []byte, name string) ([]byte, error) { gz, err := gzip.NewReader(bytes.NewBuffer(data)) if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %w", name, err) } var buf bytes.Buffer _, err = io.Copy(&buf, gz) - gz.Close() + clErr := gz.Close() if err != nil { - return nil, fmt.Errorf("Read %q: %v", name, err) + return nil, fmt.Errorf("read %q: %w", name, err) + } + if clErr != nil { + return nil, err } return buf.Bytes(), nil } -var __000001_init_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xb6\xe6\x02\x04\x00\x00\xff\xff\x45\xbe\x01\x0f\x13\x00\x00\x00") +type asset struct { + bytes []byte + info os.FileInfo + digest [sha256.Size]byte +} -func _000001_init_down_sql() ([]byte, error) { - return bindata_read( - __000001_init_down_sql, +type bindataFileInfo struct { + name string + size int64 + mode os.FileMode + modTime time.Time +} + +func (fi bindataFileInfo) Name() string { + return fi.name +} +func (fi bindataFileInfo) Size() int64 { + return fi.size +} +func (fi bindataFileInfo) Mode() os.FileMode { + return fi.mode +} +func (fi bindataFileInfo) ModTime() time.Time { + return fi.modTime +} +func (fi bindataFileInfo) IsDir() bool { + return false +} +func (fi bindataFileInfo) Sys() interface{} { + return nil +} + +var __000001_initDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xb6\xe6\x02\x04\x00\x00\xff\xff\x45\xbe\x01\x0f\x13\x00\x00\x00") + +func _000001_initDownSqlBytes() ([]byte, error) { + return bindataRead( + __000001_initDownSql, "000001_init.down.sql", ) } -var __000001_init_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x8f\xbf\x6e\x83\x30\x10\x87\x67\xfc\x14\xb7\x58\x18\x89\x4c\x95\x3a\xa4\x93\x93\x1c\x8d\x55\x20\x95\xb9\xb4\x61\x8a\x28\xbe\xa8\x56\x49\x8a\xc0\x1d\xfa\xf6\x15\x91\xca\x90\x6c\xf7\xfb\x4e\xdf\xfd\x59\x5b\xd4\x84\x40\x7a\x95\x23\x98\x0c\xca\x1d\x01\x1e\x4c\x45\x15\x7c\x74\xdf\xed\xd7\x08\x4a\x44\xde\xc1\x9b\xb6\xeb\xad\xb6\xea\xe1\x31\x49\x45\xe4\x2f\x23\x0f\xe1\xd8\x04\xd8\x68\x42\x32\x05\x5e\xc5\x72\x9f\xe7\xb0\xc1\x4c\xef\x73\x52\x15\xd9\x6c\xea\xa8\x58\xd6\x0b\x79\x5e\x48\x07\x72\xbb\x94\xc5\x52\x9e\xe2\x14\xe2\x72\xf7\x1e\x27\xd3\xac\xbe\x19\xf8\x12\x8e\x77\x3b\xc6\xf6\x93\xcf\x0d\xac\xcc\xb3\x29\x29\x15\x51\xf8\xed\x19\x08\x0f\xd7\xda\x87\x6e\x0e\x27\xcf\x9d\x1b\xff\x53\x3b\x70\x13\x78\x3a\x6d\x36\x7f\x7a\x77\x8b\x1c\x77\x7c\x83\x5e\xad\x29\xb4\xad\xe1\x05\x6b\x50\xde\xa5\x30\x3f\x99\x88\xe4\x49\xfc\x05\x00\x00\xff\xff\xa2\x33\x30\x8e\x29\x01\x00\x00") +func _000001_initDownSql() (*asset, error) { + bytes, err := _000001_initDownSqlBytes() + if err != nil { + return nil, err + } -func _000001_init_up_sql() ([]byte, error) { - return bindata_read( - __000001_init_up_sql, + info := bindataFileInfo{name: "000001_init.down.sql", size: 19, mode: os.FileMode(0644), modTime: time.Unix(1602958996, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe4, 0x9a, 0x67, 0x49, 0xbf, 0xf7, 0xaa, 0x0, 0xe2, 0x48, 0xe4, 0xe2, 0xdc, 0x1, 0x8b, 0xa3, 0x24, 0x4, 0xcc, 0xbd, 0x3d, 0xbd, 0xf8, 0xec, 0xcf, 0x54, 0x9e, 0xc6, 0x83, 0x69, 0x7c, 0xb0}} + return a, nil +} + +var __000001_initUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x8f\xbf\x6e\x83\x30\x10\x87\x67\xfc\x14\xb7\x58\x18\x89\x4c\x95\x3a\xa4\x93\x93\x1c\x8d\x55\x20\x95\xb9\xb4\x61\x8a\x28\xbe\xa8\x56\x49\x8a\xc0\x1d\xfa\xf6\x15\x91\xca\x90\x6c\xf7\xfb\x4e\xdf\xfd\x59\x5b\xd4\x84\x40\x7a\x95\x23\x98\x0c\xca\x1d\x01\x1e\x4c\x45\x15\x7c\x74\xdf\xed\xd7\x08\x4a\x44\xde\xc1\x9b\xb6\xeb\xad\xb6\xea\xe1\x31\x49\x45\xe4\x2f\x23\x0f\xe1\xd8\x04\xd8\x68\x42\x32\x05\x5e\xc5\x72\x9f\xe7\xb0\xc1\x4c\xef\x73\x52\x15\xd9\x6c\xea\xa8\x58\xd6\x0b\x79\x5e\x48\x07\x72\xbb\x94\xc5\x52\x9e\xe2\x14\xe2\x72\xf7\x1e\x27\xd3\xac\xbe\x19\xf8\x12\x8e\x77\x3b\xc6\xf6\x93\xcf\x0d\xac\xcc\xb3\x29\x29\x15\x51\xf8\xed\x19\x08\x0f\xd7\xda\x87\x6e\x0e\x27\xcf\x9d\x1b\xff\x53\x3b\x70\x13\x78\x3a\x6d\x36\x7f\x7a\x77\x8b\x1c\x77\x7c\x83\x5e\xad\x29\xb4\xad\xe1\x05\x6b\x50\xde\xa5\x30\x3f\x99\x88\xe4\x49\xfc\x05\x00\x00\xff\xff\xa2\x33\x30\x8e\x29\x01\x00\x00") + +func _000001_initUpSqlBytes() ([]byte, error) { + return bindataRead( + __000001_initUpSql, "000001_init.up.sql", ) } -var __000002_system_settings_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\xb6\xe6\x02\x04\x00\x00\xff\xff\x8b\x60\xbf\x1e\x1c\x00\x00\x00") +func _000001_initUpSql() (*asset, error) { + bytes, err := _000001_initUpSqlBytes() + if err != nil { + return nil, err + } -func _000002_system_settings_table_down_sql() ([]byte, error) { - return bindata_read( - __000002_system_settings_table_down_sql, + info := bindataFileInfo{name: "000001_init.up.sql", size: 297, mode: os.FileMode(0644), modTime: time.Unix(1603029845, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xcc, 0xb4, 0xcf, 0xde, 0xfb, 0xcc, 0xa1, 0xf8, 0x73, 0x51, 0x3b, 0xbf, 0x6a, 0xb0, 0xd0, 0x6b, 0xdf, 0xb0, 0x76, 0xaf, 0x4e, 0x89, 0x21, 0xc7, 0x8e, 0xe8, 0x18, 0x50, 0xc7, 0x9, 0xd8, 0xbf}} + return a, nil +} + +var __000002_system_settings_tableDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\xb6\xe6\x02\x04\x00\x00\xff\xff\x8b\x60\xbf\x1e\x1c\x00\x00\x00") + +func _000002_system_settings_tableDownSqlBytes() ([]byte, error) { + return bindataRead( + __000002_system_settings_tableDownSql, "000002_system_settings_table.down.sql", ) } -var __000002_system_settings_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x0e\x72\x75\x0c\x71\x55\x08\x71\x74\xf2\x71\x55\xf0\x74\x53\xf0\xf3\x0f\x51\x70\x8d\xf0\x0c\x0e\x09\x56\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\xd6\xe0\xe2\xcc\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x34\x30\xd0\xd4\xe1\xe2\x2c\x4b\xcc\x29\x4d\x55\x08\x71\x8d\x08\xd1\xe1\xe2\x0c\x08\xf2\xf4\x75\x0c\x8a\x54\xf0\x76\x8d\x54\xd0\xc8\x4c\xd1\xe4\xd2\xb4\xe6\x02\x04\x00\x00\xff\xff\x1e\xfb\x02\xf2\x60\x00\x00\x00") +func _000002_system_settings_tableDownSql() (*asset, error) { + bytes, err := _000002_system_settings_tableDownSqlBytes() + if err != nil { + return nil, err + } -func _000002_system_settings_table_up_sql() ([]byte, error) { - return bindata_read( - __000002_system_settings_table_up_sql, + info := bindataFileInfo{name: "000002_system_settings_table.down.sql", size: 28, mode: os.FileMode(0644), modTime: time.Unix(1603112131, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe4, 0xfd, 0xe6, 0x5d, 0xd1, 0xb2, 0xe9, 0x49, 0x14, 0x3b, 0xec, 0xb, 0x5f, 0x9d, 0x1d, 0x56, 0x13, 0x70, 0x76, 0x78, 0x7e, 0xd7, 0xd2, 0x57, 0x1e, 0xe7, 0x11, 0xb, 0xf9, 0xfb, 0x67, 0xb9}} + return a, nil +} + +var __000002_system_settings_tableUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x0e\x72\x75\x0c\x71\x55\x08\x71\x74\xf2\x71\x55\xf0\x74\x53\xf0\xf3\x0f\x51\x70\x8d\xf0\x0c\x0e\x09\x56\x28\xae\x2c\x2e\x49\xcd\x8d\x2f\x4e\x2d\x29\xc9\xcc\x4b\x2f\xd6\xe0\xe2\xcc\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x34\x30\xd0\xd4\xe1\xe2\x2c\x4b\xcc\x29\x4d\x55\x08\x71\x8d\x08\xd1\xe1\xe2\x0c\x08\xf2\xf4\x75\x0c\x8a\x54\xf0\x76\x8d\x54\xd0\xc8\x4c\xd1\xe4\xd2\xb4\xe6\x02\x04\x00\x00\xff\xff\x1e\xfb\x02\xf2\x60\x00\x00\x00") + +func _000002_system_settings_tableUpSqlBytes() ([]byte, error) { + return bindataRead( + __000002_system_settings_tableUpSql, "000002_system_settings_table.up.sql", ) } -var __000003_blocks_rootid_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x09\xf2\x0f\x50\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\xb1\xe6\x02\x04\x00\x00\xff\xff\x94\x1c\x55\xb9\x28\x00\x00\x00") +func _000002_system_settings_tableUpSql() (*asset, error) { + bytes, err := _000002_system_settings_tableUpSqlBytes() + if err != nil { + return nil, err + } -func _000003_blocks_rootid_down_sql() ([]byte, error) { - return bindata_read( - __000003_blocks_rootid_down_sql, + info := bindataFileInfo{name: "000002_system_settings_table.up.sql", size: 96, mode: os.FileMode(0644), modTime: time.Unix(1603112131, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x24, 0x6f, 0x66, 0x74, 0xbf, 0xec, 0xae, 0x5a, 0x56, 0x95, 0x4b, 0x4d, 0xaa, 0xf6, 0x8, 0xae, 0x88, 0x62, 0x21, 0x4f, 0xdc, 0xb1, 0x3, 0x30, 0x79, 0xd4, 0xc2, 0x17, 0x2, 0x9e, 0x1c, 0x8e}} + return a, nil +} + +var __000003_blocks_rootidDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x09\xf2\x0f\x50\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\xb1\xe6\x02\x04\x00\x00\xff\xff\x94\x1c\x55\xb9\x28\x00\x00\x00") + +func _000003_blocks_rootidDownSqlBytes() ([]byte, error) { + return bindataRead( + __000003_blocks_rootidDownSql, "000003_blocks_rootid.down.sql", ) } -var __000003_blocks_rootid_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x74\x71\x51\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x36\xd3\xb4\xe6\x02\x04\x00\x00\xff\xff\xce\x60\x70\x4e\x33\x00\x00\x00") +func _000003_blocks_rootidDownSql() (*asset, error) { + bytes, err := _000003_blocks_rootidDownSqlBytes() + if err != nil { + return nil, err + } -func _000003_blocks_rootid_up_sql() ([]byte, error) { - return bindata_read( - __000003_blocks_rootid_up_sql, + info := bindataFileInfo{name: "000003_blocks_rootid.down.sql", size: 40, mode: os.FileMode(0644), modTime: time.Unix(1607094225, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1a, 0xb4, 0xdd, 0x99, 0x6e, 0xb8, 0x1a, 0xfd, 0x54, 0x43, 0x8f, 0x9, 0x3a, 0xd1, 0xe4, 0x32, 0x20, 0x3b, 0x43, 0xf4, 0x61, 0x17, 0x9b, 0xff, 0x85, 0xb0, 0x9a, 0x48, 0x9f, 0xfd, 0xbb, 0xcc}} + return a, nil +} + +var __000003_blocks_rootidUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x08\x71\x74\xf2\x71\x55\x48\xca\xc9\x4f\xce\x2e\xe6\x72\x74\x71\x51\x70\xf6\xf7\x09\xf5\xf5\x53\x28\xca\xcf\x2f\x89\xcf\x4c\x51\x08\x73\x0c\x72\xf6\x70\x0c\xd2\x30\x36\xd3\xb4\xe6\x02\x04\x00\x00\xff\xff\xce\x60\x70\x4e\x33\x00\x00\x00") + +func _000003_blocks_rootidUpSqlBytes() ([]byte, error) { + return bindataRead( + __000003_blocks_rootidUpSql, "000003_blocks_rootid.up.sql", ) } -var __000004_auth_table_down_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") +func _000003_blocks_rootidUpSql() (*asset, error) { + bytes, err := _000003_blocks_rootidUpSqlBytes() + if err != nil { + return nil, err + } -func _000004_auth_table_down_sql() ([]byte, error) { - return bindata_read( - __000004_auth_table_down_sql, + info := bindataFileInfo{name: "000003_blocks_rootid.up.sql", size: 51, mode: os.FileMode(0644), modTime: time.Unix(1607094225, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x20, 0x3e, 0xe3, 0x25, 0xb9, 0x60, 0x1a, 0xc2, 0xbf, 0x51, 0x2e, 0xf0, 0xbb, 0x9e, 0x61, 0xb9, 0x16, 0x19, 0xc0, 0x6, 0x7a, 0x81, 0x1c, 0xce, 0xa0, 0xea, 0x91, 0xb2, 0xf, 0xb9, 0xba, 0x91}} + return a, nil +} + +var __000004_auth_tableDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\x2d\x4e\x2d\x2a\xb6\xe6\x42\x12\x29\x4e\x2d\x2e\xce\xcc\xcf\x2b\xb6\xe6\x02\x04\x00\x00\xff\xff\xa5\xe0\x77\xaa\x27\x00\x00\x00") + +func _000004_auth_tableDownSqlBytes() ([]byte, error) { + return bindataRead( + __000004_auth_tableDownSql, "000004_auth_table.down.sql", ) } -var __000004_auth_table_up_sql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\xd0\xc1\x4a\x03\x31\x10\x06\xe0\xf3\xe6\x29\xe6\xb8\x0b\x3d\xd4\x42\x4f\x9e\xd2\x12\x35\xa8\x55\xd2\x20\xed\x69\x19\x36\x23\x06\xbb\x9b\x25\x93\xd5\xd7\x97\x20\x28\x76\xa3\x17\x73\xfc\xfe\x90\xc9\xfc\x5b\xa3\xa4\x55\x60\xe5\xe6\x4e\x81\xbe\x82\xdd\x83\x05\x75\xd0\x7b\xbb\x87\x89\x29\x32\xd4\xa2\xf2\x0e\x9e\xa4\xd9\xde\x48\x53\x5f\x2c\x97\xcd\x42\x54\x39\x1a\xb0\xa7\x73\xa7\x1e\xfd\xe9\x0b\x57\xeb\x75\xc6\x11\x99\xdf\x43\x3c\x7f\x04\x00\xa0\x7f\xc6\x96\xa9\x8b\x94\x0a\x21\x4e\xe9\xa5\x65\x8a\x6f\xbe\xfb\x1e\xb4\xfa\x91\x3a\x4c\x38\x1b\x17\xc3\xc8\xf0\x79\xac\x3a\xd8\x85\xa8\xba\x48\x98\xa8\xc5\x94\x6d\xa3\xaf\xf5\x2e\xeb\x34\xba\x82\x3a\x3a\xd1\x5c\x1f\x8d\xbe\x97\xe6\x08\xb7\xea\x08\xb5\x77\x8d\x68\x2e\x85\xf8\xa3\x3b\x26\x66\x1f\x86\x5f\xea\x4b\xe1\x95\x86\x52\xa7\xed\xfc\xee\x3f\xd7\x29\x7d\xfc\x23\x00\x00\xff\xff\xa1\xd6\x24\xe6\xf4\x01\x00\x00") +func _000004_auth_tableDownSql() (*asset, error) { + bytes, err := _000004_auth_tableDownSqlBytes() + if err != nil { + return nil, err + } -func _000004_auth_table_up_sql() ([]byte, error) { - return bindata_read( - __000004_auth_table_up_sql, + info := bindataFileInfo{name: "000004_auth_table.down.sql", size: 39, mode: os.FileMode(0644), modTime: time.Unix(1606923777, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xb6, 0x5b, 0x33, 0x33, 0xd4, 0x68, 0x4, 0xb4, 0xfd, 0x61, 0x61, 0x33, 0x1b, 0xc6, 0x44, 0x5e, 0x57, 0xd0, 0xe7, 0x21, 0x36, 0xff, 0x10, 0x1a, 0xc2, 0xa2, 0xe7, 0x3a, 0xbd, 0x2e, 0xba, 0x66}} + return a, nil +} + +var __000004_auth_tableUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\xd0\xc1\x4a\x03\x31\x10\x06\xe0\xf3\xe6\x29\xe6\xb8\x0b\x3d\xd4\x42\x4f\x9e\xd2\x12\x35\xa8\x55\xd2\x20\xed\x69\x19\x36\x23\x06\xbb\x9b\x25\x93\xd5\xd7\x97\x20\x58\x68\x56\x2f\xe6\xf8\xfd\x21\x93\xf9\xb7\x46\x49\xab\xc0\xca\xcd\x83\x02\x7d\x03\xbb\x27\x0b\xea\xa0\xf7\x76\x0f\x13\x53\x64\xa8\x45\xe5\x1d\xbc\x48\xb3\xbd\x93\xa6\xbe\x5a\x2e\x9b\x85\xa8\x72\x34\x60\x4f\x97\x4e\x3d\xfa\xd3\x0f\xae\xd6\xeb\x8c\x23\x32\x7f\x86\x58\x3c\xd2\xbf\x62\xcb\xd4\x45\x4a\x97\x09\x4e\xe9\xad\x65\x8a\x1f\xbe\x3b\x8f\x58\x9d\x23\x87\x09\x8b\x29\x31\x8c\x0c\xdf\xc7\xaa\x83\x5d\x88\xaa\x8b\x84\x89\x5a\x4c\xd9\x36\xfa\x56\xef\xb2\x4e\xa3\x9b\x51\x47\x27\x2a\xf5\xd9\xe8\x47\x69\x8e\x70\xaf\x8e\x50\x7b\xd7\x88\xe6\x5a\x88\x3f\x2a\x63\x62\xf6\x61\xf8\xa5\xb5\x14\xde\x69\x98\xab\xb2\x2d\xef\xfe\x73\x9d\xb9\x8f\x7f\x05\x00\x00\xff\xff\x82\xd7\x37\x19\xeb\x01\x00\x00") + +func _000004_auth_tableUpSqlBytes() ([]byte, error) { + return bindataRead( + __000004_auth_tableUpSql, "000004_auth_table.up.sql", ) } +func _000004_auth_tableUpSql() (*asset, error) { + bytes, err := _000004_auth_tableUpSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "000004_auth_table.up.sql", size: 491, mode: os.FileMode(0644), modTime: time.Unix(1610375590, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xb6, 0xf9, 0x52, 0xb2, 0x63, 0x7b, 0x65, 0xea, 0x26, 0xeb, 0xa3, 0x8c, 0x5, 0x70, 0xd7, 0x4e, 0xd0, 0x63, 0x75, 0xc6, 0x7d, 0xf4, 0x5d, 0x11, 0x62, 0x8e, 0x94, 0x43, 0x25, 0x0, 0xaa, 0xf2}} + return a, nil +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. func Asset(name string) ([]byte, error) { - cannonicalName := strings.Replace(name, "\\", "/", -1) - if f, ok := _bindata[cannonicalName]; ok { - return f() + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) + } + return a.bytes, nil } return nil, fmt.Errorf("Asset %s not found", name) } +// AssetString returns the asset contents as a string (instead of a []byte). +func AssetString(name string) (string, error) { + data, err := Asset(name) + return string(data), err +} + +// MustAsset is like Asset but panics when Asset would return an error. +// It simplifies safe initialization of global variables. +func MustAsset(name string) []byte { + a, err := Asset(name) + if err != nil { + panic("asset: Asset(" + name + "): " + err.Error()) + } + + return a +} + +// MustAssetString is like AssetString but panics when Asset would return an +// error. It simplifies safe initialization of global variables. +func MustAssetString(name string) string { + return string(MustAsset(name)) +} + +// AssetInfo loads and returns the asset info for the given name. +// It returns an error if the asset could not be found or +// could not be loaded. +func AssetInfo(name string) (os.FileInfo, error) { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) + } + return a.info, nil + } + return nil, fmt.Errorf("AssetInfo %s not found", name) +} + +// AssetDigest returns the digest of the file with the given name. It returns an +// error if the asset could not be found or the digest could not be loaded. +func AssetDigest(name string) ([sha256.Size]byte, error) { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { + a, err := f() + if err != nil { + return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s can't read by error: %v", name, err) + } + return a.digest, nil + } + return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s not found", name) +} + +// Digests returns a map of all known files and their checksums. +func Digests() (map[string][sha256.Size]byte, error) { + mp := make(map[string][sha256.Size]byte, len(_bindata)) + for name := range _bindata { + a, err := _bindata[name]() + if err != nil { + return nil, err + } + mp[name] = a.digest + } + return mp, nil +} + // AssetNames returns the names of the assets. func AssetNames() []string { names := make([]string, 0, len(_bindata)) @@ -118,16 +326,20 @@ func AssetNames() []string { } // _bindata is a table, holding each asset generator, mapped to its name. -var _bindata = map[string]func() ([]byte, error){ - "000001_init.down.sql": _000001_init_down_sql, - "000001_init.up.sql": _000001_init_up_sql, - "000002_system_settings_table.down.sql": _000002_system_settings_table_down_sql, - "000002_system_settings_table.up.sql": _000002_system_settings_table_up_sql, - "000003_blocks_rootid.down.sql": _000003_blocks_rootid_down_sql, - "000003_blocks_rootid.up.sql": _000003_blocks_rootid_up_sql, - "000004_auth_table.down.sql": _000004_auth_table_down_sql, - "000004_auth_table.up.sql": _000004_auth_table_up_sql, +var _bindata = map[string]func() (*asset, error){ + "000001_init.down.sql": _000001_initDownSql, + "000001_init.up.sql": _000001_initUpSql, + "000002_system_settings_table.down.sql": _000002_system_settings_tableDownSql, + "000002_system_settings_table.up.sql": _000002_system_settings_tableUpSql, + "000003_blocks_rootid.down.sql": _000003_blocks_rootidDownSql, + "000003_blocks_rootid.up.sql": _000003_blocks_rootidUpSql, + "000004_auth_table.down.sql": _000004_auth_tableDownSql, + "000004_auth_table.up.sql": _000004_auth_tableUpSql, } + +// AssetDebug is true if the assets were built with the debug flag enabled. +const AssetDebug = false + // AssetDir returns the file names below a certain // directory embedded in the file by go-bindata. // For example if you run go-bindata on data/... and data contains the @@ -137,15 +349,15 @@ var _bindata = map[string]func() ([]byte, error){ // img/ // a.png // b.png -// then AssetDir("data") would return []string{"foo.txt", "img"} -// AssetDir("data/img") would return []string{"a.png", "b.png"} -// AssetDir("foo.txt") and AssetDir("notexist") would return an error +// then AssetDir("data") would return []string{"foo.txt", "img"}, +// AssetDir("data/img") would return []string{"a.png", "b.png"}, +// AssetDir("foo.txt") and AssetDir("notexist") would return an error, and // AssetDir("") will return []string{"data"}. func AssetDir(name string) ([]string, error) { node := _bintree if len(name) != 0 { - cannonicalName := strings.Replace(name, "\\", "/", -1) - pathList := strings.Split(cannonicalName, "/") + canonicalName := strings.Replace(name, "\\", "/", -1) + pathList := strings.Split(canonicalName, "/") for _, p := range pathList { node = node.Children[p] if node == nil { @@ -157,31 +369,67 @@ func AssetDir(name string) ([]string, error) { return nil, fmt.Errorf("Asset %s not found", name) } rv := make([]string, 0, len(node.Children)) - for name := range node.Children { - rv = append(rv, name) + for childName := range node.Children { + rv = append(rv, childName) } return rv, nil } -type _bintree_t struct { - Func func() ([]byte, error) - Children map[string]*_bintree_t +type bintree struct { + Func func() (*asset, error) + Children map[string]*bintree } -var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ - "000001_init.down.sql": &_bintree_t{_000001_init_down_sql, map[string]*_bintree_t{ - }}, - "000001_init.up.sql": &_bintree_t{_000001_init_up_sql, map[string]*_bintree_t{ - }}, - "000002_system_settings_table.down.sql": &_bintree_t{_000002_system_settings_table_down_sql, map[string]*_bintree_t{ - }}, - "000002_system_settings_table.up.sql": &_bintree_t{_000002_system_settings_table_up_sql, map[string]*_bintree_t{ - }}, - "000003_blocks_rootid.down.sql": &_bintree_t{_000003_blocks_rootid_down_sql, map[string]*_bintree_t{ - }}, - "000003_blocks_rootid.up.sql": &_bintree_t{_000003_blocks_rootid_up_sql, map[string]*_bintree_t{ - }}, - "000004_auth_table.down.sql": &_bintree_t{_000004_auth_table_down_sql, map[string]*_bintree_t{ - }}, - "000004_auth_table.up.sql": &_bintree_t{_000004_auth_table_up_sql, map[string]*_bintree_t{ - }}, + +var _bintree = &bintree{nil, map[string]*bintree{ + "000001_init.down.sql": {_000001_initDownSql, map[string]*bintree{}}, + "000001_init.up.sql": {_000001_initUpSql, map[string]*bintree{}}, + "000002_system_settings_table.down.sql": {_000002_system_settings_tableDownSql, map[string]*bintree{}}, + "000002_system_settings_table.up.sql": {_000002_system_settings_tableUpSql, map[string]*bintree{}}, + "000003_blocks_rootid.down.sql": {_000003_blocks_rootidDownSql, map[string]*bintree{}}, + "000003_blocks_rootid.up.sql": {_000003_blocks_rootidUpSql, map[string]*bintree{}}, + "000004_auth_table.down.sql": {_000004_auth_tableDownSql, map[string]*bintree{}}, + "000004_auth_table.up.sql": {_000004_auth_tableUpSql, map[string]*bintree{}}, }} + +// RestoreAsset restores an asset under the given directory. +func RestoreAsset(dir, name string) error { + data, err := Asset(name) + if err != nil { + return err + } + info, err := AssetInfo(name) + if err != nil { + return err + } + err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755)) + if err != nil { + return err + } + err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + if err != nil { + return err + } + return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) +} + +// RestoreAssets restores an asset under the given directory recursively. +func RestoreAssets(dir, name string) error { + children, err := AssetDir(name) + // File + if err != nil { + return RestoreAsset(dir, name) + } + // Dir + for _, child := range children { + err = RestoreAssets(dir, filepath.Join(name, child)) + if err != nil { + return err + } + } + return nil +} + +func _filePath(dir, name string) string { + canonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(canonicalName, "/")...)...) +} diff --git a/server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.up.sql b/server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.up.sql index 814322eea..0cb1a369b 100644 --- a/server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.up.sql +++ b/server/services/store/sqlstore/migrations/sqlite_files/000004_auth_table.up.sql @@ -3,9 +3,9 @@ CREATE TABLE IF NOT EXISTS users ( username VARCHAR(100), email VARCHAR(255), password VARCHAR(100), - mfa_secret VARCHAR(100), - auth_service VARCHAR(20), - auth_data VARCHAR(255), + mfa_secret VARCHAR(100), + auth_service VARCHAR(20), + auth_data VARCHAR(255), props TEXT, create_at BIGINT, update_at BIGINT, From 41ce3fdb912ad20fed1e51eb19eef5f475d07cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 11 Jan 2021 16:57:29 +0100 Subject: [PATCH 13/13] Adding singleuser to the ci tests --- webapp/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/package.json b/webapp/package.json index 457bdb601..104593aa1 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -11,7 +11,7 @@ "check": "eslint --ext .tsx,.ts . --quiet --cache", "fix": "eslint --ext .tsx,.ts . --quiet --fix --cache", "i18n-extract": "formatjs extract src/**/*.tsx src/**/*.ts --out-file i18n/tmp.json; formatjs compile i18n/tmp.json --out-file i18n/en.json; rm i18n/tmp.json", - "runserver-test": "cd cypress && ../../bin/octoserver", + "runserver-test": "cd cypress && ../../bin/octoserver --single-user", "cypress:ci": "start-server-and-test runserver-test http://localhost:8088 cypress:run", "cypress:run": "cypress run", "cypress:run:chrome": "cypress run --browser chrome",