Fix adding login without token on private instances (#392)

fixes #365

Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/392
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin 2021-08-30 23:19:45 +08:00 committed by Andrew Thornton
parent d6df0a53b5
commit 3fca309f2c
2 changed files with 13 additions and 12 deletions

View File

@ -142,8 +142,9 @@ func AddLogin(login *Login) error {
return saveConfig() return saveConfig()
} }
// Client returns a client to operate Gitea API // Client returns a client to operate Gitea API. You may provide additional modifiers
func (l *Login) Client() *gitea.Client { // for the client like gitea.SetBasicAuth() for customization
func (l *Login) Client(options ...func(*gitea.Client)) *gitea.Client {
httpClient := &http.Client{} httpClient := &http.Client{}
if l.Insecure { if l.Insecure {
cookieJar, _ := cookiejar.New(nil) cookieJar, _ := cookiejar.New(nil)
@ -155,10 +156,9 @@ func (l *Login) Client() *gitea.Client {
}} }}
} }
client, err := gitea.NewClient(l.URL, options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient))
gitea.SetToken(l.Token),
gitea.SetHTTPClient(httpClient), client, err := gitea.NewClient(l.URL, options...)
)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -56,14 +56,14 @@ func CreateLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bo
Created: time.Now().Unix(), Created: time.Now().Unix(),
} }
client := login.Client()
if len(token) == 0 { if len(token) == 0 {
if login.Token, err = generateToken(client, user, passwd); err != nil { if login.Token, err = generateToken(login, user, passwd); err != nil {
return err return err
} }
} }
client := login.Client()
// Verify if authentication works and get user info // Verify if authentication works and get user info
u, _, err := client.GetMyUserInfo() u, _, err := client.GetMyUserInfo()
if err != nil { if err != nil {
@ -98,16 +98,17 @@ func CreateLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bo
} }
// generateToken creates a new token when given BasicAuth credentials // generateToken creates a new token when given BasicAuth credentials
func generateToken(client *gitea.Client, user, pass string) (string, error) { func generateToken(login config.Login, user, pass string) (string, error) {
gitea.SetBasicAuth(user, pass)(client) client := login.Client(gitea.SetBasicAuth(user, pass))
host, _ := os.Hostname()
tl, _, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{}) tl, _, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{})
if err != nil { if err != nil {
return "", err return "", err
} }
host, _ := os.Hostname()
tokenName := host + "-tea" tokenName := host + "-tea"
// append timestamp, if a token with this hostname already exists
for i := range tl { for i := range tl {
if tl[i].Name == tokenName { if tl[i].Name == tokenName {
tokenName += time.Now().Format("2006-01-02_15-04-05") tokenName += time.Now().Format("2006-01-02_15-04-05")