2 * Copyright 2001-2009 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.
20 * Base class for SAML 1.x and 2.0 artifacts.
24 #include "binding/SAMLArtifact.h"
26 #include <xercesc/util/Base64.hpp>
27 #include <xsec/framework/XSECDefs.hpp>
28 #include <xmltooling/unicode.h>
30 using namespace opensaml;
31 using namespace xmltooling;
36 SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAMLArtifactType0001Factory;
37 SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAMLArtifactType0002Factory;
41 SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAML2ArtifactType0004Factory;
45 void SAML_API opensaml::registerSAMLArtifacts()
47 SAMLConfig& conf=SAMLConfig::getConfig();
52 conf.SAMLArtifactManager.registerFactory(typecode, saml1p::SAMLArtifactType0001Factory);
53 typecode[1]=(char)0x2;
54 conf.SAMLArtifactManager.registerFactory(typecode, saml1p::SAMLArtifactType0002Factory);
55 typecode[1]=(char)0x4;
56 conf.SAMLArtifactManager.registerFactory(typecode, saml2p::SAML2ArtifactType0004Factory);
59 const unsigned int SAMLArtifact::TYPECODE_LENGTH = 2;
61 // Basic constructor just decodes the string and saves it off.
62 // Subclasses will handle pulling it apart.
64 SAMLArtifact::SAMLArtifact(const char* s)
67 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(s),&len);
69 throw ArtifactException("Unable to decode base64 artifact.");
73 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
74 XMLString::release(&decoded);
76 XMLString::release((char**)&decoded);
80 string SAMLArtifact::getBytes() const
85 string SAMLArtifact::getTypeCode() const
87 return m_raw.substr(0,TYPECODE_LENGTH);
90 string SAMLArtifact::getRemainingArtifact() const
92 return m_raw.substr(TYPECODE_LENGTH);
95 string SAMLArtifact::encode() const
98 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(m_raw.data()),m_raw.size(),&len);
100 string ret(reinterpret_cast<char*>(out),len);
101 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
102 XMLString::release(&out);
104 XMLString::release((char**)&out);
111 SAMLArtifact* SAMLArtifact::parse(const char* s)
113 // Decode and extract the type code first.
115 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(s),&len);
117 throw ArtifactException("Artifact parser unable to decode base64-encoded artifact.");
122 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
123 XMLString::release(&decoded);
125 XMLString::release((char**)&decoded);
128 return SAMLConfig::getConfig().SAMLArtifactManager.newPlugin(type,s);
131 SAMLArtifact* SAMLArtifact::parse(const XMLCh* s)
133 auto_ptr_char temp(s);
134 return parse(temp.get());
137 string SAMLArtifact::toHex(const string& s)
139 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
140 string::size_type len = s.length();
143 // two characters form the hex value.
144 for (string::size_type i=0; i < len; i++) {
145 ret+=(DIGITS[((unsigned char)(0xF0 & s[i])) >> 4 ]);
146 ret+=(DIGITS[0x0F & s[i]]);