|
| 1 | +import os |
| 2 | + |
| 3 | +from bottle import Bottle, run, redirect, request, response, ServerAdapter, jinja2_view |
| 4 | +from beaker.middleware import SessionMiddleware |
| 5 | + |
| 6 | +from urlparse import urlparse |
| 7 | + |
| 8 | +from onelogin.saml2.auth import OneLogin_Saml2_Auth |
| 9 | +from onelogin.saml2.utils import OneLogin_Saml2_Utils |
| 10 | + |
| 11 | + |
| 12 | +app = Bottle(__name__) |
| 13 | +app.config['SECRET_KEY'] = 'onelogindemopytoolkit' |
| 14 | +app.config['SAML_PATH'] = os.path.join(os.path.dirname(__file__), 'saml') |
| 15 | + |
| 16 | + |
| 17 | +session_opts = { |
| 18 | + 'session.type': 'file', |
| 19 | + 'session.cookie_expires': 300, |
| 20 | + 'session.data_dir': './.data', |
| 21 | + 'session.auto': True |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +def init_saml_auth(req): |
| 26 | + auth = OneLogin_Saml2_Auth(req, custom_base_path=app.config['SAML_PATH']) |
| 27 | + return auth |
| 28 | + |
| 29 | + |
| 30 | +def prepare_bottle_request(req): |
| 31 | + url_data = urlparse(req.url) |
| 32 | + return { |
| 33 | + 'http_host': req.get_header('host'), |
| 34 | + 'server_port': url_data.port, |
| 35 | + 'script_name': req.fullpath, |
| 36 | + 'get_data': req.query, |
| 37 | + 'post_data': req.forms, |
| 38 | + 'https': 'on' if req.urlparts.scheme == 'https' else 'off' |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +@app.route('/acs', method='POST') |
| 43 | +@jinja2_view('index.html', template_lookup=['templates']) |
| 44 | +def index(): |
| 45 | + req = prepare_bottle_request(request) |
| 46 | + auth = init_saml_auth(req) |
| 47 | + paint_logout = False |
| 48 | + attributes = False |
| 49 | + |
| 50 | + session = request.environ['beaker.session'] |
| 51 | + |
| 52 | + auth.process_response() |
| 53 | + errors = auth.get_errors() |
| 54 | + not_auth_warn = not auth.is_authenticated() |
| 55 | + if len(errors) == 0: |
| 56 | + session['samlUserdata'] = auth.get_attributes() |
| 57 | + session['samlNameId'] = auth.get_nameid() |
| 58 | + session['samlSessionIndex'] = auth.get_session_index() |
| 59 | + self_url = OneLogin_Saml2_Utils.get_self_url(req) |
| 60 | + if 'RelayState' in request.forms and self_url != request.forms['RelayState']: |
| 61 | + return redirect(request.forms['RelayState']) |
| 62 | + |
| 63 | + if 'samlUserdata' in session: |
| 64 | + paint_logout = True |
| 65 | + if len(session['samlUserdata']) > 0: |
| 66 | + attributes = session['samlUserdata'].items() |
| 67 | + |
| 68 | + return { |
| 69 | + 'errors':errors, |
| 70 | + 'not_auth_warn':not_auth_warn, |
| 71 | + 'attributes':attributes, |
| 72 | + 'paint_logout':paint_logout |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +@app.route('/', method='GET') |
| 77 | +@jinja2_view('index.html', template_lookup=['templates']) |
| 78 | +def index(): |
| 79 | + req = prepare_bottle_request(request) |
| 80 | + auth = init_saml_auth(req) |
| 81 | + errors = [] |
| 82 | + not_auth_warn = False |
| 83 | + success_slo = False |
| 84 | + attributes = False |
| 85 | + paint_logout = False |
| 86 | + |
| 87 | + session = request.environ['beaker.session'] |
| 88 | + |
| 89 | + if 'sso' in request.query: |
| 90 | + return_to = '{0}://{1}/'.format(request.urlparts.scheme, request.get_header('host')) |
| 91 | + return redirect(auth.login(return_to)) |
| 92 | + elif 'sso2' in request.query: |
| 93 | + return_to = '{0}://{1}/attrs/'.format(request.urlparts.scheme, request.get_header('host')) |
| 94 | + return redirect(auth.login(return_to)) |
| 95 | + elif 'slo' in request.query: |
| 96 | + name_id = None |
| 97 | + session_index = None |
| 98 | + if 'samlNameId' in session: |
| 99 | + name_id = session['samlNameId'] |
| 100 | + if 'samlSessionIndex' in session: |
| 101 | + session_index = session['samlSessionIndex'] |
| 102 | + |
| 103 | + return redirect(auth.logout(name_id=name_id, session_index=session_index)) |
| 104 | + elif 'sls' in request.query: |
| 105 | + dscb = lambda: session.clear() |
| 106 | + url = auth.process_slo(delete_session_cb=dscb) |
| 107 | + errors = auth.get_errors() |
| 108 | + if len(errors) == 0: |
| 109 | + if url is not None: |
| 110 | + return redirect(url) |
| 111 | + else: |
| 112 | + success_slo = True |
| 113 | + |
| 114 | + if 'samlUserdata' in session: |
| 115 | + paint_logout = True |
| 116 | + if len(session['samlUserdata']) > 0: |
| 117 | + attributes = session['samlUserdata'].items() |
| 118 | + |
| 119 | + return { |
| 120 | + 'errors':errors, |
| 121 | + 'not_auth_warn':not_auth_warn, |
| 122 | + 'success_slo':success_slo, |
| 123 | + 'attributes':attributes, |
| 124 | + 'paint_logout':paint_logout |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | +@app.route('/attrs/') |
| 129 | +@jinja2_view('attrs.html', template_lookup=['templates']) |
| 130 | +def attrs(): |
| 131 | + paint_logout = False |
| 132 | + attributes = False |
| 133 | + session = request.environ['beaker.session'] |
| 134 | + |
| 135 | + if 'samlUserdata' in session: |
| 136 | + paint_logout = True |
| 137 | + if len(session['samlUserdata']) > 0: |
| 138 | + attributes = session['samlUserdata'].items() |
| 139 | + |
| 140 | + return {'paint_logout':paint_logout, |
| 141 | + 'attributes':attributes} |
| 142 | + |
| 143 | + |
| 144 | +@app.route('/metadata/') |
| 145 | +def metadata(): |
| 146 | + req = prepare_bottle_request(request) |
| 147 | + auth = init_saml_auth(req) |
| 148 | + settings = auth.get_settings() |
| 149 | + metadata = settings.get_sp_metadata() |
| 150 | + errors = settings.validate_metadata(metadata) |
| 151 | + |
| 152 | + if len(errors) == 0: |
| 153 | + response.status = 200 |
| 154 | + response.set_header('Content-Type', 'text/xml') |
| 155 | + return metadata |
| 156 | + else: |
| 157 | + response.status = 500 |
| 158 | + return ','.join(errors) |
| 159 | + |
| 160 | + |
| 161 | +class SSLPasteServer(ServerAdapter): |
| 162 | + def run(self, handler): |
| 163 | + from paste import httpserver |
| 164 | + |
| 165 | + server = httpserver.serve(handler, '0.0.0.0', '8000', ssl_pem='local.pem', start_loop=False) |
| 166 | + try: |
| 167 | + server.serve_forever() |
| 168 | + finally: |
| 169 | + server.server_close() |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + # To run HTTPS |
| 174 | + #run(SessionMiddleware(app, config=session_opts), host='0.0.0.0', port=8000, debug=True, reloader=True, server=SSLPasteServer) |
| 175 | + |
| 176 | + # To run HTTP |
| 177 | + run(SessionMiddleware(app, config=session_opts), host='0.0.0.0', port=8000, debug=True, reloader=True, server='paste') |
0 commit comments