(router *gin.Engine, basicRes context.BasicRes)
| 102 | } |
| 103 | |
| 104 | func RestAuthentication(router *gin.Engine, basicRes context.BasicRes) gin.HandlerFunc { |
| 105 | |
| 106 | db := basicRes.GetDal() |
| 107 | logger := basicRes.GetLogger() |
| 108 | if db == nil { |
| 109 | panic(fmt.Errorf("db is not initialised")) |
| 110 | } |
| 111 | apiKeyHelper := apikeyhelper.NewApiKeyHelper(basicRes, logger) |
| 112 | return func(c *gin.Context) { |
| 113 | path := c.Request.URL.Path |
| 114 | // Only open api needs to check api key |
| 115 | if !strings.HasPrefix(path, "/rest") { |
| 116 | logger.Debug("path %s will continue", path) |
| 117 | c.Next() |
| 118 | return |
| 119 | } |
| 120 | path = strings.TrimPrefix(path, "/rest") |
| 121 | authHeader := c.GetHeader("Authorization") |
| 122 | ok := CheckAuthorizationHeader(c, logger, db, apiKeyHelper, authHeader, path) |
| 123 | if !ok { |
| 124 | c.Abort() |
| 125 | return |
| 126 | } else { |
| 127 | router.HandleContext(c) |
| 128 | c.Abort() |
| 129 | return |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func CheckAuthorizationHeader(c *gin.Context, logger log.Logger, db dal.Dal, apiKeyHelper *apikeyhelper.ApiKeyHelper, authHeader, path string) bool { |
| 135 | if authHeader == "" { |
no test coverage detected