2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * Base class for SAML 1.x and 2.0 artifacts.
28 #include "binding/SAMLArtifact.h"
30 #include <xercesc/util/Base64.hpp>
31 #include <xsec/framework/XSECDefs.hpp>
32 #include <xmltooling/unicode.h>
34 using namespace opensaml;
35 using namespace xmltooling;
40 SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAMLArtifactType0001Factory;
41 SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAMLArtifactType0002Factory;
45 SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAML2ArtifactType0004Factory;
49 void SAML_API opensaml::registerSAMLArtifacts()
51 SAMLConfig& conf=SAMLConfig::getConfig();
56 conf.SAMLArtifactManager.registerFactory(typecode, saml1p::SAMLArtifactType0001Factory);
57 typecode[1]=(char)0x2;
58 conf.SAMLArtifactManager.registerFactory(typecode, saml1p::SAMLArtifactType0002Factory);
59 typecode[1]=(char)0x4;
60 conf.SAMLArtifactManager.registerFactory(typecode, saml2p::SAML2ArtifactType0004Factory);
63 const unsigned int SAMLArtifact::TYPECODE_LENGTH = 2;
65 SAMLArtifact::SAMLArtifact()
69 SAMLArtifact::~SAMLArtifact()
73 SAMLArtifact::SAMLArtifact(const SAMLArtifact& src) : m_raw(src.m_raw)
77 // Basic constructor just decodes the string and saves it off.
78 // Subclasses will handle pulling it apart.
80 SAMLArtifact::SAMLArtifact(const char* s)
83 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(s),&len);
85 throw ArtifactException("Unable to decode base64 artifact.");
89 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
90 XMLString::release(&decoded);
92 XMLString::release((char**)&decoded);
96 string SAMLArtifact::getBytes() const
101 string SAMLArtifact::getTypeCode() const
103 return m_raw.substr(0,TYPECODE_LENGTH);
106 string SAMLArtifact::getRemainingArtifact() const
108 return m_raw.substr(TYPECODE_LENGTH);
111 string SAMLArtifact::encode() const
114 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(m_raw.data()),m_raw.size(),&len);
116 string ret(reinterpret_cast<char*>(out),len);
117 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
118 XMLString::release(&out);
120 XMLString::release((char**)&out);
127 SAMLArtifact* SAMLArtifact::parse(const char* s)
129 // Decode and extract the type code first.
131 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(s),&len);
133 throw ArtifactException("Artifact parser unable to decode base64-encoded artifact.");
138 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
139 XMLString::release(&decoded);
141 XMLString::release((char**)&decoded);
144 return SAMLConfig::getConfig().SAMLArtifactManager.newPlugin(type,s);
147 SAMLArtifact* SAMLArtifact::parse(const XMLCh* s)
149 auto_ptr_char temp(s);
150 return parse(temp.get());
153 string SAMLArtifact::toHex(const string& s)
155 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
156 string::size_type len = s.length();
159 // two characters form the hex value.
160 for (string::size_type i=0; i < len; i++) {
161 ret+=(DIGITS[((unsigned char)(0xF0 & s[i])) >> 4 ]);
162 ret+=(DIGITS[0x0F & s[i]]);