-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathjwt.go
330 lines (296 loc) · 10 KB
/
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package middleware
import (
"fmt"
"log/slog"
"net/http"
"os"
"strconv"
"strings"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/segment"
"github.com/diggerhq/digger/backend/services"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
)
func SetContextParameters(c *gin.Context, auth services.Auth, token *jwt.Token) error {
if claims, ok := token.Claims.(jwt.MapClaims); ok {
if claims.Valid() != nil {
slog.Warn("Token's claim is invalid")
return fmt.Errorf("token is invalid")
}
var org *models.Organisation
tenantId := claims["tenantId"]
if tenantId == nil {
slog.Warn("Claim's tenantId is nil")
return fmt.Errorf("token is invalid")
}
tenantId = tenantId.(string)
slog.Debug("Processing tenant ID", "tenantId", tenantId)
org, err := models.DB.GetOrganisation(tenantId)
if err != nil {
slog.Error("Error while fetching organisation", "tenantId", tenantId, "error", err)
return err
} else if org == nil {
slog.Warn("No organisation found for tenantId", "tenantId", tenantId)
return fmt.Errorf("token is invalid")
}
c.Set(ORGANISATION_ID_KEY, org.ID)
segment.GetClient()
segment.IdentifyClient(strconv.Itoa(int(org.ID)), org.Name, org.Name, org.Name, org.Name, strconv.Itoa(int(org.ID)), "")
slog.Debug("Set organisation ID in context", "orgId", org.ID)
tokenType := claims["type"].(string)
permissions := make([]string, 0)
if tokenType == "tenantAccessToken" {
permission, err := auth.FetchTokenPermissions(claims["sub"].(string))
if err != nil {
slog.Error("Error while fetching permissions", "subject", claims["sub"].(string), "error", err)
return fmt.Errorf("token is invalid")
}
permissions = permission
} else {
permissionsClaims := claims["permissions"]
if permissionsClaims == nil {
slog.Warn("Claim's permissions is nil")
return fmt.Errorf("token is invalid")
}
for _, permissionClaim := range permissionsClaims.([]interface{}) {
permissions = append(permissions, permissionClaim.(string))
}
}
for _, permission := range permissions {
if permission == "digger.all.*" {
slog.Debug("Setting admin access level", "permission", permission)
c.Set(ACCESS_LEVEL_KEY, models.AdminPolicyType)
return nil
}
}
for _, permission := range permissions {
if permission == "digger.all.read.*" {
slog.Debug("Setting read access level", "permission", permission)
c.Set(ACCESS_LEVEL_KEY, models.AccessPolicyType)
return nil
}
}
} else {
slog.Warn("Token's claim is invalid")
return fmt.Errorf("token is invalid")
}
return nil
}
func JWTWebAuth(auth services.Auth) gin.HandlerFunc {
return func(c *gin.Context) {
var tokenString string
tokenString, err := c.Cookie("token")
if err != nil {
slog.Warn("Can't get a cookie token", "error", err)
c.AbortWithStatus(http.StatusForbidden)
return
}
if tokenString == "" {
slog.Warn("Auth token is empty")
c.AbortWithStatus(http.StatusForbidden)
return
}
jwtPublicKey := os.Getenv("JWT_PUBLIC_KEY")
if jwtPublicKey == "" {
slog.Error("No JWT_PUBLIC_KEY environment variable provided")
c.String(http.StatusInternalServerError, "Error occurred while reading public key")
c.Abort()
return
}
publicKeyData := []byte(jwtPublicKey)
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(publicKeyData)
if err != nil {
slog.Error("Error while parsing public key", "error", err)
c.String(http.StatusInternalServerError, "Error occurred while parsing public key")
c.Abort()
return
}
// validate token
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return publicKey, nil
})
if err != nil {
slog.Warn("Can't parse a token", "error", err)
c.AbortWithStatus(http.StatusForbidden)
return
}
if token.Valid {
err = SetContextParameters(c, auth, token)
if err != nil {
slog.Error("Error while setting context parameters", "error", err)
c.String(http.StatusForbidden, "Failed to parse token")
c.Abort()
return
}
c.Next()
return
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
slog.Warn("That's not even a token")
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
slog.Warn("Token is either expired or not active yet")
} else {
slog.Warn("Couldn't handle this token", "error", err)
}
} else {
slog.Warn("Couldn't handle this token", "error", err)
}
c.AbortWithStatus(http.StatusForbidden)
}
}
func SecretCodeAuth() gin.HandlerFunc {
return func(c *gin.Context) {
secret := c.Request.Header.Get("x-webhook-secret")
if secret == "" {
slog.Warn("No x-webhook-secret header provided")
c.String(http.StatusForbidden, "No x-webhook-secret header provided")
c.Abort()
return
}
_, err := jwt.Parse(secret, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(os.Getenv("WEBHOOK_SECRET")), nil
})
if err != nil {
slog.Error("Error parsing webhook secret", "error", err)
c.String(http.StatusForbidden, "Invalid x-webhook-secret header provided")
c.Abort()
return
}
slog.Debug("Webhook secret verified successfully")
c.Next()
}
}
func JWTBearerTokenAuth(auth services.Auth) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.Request.Header.Get("Authorization")
if authHeader == "" {
slog.Warn("No Authorization header provided")
c.String(http.StatusForbidden, "No Authorization header provided")
c.Abort()
return
}
token := strings.TrimPrefix(authHeader, "Bearer ")
if token == authHeader {
slog.Warn("Could not find bearer token in Authorization header")
c.String(http.StatusForbidden, "Could not find bearer token in Authorization header")
c.Abort()
return
}
if strings.HasPrefix(token, "cli:") {
slog.Debug("Processing CLI token")
if jobToken, err := CheckJobToken(c, token); err != nil {
slog.Warn("Invalid job token", "error", err)
c.String(http.StatusForbidden, err.Error())
c.Abort()
return
} else {
c.Set(ORGANISATION_ID_KEY, jobToken.OrganisationID)
c.Set(ACCESS_LEVEL_KEY, jobToken.Type)
slog.Debug("Job token verified", "organisationId", jobToken.OrganisationID, "accessLevel", jobToken.Type)
}
} else if strings.HasPrefix(token, "t:") {
slog.Debug("Processing API token")
var dbToken models.Token
tokenObj, err := models.DB.GetToken(token)
if tokenObj == nil {
slog.Warn("Invalid bearer token", "token", token)
c.String(http.StatusForbidden, "Invalid bearer token")
c.Abort()
return
}
if err != nil {
slog.Error("Error while fetching token from database", "error", err)
c.String(http.StatusInternalServerError, "Error occurred while fetching database")
c.Abort()
return
}
c.Set(ORGANISATION_ID_KEY, dbToken.OrganisationID)
c.Set(ACCESS_LEVEL_KEY, dbToken.Type)
slog.Debug("API token verified", "organisationId", dbToken.OrganisationID, "accessLevel", dbToken.Type)
} else {
slog.Debug("Processing JWT token")
jwtPublicKey := os.Getenv("JWT_PUBLIC_KEY")
if jwtPublicKey == "" {
slog.Error("No JWT_PUBLIC_KEY environment variable provided")
c.String(http.StatusInternalServerError, "Error occurred while reading public key")
c.Abort()
return
}
publicKeyData := []byte(jwtPublicKey)
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(publicKeyData)
if err != nil {
slog.Error("Error while parsing public key", "error", err)
c.String(http.StatusInternalServerError, "Error occurred while parsing public key")
c.Abort()
return
}
parsedToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return publicKey, nil
})
if err != nil {
slog.Error("Error while parsing token", "error", err)
c.String(http.StatusForbidden, "Authorization header is invalid")
c.Abort()
return
}
if !parsedToken.Valid {
slog.Warn("Token is invalid")
c.String(http.StatusForbidden, "Authorization header is invalid")
c.Abort()
return
}
err = SetContextParameters(c, auth, parsedToken)
if err != nil {
slog.Error("Error while setting context parameters", "error", err)
c.String(http.StatusForbidden, "Failed to parse token")
c.Abort()
return
}
slog.Debug("JWT token verified successfully")
}
c.Next()
}
}
func AccessLevel(allowedAccessLevels ...string) gin.HandlerFunc {
return func(c *gin.Context) {
accessLevel := c.GetString(ACCESS_LEVEL_KEY)
for _, allowedAccessLevel := range allowedAccessLevels {
if accessLevel == allowedAccessLevel {
slog.Debug("Access level authorized", "accessLevel", accessLevel, "allowedLevel", allowedAccessLevel)
c.Next()
return
}
}
slog.Warn("Access level not allowed", "accessLevel", accessLevel, "allowedLevels", allowedAccessLevels)
c.String(http.StatusForbidden, "Not allowed to access this resource with this access level")
c.Abort()
}
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
const ORGANISATION_ID_KEY = "organisation_ID"
const ORGANISATION_SOURCE_KEY = "organisation_Source"
const USER_ID_KEY = "user_ID"
const ACCESS_LEVEL_KEY = "access_level"
const JOB_TOKEN_KEY = "job_token"