async def create_new_access_token(request): jwt_token_data = await get_token_data_from_request(request, token_type="refresh") user = jwt_token_data["identity"] # Staff if "role_id" in user: user = await User.get(id=user["id"]) else: # Visitor user = await Visitor.get(id=user["id"]) # Return a new pair of access and refresh token # As the user's information may have changed access_token = await create_access_token(identity=user, app=request.app) refresh_token = await create_refresh_token(identity=user, app=request.app) # Sign the tokens to avoid modifications signed_access_token = sign_str(access_token) signed_refresh_token = sign_str(refresh_token) response = json({"access_token": signed_access_token}) response.cookies["access_token"] = signed_access_token response.cookies["refresh_token"] = signed_refresh_token response.cookies["access_token"]["httponly"] = True response.cookies["refresh_token"]["httponly"] = True return response
def anonymous2_client(loop, app, sanic_client): access_token = loop.run_until_complete( get_access_token_for_user(_visitors[-3], app=app)) refresh_token = loop.run_until_complete( get_refresh_token_for_user(_visitors[-3], app=app)) cookies = { "access_token": sign_str(access_token), "refresh_token": sign_str(refresh_token), } return loop.run_until_complete(sanic_client(app, cookies=cookies))
def disabled_agent_client(loop, app, sanic_client): _user = { **_users[2], "disabled": True, "role_id": 2, "organisation_id": _orgs[0]["id"], } access_token = loop.run_until_complete( get_access_token_for_user(_user, app=app)) refresh_token = loop.run_until_complete( get_refresh_token_for_user(_user, app=app)) cookies = { "access_token": sign_str(access_token), "refresh_token": sign_str(refresh_token), } return loop.run_until_complete(sanic_client(app, cookies=cookies))
async def token_agent_1(app): return sign_str(await get_access_token_for_user( { **_users[-6], "role_id": 3, "organisation_id": _orgs[0]["id"] }, app=app))
async def login(request, identity): # Identity can be any data that is json serializable access_token = await create_access_token(identity=identity, app=request.app) refresh_token = await create_refresh_token(identity=identity, app=request.app) # Sign the tokens to avoid modifications signed_access_token = sign_str(access_token) signed_refresh_token = sign_str(refresh_token) # Attach the tokens in a cookie response = json({"user": identity, "access_token": signed_access_token}) response.cookies["access_token"] = signed_access_token response.cookies["refresh_token"] = signed_refresh_token response.cookies["access_token"]["httponly"] = True response.cookies["refresh_token"]["httponly"] = True return response
def agent1_client(loop, app, sanic_client): access_token = loop.run_until_complete( get_access_token_for_user( { **_users[-6], "role_id": 3, "organisation_id": _orgs[0]["id"] }, app=app)) refresh_token = loop.run_until_complete( get_refresh_token_for_user( { **_users[-6], "role_id": 3, "organisation_id": _orgs[0]["id"] }, app=app)) cookies = { "access_token": sign_str(access_token), "refresh_token": sign_str(refresh_token), } return loop.run_until_complete(sanic_client(app, cookies=cookies))
async def token_anonymous_2(app): return sign_str(await get_access_token_for_user(_visitors[-3], app=app))
async def token_visitor_1(app): return sign_str(await get_access_token_for_user(_visitors[-1], app=app))