From b867370ceae2a2152a6ae2a7c57a45f35f824586 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 14 Oct 2009 07:44:24 +0000
Subject: [PATCH] SimpleSAML_Auth_Simple: Add getLoginURL().

This patch adds a function to retrieve a login URL that can be used
to log the user in with the authentication source.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1851 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Auth/Simple.php | 25 +++++++++++++++++++++++++
 modules/core/www/as_login.php  | 23 +++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 modules/core/www/as_login.php

diff --git a/lib/SimpleSAML/Auth/Simple.php b/lib/SimpleSAML/Auth/Simple.php
index 156ce1f93..d426c7ee7 100644
--- a/lib/SimpleSAML/Auth/Simple.php
+++ b/lib/SimpleSAML/Auth/Simple.php
@@ -137,6 +137,31 @@ class SimpleSAML_Auth_Simple {
 		return $session->getAttributes();
 	}
 
+
+	/**
+	 * Retrieve an URL that can be used to log the user in.
+	 *
+	 * @param string|NULL $returnTo
+	 *   The page the user should be returned to afterwards. If this parameter
+	 *   is NULL, the user will be returned to the current page.
+	 * @return string
+	 *   An URL which is suitable for use in link-elements.
+	 */
+	public function getLoginURL($returnTo = NULL) {
+
+		if ($returnTo === NULL) {
+			$returnTo = SimpleSAML_Utilities::selfURL();
+		}
+
+		$login = SimpleSAML_Module::getModuleURL('core/as_login.php');
+		$login = SimpleSAML_Utilities::addURLparameter($login, array(
+			'AuthId' => $this->authSource,
+			'ReturnTo' => $returnTo,
+		));
+
+		return $login;
+	}
+
 }
 
 ?>
\ No newline at end of file
diff --git a/modules/core/www/as_login.php b/modules/core/www/as_login.php
new file mode 100644
index 000000000..5a02bdbfe
--- /dev/null
+++ b/modules/core/www/as_login.php
@@ -0,0 +1,23 @@
+<?php
+
+/**
+ * Endpoint for logging in with an authentication source.
+ *
+ * @package simpleSAMLphp
+ * @version $Id$
+ */
+
+if (!is_string($_REQUEST['ReturnTo'])) {
+	throw new SimpleSAML_Error_BadRequest('Missing ReturnTo parameter.');
+}
+
+if (!is_string($_REQUEST['AuthId'])) {
+	throw new SimpleSAML_Error_BadRequest('Missing AuthId parameter.');
+}
+
+$as = new SimpleSAML_Auth_Simple($_REQUEST['AuthId']);
+$as->requireAuth(array(
+	'url' => $_REQUEST['ReturnTo'],
+));
+
+SimpleSAML_Utilities::redirect($_REQUEST['ReturnTo']);
-- 
GitLab