From cee33e80873008e370bb1e31a64a202c5373b5f0 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Tue, 8 Feb 2011 13:50:03 +0000
Subject: [PATCH] Auth_State: Add getStateId function.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2732 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Auth/State.php | 48 ++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/lib/SimpleSAML/Auth/State.php b/lib/SimpleSAML/Auth/State.php
index 681c973d7..6672ebec7 100644
--- a/lib/SimpleSAML/Auth/State.php
+++ b/lib/SimpleSAML/Auth/State.php
@@ -79,6 +79,35 @@ class SimpleSAML_Auth_State {
 	const EXCEPTION_PARAM = 'SimpleSAML_Auth_State_exceptionId';
 
 
+	/**
+	 * Retrieve the ID of a state array.
+	 *
+	 * Note that this function will not save the state.
+	 *
+	 * @param array &$state  The state array.
+	 * @param bool $rawId  Return a raw ID, without a restart URL. Defaults to FALSE.
+	 * @return string  Identifier which can be used to retrieve the state later.
+	 */
+	public static function getStateId(&$state, $rawId = FALSE) {
+		assert('is_array($state)');
+		assert('is_bool($rawId)');
+
+		if (!array_key_exists(self::ID, $state)) {
+			$state[self::ID] = SimpleSAML_Utilities::generateID();
+		}
+
+		$id = $state[self::ID];
+
+		if ($rawId || !array_key_exists(self::RESTART, $state)) {
+			/* Either raw ID or no restart URL. In any case, return the raw ID. */
+			return $id;
+		}
+
+		/* We have a restart URL. Return the ID with that URL. */
+		return $id . ':' . $state[self::RESTART];
+	}
+
+
 	/**
 	 * Save the state.
 	 *
@@ -95,25 +124,14 @@ class SimpleSAML_Auth_State {
 		assert('is_string($stage)');
 		assert('is_bool($rawId)');
 
-		/* Save stage. */
-		$state[self::STAGE] = $stage;
-
-		if (!array_key_exists(self::ID, $state)) {
-			$state[self::ID] = SimpleSAML_Utilities::generateID();
-		}
-
+		$return = self::getStateId($state, $rawId);
 		$id = $state[self::ID];
 
-		/* Embed the restart URL in the state identifier, if it is available. */
-		if (array_key_exists(self::RESTART, $state) && !$rawId) {
-			assert('is_string($state[self::RESTART])');
-			$return = $id . ':' . $state[self::RESTART];
-		} else {
-			$return = $id;
-		}
+		/* Save stage. */
+		$state[self::STAGE] = $stage;
 
+		/* Save state. */
 		$serializedState = serialize($state);
-
 		$session = SimpleSAML_Session::getInstance();
 		$session->setData('SimpleSAML_Auth_State', $id, $serializedState, 60*60);
 
-- 
GitLab