From aa219e701ec54e5cb8015d1b4297ca3aee08d217 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 2 Aug 2017 14:10:05 +0100 Subject: [PATCH] Add option to use FM w/o login Former-commit-id: fd6209370028490ac4766e61ebd87350e12efd22 [formerly 88b307e153445f3a22a38b5113f8a184d87dc75c] [formerly 7c5d74411dee7036012e734555763df009190de6 [formerly f572fc78374507efc90c3412acd8b1fea5976224]] Former-commit-id: 791eab82673bdd5b41947b20f444066bb9070406 [formerly 582a15ff9879494afac6893a675bab094973d5a4] Former-commit-id: a936d8b2e15203541a2b0ff074b4146867352426 --- assets/src/components/Sidebar.vue | 2 +- assets/src/router/index.js | 6 ++++++ auth.go | 16 ++++++++++++++-- cmd/filemanager/main.go | 8 ++++++++ filemanager.go | 4 ++++ rice-box.go.REMOVED.git-id | 1 - 6 files changed, 33 insertions(+), 4 deletions(-) delete mode 100644 rice-box.go.REMOVED.git-id diff --git a/assets/src/components/Sidebar.vue b/assets/src/components/Sidebar.vue index 87b9f40b..28167174 100644 --- a/assets/src/components/Sidebar.vue +++ b/assets/src/components/Sidebar.vue @@ -24,7 +24,7 @@ -
+
settings_applications {{ $t('sidebar.settings') }} diff --git a/assets/src/router/index.js b/assets/src/router/index.js index e03ea50a..d01018b3 100644 --- a/assets/src/router/index.js +++ b/assets/src/router/index.js @@ -97,6 +97,12 @@ const router = new Router({ requiresAdmin: true } }, + { + path: '/files', + redirect: { + path: '/files/' + } + }, { path: '/*', redirect: { diff --git a/auth.go b/auth.go index d48f7e92..711c8992 100644 --- a/auth.go +++ b/auth.go @@ -15,6 +15,11 @@ import ( // authHandler proccesses the authentication for the user. func authHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { + // NoAuth instances shouldn't call this method. + if c.NoAuth { + return 0, nil + } + // Receive the credentials from the request and unmarshal them. var cred User if r.Body == nil { @@ -56,6 +61,7 @@ func renewAuthHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) // claims is the JWT claims. type claims struct { User + NoAuth bool `json:"noAuth"` jwt.StandardClaims } @@ -70,6 +76,7 @@ func printToken(c *RequestContext, w http.ResponseWriter) (int, error) { // Builds the claims. claims := claims{ u, + c.NoAuth, jwt.StandardClaims{ ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), Issuer: "File Manager", @@ -78,7 +85,7 @@ func printToken(c *RequestContext, w http.ResponseWriter) (int, error) { // Creates the token and signs it. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - string, err := token.SignedString(c.key) + signed, err := token.SignedString(c.key) if err != nil { return http.StatusInternalServerError, err @@ -86,7 +93,7 @@ func printToken(c *RequestContext, w http.ResponseWriter) (int, error) { // Writes the token. w.Header().Set("Content-Type", "cty") - w.Write([]byte(string)) + w.Write([]byte(signed)) return 0, nil } @@ -113,6 +120,11 @@ func (e extractor) ExtractToken(r *http.Request) (string, error) { // validateAuth is used to validate the authentication and returns the // User if it is valid. func validateAuth(c *RequestContext, r *http.Request) (bool, *User) { + if c.NoAuth { + c.User = c.DefaultUser + return true, c.User + } + keyFunc := func(token *jwt.Token) (interface{}, error) { return c.key, nil } diff --git a/cmd/filemanager/main.go b/cmd/filemanager/main.go index c7e8899e..3305c695 100644 --- a/cmd/filemanager/main.go +++ b/cmd/filemanager/main.go @@ -30,6 +30,7 @@ var ( plugin string locale string port int + noAuth bool allowCommands bool allowEdit bool allowNew bool @@ -48,6 +49,7 @@ func init() { flag.BoolVar(&allowCommands, "allow-commands", true, "Default allow commands option for new users") flag.BoolVar(&allowEdit, "allow-edit", true, "Default allow edit option for new users") flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users") + flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication") flag.StringVar(&locale, "locale", "en", "Default locale for new users") flag.StringVar(&plugin, "plugin", "", "Plugin you want to enable") flag.BoolVarP(&showVer, "version", "v", false, "Show version") @@ -65,6 +67,7 @@ func setupViper() { viper.SetDefault("AllowNew", true) viper.SetDefault("Plugin", "") viper.SetDefault("Locale", "en") + viper.SetDefault("NoAuth", false) viper.BindPFlag("Port", flag.Lookup("port")) viper.BindPFlag("Address", flag.Lookup("address")) @@ -77,6 +80,7 @@ func setupViper() { viper.BindPFlag("AlowNew", flag.Lookup("allow-new")) viper.BindPFlag("Locale", flag.Lookup("locale")) viper.BindPFlag("Plugin", flag.Lookup("plugin")) + viper.BindPFlag("NoAuth", flag.Lookup("no-auth")) viper.SetConfigName("filemanager") viper.AddConfigPath(".") @@ -142,6 +146,10 @@ func main() { FileSystem: fileutils.Dir(viper.GetString("Scope")), }) + if viper.GetBool("NoAuth") { + fm.NoAuth = true + } + if err != nil { log.Fatal(err) } diff --git a/filemanager.go b/filemanager.go index b3788c94..c0d0b512 100644 --- a/filemanager.go +++ b/filemanager.go @@ -103,6 +103,10 @@ type FileManager struct { // edited directly. Use SetBaseURL. BaseURL string + // NoAuth disables the authentication. When the authentication is disabled, + // there will only exist one user, called "admin". + NoAuth bool + // The Default User needed to build the New User page. DefaultUser *User diff --git a/rice-box.go.REMOVED.git-id b/rice-box.go.REMOVED.git-id deleted file mode 100644 index 70ad3bed..00000000 --- a/rice-box.go.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -b5a8f3badeeb5ea5e285f23298ddef20ce247376 \ No newline at end of file