-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_routes.go
116 lines (93 loc) · 2.62 KB
/
auth_routes.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
107
108
109
110
111
112
113
114
115
116
package auth
import (
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"github.com/concepts-system/blueprint-service-go/api"
"github.com/concepts-system/blueprint-service-go/common"
"github.com/concepts-system/blueprint-service-go/errors"
)
const (
keyGrantType = "grant_type"
grantTypePassword = "password"
grantTypeRefreshToken = "refresh_token"
)
var (
errorBadCredentials = errors.Unauthorized.New("Bad credentials")
)
// RegisterRoutes registers all related routes for managing users.
func RegisterRoutes(r *echo.Group) {
authgroup := r.Group("/auth")
authgroup.POST("/token", getToken)
}
func getToken(ec echo.Context) error {
c, _ := ec.(api.Context)
validator := NewAuthenticationRequestValidator()
if err := validator.Bind(c); err != nil {
return err
}
switch validator.GrantType {
case grantTypePassword:
return getAccessTokenByPassword(c)
case grantTypeRefreshToken:
return getAccessTokenByRefreshToken(c)
default:
return errors.BadRequest.New("Unknown grant type")
}
}
func getAccessTokenByPassword(ec echo.Context) error {
c, _ := ec.(api.Context)
validator := NewPasswordAuthenticationRequestValidator()
if err := validator.Bind(c); err != nil {
return err
}
token, err := AuthenticateUserWithCredentials(validator.Username, validator.Password)
if err != nil {
return errorBadCredentials
}
return sendTokenResponse(c, token)
}
func getAccessTokenByRefreshToken(ec echo.Context) error {
c, _ := ec.(api.Context)
// Verify token
validator := NewRefreshTokenAuthenticationRequestValidator()
if err := validator.Bind(c); err != nil {
return err
}
refreshToken, err := jwt.Parse(validator.Request.RefreshToken, func(token *jwt.Token) (interface{}, error) {
return common.Config().GetJWTKey(), nil
})
if err != nil {
return errors.Unauthorized.Wrap(err, "Invalid refresh token")
}
if claims, ok := refreshToken.Claims.(jwt.MapClaims); ok && refreshToken.Valid {
userID, ok := claims[claimUserID].(float64)
if !ok || userID < 0 {
return errors.Unauthorized.New("Invalid refressh token: Invalid user ID claim")
}
token, err := AuthenticateUserByID(uint(userID))
if err != nil {
return err
}
return sendTokenResponse(c, token)
}
// Fall back to unauthenticated
return errorUnauthorized
}
func sendTokenResponse(c echo.Context, token Token) error {
accessToken, err := SignAccessToken(token)
if err != nil {
return err
}
refreshToken, err := SignRefreshToken(token)
if err != nil {
return err
}
serializer := AccessTokenSerializer{
c,
token,
accessToken,
refreshToken,
}
return c.JSON(http.StatusOK, serializer.Response())
}