def post(self, jwt: JWT): self.shard.client.call('demote_connection', jwt.get_token(), routing_key='gateway_rpc') return { 'message': 'ok I definitetly did something :)' }, StatusCodes.OK_200, make_token_cookie_header(None, -1)
def generate_refresh_response(jwt: JWT) -> tuple: '''makes a refresh request and compiles the json, status code, and set-cookie header''' data, sc = refresh_token_request(jwt.refresh_token) if sc == StatusCodes.OK_200: now = datetime.now() jwt.access_token = data['access_token'] jwt.refresh_token = data['refresh_token'] jwt.expires_in = data['expires_in'] jwt.issued_at = now.isoformat() return { 'access': { 'issuedAt': now.isoformat(), 'expiresIn': jwt.expires_in, 'refresh_in': jwt.expires_in // 2 } }, sc, make_token_cookie_header(jwt.get_token(), jwt.expires_in * 2) return data, sc
async def connect(sid: str, environ: dict): print(f"{environ['REMOTE_ADDR']} has connected with sid: {sid}") request = environ['aiohttp.request'] try: jwt = JWT(token=request.cookies['token']) except (InvalidTokenError, KeyError): print("No valid token found, logging into unprivileged gateway...") else: print("Found valid token, logging into elevated gateway...") sio.enter_room(sid, f"{sid}_auth") async with sio.session(sid) as session: session['token'] = jwt
def post(self, code: str): ex_data, status_code = token_exchange_request(code) if status_code == StatusCodes.OK_200: discord_token = ex_data['access_token'] id_data, status_code = identify_request(discord_token) if status_code == StatusCodes.OK_200: now = datetime.now() expires_in = ex_data['expires_in'] refresh_in = timedelta(seconds=expires_in) / 2 jwt = JWT({ 'access_token': discord_token, 'refresh_token': ex_data['refresh_token'], 'expires_in': expires_in, 'issued_at': now.isoformat(), 'id': int(id_data['id']), 'permissions': 274, }) nonce = randbits(32) data = { # 'token': jwt.get_token() 'user': id_data, 'access': { 'issuedAt': now.isoformat(), 'expiresIn': expires_in, 'refreshIn': int(refresh_in.total_seconds()), }, 'gatewayNonce': nonce, } self.shard.client.call('register_nonce', nonce, jwt.get_token(), routing_key='gateway_rpc') return data, StatusCodes.OK_200, make_token_cookie_header( jwt.get_token(), expires_in * 2) return ex_data, status_code
async def request_elevation(sid: str, nonce: int): print(f"{sid} requesting elevation...") try: jwt = JWT(token=auth_nonces[nonce]) del auth_nonces[nonce] except (InvalidTokenError, KeyError): print(f"{sid} requested room elevation but didn't provide a valid jwt") await sio.emit('elevation_return', {'message': "Missing or invalid jwt"}, room=sid) else: print(f"valid nonce provided, granting access...") sio.enter_room(sid, f"{sid}_auth") await sio.emit('elevation_return', {'message': "success"}, room=sid) async with sio.session(sid) as session: session['token'] = jwt
async def on_free_elevation(self, sid, data): if True: return async with self.session(sid) as session: session['jwt'] = JWT(token=data['token']) self.enter_room(sid, f"{sid}_auth")