-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
162 lines (137 loc) · 5.22 KB
/
auth.go
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
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/gofrs/uuid/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
var instanceState = uuid.Must(uuid.NewV4()).String()
func init() {
for _, rd := range []plugins.HTTPRouteRegistrationArgs{
{
Description: "Updates the bots token for connection to chat and API",
HandlerFunc: handleAuthUpdateBotToken,
Method: http.MethodGet,
Module: "auth",
Name: "Update bot token",
Path: "/update-bot-token",
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
},
{
Description: "Updates scope configuration for EventSub subscription of a channel",
HandlerFunc: handleAuthUpdateChannelGrant,
Method: http.MethodGet,
Module: "auth",
Name: "Update channel scopes",
Path: "/update-channel-scopes",
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
},
} {
if err := registerRoute(rd); err != nil {
logrus.WithError(err).Fatal("Unable to register auth routes")
}
}
}
func handleAuthUpdateBotToken(w http.ResponseWriter, r *http.Request) {
var (
code = r.FormValue("code")
state = r.FormValue("state")
)
if state != instanceState {
http.Error(w, "invalid state, please start again", http.StatusBadRequest)
return
}
params := make(url.Values)
params.Set("client_id", cfg.TwitchClient)
params.Set("client_secret", cfg.TwitchClientSecret)
params.Set("code", code)
params.Set("grant_type", "authorization_code")
params.Set("redirect_uri", strings.Join([]string{
strings.TrimRight(cfg.BaseURL, "/"),
"auth", "update-bot-token",
}, "/"))
req, _ := http.NewRequestWithContext(r.Context(), http.MethodPost, fmt.Sprintf("https://id.twitch.tv/oauth2/token?%s", params.Encode()), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, errors.Wrap(err, "getting access token").Error(), http.StatusInternalServerError)
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.WithError(err).Error("closing response body (leaked fd)")
}
}()
var rData twitch.OAuthTokenResponse
if err := json.NewDecoder(resp.Body).Decode(&rData); err != nil {
http.Error(w, errors.Wrap(err, "decoding access token").Error(), http.StatusInternalServerError)
return
}
_, botUser, err := twitch.New(cfg.TwitchClient, cfg.TwitchClientSecret, rData.AccessToken, "").GetAuthorizedUser(r.Context())
if err != nil {
http.Error(w, errors.Wrap(err, "getting authorized user").Error(), http.StatusInternalServerError)
return
}
if err = accessService.SetBotUsername(botUser); err != nil {
http.Error(w, errors.Wrap(err, "storing bot username").Error(), http.StatusInternalServerError)
return
}
twitchClient.UpdateToken(rData.AccessToken, rData.RefreshToken)
if err = accessService.SetExtendedTwitchCredentials(botUser, rData.AccessToken, rData.RefreshToken, rData.Scope); err != nil {
http.Error(w, errors.Wrap(err, "storing access scopes").Error(), http.StatusInternalServerError)
return
}
http.Error(w, fmt.Sprintf("Authorization as %q complete, you can now close this window.", botUser), http.StatusOK)
frontendNotifyHooks.Ping(frontendNotifyTypeReload) // Tell frontend to update its config
}
func handleAuthUpdateChannelGrant(w http.ResponseWriter, r *http.Request) {
var (
code = r.FormValue("code")
state = r.FormValue("state")
)
if state != instanceState {
http.Error(w, "invalid state, please start again", http.StatusBadRequest)
return
}
params := make(url.Values)
params.Set("client_id", cfg.TwitchClient)
params.Set("client_secret", cfg.TwitchClientSecret)
params.Set("code", code)
params.Set("grant_type", "authorization_code")
params.Set("redirect_uri", strings.Join([]string{
strings.TrimRight(cfg.BaseURL, "/"),
"auth", "update-channel-scopes",
}, "/"))
req, _ := http.NewRequestWithContext(r.Context(), http.MethodPost, fmt.Sprintf("https://id.twitch.tv/oauth2/token?%s", params.Encode()), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, errors.Wrap(err, "getting access token").Error(), http.StatusInternalServerError)
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.WithError(err).Error("closing response body (leaked fd)")
}
}()
var rData twitch.OAuthTokenResponse
if err := json.NewDecoder(resp.Body).Decode(&rData); err != nil {
http.Error(w, errors.Wrap(err, "decoding access token").Error(), http.StatusInternalServerError)
return
}
_, grantUser, err := twitch.New(cfg.TwitchClient, cfg.TwitchClientSecret, rData.AccessToken, "").GetAuthorizedUser(r.Context())
if err != nil {
http.Error(w, errors.Wrap(err, "getting authorized user").Error(), http.StatusInternalServerError)
return
}
if err = accessService.SetExtendedTwitchCredentials(grantUser, rData.AccessToken, rData.RefreshToken, rData.Scope); err != nil {
http.Error(w, errors.Wrap(err, "storing access token").Error(), http.StatusInternalServerError)
return
}
http.Error(w, fmt.Sprintf("Scopes for %q updated, you can now close this window.", grantUser), http.StatusOK)
frontendNotifyHooks.Ping(frontendNotifyTypeReload) // Tell frontend to update its config
}