def test_assertion_with_authn_instant(): ava = {} ast = Assertion(ava) policy = Policy({ "default": { "lifetime": {"minutes": 240}, "attribute_restrictions": None, # means all I have "name_form": NAME_FORMAT_URI }, }) name_id = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar") issuer = Issuer(text="entityid", format=NAMEID_FORMAT_ENTITY) farg = add_path( {}, ['subject', 'subject_confirmation', 'method', saml.SCM_BEARER]) add_path( farg['subject']['subject_confirmation'], ['subject_confirmation_data', 'in_response_to', 'in_response_to']) add_path( farg['subject']['subject_confirmation'], ['subject_confirmation_data', 'recipient', 'consumer_url']) msg = ast.construct( "sp_entity_id", [AttributeConverterNOOP(NAME_FORMAT_URI)], policy, issuer=issuer, authn_decl=ACD, authn_auth="authn_authn", authn_instant=1234567890, name_id=name_id, farg=farg) print(msg) assert msg.authn_statement[0].authn_instant == "2009-02-13T23:31:30Z"
def test_request_response(): sp = Saml2Client(config_file="servera_conf") with closing(Server(config_file="idp_all_conf")) as idp: binding, destination = sp.pick_binding("name_id_mapping_service", entity_id=idp.config.entityid) policy = NameIDPolicy(format=NAMEID_FORMAT_TRANSIENT, sp_name_qualifier="urn:mace:swamid:junk", allow_create="true") nameid = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar") mid, nmr = sp.create_name_id_mapping_request(policy, nameid, destination) print(nmr) args = sp.use_soap(nmr, destination) # ------- IDP ------------ req = idp.parse_name_id_mapping_request(args["data"], binding) in_response_to = req.message.id name_id = NameID(format=NAMEID_FORMAT_PERSISTENT, text="foobar") idp_response = idp.create_name_id_mapping_response( name_id, in_response_to=in_response_to) print(idp_response) ht_args = sp.use_soap(idp_response) # ------- SP ------------ _resp = sp.parse_name_id_mapping_request_response( ht_args["data"], binding) print(_resp.response) r_name_id = _resp.response.name_id assert r_name_id.format == NAMEID_FORMAT_PERSISTENT assert r_name_id.text == "foobar"
def decode(txt): """Turns a coded string by code() into a NameID class instance. :param txt: The coded string """ _nid = NameID() for part in txt.split(","): if part.find("=") != -1: i, val = part.split("=") try: setattr(_nid, ATTR[int(i)], unquote(val)) except: pass return _nid
def test_basic(): sp = Saml2Client(config_file="servera_conf") with closing(Server(config_file="idp_all_conf")) as idp: srvs = sp.metadata.authn_query_service(idp.config.entityid) destination = srvs[0]["location"] authn_context = requested_authn_context(INTERNETPROTOCOLPASSWORD) subject = Subject(text="abc", name_id=NameID(format=NAMEID_FORMAT_TRANSIENT)) _id, aq = sp.create_authn_query(subject, destination, authn_context) print(aq) assert isinstance(aq, AuthnQuery)
def get_nameid(self, userid, nformat, sp_name_qualifier, name_qualifier): _id = self.create_id(nformat, name_qualifier, sp_name_qualifier) if nformat == NAMEID_FORMAT_EMAILADDRESS: if not self.domain: raise SAMLError("Can't issue email nameids, unknown domain") _id = "%s@%s" % (_id, self.domain) # if nformat == NAMEID_FORMAT_PERSISTENT: # _id = userid nameid = NameID(format=nformat, sp_name_qualifier=sp_name_qualifier, name_qualifier=name_qualifier, text=_id) self.store(userid, nameid) return nameid
def test_base_request(): sp = Saml2Client(config_file="servera_conf") with closing(Server(config_file="idp_all_conf")) as idp: binding, destination = sp.pick_binding("name_id_mapping_service", entity_id=idp.config.entityid) policy = NameIDPolicy(format=NAMEID_FORMAT_TRANSIENT, sp_name_qualifier="urn:mace:swamid:junk", allow_create="true") nameid = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar") mid, nmr = sp.create_name_id_mapping_request(policy, nameid, destination) print(nmr) assert isinstance(nmr, NameIDMappingRequest)
def test_flow(): sp = Saml2Client(config_file="servera_conf") with closing(Server(config_file="idp_all_conf")) as idp: binding, destination = sp.pick_binding("manage_name_id_service", entity_id=idp.config.entityid) nameid = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar") newid = NewID(text="Barfoo") mid, midq = sp.create_manage_name_id_request(destination, name_id=nameid, new_id=newid) print(midq) rargs = sp.apply_binding(binding, "%s" % midq, destination, "") # --------- @IDP -------------- _req = idp.parse_manage_name_id_request(rargs["data"], binding) print(_req.message) mnir = idp.create_manage_name_id_response(_req.message, [binding]) if binding != BINDING_SOAP: binding, destination = idp.pick_binding( "manage_name_id_service", entity_id=sp.config.entityid) else: destination = "" respargs = idp.apply_binding(binding, "%s" % mnir, destination, "") print(respargs) # ---------- @SP --------------- _response = sp.parse_manage_name_id_request_response( respargs["data"], binding) print(_response.response) assert _response.response.id == mnir.id
def test_assertion_with_noop_attribute_conv(): ava = {"urn:oid:2.5.4.4": "Roland", "urn:oid:2.5.4.42": "Hedberg"} ast = Assertion(ava) policy = Policy({ "default": { "lifetime": {"minutes": 240}, "attribute_restrictions": None, # means all I have "name_form": NAME_FORMAT_URI }, }) name_id = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar") issuer = Issuer(text="entityid", format=NAMEID_FORMAT_ENTITY) farg = add_path( {}, ['subject', 'subject_confirmation', 'method', saml.SCM_BEARER]) add_path( farg['subject']['subject_confirmation'], ['subject_confirmation_data', 'in_response_to', 'in_response_to']) add_path( farg['subject']['subject_confirmation'], ['subject_confirmation_data', 'recipient', 'consumer_url']) msg = ast.construct( "sp_entity_id", [AttributeConverterNOOP(NAME_FORMAT_URI)], policy, issuer=issuer, farg=farg, authn_decl=ACD, name_id=name_id, authn_auth="authn_authn") print(msg) for attr in msg.attribute_statement[0].attribute: assert attr.name_format == NAME_FORMAT_URI assert len(attr.attribute_value) == 1 if attr.name == "urn:oid:2.5.4.42": assert attr.attribute_value[0].text == "Hedberg" elif attr.name == "urn:oid:2.5.4.4": assert attr.attribute_value[0].text == "Roland"
def test_basic(): sp = Saml2Client(config_file="servera_conf") with closing(Server(config_file="idp_all_conf")) as idp: # -------- @SP ------------ binding, destination = sp.pick_binding("manage_name_id_service", entity_id=idp.config.entityid) nameid = NameID(format=NAMEID_FORMAT_TRANSIENT, text="foobar") newid = NewID(text="Barfoo") mid, mreq = sp.create_manage_name_id_request(destination, name_id=nameid, new_id=newid) print(mreq) rargs = sp.apply_binding(binding, "%s" % mreq, destination, "") # --------- @IDP -------------- _req = idp.parse_manage_name_id_request(rargs["data"], binding) print(_req.message) assert mid == _req.message.id
__author__ = 'rolandh' from saml2_tophat import config from saml2_tophat.client import Saml2Client from saml2_tophat.time_util import str_to_time, in_a_while SESSION_INFO_PATTERN = { "ava": {}, "came from": "", "not_on_or_after": 0, "issuer": "", "session_id": -1 } nid = NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="abcdefgh") nid0 = NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="01234567") def add_derek_info(sp): not_on_or_after = str_to_time(in_a_while(days=1)) session_info = SESSION_INFO_PATTERN.copy() session_info["ava"] = {"givenName": ["Derek"], "umuselin": ["deje0001"]} session_info["issuer"] = "urn:mace:example.com:saml:idp" session_info["name_id"] = nid session_info["not_on_or_after"] = not_on_or_after # subject_id, entity_id, info, timestamp sp.users.add_information_about_person(session_info)
"not_on_or_after": 0, "issuer": "", "session_id": -1 } def _eq(l1, l2): return set(l1) == set(l2) def nid_eq(l1, l2): return _eq([code(c) for c in l1], [code(c) for c in l2]) nid = [ NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="1234"), NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="9876"), NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="1000") ] class TestClass: def setup_class(self): self.cache = Cache() def test_set(self): not_on_or_after = str_to_time(in_a_while(days=1)) session_info = SESSION_INFO_PATTERN.copy() session_info["ava"] = {"givenName": ["Derek"]} self.cache.set(nid[0], "abcd", session_info, not_on_or_after)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os from saml2_tophat.authn_context import INTERNETPROTOCOLPASSWORD from saml2_tophat.saml import NameID, NAMEID_FORMAT_TRANSIENT from saml2_tophat.samlp import response_from_string from saml2_tophat.server import Server from saml2_tophat import client from saml2_tophat import config from mock.mock import Mock, MagicMock import saml2_tophat.xmldsig as ds nid = NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="123456") AUTHN = { "class_ref": INTERNETPROTOCOLPASSWORD, "authn_auth": "http://www.example.com/login" } def _eq(l1, l2): return set(l1) == set(l2) BASEDIR = os.path.abspath(os.path.dirname(__file__)) def get_ava(assertion): ava = {}