170 lines
4.1 KiB
Go
170 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"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) 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 {
|
|
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)
|
|
|
|
serverDone := &sync.WaitGroup{}
|
|
serverDone.Add(1)
|
|
handleAniListCallback(serverDone)
|
|
serverDone.Wait()
|
|
} else {
|
|
aniListJwt.TokenType = string(tokenType.Data)
|
|
aniListJwt.AccessToken = string(accessToken.Data)
|
|
aniListJwt.RefreshToken = string(refreshToken.Data)
|
|
expiresInString := string(expiresIn.Data)
|
|
aniListJwt.ExpiresIn, _ = strconv.Atoi(expiresInString)
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleAniListCallback(wg *sync.WaitGroup) {
|
|
srv := &http.Server{Addr: ":6734"}
|
|
http.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(string(aniListJwt.ExpiresIn)),
|
|
})
|
|
_ = aniRing.Set(keyring.Item{
|
|
Key: "anilistAccessToken",
|
|
Data: []byte(aniListJwt.AccessToken),
|
|
})
|
|
_ = aniRing.Set(keyring.Item{
|
|
Key: "anilistRefreshToken",
|
|
Data: []byte(aniListJwt.RefreshToken),
|
|
})
|
|
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", os.Getenv("ANILIST_APP_ID"))
|
|
data.Set("client_secret", os.Getenv("ANILIST_SECRET_TOKEN"))
|
|
data.Set("redirect_uri", os.Getenv("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("Content-Type", "application/json")
|
|
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) GetAniListLoggedInUserId() AniListUser {
|
|
a.AniListLogin()
|
|
body := struct {
|
|
Query string `json:"query"`
|
|
}{
|
|
Query: `
|
|
query {
|
|
Viewer {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
`,
|
|
}
|
|
|
|
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
|
|
}
|