2024-07-24 09:15:52 -04:00
|
|
|
package main
|
2024-07-24 09:18:45 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2024-07-30 12:59:21 -04:00
|
|
|
|
|
|
|
"github.com/99designs/keyring"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
2024-07-24 09:18:45 -04:00
|
|
|
)
|
|
|
|
|
2024-07-30 12:59:21 -04:00
|
|
|
var aniListJwt AniListJWT
|
2024-07-24 09:18:45 -04:00
|
|
|
|
2024-07-30 12:59:21 -04:00
|
|
|
var aniRing, _ = keyring.Open(keyring.Config{
|
2024-07-24 09:18:45 -04:00
|
|
|
ServiceName: "AniTrack",
|
|
|
|
})
|
|
|
|
|
2024-07-30 12:59:21 -04:00
|
|
|
var aniCtxShutdown, aniCancel = context.WithCancel(context.Background())
|
2024-07-24 09:18:45 -04:00
|
|
|
|
2024-07-30 13:07:30 -04:00
|
|
|
func (a *App) CheckIfAniListLoggedIn() bool {
|
|
|
|
if (AniListJWT{} == aniListJwt) {
|
2024-07-30 20:38:55 -04:00
|
|
|
tokenType, err := aniRing.Get("anilistTokenType")
|
|
|
|
expiresIn, err := aniRing.Get("anilistTokenExpiresIn")
|
|
|
|
accessToken, err := aniRing.Get("anilistAccessToken")
|
|
|
|
refreshToken, err := aniRing.Get("anilistRefreshToken")
|
2024-08-13 18:54:27 -04:00
|
|
|
if err != nil || len(accessToken.Data) == 0 {
|
2024-07-30 20:38:55 -04:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
aniListJwt.TokenType = string(tokenType.Data)
|
|
|
|
aniListJwt.AccessToken = string(accessToken.Data)
|
|
|
|
aniListJwt.RefreshToken = string(refreshToken.Data)
|
2024-08-13 18:54:27 -04:00
|
|
|
aniListJwt.ExpiresIn, _ = strconv.Atoi(string(expiresIn.Data))
|
2024-07-30 20:38:55 -04:00
|
|
|
return true
|
|
|
|
}
|
2024-07-30 13:07:30 -04:00
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
func (a *App) AniListLogin() {
|
2024-07-30 20:38:55 -04:00
|
|
|
if (AniListJWT{} == aniListJwt) {
|
2024-07-30 12:59:21 -04:00
|
|
|
tokenType, err := aniRing.Get("anilistTokenType")
|
|
|
|
expiresIn, err := aniRing.Get("anilistTokenExpiresIn")
|
|
|
|
accessToken, err := aniRing.Get("anilistAccessToken")
|
|
|
|
refreshToken, err := aniRing.Get("anilistRefreshToken")
|
2024-08-13 18:54:27 -04:00
|
|
|
if err != nil || len(accessToken.Data) == 0 {
|
2024-08-16 23:32:16 -04:00
|
|
|
getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + Environment.ANILIST_APP_ID + "&redirect_uri=" + Environment.ANILIST_CALLBACK_URI + "&response_type=code"
|
2024-09-07 22:35:51 -04:00
|
|
|
runtime.BrowserOpenURL(*wailsContext, getAniListCodeUrl)
|
2024-07-24 09:18:45 -04:00
|
|
|
|
|
|
|
serverDone := &sync.WaitGroup{}
|
|
|
|
serverDone.Add(1)
|
2024-08-13 20:05:25 -04:00
|
|
|
a.handleAniListCallback(serverDone)
|
2024-07-24 09:18:45 -04:00
|
|
|
serverDone.Wait()
|
|
|
|
} else {
|
2024-07-30 12:59:21 -04:00
|
|
|
aniListJwt.TokenType = string(tokenType.Data)
|
|
|
|
aniListJwt.AccessToken = string(accessToken.Data)
|
|
|
|
aniListJwt.RefreshToken = string(refreshToken.Data)
|
2024-08-13 18:54:27 -04:00
|
|
|
aniListJwt.ExpiresIn, _ = strconv.Atoi(string(expiresIn.Data))
|
2024-07-24 09:18:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 20:05:25 -04:00
|
|
|
func (a *App) handleAniListCallback(wg *sync.WaitGroup) {
|
2024-08-13 19:13:26 -04:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
srv := &http.Server{Addr: ":6734", Handler: mux}
|
|
|
|
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
|
2024-07-24 09:18:45 -04:00
|
|
|
select {
|
2024-07-30 12:59:21 -04:00
|
|
|
case <-aniCtxShutdown.Done():
|
2024-07-24 09:18:45 -04:00
|
|
|
fmt.Println("Shutting down...")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
content := r.FormValue("code")
|
|
|
|
if content != "" {
|
2024-07-30 12:59:21 -04:00
|
|
|
aniListJwt = getAniListAuthorizationToken(content)
|
|
|
|
_ = aniRing.Set(keyring.Item{
|
2024-07-24 09:18:45 -04:00
|
|
|
Key: "anilistTokenType",
|
2024-07-30 12:59:21 -04:00
|
|
|
Data: []byte(aniListJwt.TokenType),
|
2024-07-24 09:18:45 -04:00
|
|
|
})
|
2024-07-30 12:59:21 -04:00
|
|
|
_ = aniRing.Set(keyring.Item{
|
2024-07-24 09:18:45 -04:00
|
|
|
Key: "anilistTokenExpiresIn",
|
2024-08-13 18:54:27 -04:00
|
|
|
Data: []byte(strconv.Itoa(aniListJwt.ExpiresIn)),
|
2024-07-24 09:18:45 -04:00
|
|
|
})
|
2024-07-30 12:59:21 -04:00
|
|
|
_ = aniRing.Set(keyring.Item{
|
2024-07-24 09:18:45 -04:00
|
|
|
Key: "anilistAccessToken",
|
2024-07-30 12:59:21 -04:00
|
|
|
Data: []byte(aniListJwt.AccessToken),
|
2024-07-24 09:18:45 -04:00
|
|
|
})
|
2024-07-30 12:59:21 -04:00
|
|
|
_ = aniRing.Set(keyring.Item{
|
2024-07-24 09:18:45 -04:00
|
|
|
Key: "anilistRefreshToken",
|
2024-07-30 12:59:21 -04:00
|
|
|
Data: []byte(aniListJwt.RefreshToken),
|
2024-07-24 09:18:45 -04:00
|
|
|
})
|
2024-09-07 22:35:51 -04:00
|
|
|
_, err := runtime.MessageDialog(*wailsContext, runtime.MessageDialogOptions{
|
2024-08-13 20:05:25 -04:00
|
|
|
Title: "AniList Authorization",
|
|
|
|
Message: "It is now safe to close your browser tab",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-07-24 09:18:45 -04:00
|
|
|
fmt.Println("Shutting down...")
|
2024-07-30 12:59:21 -04:00
|
|
|
aniCancel()
|
2024-08-13 20:05:25 -04:00
|
|
|
err = srv.Shutdown(context.Background())
|
2024-07-24 09:18:45 -04:00
|
|
|
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")
|
2024-08-16 23:32:16 -04:00
|
|
|
data.Set("client_id", Environment.ANILIST_APP_ID)
|
|
|
|
data.Set("client_secret", Environment.ANILIST_SECRET_TOKEN)
|
|
|
|
data.Set("redirect_uri", Environment.ANILIST_CALLBACK_URI)
|
2024-07-24 09:18:45 -04:00
|
|
|
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)
|
|
|
|
}
|
2024-10-01 15:07:02 -04:00
|
|
|
response.Header.Add("Content-type", "application/x-www-form-urlencoded")
|
2024-07-24 09:18:45 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-30 20:38:55 -04:00
|
|
|
func (a *App) GetAniListLoggedInUser() AniListUser {
|
2024-07-24 09:18:45 -04:00
|
|
|
a.AniListLogin()
|
|
|
|
body := struct {
|
|
|
|
Query string `json:"query"`
|
|
|
|
}{
|
|
|
|
Query: `
|
|
|
|
query {
|
|
|
|
Viewer {
|
|
|
|
id
|
|
|
|
name
|
2024-07-30 20:38:55 -04:00
|
|
|
avatar {
|
|
|
|
large
|
|
|
|
medium
|
|
|
|
}
|
|
|
|
bannerImage
|
|
|
|
siteUrl
|
2024-07-24 09:18:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-08-15 16:16:40 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-08-16 23:32:16 -04:00
|
|
|
aniListJwt = AniListJWT{}
|
2024-08-15 16:16:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return "AniList Logged Out Successfully"
|
|
|
|
}
|