-
Notifications
You must be signed in to change notification settings - Fork 0
/
authnController.go
98 lines (84 loc) · 2.77 KB
/
authnController.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
package rest
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/go-playground/validator/v10"
"github.com/kdefombelle/go-sample/jwt"
"github.com/kdefombelle/go-sample/logger"
"github.com/kdefombelle/go-sample/nursery/account"
)
// use a single instance of Validate, it caches struct info
var validate *validator.Validate = validator.New()
// LoginRequest the login request parameters.
type LoginRequest struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
}
// LoginResponse the login request response.
type LoginResponse struct {
UserID string `json:"userid" validate:"required"`
}
// AuthnController the Authentication Controller.
type AuthnController struct {
AccountService *account.Service
JwtService jwt.Service
}
// Signin to authenticate user based on a LoginRequest.
// It returns a LoginResponse.
func (ac *AuthnController) Signin(w http.ResponseWriter, r *http.Request) {
logger.Logger.Info("Signin attempt")
bytesUser, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Logger.Warnf("Cannot read login request [%v]", err)
invalidRequest(w, err)
return
}
request := &LoginRequest{}
err = json.Unmarshal(bytesUser, request)
logger.Logger.Infof("Signin attempt for [%s]", request.Username)
if err != nil {
logger.Logger.Warnw("Cannot deserialise", "request", request)
invalidRequest(w, err)
return
}
err = validate.Struct(request)
if err != nil {
logger.Logger.Warnw("Request not validated", "request", request)
invalidRequest(w, err)
return
}
// authenticating the user
account, err := ac.AccountService.Authenticate(request.Username, request.Password)
if err != nil {
logger.Logger.Warnw("Cannot check user authorization", "err", err)
invalidRequest(w, errors.New("invalid username or password"))
return
}
// creating the token
token, err := ac.JwtService.NewToken(request.Username)
if err != nil {
logger.Logger.Warnw("Cannot create authorization", "err", err)
invalidRequest(w, errors.New("cannot create authorization"))
return
}
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: token.AccessToken,
Expires: token.ExpriresIn,
HttpOnly: true,
Secure: false, //TODO: set to true once HTTPS (i.e. the cookie cannot be used out of HTTPS)
Path: "/",
// SameSite: http.SameSiteStrictMode, //TODO: check if needed to add later when running on samesite - this is to avoid CSRF
})
//https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Pragma", "no-cache")
response := LoginResponse{
UserID: account.Username,
}
logger.Logger.Infof("Signin successfull for [%s]", request.Username)
json, _ := json.Marshal(response)
_, _ = w.Write(json)
}