mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
d50bec8caa
* update dependencies to latest version
* add mising dependencies
* Syntax updates and such
* Reorganize files and translate login to portuguese
* Add i18n to buttons
* Error translations and some bug fixes
* Add i18n to files
* i18n on prompts
* update search
* Prompts and Sidebar in
* i18n to the header
* Change to YAML
* alphabetical order
* # Add simplified Chinese language (#180)
* Add Simplified Chinese and sort by alphabet
* Add more text to translations
* API Updates
* Update zh_cn.yaml (#182)
* Api Upgrades
* Simplify api and clean zh_cn lang file
* Improve error logging
* Fix some route bugs and separate login styles
* better organization
* Fix bug on api
* Build assets Tue, Aug 1, 2017 11:32:23 AM
* Rename users path and fix bug scroll event
* Start Portuguese translation and file org
* Add more to the PT translation
* Add show
* Build assets Tue Aug 1 12:01:39 GMTST 2017
* Add locale to cofnig
* Update portuguese translation
* You can change the language :)
* :D
* Build assets Tue Aug 1 17:50:31 GMTST 2017
* Update requestContext variable names
* Remove assets
* Build assets Tue Aug 1 20:48:21 GMTST 2017
Former-commit-id: 08f373725c14990f61dbb00bea43118c496c5d32 [formerly 281e23007c79dac1e9b86424201891a99d20f73a] [formerly b1b73f42debbce06b4f36e4cf97e319789c85b9f [formerly d8bc73390c
]]
Former-commit-id: 92e99405cbf9935d1cf77b0fe70b122fca552be6 [formerly 3cd365e862f2a54ada60e226a19ac607b8d0c43b]
Former-commit-id: cf9815114ac686cdf75a6b1cba15adafe493d083
165 lines
3.9 KiB
Go
165 lines
3.9 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
jwt "github.com/dgrijalva/jwt-go"
|
|
"github.com/dgrijalva/jwt-go/request"
|
|
)
|
|
|
|
// authHandler proccesses the authentication for the user.
|
|
func authHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
// Receive the credentials from the request and unmarshal them.
|
|
var cred User
|
|
if r.Body == nil {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&cred)
|
|
if err != nil {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
// Checks if the user exists.
|
|
u, ok := c.Users[cred.Username]
|
|
if !ok {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
// Checks if the password is correct.
|
|
if !checkPasswordHash(cred.Password, u.Password) {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
c.User = u
|
|
return printToken(c, w)
|
|
}
|
|
|
|
// renewAuthHandler is used when the front-end already has a JWT token
|
|
// and is checking if it is up to date. If so, updates its info.
|
|
func renewAuthHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
ok, u := validateAuth(c, r)
|
|
if !ok {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
c.User = u
|
|
return printToken(c, w)
|
|
}
|
|
|
|
// claims is the JWT claims.
|
|
type claims struct {
|
|
User
|
|
jwt.StandardClaims
|
|
}
|
|
|
|
// printToken prints the final JWT token to the user.
|
|
func printToken(c *RequestContext, w http.ResponseWriter) (int, error) {
|
|
// Creates a copy of the user and removes it password
|
|
// hash so it never arrives to the user.
|
|
u := User{}
|
|
u = *c.User
|
|
u.Password = ""
|
|
|
|
// Builds the claims.
|
|
claims := claims{
|
|
u,
|
|
jwt.StandardClaims{
|
|
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
|
|
Issuer: "File Manager",
|
|
},
|
|
}
|
|
|
|
// Creates the token and signs it.
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
string, err := token.SignedString(c.key)
|
|
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
// Writes the token.
|
|
w.Header().Set("Content-Type", "cty")
|
|
w.Write([]byte(string))
|
|
return 0, nil
|
|
}
|
|
|
|
type extractor []string
|
|
|
|
func (e extractor) ExtractToken(r *http.Request) (string, error) {
|
|
token, _ := request.AuthorizationHeaderExtractor.ExtractToken(r)
|
|
|
|
// Checks if the token isn't empty and if it contains two dots.
|
|
// The former prevents incompatibility with URLs that previously
|
|
// used basic auth.
|
|
if token != "" && strings.Count(token, ".") == 2 {
|
|
return token, nil
|
|
}
|
|
|
|
cookie, err := r.Cookie("auth")
|
|
if err != nil {
|
|
return "", request.ErrNoTokenInRequest
|
|
}
|
|
|
|
return cookie.Value, nil
|
|
}
|
|
|
|
// validateAuth is used to validate the authentication and returns the
|
|
// User if it is valid.
|
|
func validateAuth(c *RequestContext, r *http.Request) (bool, *User) {
|
|
keyFunc := func(token *jwt.Token) (interface{}, error) {
|
|
return c.key, nil
|
|
}
|
|
var claims claims
|
|
token, err := request.ParseFromRequestWithClaims(r,
|
|
extractor{},
|
|
&claims,
|
|
keyFunc,
|
|
)
|
|
|
|
if err != nil || !token.Valid {
|
|
return false, nil
|
|
}
|
|
|
|
u, ok := c.Users[claims.User.Username]
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
c.User = u
|
|
return true, u
|
|
}
|
|
|
|
// hashPassword generates an hash from a password using bcrypt.
|
|
func hashPassword(password string) (string, error) {
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(bytes), err
|
|
}
|
|
|
|
// checkPasswordHash compares a password with an hash to check if they match.
|
|
func checkPasswordHash(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
// generateRandomBytes returns securely generated random bytes.
|
|
// It will return an error if the system's secure random
|
|
// number generator fails to function correctly, in which
|
|
// case the caller should not continue.
|
|
func generateRandomBytes(n int) ([]byte, error) {
|
|
b := make([]byte, n)
|
|
_, err := rand.Read(b)
|
|
// Note that err == nil only if we read len(b) bytes.
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b, nil
|
|
}
|