mirror of
				https://github.com/khairul169/garage-webui.git
				synced 2025-10-30 22:59:31 +07:00 
			
		
		
		
	
		
			
				
	
	
		
			65 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package router
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"errors"
 | |
| 	"khairul169/garage-webui/utils"
 | |
| 	"net/http"
 | |
| 	"strings"
 | |
| 
 | |
| 	"golang.org/x/crypto/bcrypt"
 | |
| )
 | |
| 
 | |
| type Auth struct{}
 | |
| 
 | |
| func (c *Auth) Login(w http.ResponseWriter, r *http.Request) {
 | |
| 	var body struct {
 | |
| 		Username string `json:"username"`
 | |
| 		Password string `json:"password"`
 | |
| 	}
 | |
| 	if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
 | |
| 		utils.ResponseError(w, err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	userPass := strings.Split(utils.GetEnv("AUTH_USER_PASS", ""), ":")
 | |
| 	if len(userPass) < 2 {
 | |
| 		utils.ResponseErrorStatus(w, errors.New("AUTH_USER_PASS not set"), 500)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	if strings.TrimSpace(body.Username) != userPass[0] || bcrypt.CompareHashAndPassword([]byte(userPass[1]), []byte(body.Password)) != nil {
 | |
| 		utils.ResponseErrorStatus(w, errors.New("invalid username or password"), 401)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	utils.Session.Set(r, "authenticated", true)
 | |
| 	utils.ResponseSuccess(w, map[string]bool{
 | |
| 		"authenticated": true,
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func (c *Auth) Logout(w http.ResponseWriter, r *http.Request) {
 | |
| 	utils.Session.Clear(r)
 | |
| 	utils.ResponseSuccess(w, true)
 | |
| }
 | |
| 
 | |
| func (c *Auth) GetStatus(w http.ResponseWriter, r *http.Request) {
 | |
| 	isAuthenticated := true
 | |
| 	authSession := utils.Session.Get(r, "authenticated")
 | |
| 	enabled := false
 | |
| 
 | |
| 	if utils.GetEnv("AUTH_USER_PASS", "") != "" {
 | |
| 		enabled = true
 | |
| 	}
 | |
| 
 | |
| 	if authSession != nil && authSession.(bool) {
 | |
| 		isAuthenticated = true
 | |
| 	}
 | |
| 
 | |
| 	utils.ResponseSuccess(w, map[string]bool{
 | |
| 		"enabled":       enabled,
 | |
| 		"authenticated": isAuthenticated,
 | |
| 	})
 | |
| }
 |