From: Fl_GUI Date: Sun, 15 Dec 2024 15:01:42 +0000 (+0100) Subject: change auth method X-Git-Url: https://git.openfl.eu/?a=commitdiff_plain;h=f25718512605445c1a0815e28328f4c48077ae1e;p=chat-scroll.git change auth method --- diff --git a/decorations/badges.go b/decorations/badges.go index 9dab742..dd6a327 100644 --- a/decorations/badges.go +++ b/decorations/badges.go @@ -27,10 +27,14 @@ func GetBadge(badgeName string) *image.Image { return &badge } -var modelValid = model.Application.Twitch.Valid +var tokenState = model.Application.Twitch.TokenState func downloadBadges() { - if val, _ := modelValid.Get(); !val { + if val, _ := tokenState.Get(); val != model.Valid { + return + } + + if len(badgeMap) != 0 { return } @@ -86,5 +90,5 @@ func saveBadge(name string, url string) { func init() { badgeMap = make(map[string]image.Image) - modelValid.AddListener(binding.NewDataListener(downloadBadges)) + tokenState.AddListener(binding.NewDataListener(downloadBadges)) } diff --git a/decorations/emotes.go b/decorations/emotes.go index 1a59242..9bf89ab 100644 --- a/decorations/emotes.go +++ b/decorations/emotes.go @@ -10,6 +10,7 @@ import ( "sync" "fyne.io/fyne/v2/data/binding" + "go.openfl.eu/chat-scroller/model" "go.openfl.eu/chat-scroller/token" ) @@ -32,7 +33,11 @@ func GetEmote(emoteId string) *image.Image { } func downloadEmotes() { - if val, _ := modelValid.Get(); !val { + if val, _ := tokenState.Get(); val != model.Valid { + return + } + + if len(emoteMap) != 0 { return } @@ -95,5 +100,5 @@ func saveEmoteId(id string, template string) *image.Image { func init() { emoteMap = make(map[string]image.Image) - modelValid.AddListener(binding.NewDataListener(downloadEmotes)) + tokenState.AddListener(binding.NewDataListener(downloadEmotes)) } diff --git a/main.go b/main.go index e97144f..dd9502f 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,16 @@ import ( "runtime/pprof" "fyne.io/fyne/v2/app" + "fyne.io/fyne/v2/data/binding" "fyne.io/fyne/v2/theme" + "go.openfl.eu/chat-scroller/model" "go.openfl.eu/chat-scroller/status" + "go.openfl.eu/chat-scroller/token" // for init purposes _ "go.openfl.eu/chat-scroller/decorations" _ "go.openfl.eu/chat-scroller/messages" + _ "go.openfl.eu/chat-scroller/model" _ "go.openfl.eu/chat-scroller/token" ) @@ -35,6 +39,9 @@ func main() { scroller := app.New() + // this listener will trigger on initialization + model.Application.Twitch.TokenState.AddListener(binding.NewDataListener(func() { token.Authenticate(&scroller) })) + scroller.Settings().SetTheme(theme.LightTheme()) go status.CreateSetupWindow(scroller) diff --git a/messages/client.go b/messages/client.go index f52da2c..b577527 100644 --- a/messages/client.go +++ b/messages/client.go @@ -14,7 +14,7 @@ import ( "go.openfl.eu/chat-scroller/token" ) -var valid = model.Application.Twitch.Valid +var state = model.Application.Twitch.TokenState var client = twitch.NewChatClient() @@ -29,8 +29,8 @@ func startClient() { core.DebugLogger = os.Stdout } - val, _ := valid.Get() - if !val { + val, _ := state.Get() + if val != model.Valid { return } if generate { @@ -74,5 +74,5 @@ func generateFake() { } func init() { - valid.AddListener(fynepatch.NewGoroutineDataListener(startClient)) + state.AddListener(fynepatch.NewGoroutineDataListener(startClient)) } diff --git a/model/model.go b/model/model.go index ab023f8..519d7c0 100644 --- a/model/model.go +++ b/model/model.go @@ -11,8 +11,8 @@ type Model struct { } type TwitchModel struct { - Valid binding.Bool - Token binding.String + TokenState binding.Untyped + Token binding.String } var Application Model @@ -20,15 +20,16 @@ var Application Model func init() { { tw := &Application.Twitch - tw.Valid = binding.NewBool() + tw.TokenState = binding.NewUntyped() + tw.TokenState.Set(Absent) tw.Token = binding.NewString() } - debug := false + debug := true if debug { - Application.Twitch.Valid.AddListener(binding.NewDataListener(func() { - fmt.Print("token valid: ") - fmt.Println(Application.Twitch.Valid.Get()) + Application.Twitch.TokenState.AddListener(binding.NewDataListener(func() { + fmt.Print("token state: ") + fmt.Println(Application.Twitch.TokenState.Get()) })) Application.Twitch.Token.AddListener(binding.NewDataListener(func() { fmt.Print("access token: ") diff --git a/status/create.go b/status/create.go index 1b413d6..5fbf979 100644 --- a/status/create.go +++ b/status/create.go @@ -8,7 +8,6 @@ import ( "go.openfl.eu/chat-scroller/chat" "go.openfl.eu/chat-scroller/model" "go.openfl.eu/chat-scroller/status/twitchconn" - "go.openfl.eu/chat-scroller/status/twitchlogin" ) type windows struct { @@ -18,7 +17,7 @@ type windows struct { var cscrWindows windows -var valid = model.Application.Twitch.Valid +var state = model.Application.Twitch.TokenState // UI items var ( @@ -31,8 +30,8 @@ func spawnChatWindow(app fyne.App) { } func ableSpawnButton() { - val, _ := valid.Get() - if val { + val, _ := state.Get() + if val == model.Valid { spawnButton.Enable() } else { spawnButton.Disable() @@ -43,7 +42,6 @@ func CreateSetupWindow(app fyne.App) { // init but after app is created createSpinner() - twitchlogin.CreateContent() twitchconn.CreateContent() // own creations @@ -58,10 +56,9 @@ func CreateSetupWindow(app fyne.App) { statusWindow.SetContent(container.NewVBox( //spinner, twitchconn.Content, - twitchlogin.Content, spawnButton, )) statusWindow.Show() - valid.AddListener(binding.NewDataListener(ableSpawnButton)) + state.AddListener(binding.NewDataListener(ableSpawnButton)) } diff --git a/status/twitchconn/twitchConnection.go b/status/twitchconn/twitchConnection.go index 1df940f..b9ccc90 100644 --- a/status/twitchconn/twitchConnection.go +++ b/status/twitchconn/twitchConnection.go @@ -10,7 +10,7 @@ import ( var Content fyne.CanvasObject -var valid = model.Application.Twitch.Valid +var tokenState = model.Application.Twitch.TokenState var token = model.Application.Twitch.Token // UI elements @@ -30,11 +30,14 @@ func showContent() { } func updateLabel() { - val, _ := valid.Get() - if val { + state, _ := tokenState.Get() + switch state { + case model.Valid: tokenLabel.Text = "access token available" - } else { + case model.Invalid: tokenLabel.Text = "access token invalid" + case model.Absent: + tokenLabel.Text = "Please log in" } Content.Refresh() @@ -44,8 +47,7 @@ func CreateContent() { Content = container.NewMax( tokenLabel, ) - Content.Hide() - token.AddListener(binding.NewDataListener(showContent)) - valid.AddListener(binding.NewDataListener(updateLabel)) + //token.AddListener(binding.NewDataListener(showContent)) + tokenState.AddListener(binding.NewDataListener(updateLabel)) } diff --git a/status/twitchlogin/twitchLogin.go b/status/twitchlogin/twitchLogin.go deleted file mode 100644 index acae1b4..0000000 --- a/status/twitchlogin/twitchLogin.go +++ /dev/null @@ -1,74 +0,0 @@ -package twitchlogin - -import ( - "net/url" - "sync" - - "fyne.io/fyne/v2" - "fyne.io/fyne/v2/container" - "fyne.io/fyne/v2/data/binding" - "fyne.io/fyne/v2/widget" - "go.openfl.eu/chat-scroller/model" - "go.openfl.eu/twitch-auth/device" -) - -var Content fyne.CanvasObject -var tokenChannel <-chan device.AuthResponse -var readingTokens = sync.Mutex{} - -var valid = model.Application.Twitch.Valid - -func login() { - if !readingTokens.TryLock() { - return - } - defer readingTokens.Unlock() - - // TODO closing channel when/how? - tokenChannel = device.Authenticate(&device.Config{ - "9mcopb33ssgli53u6cor8ou2pvyb0g", - []string{"user:read:chat", "user:write:chat", "chat:read"}, - }) - - app := fyne.CurrentApp() - - for token := range tokenChannel { - if token.Err == nil { - model.Application.Twitch.Token.Set(token.AccessCode) - } else { - valid.Set(false) - err := token.Err - validating, ok := err.(device.AuthorizationPendingError) - if ok { - u, err := url.Parse(validating.Url) - if err != nil { - panic(err) - } - err = app.OpenURL(u) - if err != nil { - panic(err) - } - } - } - } - -} - -func CreateContent() { - label := widget.NewLabel("hi") - loginButton := widget.NewButton("login to twitch", func() { - go login() - }) - Content = container.NewVBox(label, loginButton) - - valid.AddListener(binding.NewDataListener(func() { - val, _ := valid.Get() - if val { - Content.Hide() - } else { - Content.Show() - } - - Content.Refresh() - })) -} diff --git a/todo b/todo index 86a3293..4bdbb4d 100644 --- a/todo +++ b/todo @@ -1,6 +1,6 @@ -#TODO +--TODO -auth again? -persistence +better emotes showing chat settings -log levels +persistence +log levels? diff --git a/token/validate.go b/token/validate.go index a5c1645..4a0f110 100644 --- a/token/validate.go +++ b/token/validate.go @@ -30,7 +30,7 @@ func validate(token string, alreadyLocked bool) { // token error if resp.StatusCode == http.StatusUnauthorized { fmt.Println("invalid token") - model.Application.Twitch.Valid.Set(false) + model.Application.Twitch.TokenState.Set(model.Invalid) return } @@ -52,7 +52,7 @@ func validate(token string, alreadyLocked bool) { fmt.Printf("couldn't decode validation response: %s\n", err) } - model.Application.Twitch.Valid.Set(true) + model.Application.Twitch.TokenState.Set(model.Valid) } func init() {