Add token auth

This commit is contained in:
2021-07-10 11:05:26 -04:00
parent 2132f1088c
commit 146b374060
2 changed files with 14 additions and 5 deletions

View File

@ -37,14 +37,19 @@ func authenticate(m chat1.MsgSummary) {
k.SendMessageByConvID(m.ConvID, "Please remember to delete your message after we have authenticated!") k.SendMessageByConvID(m.ConvID, "Please remember to delete your message after we have authenticated!")
} }
parts := strings.Split(m.Content.Text.Body, " ") 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.") k.SendMessageByConvID(m.ConvID, "Invalid input for command authenticate. Requires username and password. This information is not stored in keybase, or logged.")
return return
} }
log.LogDebug("Proper amount of parameters has been received, calling login()") log.LogDebug("Proper amount of parameters has been received, calling login()")
username := parts[1] var username, password, tok string
password := parts[2] if len(parts) == 3 {
t, err := login(context.Background(), username, password) username = parts[1]
password = parts[2]
} else {
tok = parts[1]
}
t, err := login(context.Background(), username, password, tok)
if err != nil { if err != nil {
handleError(err, m, "There was an error logging in. Contact @rudi9719 for more information with code %+v") handleError(err, m, "There was an error logging in. Contact @rudi9719 for more information with code %+v")
return return

View File

@ -5,16 +5,20 @@ import (
"encoding/json" "encoding/json"
"github.com/bogosj/tesla" "github.com/bogosj/tesla"
"golang.org/x/oauth2"
) )
func noDevice(ctx context.Context, devices []tesla.Device) (d tesla.Device, passcode string, err error) { func noDevice(ctx context.Context, devices []tesla.Device) (d tesla.Device, passcode string, err error) {
return tesla.Device{}, "", nil 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( client, err := tesla.NewClient(
ctx, ctx,
tesla.WithMFAHandler(noDevice), tesla.WithMFAHandler(noDevice),
tesla.WithToken(&oauth2.Token{
RefreshToken: tok,
}),
tesla.WithCredentials(username, password), tesla.WithCredentials(username, password),
) )
if err != nil { if err != nil {