本文整理汇总了Python中saml2.ident.IdentDB类的典型用法代码示例。如果您正苦于以下问题:Python IdentDB类的具体用法?Python IdentDB怎么用?Python IdentDB使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IdentDB类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestIdentifier
class TestIdentifier():
def setup_class(self):
self.id = IdentDB("subject.db", "example.com", "example")
def test_persistent_1(self):
policy = Policy({
"default": {
"name_form": "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
"nameid_format": NAMEID_FORMAT_PERSISTENT,
"attribute_restrictions": {
"surName": [".*berg"],
}
}
})
nameid = self.id.construct_nameid("foobar", policy,
"urn:mace:example.com:sp:1")
assert _eq(nameid.keyswv(), ['format', 'text', 'sp_name_qualifier',
'name_qualifier'])
assert nameid.sp_name_qualifier == "urn:mace:example.com:sp:1"
assert nameid.format == NAMEID_FORMAT_PERSISTENT
id = self.id.find_local_id(nameid)
assert id == "foobar"
def test_transient_1(self):
policy = Policy({
"default": {
"name_form": "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
"nameid_format": NAMEID_FORMAT_TRANSIENT,
"attribute_restrictions": {
"surName": [".*berg"],
}
}
})
nameid = self.id.construct_nameid("foobar", policy,
"urn:mace:example.com:sp:1")
assert _eq(nameid.keyswv(), ['text', 'format', 'sp_name_qualifier',
'name_qualifier'])
assert nameid.format == NAMEID_FORMAT_TRANSIENT
def test_vo_1(self):
policy = Policy({
"default": {
"name_form": "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
"nameid_format": NAMEID_FORMAT_PERSISTENT,
"attribute_restrictions": {
"surName": [".*berg"],
}
}
})
name_id_policy = samlp.name_id_policy_from_string(NAME_ID_POLICY_1)
print(name_id_policy)
nameid = self.id.construct_nameid("foobar", policy,
'http://vo.example.org/biomed',
name_id_policy)
print(nameid)
assert _eq(nameid.keyswv(), ['text', 'sp_name_qualifier', 'format',
'name_qualifier'])
assert nameid.sp_name_qualifier == 'http://vo.example.org/biomed'
assert nameid.format == NAMEID_FORMAT_PERSISTENT
assert nameid.text != "foobar"
def test_vo_2(self):
policy = Policy({
"default": {
"name_form": "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
"nameid_format": NAMEID_FORMAT_PERSISTENT,
"attribute_restrictions": {
"surName": [".*berg"],
}
}
})
name_id_policy = samlp.name_id_policy_from_string(NAME_ID_POLICY_2)
nameid = self.id.construct_nameid("foobar", policy,
'http://vo.example.org/design',
name_id_policy)
assert _eq(nameid.keyswv(), ['text', 'sp_name_qualifier', 'format',
'name_qualifier'])
assert nameid.sp_name_qualifier == 'http://vo.example.org/design'
assert nameid.format == NAMEID_FORMAT_PERSISTENT
assert nameid.text != "foobar01"
def test_persistent_nameid(self):
sp_id = "urn:mace:umu.se:sp"
nameid = self.id.persistent_nameid("abcd0001", sp_id)
remote_id = nameid.text.strip()
print(remote_id)
local = self.id.find_local_id(nameid)
assert local == "abcd0001"
#.........这里部分代码省略.........
示例2: setup_class
def setup_class(self):
self.id = IdentDB("subject.db", "example.com", "example")
示例3: TargetIdTestCase
class TargetIdTestCase(unittest.TestCase):
def setup_class(self):
self.id = IdentDB({})
self.sp_entity_id = "https://localhost:/sp.xml"
sp_id = "urn:mace:umu.se:sp"
self.nameid1_text = "abcd0001"
self.nameid1 = self.id.persistent_nameid(self.nameid1_text, sp_id)
def get_aes_128_key(self):
return os.urandom(16)
def get_aes_192_key(self):
return os.urandom(24)
def get_aes_256_key(self):
return os.urandom(32)
def test_create_tid2_json(self):
tih = TargetIdHandler()
tid1 = self.nameid1.text.strip()
tid2_json = tih.tid2_json(tid1, self.sp_entity_id)
tid2_dict = tih.tid2_dict(tid2_json)
assert tid2_dict["tid1"] == self.nameid1_text, "Cannot verify tid1."
assert tid2_dict["sp_entityid"] == self.sp_entity_id, "Cannot verify SP entity id."
assert tid2_dict["uuid"] is not None and len(tid2_dict["uuid"]) > 0, "Cannot verify SP entity id."
def test_hash_tid1(self):
hash_verify = {'md5': 32, 'sha1': 40, 'sha224': 56, 'sha256': 64, 'sha384': 96, 'sha512': 128}
try:
TargetIdHandler(h_alg="do_not_exist_alg")
assert False, "Must be an assert since the alg. do not exist."
except:
pass
tih = TargetIdHandler()
tid1 = self.nameid1.text.strip()
tid2_hash = tih.tid2_hash(tid1, self.sp_entity_id)
assert len(tid2_hash) == 64, "Default is sha256 alg and i should generate a string with length 64."
for tmp_h_alg in hash_verify:
tih = TargetIdHandler(h_alg=tmp_h_alg)
tid1 = self.nameid1.text.strip()
tid2_hash = tih.tid2_hash(tid1, self.sp_entity_id)
assert len(tid2_hash) == hash_verify[tmp_h_alg], "%s alg should generate a string with length %d." % \
(tmp_h_alg, hash_verify[tmp_h_alg])
def test_encrypt_tid1(self):
tih = TargetIdHandler()
tid1 = self.nameid1.text.strip()
tid2_encrypted = tih.tid2_encrypt(tid1, self.sp_entity_id)
tid2_dict = tih.tid2_decrypt(tid2_encrypted)
assert tid2_dict["tid1"] == self.nameid1_text, "Cannot verify tid1."
assert tid2_dict["sp_entityid"] == self.sp_entity_id, "Cannot verify SP entity id."
assert tid2_dict["uuid"] is not None and len(tid2_dict["uuid"]) > 0, "Cannot verify SP entity id."
def test_encrypt_alg_1(self):
for alg in TargetIdHandler.AES_ALG:
typ, bits, cmode = alg.split("_")
tih = TargetIdHandler(e_alg=alg)
tid1 = self.nameid1.text.strip()
tid2_encrypted = tih.tid2_encrypt(tid1, self.sp_entity_id)
tid2_dict = tih.tid2_decrypt(tid2_encrypted)
assert tid2_dict["tid1"] == self.nameid1_text, "Cannot verify tid1."
assert tid2_dict["sp_entityid"] == self.sp_entity_id, "Cannot verify SP entity id."
assert tid2_dict["uuid"] is not None and len(tid2_dict["uuid"]) > 0, "Cannot verify SP entity id."
def test_encrypt_alg_2(self):
for alg in TargetIdHandler.AES_ALG:
typ, bits, cmode = alg.split("_")
iv = os.urandom(16)
key = os.urandom(int(bits) >> 3)
tih = TargetIdHandler(e_alg=alg, iv=iv, key=key)
tid1 = self.nameid1.text.strip()
tid2_encrypted = tih.tid2_encrypt(tid1, self.sp_entity_id)
tid2_dict = tih.tid2_decrypt(tid2_encrypted)
assert tid2_dict["tid1"] == self.nameid1_text, "Cannot verify tid1."
assert tid2_dict["sp_entityid"] == self.sp_entity_id, "Cannot verify SP entity id."
assert tid2_dict["uuid"] is not None and len(tid2_dict["uuid"]) > 0, "Cannot verify uuid."
def test_encrypt_alg_3(self):
for alg in TargetIdHandler.AES_ALG:
typ, bits, cmode = alg.split("_")
iv = os.urandom(16)
key = os.urandom(int(bits) >> 3)
tih = TargetIdHandler(e_alg=alg, iv=iv, key=key)
tid1 = self.nameid1.text.strip()
tid2_encrypted_1 = tih.tid2_encrypt(tid1, self.sp_entity_id)
tid2_encrypted_2 = tih.tid2_encrypt(tid1, self.sp_entity_id)
tid2_dict_1 = tih.tid2_decrypt(tid2_encrypted_1)
tid2_dict_2 = tih.tid2_decrypt(tid2_encrypted_2)
assert tid2_encrypted_1 != tid2_encrypted_2, "Two encryption of the same data must never be the same!"
assert tid2_dict_1["tid1"] == tid2_dict_2["tid1"] == self.nameid1_text, "Cannot verify tid1."
assert tid2_dict_1["sp_entityid"] == tid2_dict_2["sp_entityid"] == self.sp_entity_id, \
"Cannot verify SP entity id."
assert tid2_dict_1["uuid"] != tid2_dict_2["uuid"], "Cannot verify uuid."
def test_encrypt_alg_4(self):
for alg in TargetIdHandler.AES_ALG:
typ, bits, cmode = alg.split("_")
#.........这里部分代码省略.........
示例4: setup_class
def setup_class(self):
self.id = IdentDB({})
self.sp_entity_id = "https://localhost:/sp.xml"
sp_id = "urn:mace:umu.se:sp"
self.nameid1_text = "abcd0001"
self.nameid1 = self.id.persistent_nameid(self.nameid1_text, sp_id)
示例5: Server
class Server(Entity):
""" A class that does things that IdPs or AAs do """
def __init__(self, config_file="", config=None, cache=None, stype="idp",
symkey="", msg_cb=None):
Entity.__init__(self, stype, config, config_file, msg_cb=msg_cb)
self.eptid = None
self.init_config(stype)
self.cache = cache
self.ticket = {}
#
self.session_db = self.choose_session_storage()
# Needed for
self.symkey = symkey
self.seed = rndstr()
self.iv = os.urandom(16)
self.lock = threading.Lock()
def getvalid_certificate_str(self):
if self.sec.cert_handler is not None:
return self.sec.cert_handler._last_validated_cert
return None
def support_AssertionIDRequest(self):
return True
def support_AuthnQuery(self):
return True
def choose_session_storage(self):
_spec = self.config.getattr("session_storage", "idp")
if not _spec:
return SessionStorage()
elif isinstance(_spec, six.string_types):
if _spec.lower() == "memory":
return SessionStorage()
else: # Should be tuple
typ, data = _spec
if typ.lower() == "mongodb":
from saml2.mongo_store import SessionStorageMDB
return SessionStorageMDB(database=data, collection="session")
raise NotImplementedError("No such storage type implemented")
def init_config(self, stype="idp"):
""" Remaining init of the server configuration
:param stype: The type of Server ("idp"/"aa")
"""
if stype == "aa":
return
# subject information is stored in a database
# default database is in memory which is OK in some setups
dbspec = self.config.getattr("subject_data", "idp")
idb = None
typ = ""
if not dbspec:
idb = {}
elif isinstance(dbspec, six.string_types):
idb = _shelve_compat(dbspec, writeback=True, protocol=2)
else: # database spec is a a 2-tuple (type, address)
# print(>> sys.stderr, "DBSPEC: %s" % (dbspec,))
(typ, addr) = dbspec
if typ == "shelve":
idb = _shelve_compat(addr, writeback=True, protocol=2)
elif typ == "memcached":
import memcache
idb = memcache.Client(addr)
elif typ == "dict": # in-memory dictionary
idb = {}
elif typ == "mongodb":
from saml2.mongo_store import IdentMDB
self.ident = IdentMDB(database=addr, collection="ident")
elif typ == "identdb":
mod, clas = addr.rsplit('.', 1)
mod = importlib.import_module(mod)
self.ident = getattr(mod, clas)()
if typ == "mongodb" or typ == "identdb":
pass
elif idb is not None:
self.ident = IdentDB(idb)
elif dbspec:
raise Exception("Couldn't open identity database: %s" %
(dbspec,))
try:
_domain = self.config.getattr("domain", "idp")
if _domain:
self.ident.domain = _domain
self.ident.name_qualifier = self.config.entityid
dbspec = self.config.getattr("edu_person_targeted_id", "idp")
if not dbspec:
#.........这里部分代码省略.........
示例6: __init__
def __init__(self, database="", collection="ident", domain="",
name_qualifier=""):
IdentDB.__init__(self, None, domain, name_qualifier)
self.mdb = MDB(database=database, collection=collection)
self.mdb.primary_key = "user_id"
示例7: init_config
def init_config(self, stype="idp"):
""" Remaining init of the server configuration
:param stype: The type of Server ("idp"/"aa")
"""
if stype == "aa":
return
# subject information is stored in a database
# default database is in memory which is OK in some setups
dbspec = self.config.getattr("subject_data", "idp")
idb = None
typ = ""
if not dbspec:
idb = {}
elif isinstance(dbspec, six.string_types):
idb = _shelve_compat(dbspec, writeback=True, protocol=2)
else: # database spec is a a 2-tuple (type, address)
# print(>> sys.stderr, "DBSPEC: %s" % (dbspec,))
(typ, addr) = dbspec
if typ == "shelve":
idb = _shelve_compat(addr, writeback=True, protocol=2)
elif typ == "memcached":
import memcache
idb = memcache.Client(addr)
elif typ == "dict": # in-memory dictionary
idb = {}
elif typ == "mongodb":
from saml2.mongo_store import IdentMDB
self.ident = IdentMDB(database=addr, collection="ident")
elif typ == "identdb":
mod, clas = addr.rsplit('.', 1)
mod = importlib.import_module(mod)
self.ident = getattr(mod, clas)()
if typ == "mongodb" or typ == "identdb":
pass
elif idb is not None:
self.ident = IdentDB(idb)
elif dbspec:
raise Exception("Couldn't open identity database: %s" %
(dbspec,))
try:
_domain = self.config.getattr("domain", "idp")
if _domain:
self.ident.domain = _domain
self.ident.name_qualifier = self.config.entityid
dbspec = self.config.getattr("edu_person_targeted_id", "idp")
if not dbspec:
pass
else:
typ = dbspec[0]
addr = dbspec[1]
secret = dbspec[2]
if typ == "shelve":
self.eptid = EptidShelve(secret, addr)
elif typ == "mongodb":
from saml2.mongo_store import EptidMDB
self.eptid = EptidMDB(secret, database=addr,
collection="eptid")
else:
self.eptid = Eptid(secret)
except Exception:
self.ident.close()
raise
示例8: __init__
def __init__(self, collection="", domain="", name_qualifier=""):
IdentDB.__init__(self, None, domain, name_qualifier)
self.mdb = MDB(collection, "ident")
self.mdb.primary_key = "user_id"
示例9: Server
class Server(Entity):
""" A class that does things that IdPs or AAs do """
def __init__(self, config_file="", config=None, cache=None, stype="idp",
symkey=""):
Entity.__init__(self, stype, config, config_file)
self.eptid = None
self.init_config(stype)
self.cache = cache
self.ticket = {}
#
self.session_db = self.choose_session_storage()
# Needed for
self.symkey = symkey
self.seed = rndstr()
self.iv = os.urandom(16)
self.lock = threading.Lock()
def getvalid_certificate_str(self):
if self.sec.cert_handler is not None:
return self.sec.cert_handler._last_validated_cert
return None
def support_AssertionIDRequest(self):
return True
def support_AuthnQuery(self):
return True
def choose_session_storage(self):
_spec = self.config.getattr("session_storage", "idp")
if not _spec:
return SessionStorage()
elif isinstance(_spec, basestring):
if _spec.lower() == "memory":
return SessionStorage()
else: # Should be tuple
typ, data = _spec
if typ.lower() == "mongodb":
from saml2.mongo_store import SessionStorageMDB
return SessionStorageMDB(database=data, collection="session")
raise NotImplementedError("No such storage type implemented")
def init_config(self, stype="idp"):
""" Remaining init of the server configuration
:param stype: The type of Server ("idp"/"aa")
"""
if stype == "aa":
return
# subject information is stored in a database
# default database is in memory which is OK in some setups
dbspec = self.config.getattr("subject_data", "idp")
idb = None
typ = ""
if not dbspec:
idb = {}
elif isinstance(dbspec, basestring):
idb = shelve.open(dbspec, writeback=True)
else: # database spec is a a 2-tuple (type, address)
#print >> sys.stderr, "DBSPEC: %s" % (dbspec,)
(typ, addr) = dbspec
if typ == "shelve":
idb = shelve.open(addr, writeback=True)
elif typ == "memcached":
import memcache
idb = memcache.Client(addr)
elif typ == "dict": # in-memory dictionary
idb = {}
elif typ == "mongodb":
from saml2.mongo_store import IdentMDB
self.ident = IdentMDB(database=addr, collection="ident")
elif typ == "identdb":
mod, clas = addr.rsplit('.', 1)
mod = importlib.import_module(mod)
self.ident = getattr(mod, clas)()
if typ == "mongodb" or typ == "identdb":
pass
elif idb is not None:
self.ident = IdentDB(idb)
elif dbspec:
raise Exception("Couldn't open identity database: %s" %
(dbspec,))
try:
_domain = self.config.getattr("domain", "idp")
if _domain:
self.ident.domain = _domain
self.ident.name_qualifier = self.config.entityid
dbspec = self.config.getattr("edu_person_targeted_id", "idp")
if not dbspec:
#.........这里部分代码省略.........
示例10: Server
class Server(Entity):
""" A class that does things that IdPs or AAs do """
def __init__(self, config_file="", config=None, cache=None, stype="idp",
symkey=""):
Entity.__init__(self, stype, config, config_file)
self.eptid = None
self.init_config(stype)
self.cache = cache
self.ticket = {}
#
self.session_db = self.choose_session_storage()
# Needed for
self.symkey = symkey
self.seed = rndbytes()
self.iv = os.urandom(16)
def support_AssertionIDRequest(self):
return True
def support_AuthnQuery(self):
return True
def choose_session_storage(self):
_spec = self.config.getattr("session_storage", "idp")
if not _spec:
return SessionStorage()
elif isinstance(_spec, str):
if _spec.lower() == "memory":
return SessionStorage()
else: # Should be tuple
typ, data = _spec
if typ.lower() == "mongodb":
from saml2.mongo_store import SessionStorageMDB
return SessionStorageMDB(database=data, collection="session")
raise NotImplementedError("No such storage type implemented")
def init_config(self, stype="idp"):
""" Remaining init of the server configuration
:param stype: The type of Server ("idp"/"aa")
"""
if stype == "aa":
return
# subject information is stored in a database
# default database is in memory which is OK in some setups
dbspec = self.config.getattr("subject_data", "idp")
idb = None
typ = ""
if not dbspec:
idb = {}
elif isinstance(dbspec, str):
idb = shelve.open(dbspec, writeback=True)
else: # database spec is a a 2-tuple (type, address)
#print >> sys.stderr, "DBSPEC: %s" % (dbspec,)
(typ, addr) = dbspec
if typ == "shelve":
idb = shelve.open(addr, writeback=True)
elif typ == "memcached":
import memcache
idb = memcache.Client(addr)
elif typ == "dict": # in-memory dictionary
idb = {}
elif typ == "mongodb":
from saml2.mongo_store import IdentMDB
self.ident = IdentMDB(database=addr, collection="ident")
if typ == "mongodb":
pass
elif idb is not None:
self.ident = IdentDB(idb)
elif dbspec:
raise Exception("Couldn't open identity database: %s" %
(dbspec,))
_domain = self.config.getattr("domain", "idp")
if _domain:
self.ident.domain = _domain
self.ident.name_qualifier = self.config.entityid
dbspec = self.config.getattr("edu_person_targeted_id", "idp")
if not dbspec:
pass
else:
typ = dbspec[0]
addr = dbspec[1]
secret = dbspec[2]
if typ == "shelve":
self.eptid = EptidShelve(secret, addr)
elif typ == "mongodb":
from saml2.mongo_store import EptidMDB
self.eptid = EptidMDB(secret, database=addr,
collection="eptid")
#.........这里部分代码省略.........
示例11: setup_class
def setup_class(self):
try:
os.remove("subject.db.db")
except:
pass
self.id = IdentDB("subject.db", "example.com", "example")