2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "signature/SAMLSignatureTestBase.h"
18 #include <saml/saml2/core/Assertions.h>
22 using namespace opensaml::saml2;
24 class SAML2AssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
27 childElementsFile = data_path + "signature/SAML2Assertion.xml";
28 SAMLSignatureTestBase::setUp();
32 SAMLSignatureTestBase::tearDown();
35 void testSignature() {
36 auto_ptr_XMLCh issuer("issuer");
37 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
38 auto_ptr_XMLCh id("ident");
39 auto_ptr_XMLCh method("method");
40 auto_ptr_XMLCh nameid("John Doe");
42 Issuer* is=IssuerBuilder::buildIssuer();
43 is->setName(issuer.get());
45 NameID* n=NameIDBuilder::buildNameID();
46 n->setName(nameid.get());
47 Subject* subject=SubjectBuilder::buildSubject();
48 subject->setNameID(n);
50 AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
51 statement->setAuthnInstant(issueInstant.get());
53 AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
54 AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
55 acc->setReference(method.get());
56 ac->setAuthnContextClassRef(acc);
57 statement->setAuthnContext(ac);
59 auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
60 assertion->setID(id.get());
61 assertion->setIssueInstant(issueInstant.get());
62 assertion->setIssuer(is);
63 assertion->setSubject(subject);
64 assertion->getAuthnStatements().push_back(statement);
66 // Append a Signature.
67 Signature* sig=SignatureBuilder::buildSignature();
68 assertion->setSignature(sig);
69 Locker locker(m_resolver);
70 sig->setSigningKey(m_resolver->getKey());
73 KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
74 X509Data* x509Data=X509DataBuilder::buildX509Data();
75 keyInfo->getX509Datas().push_back(x509Data);
76 for_each(m_resolver->getCertificates().begin(),m_resolver->getCertificates().end(),bind1st(_addcert(),x509Data));
77 sig->setKeyInfo(keyInfo);
79 // Sign while marshalling.
80 vector<Signature*> sigs(1,sig);
81 DOMElement* rootElement = NULL;
83 rootElement=assertion->marshall((DOMDocument*)NULL,&sigs);
85 catch (XMLToolingException& e) {
91 XMLHelper::serialize(rootElement, buf);
92 istringstream in(buf);
93 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
94 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
96 auto_ptr<XMLObject> assertion2(b->buildFromDocument(doc));
97 assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
100 SignatureProfileValidator spv;
101 SignatureValidator sv(new KeyResolver(m_resolver->getKey()));
102 spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
103 sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
105 catch (XMLToolingException& e) {