2024-06-02 07:53:13 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
2024-02-25 06:41:13 +00:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
2024-02-23 09:44:57 +00:00
|
|
|
)
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
type LoginExecutor struct {
|
2024-02-23 09:44:57 +00:00
|
|
|
*flag.FlagSet
|
2024-05-22 23:30:09 +01:00
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
topLevelFlags TopLevelFlags
|
|
|
|
instance string
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func NewLoginExecutor(tlf TopLevelFlags, name, summary string) *LoginExecutor {
|
|
|
|
command := LoginExecutor{
|
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
2024-05-22 23:30:09 +01:00
|
|
|
topLevelFlags: tlf,
|
2024-05-23 18:06:49 +01:00
|
|
|
instance: "",
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
command.StringVar(&command.instance, flagInstance, "", "Specify the instance that you want to login to.")
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
|
|
|
|
|
|
|
return &command
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
func (c *LoginExecutor) Execute() error {
|
2024-02-23 09:44:57 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if c.instance == "" {
|
2024-05-23 18:06:49 +01:00
|
|
|
return FlagNotSetError{flagText: flagInstance}
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instance := c.instance
|
|
|
|
|
|
|
|
if !strings.HasPrefix(instance, "https") || !strings.HasPrefix(instance, "http") {
|
|
|
|
instance = "https://" + instance
|
|
|
|
}
|
|
|
|
|
|
|
|
for strings.HasSuffix(instance, "/") {
|
|
|
|
instance = instance[:len(instance)-1]
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:31:17 +00:00
|
|
|
credentials := config.Credentials{
|
2024-02-23 09:44:57 +00:00
|
|
|
Instance: instance,
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:31:17 +00:00
|
|
|
gtsClient := client.NewClient(credentials)
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
if err := gtsClient.Register(); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to register the application: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 06:41:13 +00:00
|
|
|
consentPageURL := gtsClient.AuthCodeURL()
|
|
|
|
|
|
|
|
utilities.OpenLink(consentPageURL)
|
|
|
|
|
|
|
|
consentMessageFormat := `
|
|
|
|
You'll need to sign into your GoToSocial's consent page in order to generate the out-of-band token to continue with
|
|
|
|
the application's login process. Your browser may have opened the link to the consent page already. If not, please
|
|
|
|
copy and paste the link below to your browser:
|
2024-02-23 09:44:57 +00:00
|
|
|
|
2024-02-25 06:41:13 +00:00
|
|
|
%s
|
2024-02-23 09:44:57 +00:00
|
|
|
|
2024-02-25 06:41:13 +00:00
|
|
|
Once you have the code please copy and paste it below.
|
|
|
|
|
|
|
|
`
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
fmt.Printf(consentMessageFormat, consentPageURL)
|
|
|
|
|
|
|
|
var code string
|
|
|
|
fmt.Print("Out-of-band token: ")
|
|
|
|
|
|
|
|
if _, err := fmt.Scanln(&code); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("failed to read access code: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 06:41:13 +00:00
|
|
|
if err := gtsClient.UpdateToken(code); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to update the client's access token: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
account, err := gtsClient.VerifyCredentials()
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to verify the credentials: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
loginName, err := config.SaveCredentials(c.topLevelFlags.ConfigDir, account.Username, gtsClient.Authentication)
|
2024-02-23 09:44:57 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to save the authentication details: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Successfully logged into %s\n", loginName)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|