-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathapp_auth.go
More file actions
152 lines (108 loc) · 2.89 KB
/
Copy pathapp_auth.go
File metadata and controls
152 lines (108 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"errors"
"fmt"
"ghost-chat/internal/auth"
"ghost-chat/internal/config"
)
type TwitchAuthStatus struct {
LoggedIn bool `json:"loggedIn"`
Login string `json:"login"`
}
type authPendingData struct {
UserCode string `json:"userCode"`
VerificationURI string `json:"verificationUri"`
ExpiresIn int `json:"expiresIn"`
}
type authSuccessData struct {
Login string `json:"login"`
}
type authErrorData struct {
Reason string `json:"reason"`
}
func (a *App) persistTwitchLogin(login string) error {
a.configMu.Lock()
defer a.configMu.Unlock()
if a.config.Twitch.Account.Login == login {
return nil
}
a.config.Twitch.Account.Login = login
return config.Save(a.config, a.configPath)
}
func (a *App) TwitchAuthStatus() (TwitchAuthStatus, error) {
return TwitchAuthStatus{
LoggedIn: a.auth.LoggedIn(),
Login: a.auth.CurrentLogin(),
}, nil
}
func (a *App) TwitchStartLogin() error {
a.authMu.Lock()
if a.authLoginPending {
a.authMu.Unlock()
return nil
}
a.authLoginPending = true
a.authMu.Unlock()
dc, err := a.auth.RequestDeviceCode(context.Background())
if err != nil {
a.clearAuthLoginPending()
return fmt.Errorf("start twitch login: %w", err)
}
a.emit("twitch:auth:pending", authPendingData{
UserCode: dc.UserCode,
VerificationURI: dc.VerificationURI,
ExpiresIn: dc.ExpiresIn,
})
go func() {
defer a.clearAuthLoginPending()
login, err := a.auth.CompleteDeviceLogin(context.Background(), dc)
if err != nil {
a.emit("twitch:auth:error", authErrorData{Reason: err.Error()})
return
}
if err := a.persistTwitchLogin(login); err != nil {
fmt.Printf("failed to save config after login: %s\n", err.Error())
}
a.emit("twitch:auth:success", authSuccessData{Login: login})
a.evaluateRedemptions()
}()
return nil
}
func (a *App) clearAuthLoginPending() {
a.authMu.Lock()
defer a.authMu.Unlock()
a.authLoginPending = false
}
func (a *App) TwitchLogout() error {
if err := a.auth.Logout(context.Background()); err != nil {
return fmt.Errorf("twitch logout: %w", err)
}
if err := a.persistTwitchLogin(""); err != nil {
return fmt.Errorf("save config after logout: %w", err)
}
a.emit("twitch:auth:loggedout", nil)
a.evaluateRedemptions()
return nil
}
func (a *App) restoreTwitchAuth() {
login, err := a.auth.Restore(context.Background())
if errors.Is(err, auth.ErrNoToken) {
return
}
if errors.Is(err, auth.ErrInvalidGrant) {
if saveErr := a.persistTwitchLogin(""); saveErr != nil {
fmt.Printf("failed to clear login after invalid grant: %s\n", saveErr.Error())
}
a.emit("twitch:auth:loggedout", nil)
return
}
if err != nil {
return
}
if saveErr := a.persistTwitchLogin(login); saveErr != nil {
fmt.Printf("failed to persist restored login: %s\n", saveErr.Error())
}
a.emit("twitch:auth:success", authSuccessData{Login: login})
a.evaluateRedemptions()
}