2 * Copyright 2001-2005 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.
18 * shib-target.h -- top-level header file for the SHIB Common Target Library
20 * Created by: Derek Atkins <derek@ihtfp.com>
29 #include <shibsp/base.h>
30 #include <shibsp/ListenerService.h>
31 #include <shibsp/PropertySet.h>
32 #include <saml/saml2/metadata/MetadataProvider.h>
33 #include <xmltooling/security/TrustEngine.h>
36 #include <saml/saml.h>
37 #include <shib/shib.h>
40 # ifndef SHIBTARGET_EXPORTS
41 # define SHIBTARGET_EXPORTS __declspec(dllimport)
43 # define SHIB_SCHEMAS "/opt/shibboleth-sp/share/xml/shibboleth"
44 # define SHIB_CONFIG "/opt/shibboleth-sp/etc/shibboleth/shibboleth.xml"
46 # include <shib-target/shib-paths.h>
47 # define SHIBTARGET_EXPORTS
50 namespace shibtarget {
52 // Abstract APIs for access to configuration information
54 // Forward declaration
55 class SHIBTARGET_EXPORTS ShibTarget;
58 * Interface to a protocol handler
60 * Protocol handlers perform system functions such as processing SAML protocol
61 * messages to create and logout sessions or creating protocol requests.
63 struct SHIBTARGET_EXPORTS IHandler : public virtual saml::IPlugIn
65 IHandler() : m_props(NULL) {}
66 virtual ~IHandler() {}
67 virtual const shibsp::PropertySet* getProperties() const { return m_props; }
68 virtual void setProperties(const shibsp::PropertySet* properties) { m_props=properties; }
69 virtual std::pair<bool,void*> run(ShibTarget* st, bool isHandler=true) const=0;
71 const shibsp::PropertySet* m_props;
75 * Interface to Shibboleth Applications, which exposes most of the functionality
76 * required to process web requests or security protocol messages for resources
77 * associated with them.
79 * Applications are implementation-specific, but generally correspond to collections
80 * of resources related to one another in logical ways, such as a virtual host or
81 * a Java servlet context. Most complex configuration data is associated with an
82 * Application. Implementations should always expose an application named "default"
85 struct SHIBTARGET_EXPORTS IApplication : public virtual shibsp::PropertySet,
86 public virtual shibboleth::ShibBrowserProfile::ITokenValidator
88 virtual const char* getId() const=0;
89 virtual const char* getHash() const=0;
91 virtual saml::Iterator<saml::SAMLAttributeDesignator*> getAttributeDesignators() const=0;
92 virtual saml::Iterator<shibboleth::IAAP*> getAAPProviders() const=0;
93 virtual opensaml::saml2md::MetadataProvider* getMetadataProvider() const=0;
94 virtual xmltooling::TrustEngine* getTrustEngine() const=0;
95 virtual saml::Iterator<const XMLCh*> getAudiences() const=0;
96 virtual const shibsp::PropertySet* getCredentialUse(const opensaml::saml2md::EntityDescriptor* provider) const=0;
98 // caller is borrowing object, must use within scope of config lock
99 virtual const saml::SAMLBrowserProfile* getBrowserProfile() const=0;
100 virtual const saml::SAMLBinding* getBinding(const XMLCh* binding) const=0;
102 // caller is given ownership of object, must use and delete within scope of config lock
103 virtual saml::SAMLBrowserProfile::ArtifactMapper* getArtifactMapper() const=0;
105 // general token validation based on conditions, signatures, etc.
106 virtual void validateToken(
107 saml::SAMLAssertion* token,
109 const opensaml::saml2md::RoleDescriptor* role=NULL,
110 const xmltooling::TrustEngine* trust=NULL
113 // Used to locate a default or designated session initiator for automatic sessions
114 virtual const IHandler* getDefaultSessionInitiator() const=0;
115 virtual const IHandler* getSessionInitiatorById(const char* id) const=0;
117 // Used by session initiators to get endpoint to forward to IdP/WAYF
118 virtual const IHandler* getDefaultAssertionConsumerService() const=0;
119 virtual const IHandler* getAssertionConsumerServiceByIndex(unsigned short index) const=0;
120 virtual saml::Iterator<const IHandler*> getAssertionConsumerServicesByBinding(const XMLCh* binding) const=0;
122 // Used by dispatcher to locate the handler for a request
123 virtual const IHandler* getHandler(const char* path) const=0;
125 virtual ~IApplication() {}
129 * OpenSAML binding hook
131 * Instead of wrapping the binding to deal with mutual authentication, we
132 * just use the HTTP hook functionality offered by OpenSAML. The hook will
133 * register "itself" as a globalCtx pointer with the SAML binding and the caller
134 * will declare and pass the embedded struct as callCtx for use by the hook.
136 class ShibHTTPHook : virtual public saml::SAMLSOAPHTTPBinding::HTTPHook
139 ShibHTTPHook(const xmltooling::TrustEngine* trust, const saml::Iterator<shibboleth::ICredentials*>& creds)
140 : m_trust(trust), m_creds(creds) {}
141 virtual ~ShibHTTPHook() {}
143 // Only hook we need here is for outgoing connection to server.
144 virtual bool outgoing(saml::HTTPClient* conn, void* globalCtx=NULL, void* callCtx=NULL);
146 // Client declares a context object and pass as callCtx to send() method.
147 class ShibHTTPHookCallContext {
149 ShibHTTPHookCallContext(const shibsp::PropertySet* credUse, const opensaml::saml2md::RoleDescriptor* role)
150 : m_credUse(credUse), m_role(role), m_hook(NULL), m_authenticated(false) {}
151 const ShibHTTPHook* getHook() {return m_hook;}
152 const shibsp::PropertySet* getCredentialUse() {return m_credUse;}
153 const opensaml::saml2md::RoleDescriptor* getRoleDescriptor() {return m_role;}
154 bool isAuthenticated() const {return m_authenticated;}
155 void setAuthenticated() {m_authenticated=true;}
158 const shibsp::PropertySet* m_credUse;
159 const opensaml::saml2md::RoleDescriptor* m_role;
160 ShibHTTPHook* m_hook;
161 bool m_authenticated;
162 friend class ShibHTTPHook;
165 const xmltooling::TrustEngine* getTrustEngine() const {return m_trust;}
166 const saml::Iterator<shibboleth::ICredentials*>& getCredentialProviders() const {return m_creds;}
168 const xmltooling::TrustEngine* m_trust;
169 saml::Iterator<shibboleth::ICredentials*> m_creds;
173 * Interface to a cached user session.
175 * Cache entries provide implementations with access to the raw SAML information they
176 * need to publish or provide access to the data for applications to use. All creation
177 * or access to entries is through the ISessionCache interface, and callers must unlock
178 * the entry when finished using it, rather than explicitly freeing them.
180 struct SHIBTARGET_EXPORTS ISessionCacheEntry : public virtual saml::ILockable
182 virtual const char* getClientAddress() const=0;
183 virtual const char* getProviderId() const=0;
184 virtual std::pair<const char*,const saml::SAMLSubject*> getSubject(bool xml=true, bool obj=false) const=0;
185 virtual const char* getAuthnContext() const=0;
186 virtual std::pair<const char*,const saml::SAMLResponse*> getTokens(bool xml=true, bool obj=false) const=0;
187 virtual std::pair<const char*,const saml::SAMLResponse*> getFilteredTokens(bool xml=true, bool obj=false) const=0;
188 virtual ~ISessionCacheEntry() {}
192 * Interface to a sink for session cache events.
194 * All caches support registration of a backing store that can be informed
195 * of significant events in the lifecycle of a cache entry.
197 struct SHIBTARGET_EXPORTS ISessionCacheStore
199 virtual HRESULT onCreate(
201 const IApplication* application,
202 const ISessionCacheEntry* entry,
207 virtual HRESULT onRead(
209 std::string& applicationId,
210 std::string& clientAddress,
211 std::string& providerId,
212 std::string& subject,
213 std::string& authnContext,
220 virtual HRESULT onRead(const char* key, time_t& accessed)=0;
221 virtual HRESULT onRead(const char* key, std::string& tokens)=0;
222 virtual HRESULT onUpdate(const char* key, const char* tokens=NULL, time_t lastAccess=0)=0;
223 virtual HRESULT onDelete(const char* key)=0;
224 virtual ~ISessionCacheStore() {}
228 * Interface to the session cache.
230 * The session cache abstracts a persistent (meaning across requests) cache of
231 * instances of the ISessionCacheEntry interface. Creation of new entries and entry
232 * lookup are confined to this interface to enable implementations to flexibly
233 * remote and/or optimize calls by implementing custom versions of the
234 * ISessionCacheEntry interface as required.
236 struct SHIBTARGET_EXPORTS ISessionCache : public virtual saml::IPlugIn
238 virtual std::string insert(
239 const IApplication* application,
240 const opensaml::saml2md::RoleDescriptor* source,
241 const char* client_addr,
242 const saml::SAMLSubject* subject,
243 const char* authnContext,
244 const saml::SAMLResponse* tokens
246 virtual ISessionCacheEntry* find(
247 const char* key, const IApplication* application, const char* client_addr
250 const char* key, const IApplication* application, const char* client_addr
253 virtual bool setBackingStore(ISessionCacheStore* store)=0;
254 virtual ~ISessionCache() {}
258 * Interface to an access control plugin
260 * Access control plugins return authorization decisions based on the intersection
261 * of the resource request and the active session. They can be implemented through
262 * cross-platform or platform-specific mechanisms.
264 struct SHIBTARGET_EXPORTS IAccessControl : public virtual saml::ILockable, public virtual saml::IPlugIn
266 virtual bool authorized(ShibTarget* st, ISessionCacheEntry* entry) const=0;
267 virtual ~IAccessControl() {}
271 * Interface to a request mapping plugin
273 * Request mapping plugins return configuration settings that apply to resource requests.
274 * They can be implemented through cross-platform or platform-specific mechanisms.
276 struct SHIBTARGET_EXPORTS IRequestMapper : public virtual saml::ILockable, public virtual saml::IPlugIn
278 typedef std::pair<const shibsp::PropertySet*,IAccessControl*> Settings;
279 virtual Settings getSettings(ShibTarget* st) const=0;
280 virtual ~IRequestMapper() {}
283 struct SHIBTARGET_EXPORTS IConfig : public virtual saml::ILockable, public virtual shibsp::PropertySet, public virtual saml::IPlugIn
285 // loads initial configuration
286 virtual void init()=0;
288 virtual shibsp::ListenerService* getListener() const=0;
289 virtual ISessionCache* getSessionCache() const=0;
290 virtual saml::IReplayCache* getReplayCache() const=0;
291 virtual IRequestMapper* getRequestMapper() const=0;
292 virtual const IApplication* getApplication(const char* applicationId) const=0;
293 virtual saml::Iterator<shibboleth::ICredentials*> getCredentialsProviders() const=0;
294 virtual ~IConfig() {}
297 class SHIBTARGET_EXPORTS ShibTargetConfig
300 ShibTargetConfig() : m_ini(NULL) {}
301 virtual ~ShibTargetConfig() {}
303 virtual bool init(const char* schemadir) = 0;
304 virtual bool load(const char* config) = 0;
305 virtual void shutdown() = 0;
307 virtual IConfig* getINI() const {return m_ini;}
309 static ShibTargetConfig& getConfig();
315 class ShibTargetPriv;
316 class SHIBTARGET_EXPORTS ShibTarget {
318 ShibTarget(const IApplication* app);
319 virtual ~ShibTarget(void);
321 // These are defined here so the subclass does not need to specifically
322 // depend on log4cpp. We could use log4cpp::Priority::PriorityLevel
323 // but this is just as easy, IMHO. It's just a case statement in the
324 // implementation to handle the event level.
333 // Note: subclasses MUST implement ALL of these virtual methods
336 // Send a message to the Webserver log
337 virtual void log(ShibLogLevel level, const std::string &msg)=0;
339 void log(ShibLogLevel level, const char* msg) {
344 // Get/Set a cookie for this request
345 virtual std::string getCookies() const=0;
346 virtual void setCookie(const std::string& name, const std::string& value)=0;
347 virtual const char* getCookie(const std::string& name) const;
348 void setCookie(const char* name, const char* value) {
349 std::string ns = name;
350 std::string vs = value;
353 void setCookie(const char* name, const std::string& value) {
354 std::string ns = name;
355 setCookie(ns, value);
358 // Get any URL-encoded arguments or the raw POST body from the server
359 virtual const char* getQueryString() const=0;
360 virtual const char* getRequestBody() const=0;
361 virtual const char* getRequestParameter(const char* param, size_t index=0) const;
363 // Clear a header, set a header
364 // These APIs are used for exporting the Assertions into the
365 // Headers. It will clear some well-known headers first to make
366 // sure none remain. Then it will process the set of assertions
367 // and export them via setHeader().
368 virtual void clearHeader(const std::string& name)=0;
369 virtual void setHeader(const std::string& name, const std::string& value)=0;
370 virtual std::string getHeader(const std::string& name)=0;
371 virtual void setRemoteUser(const std::string& user)=0;
372 virtual std::string getRemoteUser()=0;
374 void clearHeader(const char* n) {
378 void setHeader(const char* n, const char* v) {
383 void setHeader(const std::string& n, const char* v) {
387 void setHeader(const char* n, const std::string& v) {
391 std::string getHeader(const char* n) {
395 void setRemoteUser(const char* n) {
400 // We're done. Finish up. Send specific result content or a redirect.
401 // If there are no headers supplied assume the content-type is text/html
402 typedef std::pair<std::string, std::string> header_t;
403 virtual void* sendPage(
404 const std::string& msg,
406 const std::string& content_type = "text/html",
407 const saml::Iterator<header_t>& headers = EMPTY(header_t)
409 void* sendPage(const char* msg) {
413 virtual void* sendRedirect(const std::string& url)=0;
415 // These next two APIs are used to obtain the module-specific "OK"
416 // and "Decline" results. OK means "we believe that this request
417 // should be accepted". Declined means "we believe that this is
418 // not a shibbolized request so we have no comment".
420 virtual void* returnDecline();
421 virtual void* returnOK();
424 // Note: Subclasses need not implement anything below this line
427 // These functions implement the server-agnostic shibboleth engine
428 // The web server modules implement a subclass and then call into
429 // these methods once they instantiate their request object.
432 // these APIs will always return the result of sendPage(), sendRedirect(),
433 // returnDecline(), or returnOK() in the void* portion of the return code.
434 // Exactly what those values are is module- (subclass-) implementation
435 // specific. The 'bool' part of the return value declares whether the
436 // void* is valid or not. If the bool is true then the void* is valid.
437 // If the bool is false then the API did not call any callback, the void*
438 // is not valid, and the caller should continue processing (the API Call
439 // finished successfully).
441 // The handleProfile argument declares whether doCheckAuthN() should
442 // automatically call doHandlePOST() when it encounters a request for
443 // the ShireURL; if false it will call returnOK() instead.
445 std::pair<bool,void*> doCheckAuthN(bool handler = false);
446 std::pair<bool,void*> doHandler();
447 std::pair<bool,void*> doCheckAuthZ();
448 std::pair<bool,void*> doExportAssertions(bool requireSession = true);
450 // Basic request access in case any plugins need the info
451 virtual const IConfig* getConfig() const;
452 virtual const IApplication* getApplication() const;
453 const char* getRequestMethod() const {return m_method.c_str();}
454 const char* getProtocol() const {return m_protocol.c_str();}
455 const char* getHostname() const {return m_hostname.c_str();}
456 int getPort() const {return m_port;}
457 const char* getRequestURI() const {return m_uri.c_str();}
458 const char* getContentType() const {return m_content_type.c_str();}
459 const char* getRemoteAddr() const {return m_remote_addr.c_str();}
460 const char* getRequestURL() const {return m_url.c_str();}
462 // Advanced methods useful to profile handlers implemented outside core
464 // Get per-application session and state cookie name and properties
465 virtual std::pair<std::string,const char*> getCookieNameProps(const char* prefix) const;
467 // Determine the effective handler URL based on the resource URL
468 virtual std::string getHandlerURL(const char* resource) const;
475 // Initialize the request from the parsed URL
476 // protocol == http, https, etc
477 // hostname == server name
478 // port == server port
479 // uri == resource path
480 // method == GET, POST, etc.
482 const char* protocol,
483 const char* hostname,
486 const char* content_type,
487 const char* remote_addr,
491 std::string m_url, m_method, m_protocol, m_hostname, m_uri, m_content_type, m_remote_addr;
495 mutable ShibTargetPriv* m_priv;
496 friend class ShibTargetPriv;
499 struct SHIBTARGET_EXPORTS XML
501 static const XMLCh SHIBTARGET_NS[];
502 static const XMLCh SHIBTARGET_SCHEMA_ID[];
503 static const XMLCh SAML2ASSERT_NS[];
504 static const XMLCh SAML2ASSERT_SCHEMA_ID[];
505 static const XMLCh SAML2META_NS[];
506 static const XMLCh SAML2META_SCHEMA_ID[];
507 static const XMLCh XMLENC_NS[];
508 static const XMLCh XMLENC_SCHEMA_ID[];
510 // Session cache implementations
511 static const char MemorySessionCacheType[];
512 static const char MySQLSessionCacheType[];
513 static const char ODBCSessionCacheType[];
515 // Replay cache implementations
516 static const char MySQLReplayCacheType[];
517 static const char ODBCReplayCacheType[];
519 // Request mapping/settings implementations
520 static const char XMLRequestMapType[]; // portable XML-based map
521 static const char NativeRequestMapType[]; // Native web server command override of XML-based map
522 static const char LegacyRequestMapType[]; // older designation of XML map, hijacked by web server
524 // Access control implementations
525 static const char htAccessControlType[]; // Apache-specific .htaccess authz module
526 static const char XMLAccessControlType[]; // Proprietary but portable XML authz syntax
528 struct SHIBTARGET_EXPORTS Literals
530 static const XMLCh AAPProvider[];
531 static const XMLCh AccessControl[];
532 static const XMLCh AccessControlProvider[];
533 static const XMLCh acl[];
534 static const XMLCh AND[];
535 static const XMLCh applicationId[];
536 static const XMLCh Application[];
537 static const XMLCh Applications[];
538 static const XMLCh AssertionConsumerService[];
539 static const XMLCh AttributeFactory[];
540 static const XMLCh config[];
541 static const XMLCh CredentialsProvider[];
542 static const XMLCh CredentialUse[];
543 static const XMLCh DiagnosticService[];
544 static const XMLCh echo[];
545 static const XMLCh Extensions[];
546 static const XMLCh fatal[];
547 static const XMLCh FederationProvider[];
548 static const XMLCh Global[];
549 static const XMLCh Host[];
550 static const XMLCh htaccess[];
551 static const XMLCh Implementation[];
552 static const XMLCh index[];
553 static const XMLCh InProcess[];
554 static const XMLCh isDefault[];
555 static const XMLCh Library[];
556 static const XMLCh Listener[];
557 static const XMLCh Local[];
558 static const XMLCh log[];
559 static const XMLCh logger[];
560 static const XMLCh MemorySessionCache[];
561 static const XMLCh MetadataProvider[];
562 static const XMLCh MySQLReplayCache[];
563 static const XMLCh MySQLSessionCache[];
564 static const XMLCh name[];
565 static const XMLCh Name[];
566 static const XMLCh NOT[];
567 static const XMLCh ODBCReplayCache[];
568 static const XMLCh ODBCSessionCache[];
569 static const XMLCh OR[];
570 static const XMLCh OutOfProcess[];
571 static const XMLCh Path[];
572 static const XMLCh path[];
573 static const XMLCh RelyingParty[];
574 static const XMLCh ReplayCache[];
575 static const XMLCh RequestMap[];
576 static const XMLCh RequestMapProvider[];
577 static const XMLCh require[];
578 static const XMLCh Rule[];
579 static const XMLCh SessionCache[];
580 static const XMLCh SessionInitiator[];
581 static const XMLCh SHAR[];
582 static const XMLCh ShibbolethTargetConfig[];
583 static const XMLCh SHIRE[];
584 static const XMLCh Signing[];
585 static const XMLCh SingleLogoutService[];
586 static const XMLCh SPConfig[];
587 static const XMLCh TCPListener[];
588 static const XMLCh TLS[];
589 static const XMLCh TrustProvider[];
590 static const XMLCh type[];
591 static const XMLCh UnixListener[];
596 #endif /* SHIB_TARGET_H */