1 | from ows_server.lib.base import * |
---|
2 | from ows_server.lib.security_util import SecuritySession |
---|
3 | import logging |
---|
4 | log = logging.getLogger(__name__) |
---|
5 | |
---|
6 | from paste.request import parse_querystring |
---|
7 | import sys # include in case tracefile is set to sys.stderr |
---|
8 | import base64 # decode the return to address |
---|
9 | from urlparse import urlsplit, urlunsplit |
---|
10 | |
---|
11 | from ndg.security.common.SessionMgr import SessionMgrClient |
---|
12 | |
---|
13 | |
---|
14 | class LogoutController(BaseController): |
---|
15 | '''Provides the pylons controller for logging out and killing the cookies |
---|
16 | ''' |
---|
17 | |
---|
18 | def __before__(self): |
---|
19 | """Get return to URL""" |
---|
20 | c.returnTo = request.params.get('r', '') |
---|
21 | |
---|
22 | # Check return to address - getCredentials should NOT be returned to |
---|
23 | # with its query args intact |
---|
24 | b64decReturnTo = base64.urlsafe_b64decode(c.returnTo) |
---|
25 | scheme, netloc, pathInfo, query, frag = urlsplit(b64decReturnTo) |
---|
26 | if 'getCredentials' in pathInfo: |
---|
27 | # Swap to discovery and remove sensitive creds query args |
---|
28 | # |
---|
29 | # TODO: re-write to be more robust and modular. Nb. |
---|
30 | # BaseController.__call__ should filter out 'getCredentials' |
---|
31 | # calls from c.requestURL so this code should never need to be |
---|
32 | # executed. |
---|
33 | filteredReturnTo = urlunsplit((scheme,netloc,'/discovery','','')) |
---|
34 | c.returnTo = base64.urlsafe_b64encode(filteredReturnTo) |
---|
35 | |
---|
36 | |
---|
37 | def index(self): |
---|
38 | ''' Ok, you really want to logout here ''' |
---|
39 | |
---|
40 | if 'ndgSec' not in session: |
---|
41 | # There's no handle to a security session |
---|
42 | log.error("logout called but no 'ndgSec' key in session object") |
---|
43 | return self.__redirect() |
---|
44 | |
---|
45 | try: |
---|
46 | smClnt = SessionMgrClient(uri=g.securityCfg.smURI, |
---|
47 | sslCACertFilePathList=g.securityCfg.sslCACertFilePathList, |
---|
48 | sslPeerCertCN=g.securityCfg.sslPeerCertCN, |
---|
49 | signingCertFilePath=g.securityCfg.wssCertFilePath, |
---|
50 | signingPriKeyFilePath=g.securityCfg.wssPriKeyFilePath, |
---|
51 | signingPriKeyPwd=g.securityCfg.wssPriKeyPwd, |
---|
52 | caCertFilePathList=g.securityCfg.wssCACertFilePathList, |
---|
53 | tracefile=g.securityCfg.tracefile) |
---|
54 | except Exception, e: |
---|
55 | log.error("logout - creating Session Manager client: %s" % e) |
---|
56 | return self.__redirect() |
---|
57 | |
---|
58 | # Disconnect from Session Manager |
---|
59 | log.info('Calling Session Manager "%s" disconnect for logout...' % \ |
---|
60 | g.securityCfg.smURI) |
---|
61 | try: |
---|
62 | smClnt.disconnect(sessID=session['ndgSec']['sid']) |
---|
63 | except Exception, e: |
---|
64 | log.error("Error with Session Manager logout: %s" % e) |
---|
65 | self.__redirect() |
---|
66 | |
---|
67 | try: |
---|
68 | # easy to kill our cookie |
---|
69 | SecuritySession.delete() |
---|
70 | if 'ndgCleared' in session: del session['ndgCleared'] |
---|
71 | session.save() |
---|
72 | |
---|
73 | except Exception, e: |
---|
74 | log.error("logout - clearing security session: %s" % e) |
---|
75 | |
---|
76 | return self.__redirect() |
---|
77 | |
---|
78 | |
---|
79 | def __redirect(self): |
---|
80 | """Handle redirect back to previous page""" |
---|
81 | if c.returnTo: |
---|
82 | # Decode the return to address |
---|
83 | try: |
---|
84 | b64decReturnTo = base64.urlsafe_b64decode(c.returnTo) |
---|
85 | except Exception, e: |
---|
86 | log.error("logout - decoding return URL: %s" % e) |
---|
87 | return render_response('content') |
---|
88 | |
---|
89 | # and now go back to whence we had come |
---|
90 | h.redirect_to(b64decReturnTo) |
---|
91 | else: |
---|
92 | return render_response('content') |
---|