anitrack/AniListUserFunctions.go

216 lines
5.4 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"github.com/99designs/keyring"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
var aniListJwt AniListJWT
var aniRing, _ = keyring.Open(keyring.Config{
ServiceName: "AniTrack",
})
var aniCtxShutdown, aniCancel = context.WithCancel(context.Background())
func (a *App) CheckIfAniListLoggedIn() bool {
if (AniListJWT{} == aniListJwt) {
tokenType, err := aniRing.Get("anilistTokenType")
expiresIn, err := aniRing.Get("anilistTokenExpiresIn")
accessToken, err := aniRing.Get("anilistAccessToken")
refreshToken, err := aniRing.Get("anilistRefreshToken")
if err != nil || len(accessToken.Data) == 0 {
return false
} else {
aniListJwt.TokenType = string(tokenType.Data)
aniListJwt.AccessToken = string(accessToken.Data)
aniListJwt.RefreshToken = string(refreshToken.Data)
aniListJwt.ExpiresIn, _ = strconv.Atoi(string(expiresIn.Data))
return true
}
} else {
return true
}
}
func (a *App) AniListLogin() {
if (AniListJWT{} == aniListJwt) {
tokenType, err := aniRing.Get("anilistTokenType")
expiresIn, err := aniRing.Get("anilistTokenExpiresIn")
accessToken, err := aniRing.Get("anilistAccessToken")
refreshToken, err := aniRing.Get("anilistRefreshToken")
if err != nil || len(accessToken.Data) == 0 {
getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + Environment.ANILIST_APP_ID + "&redirect_uri=" + Environment.ANILIST_CALLBACK_URI + "&response_type=code"
runtime.BrowserOpenURL(*wailsContext, getAniListCodeUrl)
serverDone := &sync.WaitGroup{}
serverDone.Add(1)
a.handleAniListCallback(serverDone)
serverDone.Wait()
} else {
aniListJwt.TokenType = string(tokenType.Data)
aniListJwt.AccessToken = string(accessToken.Data)
aniListJwt.RefreshToken = string(refreshToken.Data)
aniListJwt.ExpiresIn, _ = strconv.Atoi(string(expiresIn.Data))
}
}
}
func (a *App) handleAniListCallback(wg *sync.WaitGroup) {
mux := http.NewServeMux()
srv := &http.Server{Addr: ":6734", Handler: mux}
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
select {
case <-aniCtxShutdown.Done():
fmt.Println("Shutting down...")
return
default:
}
content := r.FormValue("code")
if content != "" {
aniListJwt = getAniListAuthorizationToken(content)
_ = aniRing.Set(keyring.Item{
Key: "anilistTokenType",
Data: []byte(aniListJwt.TokenType),
})
_ = aniRing.Set(keyring.Item{
Key: "anilistTokenExpiresIn",
Data: []byte(strconv.Itoa(aniListJwt.ExpiresIn)),
})
_ = aniRing.Set(keyring.Item{
Key: "anilistAccessToken",
Data: []byte(aniListJwt.AccessToken),
})
_ = aniRing.Set(keyring.Item{
Key: "anilistRefreshToken",
Data: []byte(aniListJwt.RefreshToken),
})
_, err := runtime.MessageDialog(*wailsContext, runtime.MessageDialogOptions{
Title: "AniList Authorization",
Message: "It is now safe to close your browser tab",
})
if err != nil {
log.Println(err)
}
fmt.Println("Shutting down...")
aniCancel()
err = srv.Shutdown(context.Background())
if err != nil {
log.Println("server.Shutdown:", err)
}
} else {
_, err := fmt.Fprintf(w, "Getting code failed.")
if err != nil {
return
}
}
})
go func() {
defer wg.Done()
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
fmt.Println("Shutting down...")
}()
}
func getAniListAuthorizationToken(content string) AniListJWT {
apiUrl := "https://anilist.co/api/v2/oauth/token"
resource := "/api/v2/oauth/token"
data := url.Values{}
data.Set("grant_type", "authorization_code")
data.Set("client_id", Environment.ANILIST_APP_ID)
data.Set("client_secret", Environment.ANILIST_SECRET_TOKEN)
data.Set("redirect_uri", Environment.ANILIST_CALLBACK_URI)
data.Set("code", content)
u, _ := url.ParseRequestURI(apiUrl)
u.Path = resource
urlStr := u.String()
response, err := http.NewRequest("POST", urlStr, strings.NewReader(data.Encode()))
if err != nil {
log.Printf("Failed at response, %s\n", err)
}
response.Header.Add("Content-type", "application/x-www-form-urlencoded")
response.Header.Add("Accept", "application/json")
client := &http.Client{}
res, reserr := client.Do(response)
if reserr != nil {
log.Printf("Failed at res, %s\n", err)
}
defer res.Body.Close()
returnedBody, err := io.ReadAll(res.Body)
var post AniListJWT
err = json.Unmarshal(returnedBody, &post)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return post
}
func (a *App) GetAniListLoggedInUser() AniListUser {
a.AniListLogin()
body := struct {
Query string `json:"query"`
}{
Query: `
query {
Viewer {
id
name
avatar {
large
medium
}
bannerImage
siteUrl
}
}
`,
}
user, _ := AniListQuery(body, true)
var post AniListUser
err := json.Unmarshal(user, &post)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return post
}
func (a *App) LogoutAniList() string {
if (AniListJWT{} != aniListJwt) {
err := aniRing.Remove("anilistTokenType")
err = aniRing.Remove("anilistTokenExpiresIn")
err = aniRing.Remove("anilistAccessToken")
err = aniRing.Remove("anilistRefreshToken")
if err != nil {
fmt.Println("AniList Logout Failed", err)
}
aniListJwt = AniListJWT{}
}
return "AniList Logged Out Successfully"
}