Skip to content

Commit

Permalink
fix gitea "cannot authenticate user. 403 Forbidden" (#221)
Browse files Browse the repository at this point in the history
Co-authored-by: slt <[email protected]>
  • Loading branch information
speatzle and slt authored Jun 5, 2021
1 parent 16bdc9f commit b1ff254
Showing 1 changed file with 14 additions and 3 deletions.
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

0 comments on commit b1ff254

Please sign in to comment.