async def enter(): client_session = aiohttp.ClientSession( headers={"User-Agent": get_environment()}, request_class=ioimpl.AuthenticatedClientRequest, loop=loop) # will be aentered/aexited by http_client http_client = await stack.enter_async_context( ioimpl.HTTPClient(http_session=client_session, storage_dir=args.cache_dir)) # type: HTTPClient session = StudIPSession(studip_base=args.studip_url, http=http_client) check_cancelled(future) log.info("Logging in via %s...", args.login_method) if args.login_method == "shib": await http_client.shib_auth(start_url=args.shib_url, username=args.user, password=args.get_password()) elif args.login_method == "oauth": await http_client.oauth1_auth(**args.get_oauth_args()) else: # if args.login_method == "basic": await http_client.basic_auth(username=args.user, password=args.get_password()) await session.check_login(username=args.user) await session.prefetch_globals() log.info("Logged in as %s on %s", args.user, await session.get_instance_name()) root_vp = vpathimpl(parent=None, path_segments=[], known_data={}, next_path_segments=args.format.split("/"), session=session, pipeline_type=ioimpl.Pipeline) root_rp = rpathimpl(parent=None, generating_vps={root_vp}) return root_rp
def main(argv=None): dirs = appdirs.AppDirs(appname="Stud.IP-Fuse", appauthor=False) # disable author/company folder on windows os.makedirs(dirs.user_data_dir, exist_ok=True) # must exist for log files os.makedirs(dirs.user_config_dir, exist_ok=True) # must exist for oauth token storage configure_logging(dirs) args, fuse_args = parse_args(dirs, argv) os.makedirs(args.cache_dir, exist_ok=True) try: if not args.debug_logging: logging.root.setLevel(logging.INFO) if not args.debug_aio: logging.getLogger("asyncio").setLevel(logging.WARNING) log_status("STARTING", args=args, level=logging.DEBUG) log.info("Starting %s" % get_environment()) if args.debug_fuse: from studip_fuse.studipfs.fuse_ops import log_ops log_ops.setLevel(logging.DEBUG) fuse_ops = FUSEView(log_args=args, loop_setup_fn=aioimpl_asyncio.setup_loop(args=args)) if args.login_method == "oauth": login_oauth_args(args) else: if args.pwfile == "-": from getpass import getpass password = getpass() else: try: with open(args.pwfile, "rt") as f: password = f.read().rstrip('\n') except FileNotFoundError as e: log.warning("%s. Either specify a file from which your Stud.IP password can be read " "or use `--pwfile -` to enter it using a prompt in the shell." % e) return args.get_password = lambda: password # wrap in lambda to prevent printing log.info("Going to mount at %s (uid=%s, gid=%s, pid=%s, python pid=%s)", os.path.abspath(args.mount), *fuse_get_context(), os.getpid()) try: # this calls fork if args.foreground == False (and breaks running asyncio loops due to https://bugs.python.org/issue21998) # XXX on windows args.mount may not exist, on Linux it must exist FUSE(fuse_ops, args.mount, debug=fuse_args.pop("debug_fuse"), **fuse_args) except RuntimeError as e: if more_itertools.first(e.args, None) in FUSE_ERROR_CODES: msg = FUSE_ERROR_CODES[e.args[0]] if e.args[0] == 1: msg += ". Please check whether the mountpoint you specified is an empty directory or another instance of studip-fuse is using it" msg += ". Please check stderr for details." raise RuntimeError(msg) from e else: raise except SystemExit: pass except: log.error("main() function quit exceptionally", exc_info=True) finally: log_status("TERMINATED", args=args, level=logging.DEBUG) log.debug("Program terminated")
async def obtain_access_token_sessionless( oauth1_client: OAuth1Client = None, studip_url="https://studip.uni-passau.de/studip/api.php/", port=OAUTH_CALLBACK_PORT, open_browser=True): from studip_fuse.launcher.aioimpl.asyncio import AuthenticatedClientRequest, HTTPClient client_session = aiohttp.ClientSession( headers={"User-Agent": get_environment()}, request_class=AuthenticatedClientRequest, auth=oauth1_client) # will be aentered/aexited by http_client async with HTTPClient(http_session=client_session, storage_dir=None) as http_client: studip_session = StudIPSession(studip_base=studip_url, http=http_client) return await obtain_access_token(studip_session, oauth1_client, port=port, open_browser=open_browser)
async def oauth_callback(request): oauth1_client = request.app["oauth1_client"] # type: OAuth1Client studip_session = request.app["studip_session"] # type: StudIPSession if not all(k in request.query for k in ["oauth_verifier", "oauth_token"]) \ or oauth1_client.resource_owner_key != request.query["oauth_token"]: raise request_error(request, None, "OAuth Access was denied.") update_client( oauth1_client, verifier=request.query["oauth_verifier"], resource_owner_key=request.query["oauth_token"], ) data = await make_subrequest(request, 'POST', studip_session.oauth1_urls.access_token) update_client( oauth1_client, resource_owner_key=data.get('oauth_token'), resource_owner_secret=data.get('oauth_token_secret'), ) try: user = await studip_session.check_login() except (HTTPClientError, ClientError) as e: raise request_error(request, e, "Login failed", HTTPBadRequest) from e request.app["finished_client_future"].set_result(oauth1_client) return Response(body="""%s <td style="vertical-align: top; padding-right: 10px"><img src="%s"></td><td> <p>Successfully logged in as %s, welcome %s!<br>You may close this window, the FUSE driver will finish starting up in the background.</p> <p style="font-size:70%%">%s. <a href="https://github.com/N-Coder/StudIP-FUSE">Source</a></p> <p style="font-size:70%%">%s. <a href="%s">Details</a></p> %s""" % (HEADER, user["avatar_normal"], user["name"]["username"], user["name"]["formatted"], get_environment(), await studip_session.get_instance_name(), studip_session.studip_url("/studip/dispatch.php/siteinfo/"), FOOTER), content_type="text/html")
async def index(request): return Response( text="This server was started by %s to obtain an OAuth1 token." % get_version(details=False), headers={"Server": get_environment()})