from server import * import requests L2P_BASE = 'https://www3.elearning.rwth-aachen.de/_vti_bin/l2pservices/api.svc/v1/' OAUTH_BASE = 'https://oauth.campus.rwth-aachen.de/oauth2waitress/oauth2.svc/' def l2pget(endpoint, token, **args): args['accessToken'] = token r = requests.request('GET', L2P_BASE+endpoint, params=args) return r.json() def oauthget(endpoint, **args): args['client_id'] = config['L2P_APIKEY'] r = requests.request('POST', OAUTH_BASE+endpoint, data=args) return r.json() @app.route('/internal/l2pauth') def start_l2pauth(): if 'L2P_APIKEY' not in config: return render_template("500.html"), 500 code = oauthget('code', scope='l2p2013.rwth') session['oauthcode'] = code['device_code'] session['oauthscope'] = 'l2p' return redirect(code['verification_url']+'?q=verify&d='+code['user_code']) @app.route('/internal/rwthauth') def start_rwthauth(): if 'L2P_APIKEY' not in config: return render_template("500.html"), 500 code = oauthget('code', scope='userinfo.rwth') session['oauthcode'] = code['device_code'] session['oauthscope'] = 'rwth' return redirect(code['verification_url']+'?q=verify&d='+code['user_code']) @app.before_request def finish_oauth(): if 'L2P_APIKEY' not in config: return if 'oauthcode' not in session or 'oauthscope' not in session: return token = oauthget('token', code=session['oauthcode'], grant_type='device') if token.get('status') != 'ok': return del session['oauthcode'] if session['oauthscope'] not in ['l2p', 'rwth']: return session['rwthintern'] = True if session['oauthscope'] == 'l2p': session['l2p_courses'] = [] for course in l2pget('viewAllCourseInfo', token['access_token'])['dataSet']: session['l2p_courses'].append(course['uniqueid']) del session['oauthscope'] oauthget('token', refresh_token=token['refresh_token'], grant_type='invalidate')