1 | """OpenID IdP Validation unit test package |
---|
2 | |
---|
3 | NERC DataGrid Project |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "16/07/09" |
---|
7 | __copyright__ = "(C) 2009 Science and Technology Facilities Council" |
---|
8 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
9 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
10 | __revision__ = '$Id$' |
---|
11 | import logging |
---|
12 | logging.basicConfig(level=logging.DEBUG) |
---|
13 | |
---|
14 | import os |
---|
15 | import unittest |
---|
16 | from ndg.security.test.unit import BaseTestCase, mkDataDirPath |
---|
17 | from ndg.security.server.wsgi.openid.relyingparty.validation import ( |
---|
18 | IdPValidator, IdPValidationDriver, IdPInvalidException, |
---|
19 | SSLIdPValidationDriver, SSLClientAuthNValidator) |
---|
20 | |
---|
21 | |
---|
22 | class ProviderWhitelistValidator(IdPValidator): |
---|
23 | """Test stub for Whitelist validator""" |
---|
24 | def __init__(self): |
---|
25 | pass |
---|
26 | |
---|
27 | def initialize(self, **parameters): |
---|
28 | '''@raise ConfigException:''' |
---|
29 | assert('config-file' in parameters) |
---|
30 | |
---|
31 | def validate(self, idpEndpoint, idpIdentity): |
---|
32 | '''@raise IdPInvalidException: |
---|
33 | @raise ConfigException:''' |
---|
34 | pass |
---|
35 | |
---|
36 | |
---|
37 | class ProviderIdentifierTestValidator(IdPValidator): |
---|
38 | """Test stub for identifier validator - fixed to reject all IdPs""" |
---|
39 | def __init__(self): |
---|
40 | pass |
---|
41 | |
---|
42 | def initialize(self, **parameters): |
---|
43 | '''@raise ConfigException:''' |
---|
44 | assert('config-file' in parameters) |
---|
45 | |
---|
46 | def validate(self, idpEndpoint, idpIdentity): |
---|
47 | '''Test method hard wired to raise an invalid IdP exception |
---|
48 | @raise IdPInvalidException: |
---|
49 | @raise ConfigException:''' |
---|
50 | raise IdPInvalidException("%s is invalid" % idpEndpoint) |
---|
51 | |
---|
52 | |
---|
53 | class DiscoveryInfoPlaceHolder(object): |
---|
54 | getOPEndpoint = lambda self: 'https://localhost/openid/provider' |
---|
55 | |
---|
56 | |
---|
57 | class IdentifierPlaceHolder(object): |
---|
58 | getIdentifier = lambda self: 'myid' |
---|
59 | |
---|
60 | from M2Crypto import X509 |
---|
61 | |
---|
62 | class X509StoreCtxPlaceHolder(object): |
---|
63 | x509CertFilePath = mkDataDirPath(os.path.join('pki', 'localhost.crt')) |
---|
64 | |
---|
65 | def get1_chain(self): |
---|
66 | return [X509.load_cert(X509StoreCtxPlaceHolder.x509CertFilePath)] |
---|
67 | |
---|
68 | class IdPValidationTestCase(BaseTestCase): |
---|
69 | thisDir = os.path.dirname(os.path.abspath(__file__)) |
---|
70 | IDP_CONFIG_FILEPATH = os.path.join(thisDir, 'idpvalidator.xml') |
---|
71 | os.environ['NDGSEC_UNITTEST_IDPVALIDATION_DIR'] = thisDir |
---|
72 | |
---|
73 | def test01IdPConfigFileEnvVarNotSet(self): |
---|
74 | identifier = IdentifierPlaceHolder() |
---|
75 | discoveries = [DiscoveryInfoPlaceHolder()] |
---|
76 | |
---|
77 | idPValidationDriver = IdPValidationDriver() |
---|
78 | validDiscoveries = idPValidationDriver.performIdPValidation(identifier, |
---|
79 | discoveries) |
---|
80 | # Expect no discoveries returned because the IDP_CONFIG_FILE |
---|
81 | # environment variable is not set |
---|
82 | self.assert_(len(validDiscoveries) == 1) |
---|
83 | |
---|
84 | def test02WithIdPConfigFile(self): |
---|
85 | identifier = 'https://pjk.badc.rl.ac.uk' |
---|
86 | |
---|
87 | os.environ[IdPValidationDriver.IDP_CONFIG_FILEPATH_ENV_VARNAME |
---|
88 | ] = IdPValidationTestCase.IDP_CONFIG_FILEPATH |
---|
89 | |
---|
90 | idPValidationDriver = IdPValidationDriver() |
---|
91 | validDiscoveries = idPValidationDriver.performIdPValidation(identifier) |
---|
92 | self.assert_(len(validDiscoveries) == 2) |
---|
93 | |
---|
94 | def test03SSLValidation(self): |
---|
95 | idpConfigFilePath = os.path.join(IdPValidationTestCase.thisDir, |
---|
96 | 'ssl-idp-validator.xml') |
---|
97 | idPValidationDriver = SSLIdPValidationDriver( |
---|
98 | idpConfigFilePath=idpConfigFilePath) |
---|
99 | |
---|
100 | # preVerifyOK set to 1 to indicate all is otherwise OK with |
---|
101 | # verification |
---|
102 | idPValidationDriver(1, X509StoreCtxPlaceHolder()) |
---|
103 | |
---|
104 | |
---|
105 | if __name__ == "__main__": |
---|
106 | unittest.main() |
---|