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 Gitea unable to login on 0.12.0+ with error "cannot authenticate user. 403 Forbidden" #221

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Changes from all commits
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
17 changes: 14 additions & 3 deletions remote/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ func (c *client) Login(res http.ResponseWriter, req *http.Request) (*model.User,
return nil, nil
}

client, err := c.newClientToken("")
// Create Client with Basic Auth
client, err := c.newClientBasicAuth(username, password)
if err != nil {
return nil, err
}

// since api does not return token secret, if drone token exists create new one
client.SetBasicAuth(username, password)
resp, err := client.DeleteAccessToken("drone")
if err != nil && !(resp != nil && resp.StatusCode == 404) {
return nil, err
Expand Down Expand Up @@ -392,7 +392,7 @@ func (c *client) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
return parseHook(r)
}

// helper function to return the Gitea client
// helper function to return the Gitea client with Token
func (c *client) newClientToken(token string) (*gitea.Client, error) {
httpClient := &http.Client{}
if c.SkipVerify {
Expand All @@ -403,6 +403,17 @@ func (c *client) newClientToken(token string) (*gitea.Client, error) {
return gitea.NewClient(c.URL, gitea.SetToken(token), gitea.SetHTTPClient(httpClient))
}

// helper function to return the Gitea client with Basic Auth
func (c *client) newClientBasicAuth(username, password string) (*gitea.Client, error) {
httpClient := &http.Client{}
if c.SkipVerify {
httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
return gitea.NewClient(c.URL, gitea.SetBasicAuth(username, password), gitea.SetHTTPClient(httpClient))
}

// helper function to return matching hooks.
func matchingHooks(hooks []*gitea.Hook, rawurl string) *gitea.Hook {
link, err := url.Parse(rawurl)
Expand Down