-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
163 lines (139 loc) · 4.68 KB
/
auth.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package auth
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/mitchellh/mapstructure"
"net/http"
"time"
)
// structure holding data for instantiation of Service
type authService struct {
authConfig Config
}
// New builds an authService instance given the config object
func New(authConfig Config) authService {
return authService{
authConfig,
}
}
// NewWithDefaults creates a new authService with a private key
func NewWithDefaults(privateKey string) authService {
config := DefaultAuthConfig([]byte(privateKey))
return authService{
config,
}
}
// FromRequest from http.Request transforms a cookie in a request in an Authentication instance
func (service authService) FromRequest(r *http.Request) (*Authentication, error) {
if cookie, cookieError := r.Cookie(service.authConfig.JWTCookieName); cookieError != nil {
return nil, cookieError
} else {
return service.FromCookie(cookie)
}
}
// FromCookie transforms a JWT cookie back to an authentication
func (service authService) FromCookie(cookie *http.Cookie) (*Authentication, error) {
token, err := jwt.Parse(cookie.Value, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return service.authConfig.JWTPrivateKey, nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return service.constructAuthentication(claims)
} else {
if validationError, ok := err.(*jwt.ValidationError); ok {
// if we have only a validation expired error, we allow the token to be generated
if validationError.Errors == jwt.ValidationErrorExpired {
authentication, conversionError := service.constructAuthentication(claims)
if conversionError != nil {
return nil, conversionError
}
return authentication, validationError
}
}
return nil, err
}
}
// ToJWTCookie transforms and Authentication into a Cookie
func (service authService) ToJWTCookie(authentication *Authentication) *http.Cookie {
type JWTToken struct {
jwt.StandardClaims
Name string `json:"name,omitempty"`
Username string `json:"username,omitempty"`
Authorities []GrantedAuthority `json:"authorities,omitempty"`
}
token := jwt.NewWithClaims(jwt.SigningMethodHS512, JWTToken{
jwt.StandardClaims{
Issuer: authentication.Issuer,
IssuedAt: authentication.IssuedAt,
Subject: authentication.Subject,
ExpiresAt: authentication.ExpiresAt,
},
authentication.Name,
authentication.Username,
authentication.Authorities,
})
signedString, _ := token.SignedString(service.authConfig.JWTPrivateKey)
return &http.Cookie{
Name: service.authConfig.JWTCookieName,
Path: "/",
HttpOnly: true,
MaxAge: 60 * 60 * 24 * 30, // one month valid TODO: configurable
Value: signedString,
}
}
// GetClearedJWTCookie gets a blank cookie with a name corresponding to the provided config
func (service authService) GetClearedJWTCookie() *http.Cookie {
return &http.Cookie{
Name: service.authConfig.JWTCookieName,
Path: "/",
HttpOnly: true,
MaxAge: 0,
Expires: time.Unix(0, 0),
Value: "",
}
}
// RefreshAuthentication refreshes Authentication expiracy date
func (service authService) RefreshAuthentication(oldAuth *Authentication) (*Authentication, error) {
var refreshedAuth Authentication
now := time.Now().In(time.UTC).Unix()
// first check if MaxRenewalTime has been reached
if now > int64(service.authConfig.MaxRenewalTime)+oldAuth.IssuedAt {
// if so, user is obliged to log in again, renewal of tokens is refused.
return nil, &Error{ErrorCode: MaxRefreshTimeReached}
}
refreshedAuth = *oldAuth
refreshedAuth.ExpiresAt = now + service.authConfig.TokenExpiresIn
return &refreshedAuth, nil
}
// --------------------------
// private stuff
// --------------------------
// constructAuthentication: from the claims of a jwt, create an authentication, or error if claims are not decodable
func (service authService) constructAuthentication(claims jwt.MapClaims) (*Authentication, error) {
var auth Authentication
// converts inner maps so that we don't have to
error := mapstructure.Decode(claims, &auth)
if error != nil {
return nil, error
}
auth.IssuedAt = int64(toFloat64(claims["iat"]))
auth.ExpiresAt = int64(toFloat64(claims["exp"]))
auth.Subject = toString(claims["sub"])
auth.Issuer = toString(claims["iss"])
return &auth, nil
}
func toString(aString interface{}) string {
if s, ok := aString.(string); ok {
return s
}
return ""
}
func toFloat64(aNumber interface{}) float64 {
if convertedNumb, ok := aNumber.(float64); ok {
return convertedNumb
}
return 0
}