diff --git a/app/__init__.py b/app/__init__.py index 98ef18b..995e905 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -47,13 +47,9 @@ async def get_user(user_id): def get_spotify_link(user_id) -> str: params = { - 'client_id': config.spotify.client_id, - 'response_type': 'code', - 'redirect_uri': config.spotify.redirect, - 'scope': 'user-read-recently-played user-read-currently-playing', 'state': user_id } - return f"https://accounts.spotify.com/authorize?{urllib.parse.urlencode(params)}" + return f"https://music.mootfrost.dev/spotify/authorize?{urllib.parse.urlencode(params)}" def get_ymusic_link(user_id) -> str: @@ -69,7 +65,7 @@ def get_ymusic_link(user_id) -> str: async def start(e: events.NewMessage.Event): payload = { 'tg_id': e.chat_id, - 'exp': int(time.time()) + 300 + 'exp': int(time.time()) + 900 } enc_user_id = jwt.encode(payload, config.jwt_secret, algorithm='HS256') buttons = [ diff --git a/app/callback_listener.py b/app/callback_listener.py index 2050128..24888da 100644 --- a/app/callback_listener.py +++ b/app/callback_listener.py @@ -1,3 +1,4 @@ +import urllib.parse from contextlib import asynccontextmanager from fastapi import FastAPI, Depends, Request from sqlalchemy.ext.asyncio import AsyncSession @@ -6,7 +7,7 @@ from fastapi.staticfiles import StaticFiles import uvicorn from telethon import TelegramClient import aiohttp -import time +from pydantic import BaseModel import jwt from app.dependencies import get_session @@ -69,7 +70,44 @@ You can change default service using /default command. """ -@app.get('/spotify_callback') +def get_spotify_link(client_id, state) -> str: + params = { + 'client_id': client_id, + 'response_type': 'code', + 'redirect_uri': config.spotify.redirect, + 'scope': 'user-read-recently-played user-read-currently-playing', + 'state': state + } + return f"https://accounts.spotify.com/authorize?{urllib.parse.urlencode(params)}" + + +class SpotifyAuthorizeRequest(BaseModel): + client_id: str + client_secret: str + state: str + + +@app.post('/spotify/authorize') +async def spotify_authorize(data: SpotifyAuthorizeRequest, session: AsyncSession = Depends(get_session)): + user_id = get_decoded_id(data.state) + creds = { + 'client_id': data.client_id, + 'client_secret': data.client_secret + } + user = await session.get(User, user_id) + if user: + user.spotify_auth = creds + else: + user = User(id=user_id, + spotify_auth=creds, + default='spotify' + ) + session.add(user) + await session.commit() + return {'redirect_url': get_spotify_link(data.client_id, client.state)} + + +@app.get('/spotify/callback') async def spotify_callback(code: str, state: str, session: AsyncSession = Depends(get_session)): user_id = get_decoded_id(state) token, refresh_token, expires_in = await code_to_token(code, 'https://accounts.spotify.com/api/token', config.spotify) @@ -93,7 +131,7 @@ async def spotify_callback(code: str, state: str, session: AsyncSession = Depend -@app.get('/ym_callback') +@app.get('/ym/callback') async def ym_callback(state: str, code: str, cid: str, session: AsyncSession = Depends(get_session)): user_id = get_decoded_id(state) token, refresh_token, expires_in = await code_to_token(code, 'https://oauth.yandex.com/token', config.ymusic, config.proxy) diff --git a/app/static/authorize_spotify.html b/app/static/authorize_spotify.html new file mode 100644 index 0000000..bbf5bdd --- /dev/null +++ b/app/static/authorize_spotify.html @@ -0,0 +1,121 @@ + + +
+ + +