-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_jwt.go
295 lines (233 loc) · 8.4 KB
/
gf_jwt.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
GloFlow application and media management/publishing platform
Copyright (C) 2021 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_identity_core
import (
"time"
"context"
"strings"
"net/http"
"crypto/rsa"
"github.com/golang-jwt/jwt"
"github.com/gloflow/gloflow/go/gf_core"
"github.com/davecgh/go-spew/spew"
)
//---------------------------------------------------
type GFjwtTokenVal string
//---------------------------------------------------
// GENERATE
//---------------------------------------------------
func JWTpipelineGenerate(pUserIdentifierStr string,
pAuthSubsystemTypeStr string,
pAudienceStr string,
pKeyServerInfo *GFkeyServerInfo,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (*GFjwtTokenVal, *gf_core.GFerror) {
// KEY_SERVER
privateKey, gfErr := ksClientJWTgetSigningKey(pAuthSubsystemTypeStr, pKeyServerInfo, pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
signingKey := privateKey
//----------------------
// JWT_GENERATE
tokenValStr, gfErr := jwtGenerate(pUserIdentifierStr,
signingKey,
pAudienceStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
//----------------------
return tokenValStr, nil
}
//---------------------------------------------------
func jwtGenerate(pUserIdentifierStr string,
pSigningKey *rsa.PrivateKey,
pAudienceStr string,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (*GFjwtTokenVal, *gf_core.GFerror) {
pRuntimeSys.LogNewFun("DEBUG", "JWT generated for user", map[string]interface{}{
"user_identifier_str": pUserIdentifierStr,})
issuerStr := "gf"
_, jwtTokenTTLsecInt := GetSessionTTL()
creationUNIXtimeF := float64(time.Now().UnixNano())/1000000000.0
expirationUNIXtimeInt := int64(creationUNIXtimeF) + jwtTokenTTLsecInt
//----------------------
// CLAIMS
/*
type StandardClaims struct {
Audience string `json:"aud,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
Id string `json:"jti,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
Issuer string `json:"iss,omitempty"`
NotBefore int64 `json:"nbf,omitempty"`
Subject string `json:"sub,omitempty"`
}
*/
claimsMap := map[string]interface{}{
//----------------------
// standard claims
"aud": pAudienceStr, // audience
"exp": expirationUNIXtimeInt, // expires_at
//-----------
// ID
// unique identifier for the token, often referred to as the "JWT ID".
// optional and can be used to uniquely identify a specific token.
// way to prevent token replay attacks, where an attacker tries to reuse a previously issued token
// ADD!! - pass in a GF session ID here to be used as the unique JWT ID,
// to be able to reference sessions with JWT tokens directly.
"jti": "", // ID
//-----------
"iat": int(creationUNIXtimeF), // issued_at
"iss": issuerStr, // issuer
"nbf": int(creationUNIXtimeF), // not_before
"sub": pUserIdentifierStr, // subject
//----------------------
}
pRuntimeSys.LogNewFun("DEBUG", "claims created for new generated JWT", nil)
if gf_core.LogsIsDebugEnabled() {
spew.Dump(claimsMap)
}
//----------------------
jwtToken := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims(claimsMap))
jwtTokenSignedStr, err := jwtToken.SignedString(pSigningKey)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to sign JWT token for user",
"crypto_jwt_sign_token_error",
map[string]interface{}{
"user_identifier_str": pUserIdentifierStr,
},
err, "gf_identity_core", pRuntimeSys)
return nil, gfErr
}
tokenValStr := GFjwtTokenVal(jwtTokenSignedStr)
return &tokenValStr, nil
}
//---------------------------------------------------
// VALIDATE
//---------------------------------------------------
func JWTpipelineValidate(pJWTtokenVal GFjwtTokenVal,
pAuthSubsystemTypeStr string,
pKeyServerInfo *GFkeyServerInfo,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (string, *gf_core.GFerror) {
pRuntimeSys.LogNewFun("DEBUG", "validating JWT token...", map[string]interface{}{
"auth_subsystem_type": pAuthSubsystemTypeStr,
})
// KEY_SERVER
publicKey, gfErr := KSclientJWTgetValidationKey(pAuthSubsystemTypeStr,
pKeyServerInfo, pRuntimeSys)
if gfErr != nil {
return "", gfErr
}
// VALIDATE
validBool, userIdentifierStr, gfErr := JWTvalidate(pJWTtokenVal,
publicKey,
pCtx,
pRuntimeSys)
if gfErr != nil {
return "", gfErr
}
if !validBool {
gfErr := gf_core.ErrorCreate("JWT token supplied for validation is invalid",
"crypto_jwt_verify_token_invalid_error",
map[string]interface{}{
"jwt_token_val_str": pJWTtokenVal,
"auth_subsystem_type_str": pAuthSubsystemTypeStr,
},
nil, "gf_identity_core", pRuntimeSys)
return "", gfErr
}
return userIdentifierStr, nil
}
//---------------------------------------------------
func JWTvalidate(pJWTtokenVal GFjwtTokenVal,
pPublicKey *rsa.PublicKey,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (bool, string, *gf_core.GFerror) {
//-------------------------
// JWT_PARSE
// token validation
jwtToken, err := jwt.Parse(string(pJWTtokenVal), func(pToken *jwt.Token) (interface{}, error) {
return pPublicKey, nil
})
if err != nil {
gfErr := gf_core.ErrorCreate("failed to verify a JWT token",
"crypto_jwt_verify_token_error",
map[string]interface{}{
"jwt_token_val_str": pJWTtokenVal,
},
err, "gf_identity_core", pRuntimeSys)
return false, "", gfErr
}
//-------------------------
pRuntimeSys.LogNewFun("DEBUG", "token validation has been executed...", nil)
if gf_core.LogsIsDebugEnabled() {
spew.Dump(jwtToken)
}
validBool := jwtToken.Valid
//-------------------------
// USER_IDENTIFIER
var userIdentifierStr string
if userIdentifierClaimStr, ok := jwtToken.Claims.(jwt.MapClaims)["sub"]; ok {
userIdentifierStr = userIdentifierClaimStr.(string)
} else {
gfErr := gf_core.ErrorCreate("validated JWT token is missing an expected 'sub' claim",
"crypto_jwt_verify_token_error",
map[string]interface{}{
"jwt_token_val_str": pJWTtokenVal,
},
err, "gf_identity_core", pRuntimeSys)
return false, "", gfErr
}
//-------------------------
pRuntimeSys.LogNewFun("DEBUG", "validated JWT token", map[string]interface{}{"valid_bool": validBool,})
return validBool, userIdentifierStr, nil
}
//-------------------------------------------------------------
// extract JWT token from a http request and return it as a string
func JWTgetTokenFromRequest(pReq *http.Request,
pRuntimeSys *gf_core.RuntimeSys) (string, bool, *gf_core.GFerror) {
// AUTHORIZATION_HEADER - set by a GF http handler /v1/identity/auth0/login_callback
// on successful completion of login at the end of the handler.
// this is the standard Oauth2 header symbol.
cookieNameStr := "Authorization"
cookieFoundBool, cookieValueStr := gf_core.HTTPgetCookieFromReq(cookieNameStr, pReq, pRuntimeSys)
pRuntimeSys.LogNewFun("DEBUG", `auth0 Authorization cookie fetch attempt from incoming request...`,
map[string]interface{}{
"cookie_found_bool": cookieFoundBool,
})
// authCookie, err := pReq.Cookie("Authorization")
if !cookieFoundBool {
return "", false, nil
}
authHeaderStr := cookieValueStr
// remove the "Bearer" header in the token
authPartsLst := strings.Split(authHeaderStr, " ")
if len(authPartsLst) != 2 || strings.ToLower(authPartsLst[0]) != "bearer" {
gfErr := gf_core.ErrorCreate("Authorization cookie is not in a valid format (not composed of 2 components, starting with 'Bearer ...')",
"http_cookie",
map[string]interface{}{
"path_str": pReq.URL.Path,
"auth_header_str": authHeaderStr,
},
nil, "gf_auth0", pRuntimeSys)
return "", false, gfErr
}
return authPartsLst[1], true, nil
}