File size: 2,198 Bytes
0bdc2d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json

import aiohttp

headers = {
    'User-Agent':
    'Mozilla/5.0 (Linux; Android 11; WayDroid x86_64 Device Build/RQ3A.211001.001; ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/109.0.5414.118 Safari/537.36 BingSapphire/24.1.410310303',
    'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}


async def get_auth_url(client_id):
    oauth_url = 'https://login.live.com/oauth20_authorize.srf'
    redirect_uri = 'https://login.live.com/oauth20_desktop.srf'
    scope = 'service::bing.com::MBI_SSL'
    response_type = 'code'
    return f'{oauth_url}?client_id={client_id}&redirect_uri={redirect_uri}&response_type={response_type}&scope={scope}'


async def auth(authCode, client_id):
    cookies = None
    has_sydney = True
    chat_uri = 'https://www.bing.com/sydchat'
    signin_uri = 'https://ssl.bing.com/fd/auth/signin'
    token_uri = 'https://login.live.com/oauth20_token.srf'
    redirect_uri = 'https://login.live.com/oauth20_desktop.srf'
    grant_type = 'authorization_code'

    uri = f'{token_uri}?client_id={client_id}&code={authCode}&redirect_uri={redirect_uri}&grant_type={grant_type}'

    async with aiohttp.ClientSession(headers=headers) as session:
        access_token = None
        cookies = None
        async with session.post(uri, data={
            'client_id': client_id,
            'code': authCode,
            'redirect_uri': redirect_uri,
            'grant_type': 'authorization_code'
        }) as response:
            if response.status == 200:
                js = await response.json()
                access_token = js["access_token"]
        if access_token:
            async with session.get(f'{signin_uri}?action=token&provider=windows_live_id&save_token=0&token={access_token}') as response:
                if response.status == 200:
                    js = json.loads(await response.text())
                    if js['success']:
                        cookies = session.cookie_jar
        if cookies:
            async with session.get(chat_uri) as response:
                if response.status != 200:
                    cookies = None
                    has_sydney = False
    return cookies, has_sydney