From 146b374060f0ea406e6e832a8964c90dae1d4365 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Sat, 10 Jul 2021 11:05:26 -0400 Subject: [PATCH] Add token auth --- commands.go | 13 +++++++++---- login.go | 6 +++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/commands.go b/commands.go index 824d985..75a28e5 100644 --- a/commands.go +++ b/commands.go @@ -37,14 +37,19 @@ func authenticate(m chat1.MsgSummary) { k.SendMessageByConvID(m.ConvID, "Please remember to delete your message after we have authenticated!") } parts := strings.Split(m.Content.Text.Body, " ") - if len(parts) != 3 { + if len(parts) != 3 || len(parts) != 2 { k.SendMessageByConvID(m.ConvID, "Invalid input for command authenticate. Requires username and password. This information is not stored in keybase, or logged.") return } log.LogDebug("Proper amount of parameters has been received, calling login()") - username := parts[1] - password := parts[2] - t, err := login(context.Background(), username, password) + var username, password, tok string + if len(parts) == 3 { + username = parts[1] + password = parts[2] + } else { + tok = parts[1] + } + t, err := login(context.Background(), username, password, tok) if err != nil { handleError(err, m, "There was an error logging in. Contact @rudi9719 for more information with code %+v") return diff --git a/login.go b/login.go index 0a1fafa..789c1f2 100644 --- a/login.go +++ b/login.go @@ -5,16 +5,20 @@ import ( "encoding/json" "github.com/bogosj/tesla" + "golang.org/x/oauth2" ) func noDevice(ctx context.Context, devices []tesla.Device) (d tesla.Device, passcode string, err error) { return tesla.Device{}, "", nil } -func login(ctx context.Context, username string, password string) (string, error) { +func login(ctx context.Context, username string, password string, tok string) (string, error) { client, err := tesla.NewClient( ctx, tesla.WithMFAHandler(noDevice), + tesla.WithToken(&oauth2.Token{ + RefreshToken: tok, + }), tesla.WithCredentials(username, password), ) if err != nil {