This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.go
67 lines (57 loc) · 1.91 KB
/
login.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
package handlers
import (
"database/sql"
"net/http"
"time"
"github.com/PacktPublishing/Echo-Essentials/chapter5/bindings"
"github.com/PacktPublishing/Echo-Essentials/chapter5/models"
"github.com/PacktPublishing/Echo-Essentials/chapter5/renderings"
jwt "github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"golang.org/x/crypto/bcrypt"
)
// Login - Login Handler will take a username and password from the request
// hash the password, verify it matches in the database and respond with a token
func Login(c echo.Context) error {
resp := renderings.LoginResponse{}
lr := new(bindings.LoginRequest)
if err := c.Bind(lr); err != nil {
resp.Success = false
resp.Message = "Unable to bind request for login"
return c.JSON(http.StatusBadRequest, resp)
}
if err := c.Validate(lr); err != nil {
resp.Success = false
resp.Message = err.Error()
return c.JSON(http.StatusBadRequest, resp)
} // get DB from context
db := c.Get(models.DBContextKey).(*sql.DB)
// get user by username from models
user, err := models.GetUserByUsername(db, lr.Username)
if err != nil {
resp.Success = false
resp.Message = "Username or Password incorrect"
return c.JSON(http.StatusUnauthorized, resp)
}
if err := bcrypt.CompareHashAndPassword(
user.PasswordHash, []byte(lr.Password)); err != nil {
resp.Success = false
resp.Message = "Username or Password incorrect"
return c.JSON(http.StatusUnauthorized, resp)
} // need to make a token, successful login
signingKey := c.Get(models.SigningContextKey).([]byte)
// Create the Claims
claims := &jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * 72).Unix(),
Issuer: "service",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(signingKey)
if err != nil {
resp.Success = false
resp.Message = "Server Error"
return c.JSON(http.StatusInternalServerError, resp)
}
resp.Token = ss
return c.JSON(http.StatusOK, resp)
}