summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/Extensions/AttributeExchange/StoreResponse.cs
blob: acc0d6b40210791f3b2bdc1d801c6d204cbaf240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Text;
using DotNetOpenId.RelyingParty;
using System.Globalization;
using System.Diagnostics;

namespace DotNetOpenId.Extensions.AttributeExchange {
	/// <summary>
	/// The Attribute Exchange Store message, response leg.
	/// </summary>
	public sealed class StoreResponse : IExtensionResponse {
		const string SuccessMode = "store_response_success";
		const string FailureMode = "store_response_failure";

		/// <summary>
		/// Whether the storage request succeeded.
		/// </summary>
		public bool Succeeded { get; set; }
		/// <summary>
		/// The reason for the failure.
		/// </summary>
		public string FailureReason { get; set; }

		#region IExtensionResponse Members
		string IExtension.TypeUri { get { return Constants.TypeUri; } }
		IEnumerable<string> IExtension.AdditionalSupportedTypeUris {
			get { return new string[0]; }
		}

		IDictionary<string, string> IExtensionResponse.Serialize(Provider.IRequest authenticationRequest) {
			var fields = new Dictionary<string, string> {
				{ "mode", Succeeded ? SuccessMode : FailureMode },
			};
			if (!Succeeded && !string.IsNullOrEmpty(FailureReason))
				fields.Add("error", FailureReason);

			return fields;
		}

		bool IExtensionResponse.Deserialize(IDictionary<string, string> fields, IAuthenticationResponse response, string typeUri) {
			if (fields == null) return false;
			string mode;
			if (!fields.TryGetValue("mode", out mode)) return false;
			switch (mode) {
				case SuccessMode:
					Succeeded = true;
					break;
				case FailureMode:
					Succeeded = false;
					break;
				default:
					return false;
			}

			if (!Succeeded) {
				string error;
				if (fields.TryGetValue("error", out error))
					FailureReason = error;
			}

			return true;
		}

		#endregion
	}
}