MCPcopy Create free account
hub / github.com/OpenCSGs/csghub-server / OnlyAPIKeyAuthenticator

Function OnlyAPIKeyAuthenticator

api/middleware/authenticator.go:137–167  ·  view source on GitHub ↗
(config *config.Config)

Source from the content-addressed store, hash-verified

135}
136
137func 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}

Callers 2

NewRouterFunction · 0.92
NewRouterFunction · 0.92

Calls 2

SetCurrentUserFunction · 0.92
GetMethod · 0.45

Tested by

no test coverage detected