def _create_html(zip_path, data): """Convert zip to html data Args: zip_path (py.path): what to embed data (dict): simulation db Returns: py.path, str: file and mime type """ from pykern import pkjinja from pykern import pkcollections from sirepo import uri_router from sirepo import simulation_db import py.path import copy # Use same tmp directory fp = py.path.local(zip_path.dirname).join(zip_path.purebasename) + '.html' values = pkcollections.Dict(data=data) values.uri = uri_router.uri_for_api('importArchive', external=False) values.server = uri_router.uri_for_api('importArchive')[:-len(values.uri)] sc = simulation_db.SCHEMA_COMMON values.appLongName = sc.appInfo[data.simulationType].longName values.appShortName = sc.appInfo[data.simulationType].shortName values.productLongName = sc.productInfo.longName values.productShortName = sc.productInfo.shortName values.zip = zip_path.read().encode('base64') with open(str(fp), 'wb') as f: fp.write(pkjinja.render_resource('archive.html', values)) return fp, 'text/html'
def authorize(simulation_type, app, oauth_type): """Redirects to an OAUTH request for the specified oauth_type ('github'). If oauth_type is 'anonymous', the current session is cleared. """ oauth_next = '/{}#{}'.format(simulation_type, flask.request.args.get('next') or '') if oauth_type == _ANONYMOUS: _update_session(_ANONYMOUS) server.clear_session_user() return server.javascript_redirect(oauth_next) state = werkzeug.security.gen_salt(64) flask.session['oauth_nonce'] = state flask.session['oauth_next'] = oauth_next callback = cfg.github_callback_uri if not callback: from sirepo import uri_router callback = uri_router.uri_for_api( 'oauthAuthorized', dict(oauth_type=oauth_type), ) return _oauth_client(app, oauth_type).authorize( callback=callback, state=state, )
def authorize(simulation_type, oauth_type): """Redirects to an OAUTH request for the specified oauth_type ('github'). If oauth_type is 'anonymous', the current session is cleared. """ oauth_next = '/{}#{}'.format(simulation_type, flask.request.args.get('next', '')) if oauth_type == _ANONYMOUS_OAUTH_TYPE: _update_session(_ANONYMOUS) cookie.clear_user() return server.javascript_redirect(oauth_next) state = util.random_base62() cookie.set_value(_COOKIE_NONCE, state) cookie.set_value(_COOKIE_NEXT, oauth_next) callback = cfg.github_callback_uri if not callback: from sirepo import uri_router callback = uri_router.uri_for_api( 'oauthAuthorized', dict(oauth_type=oauth_type), ) return _oauth_client(oauth_type).authorize( callback=callback, state=state, )
def api_authGithubLogin(simulation_type): """Redirects to Github""" t = sirepo.template.assert_sim_type(simulation_type) s = util.random_base62() cookie.set_value(_COOKIE_NONCE, s) cookie.set_value(_COOKIE_SIM_TYPE, t) if not cfg.callback_uri: # must be executed in an app and request context so can't # initialize earlier. cfg.callback_uri = uri_router.uri_for_api('authGithubAuthorized') return _oauth_client().authorize(callback=cfg.callback_uri, state=s)
def api_authGithubLogin(simulation_type): """Redirects to Github""" req = http_request.parse_params(type=simulation_type) s = util.random_base62() cookie.set_value(_COOKIE_NONCE, s) cookie.set_value(_COOKIE_SIM_TYPE, req.type) if not cfg.callback_uri: # must be executed in an app and request context so can't # initialize earlier. cfg.callback_uri = uri_router.uri_for_api('authGithubAuthorized') return _client(s).authorize_redirect(redirect_uri=cfg.callback_uri, state=s)
def _create_html(zip_path, data): """Convert zip to html data Args: zip_path (py.path): what to embed data (dict): simulation db Returns: py.path, str: file and mime type """ # Use same tmp directory fp = zip_path.new(ext='.html') values = pkcollections.Dict(data=data) values.uri = uri_router.uri_for_api('importArchive', external=False) values.server = uri_router.uri_for_api('importArchive')[:-len(values.uri)] sc = simulation_db.SCHEMA_COMMON values.appLongName = sc.appInfo[data.simulationType].longName values.appShortName = sc.appInfo[data.simulationType].shortName values.productLongName = sc.productInfo.longName values.productShortName = sc.productInfo.shortName values.zip = pkcompat.from_bytes(base64.b64encode(zip_path.read_binary())) with open(str(fp), 'wb') as f: fp.write(pkjinja.render_resource('archive.html', values)) return fp, 'text/html'
def api_robotsTxt(): """Disallow the app (dev, prod) or / (alpha, beta)""" global _ROBOTS_TXT if not _ROBOTS_TXT: # We include dev so we can test if pkconfig.channel_in('prod', 'dev'): u = [ uri_router.uri_for_api('root', params={'simulation_type': x}) for x in sorted(feature_config.cfg.sim_types) ] else: u = ['/'] _ROBOTS_TXT = ''.join(['User-agent: *\n'] + ['Disallow: /{}\n'.format(x) for x in u], ) return flask.Response(_ROBOTS_TXT, mimetype='text/plain')
def api_authEmailLogin(): """Start the login process for the user. User has sent an email, which needs to be verified. """ req = http_request.parse_post() email = _parse_email(req.req_data) with auth_db.thread_lock: u = AuthEmailUser.search_by(unverified_email=email) if not u: u = AuthEmailUser(unverified_email=email) u.create_token() u.save() return _send_login_email( u, uri_router.uri_for_api( 'authEmailAuthorized', dict(simulation_type=req.type, token=u.token), ), )
def t(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkexcept, pkre from sirepo import uri_router import re fc = srunit.flask_client() uri = uri_router.uri_for_api('homePage') pkre('http://[^/]+/about$', uri) uri = uri_router.uri_for_api('homePage', external=False) pkre('^/about$', uri) with pkexcept(KeyError): uri_router.uri_for_api('notAnApi') with pkexcept('missing parameter'): uri_router.uri_for_api('exportArchive', {'simulation_type': 'srw'})
def t(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkexcept, pkre, pkeq from sirepo import uri_router import re fc = srunit.flask_client() uri = uri_router.uri_for_api('homePage', params={'path_info': None}) pkre('http://[^/]+/en$', uri) uri = uri_router.uri_for_api( 'homePage', params={'path_info': 'terms.html'}, external=False, ) pkeq('/en/terms.html', uri) with pkexcept(KeyError): uri_router.uri_for_api('notAnApi') with pkexcept('missing parameter'): uri_router.uri_for_api('exportArchive', {'simulation_type': 'srw'})
def api(*args, **kwargs): """Alias for `uri_router.uri_for_api`""" return uri_router.uri_for_api(*args, **kwargs)