2017-08-18 16:00:32 +08:00
|
|
|
package http
|
2017-07-03 00:40:52 +08:00
|
|
|
|
|
|
|
import (
|
2017-09-11 16:00:59 +08:00
|
|
|
"bytes"
|
2017-07-03 15:59:49 +08:00
|
|
|
"encoding/json"
|
2017-09-11 16:00:59 +08:00
|
|
|
"fmt"
|
2017-07-03 00:40:52 +08:00
|
|
|
"net/http"
|
2017-09-11 16:00:59 +08:00
|
|
|
"net/url"
|
2017-07-22 20:46:21 +08:00
|
|
|
"strings"
|
2017-07-03 00:40:52 +08:00
|
|
|
"time"
|
|
|
|
|
2017-10-30 23:47:59 +08:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
2017-07-03 00:40:52 +08:00
|
|
|
"github.com/dgrijalva/jwt-go/request"
|
2017-08-18 16:00:32 +08:00
|
|
|
fm "github.com/hacdias/filemanager"
|
2017-07-03 00:40:52 +08:00
|
|
|
)
|
|
|
|
|
2017-09-11 16:00:59 +08:00
|
|
|
type cred struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Recaptcha string `json:"recaptcha"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// recaptcha checks the recaptcha code.
|
|
|
|
func recaptcha(secret string, response string) (bool, error) {
|
|
|
|
api := "https://www.google.com/recaptcha/api/siteverify"
|
|
|
|
|
|
|
|
body := url.Values{}
|
|
|
|
body.Set("secret", secret)
|
|
|
|
body.Add("response", response)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Post(api, "application/x-www-form-urlencoded", bytes.NewBufferString(body.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
ChallengeTS time.Time `json:"challenge_ts"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
ErrorCodes interface{} `json:"error-codes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.Success, nil
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
// authHandler proccesses the authentication for the user.
|
2017-08-18 16:00:32 +08:00
|
|
|
func authHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-08-02 21:10:05 +08:00
|
|
|
// NoAuth instances shouldn't call this method.
|
|
|
|
if c.NoAuth {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
// Receive the credentials from the request and unmarshal them.
|
2017-09-11 16:00:59 +08:00
|
|
|
var cred cred
|
2017-07-03 15:59:49 +08:00
|
|
|
if r.Body == nil {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&cred)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:00:59 +08:00
|
|
|
// If ReCaptcha is enabled, check the code.
|
|
|
|
if len(c.ReCaptchaSecret) > 0 {
|
|
|
|
ok, err := recaptcha(c.ReCaptchaSecret, cred.Recaptcha)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return http.StatusForbidden, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
// Checks if the user exists.
|
2017-08-20 16:55:45 +08:00
|
|
|
u, err := c.Store.Users.GetByUsername(cred.Username, c.NewFS)
|
2017-08-19 19:35:44 +08:00
|
|
|
if err != nil {
|
2017-07-03 15:59:49 +08:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the password is correct.
|
2017-08-19 19:35:44 +08:00
|
|
|
if !fm.CheckPasswordHash(cred.Password, u.Password) {
|
2017-07-03 15:59:49 +08:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
2017-07-03 00:40:52 +08:00
|
|
|
|
2017-07-11 23:58:18 +08:00
|
|
|
c.User = u
|
2017-07-06 16:31:07 +08:00
|
|
|
return printToken(c, w)
|
2017-07-03 00:40:52 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
// 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.
|
2017-08-18 16:00:32 +08:00
|
|
|
func renewAuthHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-03 15:59:49 +08:00
|
|
|
ok, u := validateAuth(c, r)
|
|
|
|
if !ok {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 23:58:18 +08:00
|
|
|
c.User = u
|
2017-07-06 16:31:07 +08:00
|
|
|
return printToken(c, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// claims is the JWT claims.
|
|
|
|
type claims struct {
|
2017-08-18 16:00:32 +08:00
|
|
|
fm.User
|
2017-07-06 16:31:07 +08:00
|
|
|
jwt.StandardClaims
|
|
|
|
}
|
|
|
|
|
|
|
|
// printToken prints the final JWT token to the user.
|
2017-08-18 16:00:32 +08:00
|
|
|
func printToken(c *fm.Context, w http.ResponseWriter) (int, error) {
|
2017-07-06 16:31:07 +08:00
|
|
|
// Creates a copy of the user and removes it password
|
|
|
|
// hash so it never arrives to the user.
|
2017-08-18 16:00:32 +08:00
|
|
|
u := fm.User{}
|
2017-07-11 23:58:18 +08:00
|
|
|
u = *c.User
|
2017-07-06 16:31:07 +08:00
|
|
|
u.Password = ""
|
|
|
|
|
|
|
|
// Builds the claims.
|
2017-07-03 15:59:49 +08:00
|
|
|
claims := claims{
|
|
|
|
u,
|
|
|
|
jwt.StandardClaims{
|
|
|
|
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
|
|
|
|
Issuer: "File Manager",
|
|
|
|
},
|
|
|
|
}
|
2017-07-03 00:40:52 +08:00
|
|
|
|
2017-07-06 16:31:07 +08:00
|
|
|
// Creates the token and signs it.
|
2017-07-03 15:59:49 +08:00
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
2017-08-19 19:35:44 +08:00
|
|
|
signed, err := token.SignedString(c.Key)
|
2017-07-06 16:31:07 +08:00
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
2017-07-03 00:40:52 +08:00
|
|
|
}
|
|
|
|
|
2017-07-06 16:31:07 +08:00
|
|
|
// Writes the token.
|
2017-07-29 21:19:09 +08:00
|
|
|
w.Header().Set("Content-Type", "cty")
|
2017-08-02 21:10:05 +08:00
|
|
|
w.Write([]byte(signed))
|
2017-07-03 15:59:49 +08:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2017-07-03 22:19:17 +08:00
|
|
|
type extractor []string
|
|
|
|
|
|
|
|
func (e extractor) ExtractToken(r *http.Request) (string, error) {
|
|
|
|
token, _ := request.AuthorizationHeaderExtractor.ExtractToken(r)
|
2017-07-22 20:46:21 +08:00
|
|
|
|
2017-07-25 18:57:27 +08:00
|
|
|
// Checks if the token isn't empty and if it contains two dots.
|
2017-07-22 20:46:21 +08:00
|
|
|
// The former prevents incompatibility with URLs that previously
|
|
|
|
// used basic auth.
|
2017-07-25 18:57:27 +08:00
|
|
|
if token != "" && strings.Count(token, ".") == 2 {
|
2017-07-03 22:19:17 +08:00
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:28:01 +08:00
|
|
|
cookie, err := r.Cookie("auth")
|
|
|
|
if err != nil {
|
|
|
|
return "", request.ErrNoTokenInRequest
|
2017-07-03 22:19:17 +08:00
|
|
|
}
|
|
|
|
|
2017-07-19 15:28:01 +08:00
|
|
|
return cookie.Value, nil
|
2017-07-03 22:19:17 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
// validateAuth is used to validate the authentication and returns the
|
|
|
|
// User if it is valid.
|
2017-08-18 16:00:32 +08:00
|
|
|
func validateAuth(c *fm.Context, r *http.Request) (bool, *fm.User) {
|
2017-08-02 21:10:05 +08:00
|
|
|
if c.NoAuth {
|
|
|
|
c.User = c.DefaultUser
|
|
|
|
return true, c.User
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
keyFunc := func(token *jwt.Token) (interface{}, error) {
|
2017-08-19 19:35:44 +08:00
|
|
|
return c.Key, nil
|
2017-07-03 15:59:49 +08:00
|
|
|
}
|
|
|
|
var claims claims
|
|
|
|
token, err := request.ParseFromRequestWithClaims(r,
|
2017-07-03 22:19:17 +08:00
|
|
|
extractor{},
|
2017-07-03 15:59:49 +08:00
|
|
|
&claims,
|
|
|
|
keyFunc,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil || !token.Valid {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-08-20 16:21:36 +08:00
|
|
|
u, err := c.Store.Users.Get(claims.User.ID, c.NewFS)
|
2017-08-19 19:35:44 +08:00
|
|
|
if err != nil {
|
2017-07-03 15:59:49 +08:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 23:58:18 +08:00
|
|
|
c.User = u
|
2017-07-03 15:59:49 +08:00
|
|
|
return true, u
|
|
|
|
}
|