def userinfo(): import pprint uid = session.get('uid') user = None migrate_data = None if uid: user = get_user(uid) if user: if not user['migrated']: migrate_data = google.drive_get_migration_diagnostics(user) del user['_id'] try: del user['google']['credentials'] except KeyError: pass user = pprint.pformat(user, indent=4) migrate_data = pprint.pformat(migrate_data, indent=4) return render_template('userinfo.html', uid=uid, user=user, migrate_data=migrate_data)
def google_auth_verify(): """Finalize google authorization""" try: if 'error' in request.args: raise Exception( _format_err('Error getting authorization', request.args.get('error'))) code = _request_get_required('code') flow = OAuth2WebServerFlow(settings.GOOGLE_CLIENT_ID, settings.GOOGLE_CLIENT_SECRET, _GOOGLE_OAUTH_SCOPES, redirect_uri=_build_oauth_redirect( request, url_for('google_auth_verify'))) credentials = flow.step2_exchange(code) # ^ this is an oauth2client.client.OAuth2Credentials object # Get user info userinfo = google.get_userinfo( google.get_userinfo_service(credentials)) if not userinfo: raise Exception('Could not get Google user info') info = { 'id': userinfo.get('id'), 'name': userinfo.get('name'), 'credentials': credentials.to_json() } if not info['id']: raise Exception('Could not get Google user ID') if 'stg-storymap.knightlab.com' in domains and not info[ 'id'] in allowed_ids: print('User id not in ALLOWED_IDS: %s ' % info['id']) raise Exception( 'You are not authorized to access this page. Please send the following information to [email protected]: storymap.knilab.com unauthorized %s' % info['id']) # Upsert user record uid = _get_uid('google:' + info['id']) user = get_user(uid) if user: user['google'] = info else: user = {'uid': uid, 'migrated': 0, 'storymaps': {}, 'google': info} user['uname'] = info['name'] save_user(user) # Update session session['uid'] = uid url = url_for('select') app.logger.info("google_auth_verify url: {}".format(url)) return redirect(url) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)})
def get_session_user(): """Enforce authenticated user""" uid = session.get('uid') user = get_user(uid) if not user: try: session.pop('uid') except KeyError: pass return None return user
def select(): check_test_user() try: uid = session.get('uid') if not uid: return render_template('select.html') user = get_user(uid) if not user: _session_pop('uid') return render_template('select.html') del user['_id'] return render_template('select.html', user=user) except Exception as e: traceback.print_exc() return render_template('select.html', error=str(e))
def check_test_user(): if settings.TEST_MODE: if not get_user('test'): create_user('test', 'Test User') session['uid'] = 'test'