-
Notifications
You must be signed in to change notification settings - Fork 13
/
auth_middleware.go
40 lines (34 loc) · 1004 Bytes
/
auth_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package server
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/tensorchord/envd-server/api/types"
)
func (s *Server) AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
amr := types.AuthMiddlewareRequest{}
if err := c.BindUri(&amr); err != nil {
respondWithError(c, http.StatusUnauthorized,
fmt.Sprintf("auth failed: %v", err))
c.Next()
return
}
for _, k := range s.authInfo {
if k.IdentityToken == amr.IdentityToken {
c.Set("identity_token", amr.IdentityToken)
c.Next()
return
}
}
respondWithError(c, http.StatusUnauthorized,
"failed to auth the identity_token")
}
}
// nolint:unparam
func respondWithError(c *gin.Context, code int, message interface{}) {
c.AbortWithStatusJSON(code, gin.H{"error": message})
}