2021-10-22 19:33:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-12-31 12:42:37 +00:00
|
|
|
"fmt"
|
2021-10-22 19:33:57 +00:00
|
|
|
"net/http"
|
2021-12-31 12:42:37 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2021-10-22 19:33:57 +00:00
|
|
|
|
2021-10-23 15:22:58 +00:00
|
|
|
"github.com/gofrs/uuid/v3"
|
|
|
|
"github.com/gorilla/mux"
|
2021-10-22 19:33:57 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-10-22 19:33:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
configEditorGeneralConfig struct {
|
2023-12-13 23:02:14 +00:00
|
|
|
BotEditors []string `json:"bot_editors"`
|
|
|
|
BotName *string `json:"bot_name,omitempty"`
|
|
|
|
Channels []string `json:"channels"`
|
|
|
|
ChannelScopes map[string][]string `json:"channel_scopes"`
|
|
|
|
ChannelHasToken map[string]bool `json:"channel_has_token"`
|
2021-10-22 19:33:57 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerEditorGeneralConfigRoutes() {
|
|
|
|
for _, rd := range []plugins.HTTPRouteRegistrationArgs{
|
2021-10-23 15:22:58 +00:00
|
|
|
{
|
|
|
|
Description: "Add new authorization token",
|
|
|
|
HandlerFunc: configEditorHandleGeneralAddAuthToken,
|
|
|
|
Method: http.MethodPost,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-23 15:22:58 +00:00
|
|
|
Name: "Add authorization token",
|
|
|
|
Path: "/auth-tokens",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Delete authorization token",
|
|
|
|
HandlerFunc: configEditorHandleGeneralDeleteAuthToken,
|
|
|
|
Method: http.MethodDelete,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-23 15:22:58 +00:00
|
|
|
Name: "Delete authorization token",
|
|
|
|
Path: "/auth-tokens/{handle}",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "UUID of the auth-token to delete",
|
|
|
|
Name: "handle",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "List authorization tokens",
|
|
|
|
HandlerFunc: configEditorHandleGeneralListAuthTokens,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-23 15:22:58 +00:00
|
|
|
Name: "List authorization tokens",
|
|
|
|
Path: "/auth-tokens",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
2021-10-22 19:33:57 +00:00
|
|
|
{
|
|
|
|
Description: "Returns the current general config",
|
|
|
|
HandlerFunc: configEditorHandleGeneralGet,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Get general config",
|
|
|
|
Path: "/general",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Updates the general config",
|
|
|
|
HandlerFunc: configEditorHandleGeneralUpdate,
|
|
|
|
Method: http.MethodPut,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Update general config",
|
|
|
|
Path: "/general",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
},
|
2021-12-31 12:42:37 +00:00
|
|
|
{
|
|
|
|
Description: "Get Bot-Auth URLs for updating bot token and channel scopes",
|
|
|
|
HandlerFunc: configEditorHandleGeneralAuthURLs,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-12-31 12:42:37 +00:00
|
|
|
Name: "Get Bot-Auth-URLs",
|
|
|
|
Path: "/auth-urls",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
2021-10-22 19:33:57 +00:00
|
|
|
} {
|
|
|
|
if err := registerRoute(rd); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to register config editor route")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 15:22:58 +00:00
|
|
|
func configEditorHandleGeneralAddAuthToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, _, err := getAuthorizationFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "getting authorized user").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload configAuthToken
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "reading payload").Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fillAuthToken(&payload); err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "hashing token").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := patchConfig(cfg.Config, user, "", "Add auth-token", func(cfg *configFile) error {
|
|
|
|
cfg.AuthTokens[uuid.Must(uuid.NewV4()).String()] = payload
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(payload); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func configEditorHandleGeneralAuthURLs(w http.ResponseWriter, _ *http.Request) {
|
2021-12-31 12:42:37 +00:00
|
|
|
var out struct {
|
2022-12-04 16:14:15 +00:00
|
|
|
AvailableExtendedScopes map[string]string `json:"available_extended_scopes"`
|
|
|
|
UpdateBotToken string `json:"update_bot_token"`
|
|
|
|
UpdateChannelScopes string `json:"update_channel_scopes"`
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 16:14:15 +00:00
|
|
|
out.AvailableExtendedScopes = channelExtendedScopes
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("client_id", cfg.TwitchClient)
|
|
|
|
params.Set("redirect_uri", strings.Join([]string{
|
|
|
|
strings.TrimRight(cfg.BaseURL, "/"),
|
|
|
|
"auth", "update-bot-token",
|
|
|
|
}, "/"))
|
|
|
|
params.Set("response_type", "code")
|
|
|
|
params.Set("scope", strings.Join(botDefaultScopes, " "))
|
|
|
|
params.Set("state", instanceState)
|
|
|
|
|
|
|
|
out.UpdateBotToken = fmt.Sprintf("https://id.twitch.tv/oauth2/authorize?%s", params.Encode())
|
|
|
|
|
|
|
|
params.Set("redirect_uri", strings.Join([]string{
|
|
|
|
strings.TrimRight(cfg.BaseURL, "/"),
|
|
|
|
"auth", "update-channel-scopes",
|
|
|
|
}, "/"))
|
2022-12-04 16:14:15 +00:00
|
|
|
params.Set("scope", "")
|
2021-12-31 12:42:37 +00:00
|
|
|
|
|
|
|
out.UpdateChannelScopes = fmt.Sprintf("https://id.twitch.tv/oauth2/authorize?%s", params.Encode())
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(out); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 15:22:58 +00:00
|
|
|
func configEditorHandleGeneralDeleteAuthToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, _, err := getAuthorizationFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "getting authorized user").Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := patchConfig(cfg.Config, user, "", "Delete auth-token", func(cfg *configFile) error {
|
|
|
|
delete(cfg.AuthTokens, mux.Vars(r)["handle"])
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func configEditorHandleGeneralGet(w http.ResponseWriter, _ *http.Request) {
|
2023-12-13 23:02:14 +00:00
|
|
|
resp := configEditorGeneralConfig{
|
|
|
|
BotEditors: config.BotEditors,
|
|
|
|
Channels: config.Channels,
|
|
|
|
ChannelHasToken: make(map[string]bool),
|
|
|
|
ChannelScopes: make(map[string][]string),
|
|
|
|
}
|
2021-12-31 12:42:37 +00:00
|
|
|
|
2023-02-08 18:10:26 +00:00
|
|
|
channels, err := accessService.ListPermittedChannels()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ch := range channels {
|
2023-12-13 23:02:14 +00:00
|
|
|
if resp.ChannelScopes[ch], err = accessService.GetChannelPermissions(ch); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ChannelHasToken[ch], err = accessService.HasTokensForChannel(ch); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 18:40:06 +00:00
|
|
|
uName, err := accessService.GetBotUsername()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|
2023-12-13 23:02:14 +00:00
|
|
|
resp.BotName = &uName
|
2021-12-31 12:42:37 +00:00
|
|
|
|
2023-12-13 23:02:14 +00:00
|
|
|
if err = json.NewEncoder(w).Encode(resp); err != nil {
|
2021-10-22 19:33:57 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2021-10-23 15:22:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func configEditorHandleGeneralListAuthTokens(w http.ResponseWriter, _ *http.Request) {
|
2021-10-23 15:22:58 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(config.AuthTokens); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2021-10-22 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func configEditorHandleGeneralUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, _, err := getAuthorizationFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "getting authorized user").Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload configEditorGeneralConfig
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range payload.BotEditors {
|
|
|
|
usr, err := twitchClient.GetUserInformation(payload.BotEditors[i])
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "getting bot editor profile").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.BotEditors[i] = usr.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := patchConfig(cfg.Config, user, "", "Update general config", func(cfg *configFile) error {
|
|
|
|
cfg.Channels = payload.Channels
|
|
|
|
cfg.BotEditors = payload.BotEditors
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|