summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/Provider/EncodableResponse.cs
blob: 47bc1cde085a73f7a507803bf507f994afe22a04 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using DotNetOpenId;
using DotNetOpenId.RelyingParty;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;

namespace DotNetOpenId.Provider {
	[DebuggerDisplay("OpenId: {Protocol.Version}")]
	internal class EncodableResponse : MarshalByRefObject, IEncodable {
		public static EncodableResponse PrepareDirectMessage(Protocol protocol) {
			EncodableResponse response = new EncodableResponse(protocol);
			if (protocol.QueryDeclaredNamespaceVersion != null)
				response.Fields.Add(protocol.openidnp.ns, protocol.QueryDeclaredNamespaceVersion);
			return response;
		}
		public static EncodableResponse PrepareIndirectMessage(Protocol protocol, Uri baseRedirectUrl, string preferredAssociationHandle) {
			EncodableResponse response = new EncodableResponse(protocol, baseRedirectUrl, preferredAssociationHandle);
			if (protocol.QueryDeclaredNamespaceVersion != null)
				response.Fields.Add(protocol.openidnp.ns, protocol.QueryDeclaredNamespaceVersion);
			return response;
		}
		EncodableResponse(Protocol protocol) {
			if (protocol == null) throw new ArgumentNullException("protocol");
			Signed = new List<string>();
			Fields = new Dictionary<string, string>();
			Protocol = protocol;
		}
		EncodableResponse(Protocol protocol, Uri baseRedirectUrl, string preferredAssociationHandle)
			: this(protocol) {
			if (baseRedirectUrl == null) throw new ArgumentNullException("baseRedirectUrl");
			RedirectUrl = baseRedirectUrl;
			PreferredAssociationHandle = preferredAssociationHandle;
		}

		public IDictionary<string, string> Fields { get; private set; }
		public List<string> Signed { get; private set; }
		public Protocol Protocol { get; private set; }
		public bool NeedsSigning { get { return Signed.Count > 0; } }
		public string PreferredAssociationHandle { get; private set; }

		#region IEncodable Members

		public EncodingType EncodingType { 
			get { return RedirectUrl != null ? EncodingType.IndirectMessage : EncodingType.DirectResponse; }
		}

		public IDictionary<string, string> EncodedFields {
			get {
				var nvc = new Dictionary<string, string>();

				foreach (var pair in Fields) {
					if (EncodingType == EncodingType.IndirectMessage) {
						nvc.Add(Protocol.openid.Prefix + pair.Key, pair.Value);
					} else {
						nvc.Add(pair.Key, pair.Value);
					}
				}

				return nvc;
			}
		}
		
		public Uri RedirectUrl { get; set; }

		#endregion

		public override string ToString() {
			string returnString = string.Format(CultureInfo.CurrentCulture,
				"Response.NeedsSigning = {0}", this.NeedsSigning);
			foreach (string key in Fields.Keys) {
				returnString += Environment.NewLine + string.Format(CultureInfo.CurrentCulture,
					"ResponseField[{0}] = '{1}'", key, Fields[key]);
			}
			return returnString;
		}
	}
}