mirror of
https://gitea.com/gitea/tea.git
synced 2025-10-30 08:45:28 +01:00
Login requires a http/https login URL and revmoe SSH as a login method. SSH will be optional (#826)
Fix #825 Reviewed-on: https://gitea.com/gitea/tea/pulls/826
This commit is contained in:
@@ -120,7 +120,7 @@ func InitCommand(cmd *cli.Command) *TeaContext {
|
|||||||
// override config user with env variable
|
// override config user with env variable
|
||||||
envLogin := GetLoginByEnvVar()
|
envLogin := GetLoginByEnvVar()
|
||||||
if envLogin != nil {
|
if envLogin != nil {
|
||||||
_, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "", false, "", "")
|
_, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err.Error())
|
log.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func CreateLogin() error {
|
|||||||
|
|
||||||
printTitleAndContent("Name of new Login: ", name)
|
printTitleAndContent("Name of new Login: ", name)
|
||||||
|
|
||||||
loginMethod, err := promptSelectV2("Login with: ", []string{"token", "ssh-key/certificate", "oauth"})
|
loginMethod, err := promptSelectV2("Login with: ", []string{"token", "oauth"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -104,7 +104,7 @@ func CreateLogin() error {
|
|||||||
printTitleAndContent("Allow Insecure connections:", strconv.FormatBool(insecure))
|
printTitleAndContent("Allow Insecure connections:", strconv.FormatBool(insecure))
|
||||||
|
|
||||||
return auth.OAuthLoginWithOptions(name, giteaURL, insecure)
|
return auth.OAuthLoginWithOptions(name, giteaURL, insecure)
|
||||||
default: // token
|
case "token":
|
||||||
var hasToken bool
|
var hasToken bool
|
||||||
if err := huh.NewConfirm().
|
if err := huh.NewConfirm().
|
||||||
Title("Do you have an access token?").
|
Title("Do you have an access token?").
|
||||||
@@ -154,7 +154,7 @@ func CreateLogin() error {
|
|||||||
Value(&tokenScopes).
|
Value(&tokenScopes).
|
||||||
Validate(func(s []string) error {
|
Validate(func(s []string) error {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
return errors.New("At least one scope is required")
|
return errors.New("at least one scope is required")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}).
|
}).
|
||||||
@@ -176,26 +176,36 @@ func CreateLogin() error {
|
|||||||
}
|
}
|
||||||
printTitleAndContent("OTP (if applicable):", otp)
|
printTitleAndContent("OTP (if applicable):", otp)
|
||||||
}
|
}
|
||||||
case "ssh-key/certificate":
|
default:
|
||||||
if err := huh.NewInput().
|
return fmt.Errorf("unknown login method: %s", loginMethod)
|
||||||
Title("SSH Key/Certificate Path (leave empty for auto-discovery in ~/.ssh and ssh-agent):").
|
}
|
||||||
Value(&sshKey).
|
|
||||||
WithTheme(theme.GetTheme()).
|
var optSettings bool
|
||||||
Run(); err != nil {
|
if err := huh.NewConfirm().
|
||||||
|
Title("Set Optional settings:").
|
||||||
|
Value(&optSettings).
|
||||||
|
WithTheme(theme.GetTheme()).
|
||||||
|
Run(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
printTitleAndContent("Set Optional settings:", strconv.FormatBool(optSettings))
|
||||||
|
|
||||||
|
if optSettings {
|
||||||
|
pubKeys := task.ListSSHPubkey()
|
||||||
|
emptyOpt := "Auto-discovery SSH Key in ~/.ssh and ssh-agent"
|
||||||
|
pubKeys = append([]string{emptyOpt}, pubKeys...)
|
||||||
|
|
||||||
|
sshKey, err = promptSelect("Select ssh-key: ", pubKeys, "", "", "")
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
printTitleAndContent("SSH Key/Certificate Path (leave empty for auto-discovery in ~/.ssh and ssh-agent):", sshKey)
|
if sshKey == emptyOpt {
|
||||||
|
sshKey = ""
|
||||||
|
}
|
||||||
|
|
||||||
if sshKey == "" {
|
printTitleAndContent("SSH Key Path (leave empty for auto-discovery) in ~/.ssh and ssh-agent):", sshKey)
|
||||||
pubKeys := task.ListSSHPubkey()
|
|
||||||
if len(pubKeys) == 0 {
|
if sshKey != "" {
|
||||||
fmt.Println("No SSH keys found in ~/.ssh or ssh-agent")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
sshKey, err = promptSelect("Select ssh-key: ", pubKeys, "", "", "")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
printTitleAndContent("Selected ssh-key:", sshKey)
|
printTitleAndContent("Selected ssh-key:", sshKey)
|
||||||
|
|
||||||
// ssh certificate
|
// ssh certificate
|
||||||
@@ -219,27 +229,6 @@ func CreateLogin() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var optSettings bool
|
|
||||||
if err := huh.NewConfirm().
|
|
||||||
Title("Set Optional settings:").
|
|
||||||
Value(&optSettings).
|
|
||||||
WithTheme(theme.GetTheme()).
|
|
||||||
Run(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
printTitleAndContent("Set Optional settings:", strconv.FormatBool(optSettings))
|
|
||||||
|
|
||||||
if optSettings {
|
|
||||||
if err := huh.NewInput().
|
|
||||||
Title("SSH Key Path (leave empty for auto-discovery):").
|
|
||||||
Value(&sshKey).
|
|
||||||
WithTheme(theme.GetTheme()).
|
|
||||||
Run(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
printTitleAndContent("SSH Key Path (leave empty for auto-discovery):", sshKey)
|
|
||||||
|
|
||||||
if err := huh.NewConfirm().
|
if err := huh.NewConfirm().
|
||||||
Title("Allow Insecure connections:").
|
Title("Allow Insecure connections:").
|
||||||
|
|||||||
@@ -68,9 +68,6 @@ func CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCe
|
|||||||
token,
|
token,
|
||||||
user,
|
user,
|
||||||
passwd,
|
passwd,
|
||||||
sshAgent,
|
|
||||||
sshKey,
|
|
||||||
sshCertPrincipal,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -95,7 +92,7 @@ func CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCe
|
|||||||
VersionCheck: versionCheck,
|
VersionCheck: versionCheck,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(token) == 0 && sshCertPrincipal == "" && !sshAgent && sshKey == "" {
|
if len(token) == 0 {
|
||||||
if login.Token, err = generateToken(login, user, passwd, otp, scopes); err != nil {
|
if login.Token, err = generateToken(login, user, passwd, otp, scopes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,25 +14,21 @@ func ValidateAuthenticationMethod(
|
|||||||
token string,
|
token string,
|
||||||
user string,
|
user string,
|
||||||
passwd string,
|
passwd string,
|
||||||
sshAgent bool,
|
|
||||||
sshKey string,
|
|
||||||
sshCertPrincipal string,
|
|
||||||
) (*url.URL, error) {
|
) (*url.URL, error) {
|
||||||
// Normalize URL
|
// Normalize URL
|
||||||
serverURL, err := NormalizeURL(giteaURL)
|
serverURL, err := NormalizeURL(giteaURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Unable to parse URL: %s", err)
|
return nil, fmt.Errorf("unable to parse URL: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sshAgent && sshCertPrincipal == "" && sshKey == "" {
|
// .. if we have enough information to authenticate
|
||||||
// .. if we have enough information to authenticate
|
if len(token) == 0 && (len(user)+len(passwd)) == 0 {
|
||||||
if len(token) == 0 && (len(user)+len(passwd)) == 0 {
|
return nil, fmt.Errorf("no token set")
|
||||||
return nil, fmt.Errorf("No token set")
|
} else if len(user) != 0 && len(passwd) == 0 {
|
||||||
} else if len(user) != 0 && len(passwd) == 0 {
|
return nil, fmt.Errorf("no password set")
|
||||||
return nil, fmt.Errorf("No password set")
|
} else if len(user) == 0 && len(passwd) != 0 {
|
||||||
} else if len(user) == 0 && len(passwd) != 0 {
|
return nil, fmt.Errorf("no user set")
|
||||||
return nil, fmt.Errorf("No user set")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return serverURL, nil
|
return serverURL, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user