2020-09-30 13:11:33 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 13:11:33 +08:00
|
|
|
|
|
|
|
package interact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-15 03:00:08 +08:00
|
|
|
"regexp"
|
2020-09-30 13:11:33 +08:00
|
|
|
"strings"
|
|
|
|
|
2024-02-15 00:49:29 +08:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-12-12 21:28:37 +08:00
|
|
|
"code.gitea.io/tea/modules/task"
|
2020-10-03 10:54:09 +08:00
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2020-09-30 13:11:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateLogin create an login interactive
|
|
|
|
func CreateLogin() error {
|
2022-10-31 09:56:23 +08:00
|
|
|
var (
|
2024-02-15 00:49:29 +08:00
|
|
|
name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint string
|
|
|
|
insecure, sshAgent, versionCheck bool
|
2022-10-31 09:56:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
versionCheck = true
|
2020-09-30 13:11:33 +08:00
|
|
|
|
2020-10-03 10:54:09 +08:00
|
|
|
promptI := &survey.Input{Message: "URL of Gitea instance: "}
|
|
|
|
if err := survey.AskOne(promptI, &giteaURL, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
2020-10-03 10:54:09 +08:00
|
|
|
giteaURL = strings.TrimSuffix(strings.TrimSpace(giteaURL), "/")
|
2020-09-30 13:11:33 +08:00
|
|
|
if len(giteaURL) == 0 {
|
|
|
|
fmt.Println("URL is required!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:28:37 +08:00
|
|
|
name, err := task.GenerateLoginName(giteaURL, "")
|
2020-09-30 13:11:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-03 10:54:09 +08:00
|
|
|
promptI = &survey.Input{Message: "Name of new Login [" + name + "]: "}
|
|
|
|
if err := survey.AskOne(promptI, &name); err != nil {
|
|
|
|
return err
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 03:00:08 +08:00
|
|
|
loginMethod, err := promptSelect("Login with: ", []string{"token", "ssh-key/certificate"}, "", "")
|
|
|
|
if err != nil {
|
2020-10-03 10:54:09 +08:00
|
|
|
return err
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 03:00:08 +08:00
|
|
|
switch loginMethod {
|
2023-06-08 00:47:52 +08:00
|
|
|
default: // token
|
2022-09-15 03:00:08 +08:00
|
|
|
var hasToken bool
|
|
|
|
promptYN := &survey.Confirm{
|
|
|
|
Message: "Do you have an access token?",
|
|
|
|
Default: false,
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
2022-09-15 03:00:08 +08:00
|
|
|
if err = survey.AskOne(promptYN, &hasToken); err != nil {
|
2020-10-03 10:54:09 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-15 03:00:08 +08:00
|
|
|
if hasToken {
|
|
|
|
promptI = &survey.Input{Message: "Token: "}
|
|
|
|
if err := survey.AskOne(promptI, &token, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
promptI = &survey.Input{Message: "Username: "}
|
|
|
|
if err = survey.AskOne(promptI, &user, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
promptPW := &survey.Password{Message: "Password: "}
|
|
|
|
if err = survey.AskOne(promptPW, &passwd, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-15 00:49:29 +08:00
|
|
|
|
|
|
|
var tokenScopes []string
|
|
|
|
promptS := &survey.MultiSelect{Message: "Token Scopes:", Options: tokenScopeOpts}
|
|
|
|
if err := survey.AskOne(promptS, &tokenScopes, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scopes = strings.Join(tokenScopes, ",")
|
|
|
|
|
|
|
|
// Ask for OTP last so it's less likely to timeout
|
|
|
|
promptO := &survey.Input{Message: "OTP (if applicable)"}
|
|
|
|
if err := survey.AskOne(promptO, &otp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-15 03:00:08 +08:00
|
|
|
}
|
|
|
|
case "ssh-key/certificate":
|
|
|
|
promptI = &survey.Input{Message: "SSH Key/Certificate Path (leave empty for auto-discovery in ~/.ssh and ssh-agent):"}
|
|
|
|
if err := survey.AskOne(promptI, &sshKey); err != nil {
|
2020-10-03 10:54:09 +08:00
|
|
|
return err
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
2022-09-15 03:00:08 +08:00
|
|
|
|
|
|
|
if sshKey == "" {
|
|
|
|
sshKey, err = promptSelect("Select ssh-key: ", task.ListSSHPubkey(), "", "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ssh certificate
|
|
|
|
if strings.Contains(sshKey, "principals") {
|
|
|
|
sshCertPrincipal = regexp.MustCompile(`.*?principals: (.*?)[,|\s]`).FindStringSubmatch(sshKey)[1]
|
|
|
|
if strings.Contains(sshKey, "(ssh-agent)") {
|
|
|
|
sshAgent = true
|
|
|
|
sshKey = ""
|
|
|
|
} else {
|
|
|
|
sshKey = regexp.MustCompile(`\((.*?)\)$`).FindStringSubmatch(sshKey)[1]
|
|
|
|
sshKey = strings.TrimSuffix(sshKey, "-cert.pub")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sshKeyFingerprint = regexp.MustCompile(`(SHA256:.*?)\s`).FindStringSubmatch(sshKey)[1]
|
|
|
|
if strings.Contains(sshKey, "(ssh-agent)") {
|
|
|
|
sshAgent = true
|
|
|
|
sshKey = ""
|
|
|
|
} else {
|
|
|
|
sshKey = regexp.MustCompile(`\((.*?)\)$`).FindStringSubmatch(sshKey)[1]
|
|
|
|
sshKey = strings.TrimSuffix(sshKey, ".pub")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-03 10:54:09 +08:00
|
|
|
var optSettings bool
|
2022-09-15 03:00:08 +08:00
|
|
|
promptYN := &survey.Confirm{
|
2020-10-03 10:54:09 +08:00
|
|
|
Message: "Set Optional settings: ",
|
|
|
|
Default: false,
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
2020-10-03 10:54:09 +08:00
|
|
|
if err = survey.AskOne(promptYN, &optSettings); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if optSettings {
|
2020-12-11 21:42:41 +08:00
|
|
|
promptI = &survey.Input{Message: "SSH Key Path (leave empty for auto-discovery):"}
|
2020-10-03 10:54:09 +08:00
|
|
|
if err := survey.AskOne(promptI, &sshKey); err != nil {
|
|
|
|
return err
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-03 10:54:09 +08:00
|
|
|
promptYN = &survey.Confirm{
|
|
|
|
Message: "Allow Insecure connections: ",
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
if err = survey.AskOne(promptYN, &insecure); err != nil {
|
|
|
|
return err
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
2022-10-31 09:56:23 +08:00
|
|
|
|
|
|
|
promptYN = &survey.Confirm{
|
|
|
|
Message: "Check version of Gitea instance: ",
|
|
|
|
Default: true,
|
|
|
|
}
|
|
|
|
if err = survey.AskOne(promptYN, &versionCheck); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|
|
|
|
|
2024-02-15 00:49:29 +08:00
|
|
|
return task.CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint, insecure, sshAgent, versionCheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenScopeOpts = []string{
|
|
|
|
string(gitea.AccessTokenScopeAll),
|
|
|
|
string(gitea.AccessTokenScopeRepo),
|
|
|
|
string(gitea.AccessTokenScopeRepoStatus),
|
|
|
|
string(gitea.AccessTokenScopePublicRepo),
|
|
|
|
string(gitea.AccessTokenScopeAdminOrg),
|
|
|
|
string(gitea.AccessTokenScopeWriteOrg),
|
|
|
|
string(gitea.AccessTokenScopeReadOrg),
|
|
|
|
string(gitea.AccessTokenScopeAdminPublicKey),
|
|
|
|
string(gitea.AccessTokenScopeWritePublicKey),
|
|
|
|
string(gitea.AccessTokenScopeReadPublicKey),
|
|
|
|
string(gitea.AccessTokenScopeAdminRepoHook),
|
|
|
|
string(gitea.AccessTokenScopeWriteRepoHook),
|
|
|
|
string(gitea.AccessTokenScopeReadRepoHook),
|
|
|
|
string(gitea.AccessTokenScopeAdminOrgHook),
|
|
|
|
string(gitea.AccessTokenScopeAdminUserHook),
|
|
|
|
string(gitea.AccessTokenScopeNotification),
|
|
|
|
string(gitea.AccessTokenScopeUser),
|
|
|
|
string(gitea.AccessTokenScopeReadUser),
|
|
|
|
string(gitea.AccessTokenScopeUserEmail),
|
|
|
|
string(gitea.AccessTokenScopeUserFollow),
|
|
|
|
string(gitea.AccessTokenScopeDeleteRepo),
|
|
|
|
string(gitea.AccessTokenScopePackage),
|
|
|
|
string(gitea.AccessTokenScopeWritePackage),
|
|
|
|
string(gitea.AccessTokenScopeReadPackage),
|
|
|
|
string(gitea.AccessTokenScopeDeletePackage),
|
|
|
|
string(gitea.AccessTokenScopeAdminGPGKey),
|
|
|
|
string(gitea.AccessTokenScopeWriteGPGKey),
|
|
|
|
string(gitea.AccessTokenScopeReadGPGKey),
|
|
|
|
string(gitea.AccessTokenScopeAdminApplication),
|
|
|
|
string(gitea.AccessTokenScopeWriteApplication),
|
|
|
|
string(gitea.AccessTokenScopeReadApplication),
|
|
|
|
string(gitea.AccessTokenScopeSudo),
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|