summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/Yadis/XrdElement.cs
blob: 5c0ba4464364cbd0045fe1bb2a30072d68adf0ef (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using DotNetOpenId.RelyingParty;

namespace DotNetOpenId.Yadis {
	class XrdElement : XrdsNode {
		public XrdElement(XPathNavigator xrdElement, XrdsDocument parent) :
			base(xrdElement, parent) {
		}

		public IEnumerable<ServiceElement> Services {
			get {
				// We should enumerate them in priority order
				List<ServiceElement> services = new List<ServiceElement>();
				foreach (XPathNavigator node in Node.Select("xrd:Service", XmlNamespaceResolver)) {
					services.Add(new ServiceElement(node.Clone(), this));
				}
				services.Sort();
				return services;
			}
		}


		int XriResolutionStatusCode {
			get {
				var n = Node.SelectSingleNode("xrd:Status", XmlNamespaceResolver);
				string codeString;
				if (n == null || string.IsNullOrEmpty(codeString = n.GetAttribute("code", ""))) {
					throw new OpenIdException(Strings.XriResolutionStatusMissing);
				}
				int code;
				if (!int.TryParse(codeString, out code) || code < 100 || code > 399) {
					throw new OpenIdException(Strings.XriResolutionStatusMissing);
				}
				return code;
			}
		}

		public bool IsXriResolutionSuccessful {
			get {
				return XriResolutionStatusCode == 100;
			}
		}

		public string CanonicalID {
			get {
				var n = Node.SelectSingleNode("xrd:CanonicalID", XmlNamespaceResolver);
				return n != null ? n.Value : null;
			}
		}

		public bool IsCanonicalIdVerified {
			get {
				var n = Node.SelectSingleNode("xrd:Status", XmlNamespaceResolver);
				return n != null && string.Equals(n.GetAttribute("cid", ""), "verified", StringComparison.Ordinal);
			}
		}

		IEnumerable<ServiceElement> searchForServiceTypeUris(Util.Func<Protocol, string> p) {
			var xpath = new StringBuilder();
			xpath.Append("xrd:Service[");
			foreach (var protocol in Protocol.AllVersions) {
				string typeUri = p(protocol);
				if (typeUri == null) continue;
				xpath.Append("xrd:Type/text()='");
				xpath.Append(typeUri);
				xpath.Append("' or ");
			}
			xpath.Length -= 4;
			xpath.Append("]");
			var services = new List<ServiceElement>();
			foreach (XPathNavigator service in Node.Select(xpath.ToString(), XmlNamespaceResolver)) {
				services.Add(new ServiceElement(service.Clone(), this));
			}
			// Put the services in their own defined priority order
			services.Sort();
			return services;
		}

		/// <summary>
		/// Returns services for OP Identifiers.
		/// </summary>
		public IEnumerable<ServiceElement> OpenIdProviderIdentifierServices {
			get { return searchForServiceTypeUris(p => p.OPIdentifierServiceTypeURI); }
		}

		/// <summary>
		/// Returns services for Claimed Identifiers.
		/// </summary>
		public IEnumerable<ServiceElement> OpenIdClaimedIdentifierServices {
			get { return searchForServiceTypeUris(p => p.ClaimedIdentifierServiceTypeURI); }
		}

		public IEnumerable<ServiceElement> OpenIdRelyingPartyReturnToServices {
			get { return searchForServiceTypeUris(p => p.RPReturnToTypeURI); }
		}

		/// <summary>
		/// An enumeration of all Service/URI elements, sorted in priority order.
		/// </summary>
		public IEnumerable<UriElement> ServiceUris {
			get {
				foreach (ServiceElement service in Services) {
					foreach (UriElement uri in service.UriElements) {
						yield return uri;
					}
				}
			}
		}
	}
}