user and password as authentication requirements (#87)
* user and password as authentication requirements * updated README.md
This commit is contained in:
@@ -14,7 +14,8 @@ type serverConfig struct {
|
||||
DownloadPath string `yaml:"downloadPath"`
|
||||
DownloaderPath string `yaml:"downloaderPath"`
|
||||
RequireAuth bool `yaml:"require_auth"`
|
||||
RPCSecret string `yaml:"rpc_secret"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
QueueSize int `yaml:"queue_size"`
|
||||
}
|
||||
|
||||
@@ -22,7 +23,7 @@ type config struct {
|
||||
cfg serverConfig
|
||||
}
|
||||
|
||||
func (c *config) TryLoadFromFile(filename string) (serverConfig, error) {
|
||||
func (c *config) LoadFromFile(filename string) (serverConfig, error) {
|
||||
fd, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return serverConfig{}, err
|
||||
@@ -55,8 +56,12 @@ func (c *config) RequireAuth(value bool) {
|
||||
c.cfg.RequireAuth = value
|
||||
}
|
||||
|
||||
func (c *config) RPCSecret(secret string) {
|
||||
c.cfg.RPCSecret = secret
|
||||
func (c *config) Username(username string) {
|
||||
c.cfg.Username = username
|
||||
}
|
||||
|
||||
func (c *config) Password(password string) {
|
||||
c.cfg.Password = password
|
||||
}
|
||||
|
||||
func (c *config) QueueSize(size int) {
|
||||
|
||||
@@ -11,18 +11,21 @@ import (
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Secret string `json:"secret"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
req := new(LoginRequest)
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
err := json.NewDecoder(r.Body).Decode(req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if config.Instance().GetConfig().RPCSecret != req.Secret {
|
||||
cfg := config.Instance().GetConfig()
|
||||
|
||||
if cfg.Username != req.Username || cfg.Password != req.Password {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -31,6 +34,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"expiresAt": expiresAt,
|
||||
"username": req.Username,
|
||||
})
|
||||
|
||||
tokenString, err := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
|
||||
|
||||
Reference in New Issue
Block a user