Yuki N. > COMMIT;

Signed-off-by: Shin'ya Minazuki <shinyoukai@laidback.moe>
This commit is contained in:
2026-01-03 11:31:22 -03:00
commit 3736c10d58
12 changed files with 328 additions and 0 deletions

86
auth.go Normal file
View File

@@ -0,0 +1,86 @@
package main
import (
"context"
"fmt"
"log"
"net/url"
"os"
"github.com/mattn/go-mastodon"
"github.com/spf13/cobra"
)
var authCmd = &cobra.Command{
Use: "auth",
Short: "Authenticate to a Mastodon/Pleroma instance",
Run: func(_ *cobra.Command, args []string) {
doLogin()
},
}
func init() {
ConfInit()
rootCmd.AddCommand(authCmd)
}
func doLogin() {
client := &mastodon.AppConfig{
Server: Config.Host,
ClientName: "Yuki",
Scopes: "read write follow",
Website: "https://projects.laidback.moe/yuki/",
RedirectURIs: "urn:ietf:wg:oauth:2.0:oob",
}
app, err := mastodon.RegisterApp(context.Background(), client)
if err != nil {
log.Fatal(err)
}
u, err := url.Parse(app.AuthURI)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Go to \n%s\n and copy/paste the given auth code\n", u)
var authCode string
fmt.Print("Paste the code here:")
fmt.Scanln(&authCode)
conf := &mastodon.Config{
Server: Config.Host,
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
}
c := mastodon.NewClient(conf)
err = c.GetUserAccessToken(context.Background(), authCode, app.RedirectURI)
if err != nil {
log.Fatal(err)
}
config, err := os.UserConfigDir()
if err != nil {
log.Println("Unable to obtain user's configuration directory")
log.Fatal(err)
}
configPath := config + "/yuki.ini"
f, err := os.OpenFile(configPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
client_id := c.Config.ClientID
client_secret := c.Config.ClientSecret
access_token := c.Config.AccessToken
f.WriteString("client_id = " + client_id + "\n")
f.WriteString("client_secret = " + client_secret + "\n")
f.WriteString("token = " + access_token + "\n")
}