#include "ServiceProvider.h"
#include "SPRequest.h"
#include "handler/AssertionConsumerService.h"
+#include "util/CGIParser.h"
#include "util/SPConstants.h"
# include <ctime>
# include "lite/CommonDomainCookie.h"
#endif
+#include <xmltooling/XMLToolingConfig.h>
+#include <xmltooling/util/URLEncoder.h>
+
using namespace shibspconstants;
using namespace shibsp;
using namespace opensaml;
pair<bool,long> AssertionConsumerService::run(SPRequest& request, bool isHandler) const
{
- string relayState;
- SPConfig& conf = SPConfig::getConfig();
+ // Check for a message back to the ACS from a post-session hook.
+ if (request.getQueryString() && strstr(request.getQueryString(), "hook=1")) {
+ // Parse the query string only to preserve any POST data.
+ CGIParser cgi(request, true);
+ pair<CGIParser::walker,CGIParser::walker> param = cgi.getParameters("hook");
+ if (param.first != param.second && param.first->second && !strcmp(param.first->second, "1")) {
+ string target;
+ param = cgi.getParameters("target");
+ if (param.first != param.second && param.first->second)
+ target = param.first->second;
+ return finalizeResponse(request.getApplication(), request, request, target);
+ }
+ }
- if (conf.isEnabled(SPConfig::OutOfProcess)) {
+ if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
// When out of process, we run natively and directly process the message.
return processMessage(request.getApplication(), request, request);
}
headers.push_back("Accept-Language");
DDF out,in = wrap(request, &headers);
DDFJanitor jin(in), jout(out);
- out=request.getServiceProvider().getListenerService()->send(in);
+ out = request.getServiceProvider().getListenerService()->send(in);
return unwrap(request, out);
}
}
void AssertionConsumerService::receive(DDF& in, ostream& out)
{
// Find application.
- const char* aid=in["application_id"].string();
- const Application* app=aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
+ const char* aid = in["application_id"].string();
+ const Application* app = aid ? SPConfig::getConfig().getServiceProvider()->getApplication(aid) : nullptr;
if (!app) {
// Something's horribly wrong.
m_log.error("couldn't find application (%s) for new session", aid ? aid : "(missing)");
{
#ifndef SHIBSP_LITE
// Locate policy key.
- pair<bool,const char*> policyId = getString("policyId", m_configNS.get()); // namespace-qualified if inside handler element
- if (!policyId.first)
- policyId = application.getString("policyId"); // unqualified in Application(s) element
+ pair<bool,const char*> prop = getString("policyId", m_configNS.get()); // may be namespace-qualified if inside handler element
+ if (!prop.first)
+ prop = getString("policyId"); // try unqualified
+ if (!prop.first)
+ prop = application.getString("policyId"); // unqualified in Application(s) element
// Lock metadata for use by policy.
Locker metadataLocker(application.getMetadataProvider());
// Create the policy.
scoped_ptr<opensaml::SecurityPolicy> policy(
- application.getServiceProvider().getSecurityPolicyProvider()->createSecurityPolicy(application, &IDPSSODescriptor::ELEMENT_QNAME, policyId.second)
+ application.getServiceProvider().getSecurityPolicyProvider()->createSecurityPolicy(
+ application, &IDPSSODescriptor::ELEMENT_QNAME, prop.second
+ )
);
string relayState;
msg.reset(m_decoder->decode(relayState, httpRequest, *(policy.get())));
if (!msg)
throw BindingException("Failed to decode an SSO protocol response.");
- DDF postData = recoverPostData(application, httpRequest, httpResponse, relayState.c_str());
- DDFJanitor postjan(postData);
- recoverRelayState(application, httpRequest, httpResponse, relayState);
- application.limitRedirect(httpRequest, relayState.c_str());
implementProtocol(application, httpRequest, httpResponse, *policy, nullptr, *msg);
- auto_ptr_char issuer(policy->getIssuer() ? policy->getIssuer()->getName() : nullptr);
-
// History cookie.
+ auto_ptr_char issuer(policy->getIssuer() ? policy->getIssuer()->getName() : nullptr);
if (issuer.get() && *issuer.get())
maintainHistory(application, httpRequest, httpResponse, issuer.get());
- // Now redirect to the state value. By now, it should be set to *something* usable.
- // First check for POST data.
- if (!postData.islist()) {
- m_log.debug("ACS returning via redirect to: %s", relayState.c_str());
- return make_pair(true, httpResponse.sendRedirect(relayState.c_str()));
- }
- else {
- m_log.debug("ACS returning via POST to: %s", relayState.c_str());
- return make_pair(true, sendPostResponse(application, httpResponse, relayState.c_str(), postData));
+ const EntityDescriptor* entity =
+ dynamic_cast<const EntityDescriptor*>(policy->getIssuerMetadata() ? policy->getIssuerMetadata()->getParent() : nullptr);
+ prop = application.getRelyingParty(entity)->getString("sessionHook");
+ if (prop.first) {
+ string hook(prop.second);
+ httpRequest.absolutize(hook);
+
+ // Compute the return URL. We use a self-referential link plus a hook indicator to break the cycle
+ // and the relay state.
+ const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
+ string returnURL = httpRequest.getRequestURL();
+ returnURL = returnURL.substr(0, returnURL.find('?')) + "?hook=1";
+ if (!relayState.empty())
+ returnURL += "&target=" + encoder->encode(relayState.c_str());
+ if (hook.find('?') == string::npos)
+ hook += '?';
+ else
+ hook += '&';
+ hook += "return=" + encoder->encode(returnURL.c_str());
+
+ // Add the translated target resource in case it's of interest.
+ if (!relayState.empty()) {
+ try {
+ recoverRelayState(application, httpRequest, httpResponse, relayState, false);
+ hook += "&target=" + encoder->encode(relayState.c_str());
+ }
+ catch (std::exception& ex) {
+ m_log.warn("error recovering relay state: %s", ex.what());
+ }
+ }
+
+ return make_pair(true, httpResponse.sendRedirect(hook.c_str()));
}
+
+ return finalizeResponse(application, httpRequest, httpResponse, relayState);
}
catch (XMLToolingException& ex) {
+ // Recover relay state.
+ if (!relayState.empty()) {
+ try {
+ recoverRelayState(application, httpRequest, httpResponse, relayState, false);
+ }
+ catch (std::exception& rsex) {
+ m_log.warn("error recovering relay state: %s", rsex.what());
+ relayState.erase();
+ recoverRelayState(application, httpRequest, httpResponse, relayState, false);
+ }
+ }
+
// Check for isPassive error condition.
const char* sc2 = ex.getProperty("statusCode2");
if (sc2 && !strcmp(sc2, "urn:oasis:names:tc:SAML:2.0:status:NoPassive")) {
- pair<bool,bool> ignore = getBool("ignoreNoPassive", m_configNS.get()); // namespace-qualified if inside handler element
+ pair<bool,bool> ignore = getBool("ignoreNoPassive", m_configNS.get()); // may be namespace-qualified inside handler element
+ if (!ignore.first)
+ ignore = getBool("ignoreNoPassive"); // try unqualified
if (ignore.first && ignore.second && !relayState.empty()) {
m_log.debug("ignoring SAML status of NoPassive and redirecting to resource...");
return make_pair(true, httpResponse.sendRedirect(relayState.c_str()));
}
}
- if (!relayState.empty())
+
+ if (!relayState.empty()) {
ex.addProperty("RelayState", relayState.c_str());
+ }
// Log the error.
try {
#endif
}
+pair<bool,long> AssertionConsumerService::finalizeResponse(
+ const Application& application, const HTTPRequest& httpRequest, HTTPResponse& httpResponse, string& relayState
+ ) const
+{
+ DDF postData = recoverPostData(application, httpRequest, httpResponse, relayState.c_str());
+ DDFJanitor postjan(postData);
+ recoverRelayState(application, httpRequest, httpResponse, relayState);
+ application.limitRedirect(httpRequest, relayState.c_str());
+
+ // Now redirect to the state value. By now, it should be set to *something* usable.
+ // First check for POST data.
+ if (!postData.islist()) {
+ m_log.debug("ACS returning via redirect to: %s", relayState.c_str());
+ return make_pair(true, httpResponse.sendRedirect(relayState.c_str()));
+ }
+ else {
+ m_log.debug("ACS returning via POST to: %s", relayState.c_str());
+ return make_pair(true, sendPostResponse(application, httpResponse, relayState.c_str(), postData));
+ }
+}
+
void AssertionConsumerService::checkAddress(const Application& application, const HTTPRequest& httpRequest, const char* issuedTo) const
{
if (!issuedTo || !*issuedTo)
return new SecurityPolicy(application, role, validate, policyId);
}
-class SHIBSP_DLLLOCAL DummyContext : public ResolutionContext
-{
-public:
- DummyContext(const vector<Attribute*>& attributes) : m_attributes(attributes) {
- }
+namespace {
+ class SHIBSP_DLLLOCAL DummyContext : public ResolutionContext
+ {
+ public:
+ DummyContext(const vector<Attribute*>& attributes) : m_attributes(attributes) {
+ }
- virtual ~DummyContext() {
- for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
- }
+ virtual ~DummyContext() {
+ for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
+ }
- vector<Attribute*>& getResolvedAttributes() {
- return m_attributes;
- }
- vector<Assertion*>& getResolvedAssertions() {
- return m_tokens;
- }
+ vector<Attribute*>& getResolvedAttributes() {
+ return m_attributes;
+ }
+ vector<Assertion*>& getResolvedAssertions() {
+ return m_tokens;
+ }
-private:
- vector<Attribute*> m_attributes;
- static vector<Assertion*> m_tokens; // never any tokens, so just share an empty vector
+ private:
+ vector<Attribute*> m_attributes;
+ static vector<Assertion*> m_tokens; // never any tokens, so just share an empty vector
+ };
};
vector<Assertion*> DummyContext::m_tokens;
nullptr,
issuer,
protocol,
+ nullptr,
v1nameid,
nullptr,
nameid,
ResolutionContext* AssertionConsumerService::resolveAttributes(
const Application& application,
const GenericRequest* request,
- const saml2md::RoleDescriptor* issuer,
+ const RoleDescriptor* issuer,
const XMLCh* protocol,
+ const xmltooling::XMLObject* protmsg,
const saml1::NameIdentifier* v1nameid,
const saml1::AuthenticationStatement* v1statement,
const saml2::NameID* nameid,
m_log.debug("extracting pushed attributes...");
+ if (protmsg) {
+ try {
+ extractor->extractAttributes(application, request, issuer, *protmsg, resolvedAttributes);
+ }
+ catch (std::exception& ex) {
+ m_log.error("caught exception extracting attributes: %s", ex.what());
+ }
+ }
+
if (v1nameid || nameid) {
try {
if (v1nameid)
AttributeFilter* filter = application.getAttributeFilter();
if (filter && !resolvedAttributes.empty()) {
- BasicFilteringContext fc(application, resolvedAttributes, issuer, authncontext_class);
+ BasicFilteringContext fc(application, resolvedAttributes, issuer, authncontext_class, authncontext_decl);
Locker filtlocker(filter);
try {
filter->filterAttributes(fc, resolvedAttributes);
auto_ptr<ResolutionContext> ctx(
resolver->createResolutionContext(
application,
+ request,
issuer ? dynamic_cast<const saml2md::EntityDescriptor*>(issuer->getParent()) : nullptr,
protocol,
nameid,
}
}
-LoginEvent* AssertionConsumerService::newLoginEvent(const Application& application, const xmltooling::HTTPRequest& request) const
+LoginEvent* AssertionConsumerService::newLoginEvent(const Application& application, const HTTPRequest& request) const
{
if (!SPConfig::getConfig().isEnabled(SPConfig::Logging))
return nullptr;
) const
{
static const char* defProps="; path=/";
+ static const char* sslProps="; path=/; secure";
- const PropertySet* sessionProps=application.getPropertySet("Sessions");
- pair<bool,bool> idpHistory=sessionProps->getBool("idpHistory");
+ const PropertySet* sessionProps = application.getPropertySet("Sessions");
+ pair<bool,bool> idpHistory = sessionProps->getBool("idpHistory");
if (idpHistory.first && idpHistory.second) {
- pair<bool,const char*> cookieProps=sessionProps->getString("idpHistoryProps");
- if (!cookieProps.first)
- cookieProps=sessionProps->getString("cookieProps");
+ pair<bool,const char*> cookieProps = sessionProps->getString("idpHistoryProps");
if (!cookieProps.first)
- cookieProps.second=defProps;
+ cookieProps = sessionProps->getString("cookieProps");
+ if (!cookieProps.first || !strcmp(cookieProps.second, "http"))
+ cookieProps.second = defProps;
+ else if (!strcmp(cookieProps.second, "https"))
+ cookieProps.second = sslProps;
// Set an IdP history cookie locally (essentially just a CDC).
CommonDomainCookie cdc(request.getCookie(CommonDomainCookie::CDCName));
// Either leave in memory or set an expiration.
- pair<bool,unsigned int> days=sessionProps->getUnsignedInt("idpHistoryDays");
- if (!days.first || days.second==0) {
+ pair<bool,unsigned int> days = sessionProps->getUnsignedInt("idpHistoryDays");
+ if (!days.first || days.second == 0) {
string c = string(cdc.set(entityID)) + cookieProps.second;
response.setCookie(CommonDomainCookie::CDCName, c.c_str());
}
else {
- time_t now=time(nullptr) + (days.second * 24 * 60 * 60);
+ time_t now = time(nullptr) + (days.second * 24 * 60 * 60);
#ifdef HAVE_GMTIME_R
struct tm res;
- struct tm* ptime=gmtime_r(&now,&res);
+ struct tm* ptime = gmtime_r(&now,&res);
#else
- struct tm* ptime=gmtime(&now);
+ struct tm* ptime = gmtime(&now);
#endif
char timebuf[64];
- strftime(timebuf,64,"%a, %d %b %Y %H:%M:%S GMT",ptime);
+ strftime(timebuf,64,"%a, %d %b %Y %H:%M:%S GMT", ptime);
string c = string(cdc.set(entityID)) + cookieProps.second + "; expires=" + timebuf;
response.setCookie(CommonDomainCookie::CDCName, c.c_str());
}