diff --git a/config-templates/authsources.php b/config-templates/authsources.php
index a1ffb94c2f59546fd555c54324601d8bf2ce3dcf..0d76a862d69c84337a95171b7ee237e95ca494c0 100644
--- a/config-templates/authsources.php
+++ b/config-templates/authsources.php
@@ -99,6 +99,8 @@ $config = array(
 		'openid:OpenIDConsumer',
 		// Googles OpenID endpoint.
 		'target' => 'https://www.google.com/accounts/o8/id',
+		// Custom realm
+		// 'realm' => 'http://*.example.org',
 		// Attributes that google can supply.
 		'attributes.ax_required' => array(
 			//'http://axschema.org/namePerson/first',
diff --git a/modules/openid/lib/Auth/Source/OpenIDConsumer.php b/modules/openid/lib/Auth/Source/OpenIDConsumer.php
index 929bbcf82f1ab154cb56c1e5f4d0e49d3dcb19ec..903f7d90aaeac9f033de70bc5fc80680ef3f1935 100644
--- a/modules/openid/lib/Auth/Source/OpenIDConsumer.php
+++ b/modules/openid/lib/Auth/Source/OpenIDConsumer.php
@@ -31,6 +31,12 @@ class sspmod_openid_Auth_Source_OpenIDConsumer extends SimpleSAML_Auth_Source {
 	 */
 	private $target;
 
+	/**
+	 * Custom realm to use.
+	 *
+	 * @var string|NULL
+	 */
+	private $realm;
 
 	/**
 	 * List of optional attributes.
@@ -65,6 +71,7 @@ class sspmod_openid_Auth_Source_OpenIDConsumer extends SimpleSAML_Auth_Source {
 			'Authentication source ' . var_export($this->authId, TRUE));
 
 		$this->target = $cfgParse->getString('target', NULL);
+		$this->realm = $cfgParse->getString('realm', NULL);
 
 		$this->optionalAttributes = $cfgParse->getArray('attributes.optional', array());
 		$this->requiredAttributes = $cfgParse->getArray('attributes.required', array());
@@ -132,7 +139,11 @@ class sspmod_openid_Auth_Source_OpenIDConsumer extends SimpleSAML_Auth_Source {
 	 * @return string  The trust root.
 	 */
 	private function getTrustRoot() {
-		return SimpleSAML_Utilities::selfURLhost();
+		if (!empty($this->realm)) {
+			return $this->realm;
+		} else {
+			return SimpleSAML_Utilities::selfURLhost();
+		}
 	}