-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
106 lines (90 loc) · 2.42 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"golang.org/x/crypto/bcrypt"
"gopkg.in/go-playground/validator.v9"
)
// LoginRequest for login requests
type LoginRequest struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
}
// Validate request returns nil or ValidationErrors ( []FieldError )
func (l *LoginRequest) Validate() error {
validate := validator.New()
err := validate.Struct(l)
if err != nil {
return err
}
return nil
}
// LoginResponse for login requests
type LoginResponse struct {
Token string `json:"token"`
}
func (s *Service) loginHandler(w http.ResponseWriter, r *http.Request) {
// Read body
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
w.WriteHeader(400)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}
defer r.Body.Close()
// Unmarshal
var requestMsg LoginRequest
err = json.Unmarshal(b, &requestMsg)
if err != nil {
fmt.Println(err)
w.WriteHeader(400)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}
// Validate
if err := requestMsg.Validate(); err != nil {
fmt.Println(err)
w.WriteHeader(400)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}
// Check if user exists in storage
if _, ok := s.Users[requestMsg.Username]; !ok {
err := errors.New("username is not recognized please register")
fmt.Println(err)
w.WriteHeader(403)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}
// Comparing the password with the hash
if err := bcrypt.CompareHashAndPassword([]byte(s.Users[requestMsg.Username].Password), []byte(requestMsg.Password)); err != nil {
http.Error(w, err.Error(), 403)
return
}
// Create the token
token := jwt.New(jwt.SigningMethodHS256)
// Create a map to store our claims
claims := token.Claims.(jwt.MapClaims)
claims["username"] = requestMsg.Username
claims["expires"] = time.Now().Add(time.Hour * 24).Unix()
// Sign the token
tokenString, _ := token.SignedString(s.TokenSigningKey)
// Create response
responseMsg := &LoginResponse{Token: tokenString}
output, err := json.Marshal(responseMsg)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// Respond
w.Header().Set("content-type", "application/json")
w.Write(output)
// Log
fmt.Printf("user logged in: %s\n", requestMsg.Username)
}