2020-12-11 04:07:29 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
package task
|
2020-12-11 04:07:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
"code.gitea.io/tea/modules/config"
|
2020-12-11 04:07:29 -05:00
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
// CreateLogin create a login to be stored in config
|
2022-10-30 21:56:23 -04:00
|
|
|
func CreateLogin(name, token, user, passwd, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint string, insecure, sshAgent, versionCheck bool) error {
|
2020-12-11 04:07:29 -05:00
|
|
|
// checks ...
|
|
|
|
// ... if we have a url
|
|
|
|
if len(giteaURL) == 0 {
|
2020-12-16 11:18:10 -05:00
|
|
|
return fmt.Errorf("You have to input Gitea server URL")
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
// ... if there already exist a login with same name
|
|
|
|
if login := config.GetLoginByName(name); login != nil {
|
|
|
|
return fmt.Errorf("login name '%s' has already been used", login.Name)
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
2020-12-12 08:28:37 -05:00
|
|
|
// ... if we already use this token
|
|
|
|
if login := config.GetLoginByToken(token); login != nil {
|
|
|
|
return fmt.Errorf("token already been used, delete login '%s' first", login.Name)
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:00:08 -04:00
|
|
|
if !sshAgent && sshCertPrincipal == "" && sshKey == "" {
|
|
|
|
// .. if we have enough information to authenticate
|
|
|
|
if len(token) == 0 && (len(user)+len(passwd)) == 0 {
|
|
|
|
return fmt.Errorf("No token set")
|
|
|
|
} else if len(user) != 0 && len(passwd) == 0 {
|
|
|
|
return fmt.Errorf("No password set")
|
|
|
|
} else if len(user) == 0 && len(passwd) != 0 {
|
|
|
|
return fmt.Errorf("No user set")
|
|
|
|
}
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize URL
|
|
|
|
serverURL, err := utils.NormalizeURL(giteaURL)
|
|
|
|
if err != nil {
|
2020-12-16 11:18:10 -05:00
|
|
|
return fmt.Errorf("Unable to parse URL: %s", err)
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:00:08 -04:00
|
|
|
// check if it's a certificate the principal doesn't matter as the user
|
|
|
|
// has explicitly selected this private key
|
|
|
|
if _, err := os.Stat(sshKey + "-cert.pub"); err == nil {
|
|
|
|
sshCertPrincipal = "yes"
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
login := config.Login{
|
2022-09-14 15:00:08 -04:00
|
|
|
Name: name,
|
|
|
|
URL: serverURL.String(),
|
|
|
|
Token: token,
|
|
|
|
Insecure: insecure,
|
|
|
|
SSHKey: sshKey,
|
|
|
|
SSHCertPrincipal: sshCertPrincipal,
|
|
|
|
SSHKeyFingerprint: sshKeyFingerprint,
|
|
|
|
SSHAgent: sshAgent,
|
|
|
|
Created: time.Now().Unix(),
|
2022-10-30 21:56:23 -04:00
|
|
|
VersionCheck: versionCheck,
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:00:08 -04:00
|
|
|
if len(token) == 0 && sshCertPrincipal == "" && !sshAgent && sshKey == "" {
|
2021-08-30 11:19:45 -04:00
|
|
|
if login.Token, err = generateToken(login, user, passwd); err != nil {
|
2020-12-16 11:18:10 -05:00
|
|
|
return err
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 11:19:45 -04:00
|
|
|
client := login.Client()
|
|
|
|
|
2020-12-11 04:07:29 -05:00
|
|
|
// Verify if authentication works and get user info
|
2020-12-12 08:28:37 -05:00
|
|
|
u, _, err := client.GetMyUserInfo()
|
2020-12-11 04:07:29 -05:00
|
|
|
if err != nil {
|
2020-12-16 11:18:10 -05:00
|
|
|
return err
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
login.User = u.UserName
|
|
|
|
|
|
|
|
if len(login.Name) == 0 {
|
2020-12-16 11:18:10 -05:00
|
|
|
if login.Name, err = GenerateLoginName(giteaURL, login.User); err != nil {
|
|
|
|
return err
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we do not have a method to get SSH config from api,
|
2023-06-06 09:14:21 -04:00
|
|
|
// so we just use the host
|
|
|
|
login.SSHHost = serverURL.Host
|
2020-12-11 04:07:29 -05:00
|
|
|
|
2020-12-11 08:42:41 -05:00
|
|
|
if len(sshKey) == 0 {
|
2020-12-12 08:28:37 -05:00
|
|
|
login.SSHKey, err = findSSHKey(client)
|
2020-12-11 08:42:41 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Warning: problem while finding a SSH key: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:18:10 -05:00
|
|
|
if err = config.AddLogin(&login); err != nil {
|
|
|
|
return err
|
2020-12-11 04:07:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Login as %s on %s successful. Added this login as %s\n", login.User, login.URL, login.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
// generateToken creates a new token when given BasicAuth credentials
|
2021-08-30 11:19:45 -04:00
|
|
|
func generateToken(login config.Login, user, pass string) (string, error) {
|
|
|
|
client := login.Client(gitea.SetBasicAuth(user, pass))
|
2020-12-11 04:07:29 -05:00
|
|
|
|
2022-04-07 15:44:16 -04:00
|
|
|
tl, _, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{
|
|
|
|
ListOptions: gitea.ListOptions{Page: -1},
|
|
|
|
})
|
2020-12-11 04:07:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-08-30 11:19:45 -04:00
|
|
|
host, _ := os.Hostname()
|
2020-12-11 04:07:29 -05:00
|
|
|
tokenName := host + "-tea"
|
|
|
|
|
2021-08-30 11:19:45 -04:00
|
|
|
// append timestamp, if a token with this hostname already exists
|
2020-12-11 04:07:29 -05:00
|
|
|
for i := range tl {
|
|
|
|
if tl[i].Name == tokenName {
|
|
|
|
tokenName += time.Now().Format("2006-01-02_15-04-05")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t, _, err := client.CreateAccessToken(gitea.CreateAccessTokenOption{Name: tokenName})
|
|
|
|
return t.Token, err
|
|
|
|
}
|
2020-12-12 08:28:37 -05:00
|
|
|
|
|
|
|
// GenerateLoginName generates a name string based on instance URL & adds username if the result is not unique
|
|
|
|
func GenerateLoginName(url, user string) (string, error) {
|
|
|
|
parsedURL, err := utils.NormalizeURL(url)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
name := parsedURL.Host
|
|
|
|
|
|
|
|
// append user name if login name already exists
|
|
|
|
if len(user) != 0 {
|
|
|
|
if login := config.GetLoginByName(name); login != nil {
|
|
|
|
return name + "_" + user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|