From bc9b090023bfc866b2e189bbad6bbfa2e37cf396 Mon Sep 17 00:00:00 2001 From: Daniele Tricoli Date: Tue, 1 Mar 2022 00:54:39 +0100 Subject: [PATCH] Add the authenticate sud command --- cmd/authenticate.go | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 cmd/authenticate.go diff --git a/cmd/authenticate.go b/cmd/authenticate.go new file mode 100644 index 0000000..427fb8e --- /dev/null +++ b/cmd/authenticate.go @@ -0,0 +1,73 @@ +package cmd + +import ( + "bufio" + "context" + "fmt" + "log" + "os" + "strings" + + "github.com/cking/go-mastodon" + "github.com/spf13/cobra" +) + +const MASTODON_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob" + +// authenticateCmd represents the authenticate command +var authenticateCmd = &cobra.Command{ + Use: "authenticate", + Short: "Authenticate the bot against Mastodon", + Long: "Authenticate against the specified Mastodon instance.", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + mastodon_instance_address := args[0] + + app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{ + Server: mastodon_instance_address, + ClientName: "tg2mastodon", + Scopes: "read write follow", + Website: "https://noa.mornie.org/eriol/tg2mastodon", + }) + + if err != nil { + log.Fatal(err) + } + + client := mastodon.NewClient(&mastodon.Config{ + Server: mastodon_instance_address, + ClientID: app.ClientID, + ClientSecret: app.ClientSecret, + }) + + fmt.Printf("Please open the following URL to authenticate: %s\n", + app.AuthURI) + fmt.Printf("And paste the authentication code here and press enter: ") + + reader := bufio.NewReader(os.Stdin) + authCode, err := reader.ReadString('\n') + if err != nil { + fmt.Println("An error occured while reading input. Please try again", err) + return + } + authCode = strings.TrimSuffix(authCode, "\n") + + err = client.AuthenticateToken( + context.Background(), + authCode, + MASTODON_REDIRECT_URI) + + if err != nil { + log.Fatal(err) + } + + fmt.Println("Authentication success!") + fmt.Println("Please exporte the following environment variables:") + fmt.Printf("export MASTODON_SERVER_ADDRESS='%s'\n", mastodon_instance_address) + fmt.Printf("export MASTODON_ACCESS_TOKEN='%s'\n", client.Config.AccessToken) + }, +} + +func init() { + rootCmd.AddCommand(authenticateCmd) +}