renamed anilist variables to allow for other services

This commit is contained in:
John O'Keefe 2024-07-30 12:59:21 -04:00
parent 237958cce5
commit d1258c54d3
2 changed files with 28 additions and 29 deletions

View File

@ -15,8 +15,8 @@ func AniListQuery(body interface{}, login bool) (json.RawMessage, string) {
if err != nil { if err != nil {
log.Printf("Failed at response, %s\n", err) log.Printf("Failed at response, %s\n", err)
} }
if login && (AniListJWT{}) != jwt { if login && (AniListJWT{}) != aniListJwt {
response.Header.Add("Authorization", "Bearer "+jwt.AccessToken) response.Header.Add("Authorization", "Bearer "+aniListJwt.AccessToken)
} else if login { } else if login {
return nil, "Please login to anilist to make this request" return nil, "Please login to anilist to make this request"
} }

View File

@ -4,8 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/99designs/keyring"
"github.com/wailsapp/wails/v2/pkg/runtime"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -14,22 +12,25 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/99designs/keyring"
"github.com/wailsapp/wails/v2/pkg/runtime"
) )
var jwt AniListJWT var aniListJwt AniListJWT
var ring, _ = keyring.Open(keyring.Config{ var aniRing, _ = keyring.Open(keyring.Config{
ServiceName: "AniTrack", ServiceName: "AniTrack",
}) })
var ctxShutdown, cancel = context.WithCancel(context.Background()) var aniCtxShutdown, aniCancel = context.WithCancel(context.Background())
func (a *App) AniListLogin() { func (a *App) AniListLogin() {
if (AniListJWT{}) == jwt { if (AniListJWT{}) == aniListJwt {
tokenType, err := ring.Get("anilistTokenType") tokenType, err := aniRing.Get("anilistTokenType")
expiresIn, err := ring.Get("anilistTokenExpiresIn") expiresIn, err := aniRing.Get("anilistTokenExpiresIn")
accessToken, err := ring.Get("anilistAccessToken") accessToken, err := aniRing.Get("anilistAccessToken")
refreshToken, err := ring.Get("anilistRefreshToken") refreshToken, err := aniRing.Get("anilistRefreshToken")
if err != nil { if err != nil {
getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + os.Getenv("ANILIST_APP_ID") + "&redirect_uri=" + os.Getenv("ANILIST_CALLBACK_URI") + "&response_type=code" getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + os.Getenv("ANILIST_APP_ID") + "&redirect_uri=" + os.Getenv("ANILIST_CALLBACK_URI") + "&response_type=code"
runtime.BrowserOpenURL(a.ctx, getAniListCodeUrl) runtime.BrowserOpenURL(a.ctx, getAniListCodeUrl)
@ -39,11 +40,11 @@ func (a *App) AniListLogin() {
handleAniListCallback(serverDone) handleAniListCallback(serverDone)
serverDone.Wait() serverDone.Wait()
} else { } else {
jwt.TokenType = string(tokenType.Data) aniListJwt.TokenType = string(tokenType.Data)
jwt.AccessToken = string(accessToken.Data) aniListJwt.AccessToken = string(accessToken.Data)
jwt.RefreshToken = string(refreshToken.Data) aniListJwt.RefreshToken = string(refreshToken.Data)
expiresInString := string(expiresIn.Data) expiresInString := string(expiresIn.Data)
jwt.ExpiresIn, _ = strconv.Atoi(expiresInString) aniListJwt.ExpiresIn, _ = strconv.Atoi(expiresInString)
} }
} }
} }
@ -52,7 +53,7 @@ func handleAniListCallback(wg *sync.WaitGroup) {
srv := &http.Server{Addr: ":6734"} srv := &http.Server{Addr: ":6734"}
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
select { select {
case <-ctxShutdown.Done(): case <-aniCtxShutdown.Done():
fmt.Println("Shutting down...") fmt.Println("Shutting down...")
return return
default: default:
@ -60,25 +61,25 @@ func handleAniListCallback(wg *sync.WaitGroup) {
content := r.FormValue("code") content := r.FormValue("code")
if content != "" { if content != "" {
jwt = getAniListAuthorizationToken(content) aniListJwt = getAniListAuthorizationToken(content)
_ = ring.Set(keyring.Item{ _ = aniRing.Set(keyring.Item{
Key: "anilistTokenType", Key: "anilistTokenType",
Data: []byte(jwt.TokenType), Data: []byte(aniListJwt.TokenType),
}) })
_ = ring.Set(keyring.Item{ _ = aniRing.Set(keyring.Item{
Key: "anilistTokenExpiresIn", Key: "anilistTokenExpiresIn",
Data: []byte(string(jwt.ExpiresIn)), Data: []byte(string(aniListJwt.ExpiresIn)),
}) })
_ = ring.Set(keyring.Item{ _ = aniRing.Set(keyring.Item{
Key: "anilistAccessToken", Key: "anilistAccessToken",
Data: []byte(jwt.AccessToken), Data: []byte(aniListJwt.AccessToken),
}) })
_ = ring.Set(keyring.Item{ _ = aniRing.Set(keyring.Item{
Key: "anilistRefreshToken", Key: "anilistRefreshToken",
Data: []byte(jwt.RefreshToken), Data: []byte(aniListJwt.RefreshToken),
}) })
fmt.Println("Shutting down...") fmt.Println("Shutting down...")
cancel() aniCancel()
err := srv.Shutdown(context.Background()) err := srv.Shutdown(context.Background())
if err != nil { if err != nil {
log.Println("server.Shutdown:", err) log.Println("server.Shutdown:", err)
@ -89,7 +90,6 @@ func handleAniListCallback(wg *sync.WaitGroup) {
return return
} }
} }
}) })
go func() { go func() {
@ -166,5 +166,4 @@ func (a *App) GetAniListLoggedInUserId() AniListUser {
} }
return post return post
} }