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

Function GetCurrentUserFromHeader

api/middleware/git_http_param.go:83–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

81}
82
83func GetCurrentUserFromHeader() gin.HandlerFunc {
84 userStore := database.NewUserStore()
85 return func(c *gin.Context) {
86 authHeader := c.Request.Header.Get("Authorization")
87 if authHeader != "" && !strings.HasPrefix(authHeader, "X-OPENCSG-Sync-Token") {
88 authHeader = strings.TrimPrefix(authHeader, "Basic ")
89 authInfo, err := base64.StdEncoding.DecodeString(authHeader)
90 if err != nil {
91 c.Header("WWW-Authenticate", "Basic realm=opencsg-git")
92 c.PureJSON(http.StatusUnauthorized, nil)
93 c.Abort()
94 return
95 }
96 username := strings.Split(string(authInfo), ":")[0]
97 password := strings.Split(string(authInfo), ":")[1]
98
99 user, err := userStore.FindByGitAccessToken(context.Background(), password)
100 if err != nil {
101 c.Header("WWW-Authenticate", "Basic realm=opencsg-git")
102 c.PureJSON(http.StatusUnauthorized, nil)
103 c.Abort()
104 return
105 }
106 if user.Username == username {
107 httpbase.SetCurrentUser(c, username)
108 }
109 }
110
111 c.Next()
112 }
113}

Callers 1

NewRouterFunction · 0.92

Calls 4

FindByGitAccessTokenMethod · 0.95
NewUserStoreFunction · 0.92
SetCurrentUserFunction · 0.92
GetMethod · 0.45

Tested by

no test coverage detected