Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gittar remove skipAuth #1856

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions modules/gittar/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,50 +196,43 @@ func doAuth(c *webcontext.Context, repo *models.Repo, repoName string) {
var gitRepository = &gitmodule.Repository{}
var err error
var userInfo *ucauth.UserInfo
//skip authentication
host := c.Host()
for _, skipUrl := range conf.SkipAuthUrls() {
if skipUrl != "" && strings.HasSuffix(host, skipUrl) {
logrus.Debugf("skip authenticate host: %s", host)
gitRepository, err = openRepository(c, repo)
if err != nil {
c.AbortWithStatus(500, err)
return
}
c.Set("repository", gitRepository)

userIdStr := c.GetHeader(httputil.UserHeader)
if userIdStr == "" {
c.AbortWithStatus(500, errors.New("the userID is empty"))
return
}

userInfoDto, err := uc.FindUserById(userIdStr)
if err != nil {
c.AbortWithStatus(500, err)
return
}
logrus.Infof("repo: %s userId: %v, username: %s", repoName, userIdStr, userInfoDto.Username)
//校验通过缓存5分钟结果
//校验失败每次都会请求
_, validateError := ValidaUserRepoWithCache(c, userIdStr, repo)
if validateError != nil {
logrus.Infof("openapi auth fail repo:%s user:%s", repoName, userInfoDto.Username)
c.AbortWithStatus(403, validateError)
return
}
c.Set("repository", gitRepository)
//c.Set("lock", repoLock.Lock)
if !isGitProtocolRequest(c) {
gitRepository, err = openRepository(c, repo)
if err != nil {
c.AbortWithStatus(500, err)
return
}
c.Set("repository", gitRepository)

c.Set("user", &models.User{
Name: userInfoDto.Username,
NickName: userInfoDto.NickName,
Email: userInfoDto.Email,
Id: userIdStr,
})
c.Next()
userIdStr := c.GetHeader(httputil.UserHeader)
if userIdStr == "" {
c.AbortWithStatus(500, errors.New("the userID is empty"))
return
}
userInfoDto, err := uc.FindUserById(userIdStr)
if err != nil {
c.AbortWithStatus(500, err)
return
}
logrus.Infof("repo: %s userId: %v, username: %s", repoName, userIdStr, userInfoDto.Username)
//校验通过缓存5分钟结果
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use english

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

//校验失败每次都会请求
_, validateError := ValidaUserRepoWithCache(c, userIdStr, repo)
if validateError != nil {
logrus.Infof("openapi auth fail repo:%s user:%s", repoName, userInfoDto.Username)
c.AbortWithStatus(403, validateError)
return
}

c.Set("user", &models.User{
Name: userInfoDto.Username,
NickName: userInfoDto.NickName,
Email: userInfoDto.Email,
Id: userIdStr,
})
c.Next()
return
}

userInfo, err = GetUserInfoByTokenOrBasicAuth(c, repo.ProjectID)
Expand Down Expand Up @@ -453,3 +446,11 @@ func getOrgIDV3(c *webcontext.Context, orgName string) (int64, error) {
}
return orgID, nil
}

// isGitProtocolRequest if request like git clone,git push... return true
func isGitProtocolRequest(c *webcontext.Context) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add unit test for this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if c.GetHeader("Git-Protocol") != "" {
return true
}
return false
}