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"
19 #include <saml/saml1/core/Assertions.h>
20 #include <saml/saml1/core/Protocols.h>
24 using namespace opensaml::saml1p;
25 using namespace opensaml::saml1;
27 class SAML1ResponseTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
30 childElementsFile = data_path + "signature/SAML1Response.xml";
31 SAMLSignatureTestBase::setUp();
35 SAMLSignatureTestBase::tearDown();
38 void testSignature() {
39 auto_ptr_XMLCh issuer("issuer");
40 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
41 auto_ptr_XMLCh aid("aident");
42 auto_ptr_XMLCh rid("rident");
43 auto_ptr_XMLCh method("method");
44 auto_ptr_XMLCh nameid("John Doe");
46 NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
47 n->setName(nameid.get());
48 Subject* subject=SubjectBuilder::buildSubject();
49 subject->setNameIdentifier(n);
51 AuthenticationStatement* statement=AuthenticationStatementBuilder::buildAuthenticationStatement();
52 statement->setAuthenticationInstant(issueInstant.get());
53 statement->setAuthenticationMethod(method.get());
54 statement->setSubject(subject);
56 Assertion* assertion=AssertionBuilder::buildAssertion();
57 assertion->setAssertionID(aid.get());
58 assertion->setIssueInstant(issueInstant.get());
59 assertion->setIssuer(issuer.get());
60 assertion->getAuthenticationStatements().push_back(statement);
62 // Append a Signature.
63 assertion->setSignature(SignatureBuilder::buildSignature());
65 // Sign assertion while marshalling.
66 vector<Signature*> sigs(1,assertion->getSignature());
67 CredentialCriteria cc;
68 cc.setUsage(Credential::SIGNING_CREDENTIAL);
69 Locker locker(m_resolver);
70 const Credential* cred = m_resolver->resolve(&cc);
71 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
73 DOMElement* rootElement = NULL;
75 rootElement=assertion->marshall((DOMDocument*)NULL,&sigs,cred);
77 catch (XMLToolingException& e) {
83 StatusCode* sc=StatusCodeBuilder::buildStatusCode();
84 sc->setValue(&StatusCode::SUCCESS);
85 Status* status=StatusBuilder::buildStatus();
86 status->setStatusCode(sc);
88 auto_ptr<Response> response(ResponseBuilder::buildResponse());
89 response->setResponseID(rid.get());
90 response->setIssueInstant(issueInstant.get());
91 response->setStatus(status);
92 response->getAssertions().push_back(assertion);
93 response->setSignature(SignatureBuilder::buildSignature());
95 // Sign response while marshalling.
97 sigs.push_back(response->getSignature());
100 rootElement=response->marshall((DOMDocument*)NULL,&sigs,cred);
102 catch (XMLToolingException& e) {
108 XMLHelper::serialize(rootElement, buf);
109 istringstream in(buf);
110 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
111 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
113 auto_ptr<XMLObject> response2(b->buildFromDocument(doc));
114 assertEquals("Unmarshalled response does not match", expectedChildElementsDOM, response2.get(), false);
117 opensaml::SignatureProfileValidator spv;
118 spv.validate(dynamic_cast<Response*>(response2.get())->getAssertions().front()->getSignature());
119 spv.validate(dynamic_cast<Response*>(response2.get())->getSignature());
121 SignatureValidator sv(cred);
122 sv.validate(dynamic_cast<Response*>(response2.get())->getAssertions().front()->getSignature());
123 sv.validate(dynamic_cast<Response*>(response2.get())->getSignature());
125 catch (XMLToolingException& e) {