From d56cd5bf338e962d40f6504f9a4210c084d455f3 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 7 Oct 2009 09:12:01 +0000
Subject: [PATCH] Add autotest module.

This module provides some webpages that can be used to test
authentication sources.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1827 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/autotest/default-disable    |  3 +++
 modules/autotest/docs/test.txt      | 18 +++++++++++++++++
 modules/autotest/www/attributes.php | 31 +++++++++++++++++++++++++++++
 modules/autotest/www/login.php      | 20 +++++++++++++++++++
 modules/autotest/www/logout.php     | 24 ++++++++++++++++++++++
 5 files changed, 96 insertions(+)
 create mode 100644 modules/autotest/default-disable
 create mode 100644 modules/autotest/docs/test.txt
 create mode 100644 modules/autotest/www/attributes.php
 create mode 100644 modules/autotest/www/login.php
 create mode 100644 modules/autotest/www/logout.php

diff --git a/modules/autotest/default-disable b/modules/autotest/default-disable
new file mode 100644
index 000000000..fa0bd82e2
--- /dev/null
+++ b/modules/autotest/default-disable
@@ -0,0 +1,3 @@
+This file indicates that the default state of this module
+is disabled. To enable, create a file named enable in the
+same directory as this file.
diff --git a/modules/autotest/docs/test.txt b/modules/autotest/docs/test.txt
new file mode 100644
index 000000000..34d9ea403
--- /dev/null
+++ b/modules/autotest/docs/test.txt
@@ -0,0 +1,18 @@
+`autotest` module
+=================
+
+This module provides an interface to do automatic testing of authentication sources.
+To enable it, create a file named "enable" in modules/autotest.
+
+It provides three webpages:
+- https://.../simplesaml/module.php/autotest/login.php
+- https://.../simplesaml/module.php/autotest/logout.php
+- https://.../simplesaml/module.php/autotest/attributes.php
+
+All the webpages have a mandatory paremeter 'SourceID', which is the name of the authentication source.
+
+On success, the web pages print a single line with "OK".
+The attributes page will also list all the attributes of the user.
+On error they set the HTTP status code to 500 Internal Server Error, print a line with "ERROR" and then any information about the error.
+
+Note that you still have to parse the login pages to extract the parameters in the login form.
diff --git a/modules/autotest/www/attributes.php b/modules/autotest/www/attributes.php
new file mode 100644
index 000000000..e50a47473
--- /dev/null
+++ b/modules/autotest/www/attributes.php
@@ -0,0 +1,31 @@
+<?php
+
+try {
+	if (!isset($_GET['SourceID'])) {
+		throw new SimpleSAML_Error_BadRequest('Missing SourceID parameter');
+	}
+	$sourceId = $_GET['SourceID'];
+
+	$as = new SimpleSAML_Auth_Simple($sourceId);
+
+	if (!$as->isAuthenticated()) {
+		throw new SimpleSAML_Error_Exception('Not authenticated.');
+	}
+
+	$attributes = $as->getAttributes();
+
+	header('Content-Type: text/plain; charset=utf-8');
+	echo("OK\n");
+	foreach ($attributes as $name => $values) {
+		echo("$name\n");
+		foreach ($values as $value) {
+			echo("\t$value\n");
+		}
+	}
+
+} catch (Exception $e) {
+	header('HTTP/1.0 500 Internal Server Error');
+	header('Content-Type: text/plain; charset=utf-8');
+	echo("ERROR\n");
+	echo($e->getMessage() . "\n");
+}
diff --git a/modules/autotest/www/login.php b/modules/autotest/www/login.php
new file mode 100644
index 000000000..da26db51e
--- /dev/null
+++ b/modules/autotest/www/login.php
@@ -0,0 +1,20 @@
+<?php
+
+try {
+	if (!isset($_GET['SourceID'])) {
+		throw new SimpleSAML_Error_BadRequest('Missing SourceID parameter');
+	}
+	$sourceId = $_GET['SourceID'];
+
+	$as = new SimpleSAML_Auth_Simple($sourceId);
+
+	$as->requireAuth();
+
+	header('Content-Type: text/plain; charset=utf-8');
+	echo("OK\n");
+} catch (Exception $e) {
+	header('HTTP/1.0 500 Internal Server Error');
+	header('Content-Type: text/plain; charset=utf-8');
+	echo("ERROR\n");
+	echo($e->getMessage() . "\n");
+}
diff --git a/modules/autotest/www/logout.php b/modules/autotest/www/logout.php
new file mode 100644
index 000000000..81f50d758
--- /dev/null
+++ b/modules/autotest/www/logout.php
@@ -0,0 +1,24 @@
+<?php
+
+try {
+	if (!isset($_GET['SourceID'])) {
+		throw new SimpleSAML_Error_BadRequest('Missing SourceID parameter');
+	}
+	$sourceId = $_GET['SourceID'];
+
+	$as = new SimpleSAML_Auth_Simple($sourceId);
+
+	if ($as->isAuthenticated()) {
+		$as->logout();
+	}
+
+
+	header('Content-Type: text/plain; charset=utf-8');
+	echo("OK\n");
+
+} catch (Exception $e) {
+	header('HTTP/1.0 500 Internal Server Error');
+	header('Content-Type: text/plain; charset=utf-8');
+	echo("ERROR\n");
+	echo($e->getMessage() . "\n");
+}
-- 
GitLab