2024-07-30 12:57:08 -04:00
|
|
|
package main
|
2024-07-30 12:59:06 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/99designs/keyring"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var simklJwt SimklJWT
|
|
|
|
|
|
|
|
var simklRing, _ = keyring.Open(keyring.Config{
|
|
|
|
ServiceName: "AniTrack",
|
|
|
|
})
|
|
|
|
|
|
|
|
var simklCtxShutdown, simklCancel = context.WithCancel(context.Background())
|
|
|
|
|
2024-07-30 13:07:30 -04:00
|
|
|
func (a *App) CheckIfSimklLoggedIn() bool {
|
|
|
|
if (SimklJWT{} == simklJwt) {
|
2024-07-30 20:38:55 -04:00
|
|
|
tokenType, err := simklRing.Get("SimklTokenType")
|
|
|
|
accessToken, err := simklRing.Get("SimklAccessToken")
|
|
|
|
scope, err := simklRing.Get("SimklScope")
|
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 {
|
|
|
|
simklJwt.TokenType = string(tokenType.Data)
|
|
|
|
simklJwt.AccessToken = string(accessToken.Data)
|
|
|
|
simklJwt.Scope = string(scope.Data)
|
|
|
|
return true
|
|
|
|
}
|
2024-07-30 13:07:30 -04:00
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 12:59:06 -04:00
|
|
|
func (a *App) SimklLogin() {
|
2024-07-30 13:07:30 -04:00
|
|
|
if a.CheckIfSimklLoggedIn() == false {
|
2024-07-30 12:59:06 -04:00
|
|
|
tokenType, err := simklRing.Get("SimklTokenType")
|
|
|
|
accessToken, err := simklRing.Get("SimklAccessToken")
|
|
|
|
scope, err := simklRing.Get("SimklScope")
|
2024-08-13 18:54:27 -04:00
|
|
|
if err != nil || len(accessToken.Data) == 0 {
|
2024-08-16 23:32:16 -04:00
|
|
|
getSimklCodeUrl := "https://simkl.com/oauth/authorize?response_type=code&client_id=" + Environment.SIMKL_CLIENT_ID + "&redirect_uri=" + Environment.SIMKL_CALLBACK_URI
|
2024-07-30 12:59:06 -04:00
|
|
|
runtime.BrowserOpenURL(a.ctx, getSimklCodeUrl)
|
|
|
|
|
|
|
|
serverDone := &sync.WaitGroup{}
|
|
|
|
serverDone.Add(1)
|
2024-08-13 20:05:25 -04:00
|
|
|
a.handleSimklCallback(serverDone)
|
2024-07-30 12:59:06 -04:00
|
|
|
serverDone.Wait()
|
|
|
|
} else {
|
|
|
|
simklJwt.TokenType = string(tokenType.Data)
|
|
|
|
simklJwt.AccessToken = string(accessToken.Data)
|
|
|
|
simklJwt.Scope = string(scope.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 20:05:25 -04:00
|
|
|
func (a *App) handleSimklCallback(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-30 12:59:06 -04:00
|
|
|
select {
|
|
|
|
case <-simklCtxShutdown.Done():
|
|
|
|
fmt.Println("Shutting down...")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
content := r.FormValue("code")
|
|
|
|
|
|
|
|
if content != "" {
|
|
|
|
simklJwt = getSimklAuthorizationToken(content)
|
|
|
|
_ = simklRing.Set(keyring.Item{
|
|
|
|
Key: "SimklTokenType",
|
|
|
|
Data: []byte(simklJwt.TokenType),
|
|
|
|
})
|
|
|
|
_ = simklRing.Set(keyring.Item{
|
|
|
|
Key: "SimklAccessToken",
|
|
|
|
Data: []byte(simklJwt.AccessToken),
|
|
|
|
})
|
|
|
|
_ = simklRing.Set(keyring.Item{
|
|
|
|
Key: "SimklScope",
|
|
|
|
Data: []byte(simklJwt.Scope),
|
|
|
|
})
|
2024-08-13 20:05:25 -04:00
|
|
|
_, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
|
|
|
Title: "Simkl Authorization",
|
|
|
|
Message: "It is now safe to close your browser tab",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-07-30 12:59:06 -04:00
|
|
|
fmt.Println("Shutting down...")
|
|
|
|
simklCancel()
|
2024-08-13 20:05:25 -04:00
|
|
|
err = srv.Shutdown(context.Background())
|
2024-07-30 12:59:06 -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 getSimklAuthorizationToken(content string) SimklJWT {
|
|
|
|
data := struct {
|
|
|
|
GrantType string `json:"grant_type"`
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
ClientSecret string `json:"client_secret"`
|
|
|
|
RedirectURI string `json:"redirect_uri"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
}{
|
|
|
|
GrantType: "authorization_code",
|
2024-08-16 23:32:16 -04:00
|
|
|
ClientID: Environment.SIMKL_CLIENT_ID,
|
|
|
|
ClientSecret: Environment.SIMKL_CLIENT_SECRET,
|
|
|
|
RedirectURI: Environment.SIMKL_CALLBACK_URI,
|
2024-07-30 12:59:06 -04:00
|
|
|
Code: content,
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.NewRequest("POST", "https://api.simkl.com/oauth/token", bytes.NewBuffer(jsonData))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at response, %s\n", err)
|
|
|
|
}
|
|
|
|
response.Header.Add("Content-Type", "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 SimklJWT
|
|
|
|
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) GetSimklLoggedInUser() SimklUser {
|
2024-07-30 12:59:06 -04:00
|
|
|
a.SimklLogin()
|
2024-07-30 20:38:55 -04:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "https://api.simkl.com/users/settings", nil)
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
|
2024-08-16 23:32:16 -04:00
|
|
|
req.Header.Add("simkl-api-key", Environment.SIMKL_CLIENT_ID)
|
2024-07-30 20:38:55 -04:00
|
|
|
|
|
|
|
response, err := client.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at request, %s\n", err)
|
|
|
|
return SimklUser{}
|
2024-07-30 12:59:06 -04:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:38:55 -04:00
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
var user SimklUser
|
2024-07-30 12:59:06 -04:00
|
|
|
|
2024-07-30 20:38:55 -04:00
|
|
|
respBody, _ := io.ReadAll(response.Body)
|
|
|
|
|
|
|
|
err = json.Unmarshal(respBody, &user)
|
2024-07-30 12:59:06 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at unmarshal, %s\n", err)
|
|
|
|
}
|
|
|
|
|
2024-07-30 20:38:55 -04:00
|
|
|
return user
|
2024-07-30 12:59:06 -04:00
|
|
|
}
|
2024-08-15 16:16:40 -04:00
|
|
|
|
|
|
|
func (a *App) LogoutSimkl() string {
|
|
|
|
if (SimklJWT{} != simklJwt) {
|
|
|
|
err := simklRing.Remove("SimklTokenType")
|
|
|
|
err = simklRing.Remove("SimklAccessToken")
|
|
|
|
err = simklRing.Remove("SimklScope")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Simkl Logout Failed", err)
|
|
|
|
}
|
2024-08-16 23:32:16 -04:00
|
|
|
simklJwt = SimklJWT{}
|
2024-08-15 16:16:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return "Simkl Logged Out Successfully"
|
|
|
|
}
|