Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
|
||
from pylons import config | ||
from repoze.who.plugins import auth_tkt as repoze_auth_tkt | ||
|
||
_bool = repoze_auth_tkt._bool | ||
|
||
import logging | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
class CkanAuthTktCookiePlugin(repoze_auth_tkt.AuthTktCookiePlugin): | ||
|
||
def __init__(self, httponly, *args, **kwargs): | ||
super(CkanAuthTktCookiePlugin, self).__init__(*args, **kwargs) | ||
self.httponly = httponly | ||
|
||
def _get_cookies(self, *args, **kwargs): | ||
''' | ||
Override method in superclass to ensure HttpOnly is set appropriately. | ||
''' | ||
super_cookies = super(CkanAuthTktCookiePlugin, self). \ | ||
_get_cookies(*args, **kwargs) | ||
|
||
cookies = [] | ||
for k, v in super_cookies: | ||
replace_with = '; HttpOnly' if self.httponly else '' | ||
v = v.replace('; HttpOnly', '') + replace_with | ||
cookies.append((k, v)) | ||
|
||
return cookies | ||
|
||
|
||
def make_plugin(secret=None, | ||
secretfile=None, | ||
cookie_name='auth_tkt', | ||
secure=False, | ||
include_ip=False, | ||
timeout=None, | ||
reissue_time=None, | ||
userid_checker=None): | ||
from repoze.who.utils import resolveDotted | ||
|
||
# ckan specific: get secret from beaker setting if necessary | ||
if secret is None or secret == 'somesecret': | ||
secret = config['beaker.session.secret'] | ||
|
||
# Set httponly based on config value. Default is True | ||
httponly = config.get('who.httponly', True) | ||
|
||
# back to repoze boilerplate | ||
if (secret is None and secretfile is None): | ||
raise ValueError("One of 'secret' or 'secretfile' must not be None.") | ||
if (secret is not None and secretfile is not None): | ||
raise ValueError("Specify only one of 'secret' or 'secretfile'.") | ||
if secretfile: | ||
secretfile = os.path.abspath(os.path.expanduser(secretfile)) | ||
if not os.path.exists(secretfile): | ||
raise ValueError("No such 'secretfile': %s" % secretfile) | ||
secret = open(secretfile).read().strip() | ||
if timeout: | ||
timeout = int(timeout) | ||
if reissue_time: | ||
reissue_time = int(reissue_time) | ||
if userid_checker is not None: | ||
userid_checker = resolveDotted(userid_checker) | ||
plugin = CkanAuthTktCookiePlugin(_bool(httponly), | ||
secret, | ||
cookie_name, | ||
_bool(secure), | ||
_bool(include_ip), | ||
timeout, | ||
reissue_time, | ||
userid_checker) | ||
return plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from ckan.new_tests import helpers | ||
from ckan.lib.auth_tkt import CkanAuthTktCookiePlugin, make_plugin | ||
|
||
|
||
class TestCkanAuthTktCookiePlugin(object): | ||
|
||
''' | ||
Test the added methods used by this subclass of | ||
repoze.who.plugins.auth_tkt.AuthTktCookiePlugin | ||
''' | ||
|
||
def _make_plugin(self, httponly): | ||
'''Only httponly needs to be set.''' | ||
return CkanAuthTktCookiePlugin(httponly=httponly, | ||
secret=None, | ||
cookie_name='auth_tkt', | ||
secure=False, | ||
include_ip=False, | ||
timeout=None, | ||
reissue_time=None, | ||
userid_checker=None) | ||
|
||
@helpers.change_config('who.httponly', True) | ||
def test_httponly_expected_cookies_with_config_httponly_true(self): | ||
''' | ||
The returned cookies are in the format we expect, with HttpOnly flag. | ||
''' | ||
plugin = make_plugin(secret='sosecret') | ||
cookies = plugin._get_cookies(environ={'SERVER_NAME': '0.0.0.0'}, | ||
value='HELLO') | ||
expected_cookies = [ | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; HttpOnly'), | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; Domain=0.0.0.0; HttpOnly'), | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; Domain=.0.0.0.0; HttpOnly') | ||
] | ||
assert cookies == expected_cookies | ||
|
||
@helpers.change_config('who.httponly', False) | ||
def test_httponly_expected_cookies_with_config_httponly_false(self): | ||
''' | ||
The returned cookies are in the format we expect, without HttpOnly | ||
flag. | ||
''' | ||
plugin = make_plugin(secret='sosecret') | ||
cookies = plugin._get_cookies(environ={'SERVER_NAME': '0.0.0.0'}, | ||
value='HELLO') | ||
expected_cookies = [ | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/'), | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; Domain=0.0.0.0'), | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; Domain=.0.0.0.0') | ||
] | ||
assert cookies == expected_cookies | ||
|
||
def test_httponly_expected_cookies_without_config_httponly(self): | ||
''' | ||
The returned cookies are in the format we expect, with HttpOnly flag. | ||
''' | ||
plugin = make_plugin(secret='sosecret') | ||
cookies = plugin._get_cookies(environ={'SERVER_NAME': '0.0.0.0'}, | ||
value='HELLO') | ||
expected_cookies = [ | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; HttpOnly'), | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; Domain=0.0.0.0; HttpOnly'), | ||
('Set-Cookie', 'auth_tkt="HELLO"; Path=/; Domain=.0.0.0.0; HttpOnly') | ||
] | ||
assert cookies == expected_cookies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters