File: SAMLArtifact.cpp

package info (click to toggle)
opensaml 3.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,480 kB
  • sloc: cpp: 27,961; sh: 4,593; xml: 1,004; makefile: 429; ansic: 18
file content (153 lines) | stat: -rw-r--r-- 4,345 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * Licensed to the University Corporation for Advanced Internet
 * Development, Inc. (UCAID) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */

/**
 * SAMLArtifact.cpp
 * 
 * Base class for SAML 1.x and 2.0 artifacts.
 */

#include "internal.h"
#include "binding/SAMLArtifact.h"

#include <xercesc/util/Base64.hpp>
#include <xsec/framework/XSECDefs.hpp>
#include <xmltooling/unicode.h>

using namespace opensaml;
using namespace xmltooling;
using namespace std;

namespace opensaml {
    namespace saml1p {
        SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAMLArtifactType0001Factory; 
        SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAMLArtifactType0002Factory; 
    };

    namespace saml2p {
        SAML_DLLLOCAL PluginManager<SAMLArtifact,string,const char*>::Factory SAML2ArtifactType0004Factory; 
    };
};

void SAML_API opensaml::registerSAMLArtifacts()
{
    SAMLConfig& conf=SAMLConfig::getConfig();

    string typecode;
    typecode+=(char)0x0;
    typecode+=(char)0x1;
    conf.SAMLArtifactManager.registerFactory(typecode, saml1p::SAMLArtifactType0001Factory);
    typecode[1]=(char)0x2;
    conf.SAMLArtifactManager.registerFactory(typecode, saml1p::SAMLArtifactType0002Factory);
    typecode[1]=(char)0x4;
    conf.SAMLArtifactManager.registerFactory(typecode, saml2p::SAML2ArtifactType0004Factory);
}

const unsigned int SAMLArtifact::TYPECODE_LENGTH = 2;

SAMLArtifact::SAMLArtifact()
{
}

SAMLArtifact::~SAMLArtifact()
{
}

SAMLArtifact::SAMLArtifact(const SAMLArtifact& src) : m_raw(src.m_raw)
{
}

// Basic constructor just decodes the string and saves it off.
// Subclasses will handle pulling it apart.

SAMLArtifact::SAMLArtifact(const char* s)
{
    XMLSize_t len=0;
    XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(s),&len);
    if (!decoded)
        throw ArtifactException("Unable to decode base64 artifact.");
    XMLByte* ptr=decoded;
    while (len--)
        m_raw+= *ptr++;
    XMLString::release((char**)&decoded);
}

string SAMLArtifact::getBytes() const
{
    return m_raw;
}

string SAMLArtifact::getTypeCode() const
{
    return m_raw.substr(0,TYPECODE_LENGTH);
}

string SAMLArtifact::getRemainingArtifact() const
{
    return m_raw.substr(TYPECODE_LENGTH);
}

string SAMLArtifact::encode() const
{
    XMLSize_t len=0;
    XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(m_raw.data()),m_raw.size(),&len);
    if (out) {
        string ret(reinterpret_cast<char*>(out),len);
        XMLString::release((char**)&out);
        return ret;
    }
    return string();
}

SAMLArtifact* SAMLArtifact::parse(const char* s)
{
    // Decode and extract the type code first.
    XMLSize_t len=0;
    XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(s),&len);
    if (!decoded)
        throw ArtifactException("Artifact parser unable to decode base64-encoded artifact.");
    
    string type;
    type+= decoded[0];
    type+= decoded[1];
    XMLString::release((char**)&decoded);
    
    return SAMLConfig::getConfig().SAMLArtifactManager.newPlugin(type,s,false);
}

SAMLArtifact* SAMLArtifact::parse(const XMLCh* s)
{
    auto_ptr_char temp(s);
    return parse(temp.get());
}

string SAMLArtifact::toHex(const string& s)
{
    static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    string::size_type len = s.length();
    string ret;
    
    // two characters form the hex value.
    for (string::size_type i=0; i < len; i++) {
        ret+=(DIGITS[((unsigned char)(0xF0 & s[i])) >> 4 ]);
        ret+=(DIGITS[0x0F & s[i]]);
    }
    return ret;
}