/
endpoints.go
76 lines (66 loc) · 2.02 KB
/
endpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package auth
import (
"net/http"
"time"
"github.com/magrandera/SPaaS/common"
jwt "github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"github.com/magrandera/SPaaS/config"
)
// ChangePassword allows for changing the password
func ChangePassword(c echo.Context) error {
newPassword := c.FormValue("password")
if len(newPassword) < 8 {
return c.JSON(http.StatusBadRequest, map[string]string{
"error": "password has to be at leat 8 characters",
})
}
hashedPassword, err := common.HashPassword(newPassword)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": err.Error(),
})
}
config.Cfg.Config.Set("password", hashedPassword)
config.Save()
return c.NoContent(http.StatusOK)
}
// Login is the endpoint for login
func Login(c echo.Context) error {
username := c.FormValue("username")
password := c.FormValue("password")
if username == config.Cfg.Config.GetString("username") && common.CheckPasswordHash(password, config.Cfg.Config.GetString("password")) {
// Create token
token := jwt.New(jwt.SigningMethodHS256)
// Set claims
claims := token.Claims.(jwt.MapClaims)
claims["username"] = username
claims["admin"] = true
claims["created"] = time.Now().Unix()
claims["exp"] = time.Now().Add(time.Hour * 24 * 365).Unix()
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte(config.Cfg.Config.GetString("secret")))
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{
"token": t,
})
}
return echo.ErrUnauthorized
}
// GetToken generates a token for internal request use
func GetToken() (string, error) {
// Create token
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["username"] = "spaas"
claims["admin"] = true
claims["exp"] = time.Now().Add(time.Hour * 24 * 365).Unix()
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte(config.Cfg.Config.GetString("secret")))
if err != nil {
return "", err
}
return t, nil
}