(config *config.Config)
| 135 | } |
| 136 | |
| 137 | func OnlyAPIKeyAuthenticator(config *config.Config) gin.HandlerFunc { |
| 138 | return func(c *gin.Context) { |
| 139 | apiToken := config.APIToken |
| 140 | |
| 141 | // Get Authorization token |
| 142 | authHeader := c.Request.Header.Get("Authorization") |
| 143 | |
| 144 | // Check Authorization Header format |
| 145 | if authHeader == "" { |
| 146 | slog.Info("missing authorization header", slog.Any("url", c.Request.URL)) |
| 147 | c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing Authorization header"}) |
| 148 | return |
| 149 | } |
| 150 | |
| 151 | // Get token |
| 152 | token := strings.TrimPrefix(authHeader, "Bearer ") |
| 153 | |
| 154 | if token == apiToken { |
| 155 | // get current user from query string |
| 156 | currentUser := c.Query(httpbase.CurrentUserQueryVar) |
| 157 | if len(currentUser) > 0 { |
| 158 | httpbase.SetCurrentUser(c, currentUser) |
| 159 | } |
| 160 | } else { |
| 161 | c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "please use API key for authentication"}) |
| 162 | return |
| 163 | } |
| 164 | |
| 165 | c.Next() |
| 166 | } |
| 167 | } |
no test coverage detected