()
| 81 | } |
| 82 | |
| 83 | func 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 | } |
no test coverage detected