| 6 | == Example Code == |
| 7 | {{{ |
| 8 | from ndg.saml.saml2.core import (AttributeQuery, SAMLVersion, Issuer, Subject, |
| 9 | NameID, Attribute, XSStringAttributeValue) |
| 10 | from uuid import uuid4 |
| 11 | from datetime import datetime |
| 12 | |
| 13 | attributeQuery = AttributeQuery() |
| 14 | attributeQuery.version = SAMLVersion(SAMLVersion.VERSION_20) |
| 15 | attributeQuery.id = str(uuid4()) |
| 16 | attributeQuery.issueInstant = datetime.utcnow() |
| 17 | |
| 18 | attributeQuery.issuer = Issuer() |
| 19 | attributeQuery.issuer.format = Issuer.X509_SUBJECT |
| 20 | attributeQuery.issuer.value = '/O=NDG/OU=BADC/CN=PolicyInformationPoint' |
| 21 | |
| 22 | attributeQuery.subject = Subject() |
| 23 | attributeQuery.subject.nameID = NameID() |
| 24 | attributeQuery.subject.nameID.format = NameID.X509_SUBJECT |
| 25 | attributeQuery.subject.nameID.value = '/O=NDG/OU=BADC/CN=PhilipKershaw' |
| 26 | |
| 27 | # special case handling for 'LastName' attribute |
| 28 | emailAddressAttribute = Attribute() |
| 29 | emailAddressAttribute.name = "urn:esg:email:address" |
| 30 | emailAddressAttribute.nameFormat = "%s#%s" % ( |
| 31 | XSStringAttributeValue.TYPE_NAME.namespaceURI, |
| 32 | XSStringAttributeValue.TYPE_NAME.localPart) |
| 33 | |
| 34 | emailAddress = XSStringAttributeValue() |
| 35 | emailAddress.value = 'pjk@somewhere.ac.uk' |
| 36 | emailAddressAttribute.attributeValues.append(emailAddress) |
| 37 | |
| 38 | attributeQuery.attributes.append(emailAddressAttribute) |
| 39 | |
| 40 | # Convert to ElementTree representation |
| 41 | from ndg.saml.xml.etree import AttributeQueryElementTree, prettyPrint |
| 42 | |
| 43 | elem = AttributeQueryElementTree.toXML(attributeQuery) |
| 44 | |
| 45 | # Serialise as string |
| 46 | xmlOut = prettyPrint(elem) |
| 47 | print(xmlOut) |
| 48 | }}} |
| 49 | |
| 50 | Produces: |
| 51 | |
| 52 | {{{ |
| 53 | <samlp:AttributeQuery xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Version="2.0" IssueInstant="2010-06-01T13:19:50.690263Z" ID="1c15e748-0f74-41f1-848c-1fbdfeef2a06"> |
| 54 | <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Format="urn:oasis:names:tc:SAML:1.1:nameid-format:x509SubjectName">/O=NDG/OU=BADC/CN=PolicyInformationPoint</saml:Issuer> |
| 55 | <saml:Subject xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"> |
| 56 | <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:x509SubjectName">/O=NDG/OU=BADC/CN=PhilipKershaw</saml:NameID> |
| 57 | </saml:Subject> |
| 58 | <saml:Attribute xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Name="urn:esg:email:address" NameFormat="http://www.w3.org/2001/XMLSchema#string"> |
| 59 | <saml:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">pjk@somewhere.ac.uk</saml:AttributeValue> |
| 60 | </saml:Attribute> |
| 61 | </samlp:AttributeQuery> |
| 62 | }}} |