/
middleware.go
89 lines (83 loc) · 2.85 KB
/
middleware.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
package auth
import (
"errors"
"fmt"
"log"
"net/http"
"strings"
srvConfig "github.com/CHESSComputing/golib/config"
services "github.com/CHESSComputing/golib/services"
"github.com/gin-gonic/gin"
)
// _token is used across all authorized APIs
var _token *Token
// gin cookies
// https://gin-gonic.com/docs/examples/cookie/
// more advanced use-case:
// https://stackoverflow.com/questions/66289603/use-existing-session-cookie-in-gin-router
func TokenMiddleware(clientId string, verbose int) gin.HandlerFunc {
return func(c *gin.Context) {
// check if user request has valid token
tokenStr := RequestToken(c.Request)
token := &Token{AccessToken: tokenStr}
if err := token.Validate(clientId); err != nil {
msg := fmt.Sprintf("TokenMiddleware: invalid token %s, error %v", tokenStr, err)
log.Println("ERROR:", msg)
rec := services.Response("authz", http.StatusUnauthorized, services.TokenError, err)
c.AbortWithStatusJSON(http.StatusUnauthorized, rec)
return
}
if verbose > 0 {
log.Println("INFO: token is validated")
}
c.Next()
}
}
// ScopeTokenMiddleware provides token validation with specific scope
func ScopeTokenMiddleware(scope, clientId string, verbose int) gin.HandlerFunc {
return func(c *gin.Context) {
// check if user request has valid token
tokenStr := RequestToken(c.Request)
token := &Token{AccessToken: tokenStr}
if err := token.Validate(clientId); err != nil {
msg := fmt.Sprintf("ScopeTokenMiddleware: invalid token %s, error %v", tokenStr, err)
log.Println("ERROR:", msg)
rec := services.Response("authz", http.StatusUnauthorized, services.TokenError, err)
c.AbortWithStatusJSON(http.StatusUnauthorized, rec)
return
}
if verbose > 0 {
log.Println("INFO: write token is validated")
}
// check if token has proper write scope
claims, err := TokenClaims(tokenStr, srvConfig.Config.Authz.ClientID)
if err != nil {
msg := fmt.Sprintf("ScopeTokenMiddleware: token '%s' error '%s'", tokenStr, err)
log.Println("ERROR:", msg)
log.Println("token", tokenStr)
rec := services.Response("authz", http.StatusUnauthorized, services.ScopeError, errors.New(msg))
c.AbortWithStatusJSON(http.StatusUnauthorized, rec)
return
}
tscope := claims.CustomClaims.Scope
if !strings.Contains(tscope, scope) {
msg := fmt.Sprintf("ScopeTokenMiddleware: token scope '%s' does not match with scope '%s'", tscope, scope)
log.Println("ERROR:", msg)
log.Println("token", tokenStr)
rec := services.Response("authz", http.StatusUnauthorized, services.ScopeError, errors.New(msg))
c.AbortWithStatusJSON(http.StatusUnauthorized, rec)
return
}
c.Next()
}
}
// RequestToken gets token from http request
func RequestToken(r *http.Request) string {
tokenStr := r.Header.Get("Authorization")
if tokenStr == "" {
return tokenStr
}
arr := strings.Split(tokenStr, " ")
token := arr[len(arr)-1]
return token
}