Compare commits
4 Commits
784e6c0b7d
...
d1258c54d3
Author | SHA1 | Date | |
---|---|---|---|
d1258c54d3 | |||
237958cce5 | |||
c6972ad765 | |||
0ed827fc9a |
@ -15,8 +15,8 @@ func AniListQuery(body interface{}, login bool) (json.RawMessage, string) {
|
||||
if err != nil {
|
||||
log.Printf("Failed at response, %s\n", err)
|
||||
}
|
||||
if login && (AniListJWT{}) != jwt {
|
||||
response.Header.Add("Authorization", "Bearer "+jwt.AccessToken)
|
||||
if login && (AniListJWT{}) != aniListJwt {
|
||||
response.Header.Add("Authorization", "Bearer "+aniListJwt.AccessToken)
|
||||
} else if login {
|
||||
return nil, "Please login to anilist to make this request"
|
||||
}
|
||||
|
@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@ -14,22 +12,25 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"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",
|
||||
})
|
||||
|
||||
var ctxShutdown, cancel = context.WithCancel(context.Background())
|
||||
var aniCtxShutdown, aniCancel = context.WithCancel(context.Background())
|
||||
|
||||
func (a *App) AniListLogin() {
|
||||
if (AniListJWT{}) == jwt {
|
||||
tokenType, err := ring.Get("anilistTokenType")
|
||||
expiresIn, err := ring.Get("anilistTokenExpiresIn")
|
||||
accessToken, err := ring.Get("anilistAccessToken")
|
||||
refreshToken, err := ring.Get("anilistRefreshToken")
|
||||
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)
|
||||
@ -39,11 +40,11 @@ func (a *App) AniListLogin() {
|
||||
handleAniListCallback(serverDone)
|
||||
serverDone.Wait()
|
||||
} else {
|
||||
jwt.TokenType = string(tokenType.Data)
|
||||
jwt.AccessToken = string(accessToken.Data)
|
||||
jwt.RefreshToken = string(refreshToken.Data)
|
||||
aniListJwt.TokenType = string(tokenType.Data)
|
||||
aniListJwt.AccessToken = string(accessToken.Data)
|
||||
aniListJwt.RefreshToken = string(refreshToken.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"}
|
||||
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case <-ctxShutdown.Done():
|
||||
case <-aniCtxShutdown.Done():
|
||||
fmt.Println("Shutting down...")
|
||||
return
|
||||
default:
|
||||
@ -60,25 +61,25 @@ func handleAniListCallback(wg *sync.WaitGroup) {
|
||||
content := r.FormValue("code")
|
||||
|
||||
if content != "" {
|
||||
jwt = getAniListAuthorizationToken(content)
|
||||
_ = ring.Set(keyring.Item{
|
||||
aniListJwt = getAniListAuthorizationToken(content)
|
||||
_ = aniRing.Set(keyring.Item{
|
||||
Key: "anilistTokenType",
|
||||
Data: []byte(jwt.TokenType),
|
||||
Data: []byte(aniListJwt.TokenType),
|
||||
})
|
||||
_ = ring.Set(keyring.Item{
|
||||
_ = aniRing.Set(keyring.Item{
|
||||
Key: "anilistTokenExpiresIn",
|
||||
Data: []byte(string(jwt.ExpiresIn)),
|
||||
Data: []byte(string(aniListJwt.ExpiresIn)),
|
||||
})
|
||||
_ = ring.Set(keyring.Item{
|
||||
_ = aniRing.Set(keyring.Item{
|
||||
Key: "anilistAccessToken",
|
||||
Data: []byte(jwt.AccessToken),
|
||||
Data: []byte(aniListJwt.AccessToken),
|
||||
})
|
||||
_ = ring.Set(keyring.Item{
|
||||
_ = aniRing.Set(keyring.Item{
|
||||
Key: "anilistRefreshToken",
|
||||
Data: []byte(jwt.RefreshToken),
|
||||
Data: []byte(aniListJwt.RefreshToken),
|
||||
})
|
||||
fmt.Println("Shutting down...")
|
||||
cancel()
|
||||
aniCancel()
|
||||
err := srv.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
log.Println("server.Shutdown:", err)
|
||||
@ -89,7 +90,6 @@ func handleAniListCallback(wg *sync.WaitGroup) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -166,5 +166,4 @@ func (a *App) GetAniListLoggedInUserId() AniListUser {
|
||||
}
|
||||
|
||||
return post
|
||||
|
||||
}
|
||||
|
36
SimklFunctions.go
Normal file
36
SimklFunctions.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SimklQuery(body interface{}, login bool) (json.RawMessage, string) {
|
||||
reader, _ := json.Marshal(body)
|
||||
response, err := http.NewRequest("POST", "https://graphql.anilist.co", bytes.NewBuffer(reader))
|
||||
if err != nil {
|
||||
log.Printf("Failed at response, %s\n", err)
|
||||
}
|
||||
if login && (SimklJWT{}) != simklJwt {
|
||||
response.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
|
||||
} else if login {
|
||||
return nil, "Please login to anilist to make this request"
|
||||
}
|
||||
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)
|
||||
|
||||
return returnedBody, ""
|
||||
}
|
16
SimklTypes.go
Normal file
16
SimklTypes.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
type SimklJWT struct {
|
||||
TokenType string `json:"token_type"`
|
||||
AccessToken string `json:"access_token"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
type SimklUser struct {
|
||||
Data struct {
|
||||
Viewer struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"Viewer"`
|
||||
} `json:"data"`
|
||||
}
|
163
SimklUserFunctions.go
Normal file
163
SimklUserFunctions.go
Normal file
@ -0,0 +1,163 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"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())
|
||||
|
||||
func (a *App) SimklLogin() {
|
||||
if (SimklJWT{}) == simklJwt {
|
||||
tokenType, err := simklRing.Get("SimklTokenType")
|
||||
accessToken, err := simklRing.Get("SimklAccessToken")
|
||||
scope, err := simklRing.Get("SimklScope")
|
||||
if err != nil {
|
||||
getSimklCodeUrl := "https://simkl.com/oauth/authorize?response_type=code&client_id=" + os.Getenv("SIMKL_CLIENT_ID") + "&redirect_uri=" + os.Getenv("SIMKL_CALLBACK_URI")
|
||||
runtime.BrowserOpenURL(a.ctx, getSimklCodeUrl)
|
||||
|
||||
serverDone := &sync.WaitGroup{}
|
||||
serverDone.Add(1)
|
||||
handleSimklCallback(serverDone)
|
||||
serverDone.Wait()
|
||||
} else {
|
||||
simklJwt.TokenType = string(tokenType.Data)
|
||||
simklJwt.AccessToken = string(accessToken.Data)
|
||||
simklJwt.Scope = string(scope.Data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleSimklCallback(wg *sync.WaitGroup) {
|
||||
srv := &http.Server{Addr: ":6734"}
|
||||
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||
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),
|
||||
})
|
||||
fmt.Println("Shutting down...")
|
||||
simklCancel()
|
||||
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 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",
|
||||
ClientID: os.Getenv("SIMKL_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("SIMKL_CLIENT_SECRET"),
|
||||
RedirectURI: os.Getenv("SIMKL_CALLBACK_URI"),
|
||||
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
|
||||
}
|
||||
|
||||
func (a *App) GetSimklLoggedInUserId() SimklUser {
|
||||
a.SimklLogin()
|
||||
body := struct {
|
||||
Query string `json:"query"`
|
||||
}{
|
||||
Query: `
|
||||
query {
|
||||
Viewer {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
||||
user, _ := SimklQuery(body, true)
|
||||
|
||||
var post SimklUser
|
||||
err := json.Unmarshal(user, &post)
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
return post
|
||||
}
|
29
bruno/AniTrack/Simkl/Get Code.bru
Normal file
29
bruno/AniTrack/Simkl/Get Code.bru
Normal file
@ -0,0 +1,29 @@
|
||||
meta {
|
||||
name: Get Code
|
||||
type: http
|
||||
seq: 1
|
||||
}
|
||||
|
||||
get {
|
||||
url: https://simkl.com/oauth/authorize?response_type=code&client_id={{SIMKL_CLIENT_ID}}&redirect_uri=http://localhost:6734/callback
|
||||
body: none
|
||||
auth: oauth2
|
||||
}
|
||||
|
||||
params:query {
|
||||
response_type: code
|
||||
client_id: {{SIMKL_CLIENT_ID}}
|
||||
redirect_uri: http://localhost:6734/callback
|
||||
}
|
||||
|
||||
auth:oauth2 {
|
||||
grant_type: authorization_code
|
||||
callback_url: http://localhost:6734/callback
|
||||
authorization_url: https://api.simkl.com/oauth/authorize
|
||||
access_token_url: https://api.simkl.com/oauth/token
|
||||
client_id: {{SIMKL_CLIENT_ID}}
|
||||
client_secret: {{SIMKL_CLIENT_SECRET}}
|
||||
scope:
|
||||
state:
|
||||
pkce: false
|
||||
}
|
37
bruno/AniTrack/Simkl/SimklGetAuthorizationToken.bru
Normal file
37
bruno/AniTrack/Simkl/SimklGetAuthorizationToken.bru
Normal file
@ -0,0 +1,37 @@
|
||||
meta {
|
||||
name: SimklGetAuthorizationToken
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
post {
|
||||
url: https://api.simkl.com/oauth/token
|
||||
body: json
|
||||
auth: none
|
||||
}
|
||||
|
||||
headers {
|
||||
Content-Type: application/json
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"grant_type": "authorization_code",
|
||||
"client_id": "{{SIMKL_CLIENT_ID}}",
|
||||
"client_secret": "{{SIMKL_CLIENT_SECRET}}",
|
||||
"redirect_uri": "http://localhost:6734/callback",
|
||||
"code": "c2b956d5086c5515ff518bfb2857d7f55453f5f8a8e245f6a37c2e3838fe1a7a"
|
||||
}
|
||||
}
|
||||
|
||||
body:form-urlencoded {
|
||||
grant_type: authorization_code
|
||||
client_id: {{SIMKL_CLIENT_ID}}
|
||||
client_secret: {{SIMKL_CLIENT_SECRET}}
|
||||
redirect_uri: http://localhost:6734/callback
|
||||
code: c2b956d5086c5515ff518bfb2857d7f55453f5f8a8e245f6a37c2e3838fe1a7a
|
||||
}
|
||||
|
||||
body:multipart-form {
|
||||
:
|
||||
}
|
@ -2,6 +2,7 @@ vars {
|
||||
ANILIST_APP_ID: {{process.env.ANILIST_APP_ID}}
|
||||
ANILIST_SECRET_TOKEN: {{process.env.ANILIST_SECRET_TOKEN}}
|
||||
SIMKL_CLIENT_ID: {{process.env.SIMKL_CLIENT_ID}}
|
||||
SIMKL_CLIENT_SECRET: {{process.env.SIMKL_CLIENT_SECRET}}
|
||||
}
|
||||
vars:secret [
|
||||
code
|
||||
|
@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import {anilistModal, GetAniListSingleItemAndOpenModal, title} from "./GetAniListSingleItemAndOpenModal.svelte";
|
||||
import {GetAniListUserWatchingList} from "../wailsjs/go/main/App";
|
||||
import {GetAniListUserWatchingList, SimklLogin} from "../wailsjs/go/main/App";
|
||||
import {MediaListSort} from "./anilist/types/AniListTypes";
|
||||
import type {AniListCurrentUserWatchList} from "./anilist/types/AniListCurrentUserWatchListType"
|
||||
import Header from "./Header.svelte";
|
||||
@ -64,6 +64,7 @@
|
||||
|
||||
|
||||
<button class="btn" on:click={anilistGetUserWatchlist}>Login to AniList</button>
|
||||
<button class="btn" on:click={SimklLogin}>Login to Simkl</button>
|
||||
{#if aniListLoggedIn}
|
||||
<div>You are logged in {aniListWatchlist.data.Page.mediaList[0].user.name}!</div>
|
||||
{/if}
|
||||
|
4
frontend/wailsjs/go/main/App.d.ts
vendored
4
frontend/wailsjs/go/main/App.d.ts
vendored
@ -14,4 +14,8 @@ export function GetAniListLoggedInUserId():Promise<main.AniListUser>;
|
||||
|
||||
export function GetAniListUserWatchingList(arg1:number,arg2:number,arg3:string):Promise<main.AniListCurrentUserWatchList>;
|
||||
|
||||
export function GetSimklLoggedInUserId():Promise<main.SimklUser>;
|
||||
|
||||
export function Greet(arg1:string):Promise<string>;
|
||||
|
||||
export function SimklLogin():Promise<void>;
|
||||
|
@ -26,6 +26,14 @@ export function GetAniListUserWatchingList(arg1, arg2, arg3) {
|
||||
return window['go']['main']['App']['GetAniListUserWatchingList'](arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
export function GetSimklLoggedInUserId() {
|
||||
return window['go']['main']['App']['GetSimklLoggedInUserId']();
|
||||
}
|
||||
|
||||
export function Greet(arg1) {
|
||||
return window['go']['main']['App']['Greet'](arg1);
|
||||
}
|
||||
|
||||
export function SimklLogin() {
|
||||
return window['go']['main']['App']['SimklLogin']();
|
||||
}
|
||||
|
@ -147,6 +147,37 @@ export namespace main {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
export class SimklUser {
|
||||
// Go type: struct { Viewer struct { ID int "json:\"id\""; Name string "json:\"name\"" } "json:\"Viewer\"" }
|
||||
data: any;
|
||||
|
||||
static createFrom(source: any = {}) {
|
||||
return new SimklUser(source);
|
||||
}
|
||||
|
||||
constructor(source: any = {}) {
|
||||
if ('string' === typeof source) source = JSON.parse(source);
|
||||
this.data = this.convertValues(source["data"], Object);
|
||||
}
|
||||
|
||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
||||
if (!a) {
|
||||
return a;
|
||||
}
|
||||
if (a.slice && a.map) {
|
||||
return (a as any[]).map(elem => this.convertValues(elem, classs));
|
||||
} else if ("object" === typeof a) {
|
||||
if (asMap) {
|
||||
for (const key of Object.keys(a)) {
|
||||
a[key] = new classs(a[key]);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
return new classs(a);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user