summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OpenId.RelyingParty.UI/OpenId/RelyingParty/OpenIdRelyingPartyControlBase.js
blob: 5e9dd29c9b9143e3b03c8911dd34c583469e1172 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//-----------------------------------------------------------------------
// <copyright file="OpenIdRelyingPartyControlBase.js" company="Outercurve Foundation>
//     Copyright (c) Outercurve Foundation. All rights reserved.
//     This file may be used and redistributed under the terms of the
//     Microsoft Public License (Ms-PL) http://opensource.org/licenses/ms-pl.html
// </copyright>
//-----------------------------------------------------------------------

// Options that can be set on the host page:
//window.openid_visible_iframe = true; // causes the hidden iframe to show up
//window.openid_trace = true; // causes lots of messages

trace = function(msg, color) {
	if (window.openid_trace) {
		if (!window.openid_tracediv) {
			window.openid_tracediv = document.createElement("ol");
			document.body.appendChild(window.openid_tracediv);
		}
		var el = document.createElement("li");
		if (color) { el.style.color = color; }
		el.appendChild(document.createTextNode(msg));
		window.openid_tracediv.appendChild(el);
		//alert(msg);
	}
};

if (window.dnoa_internal === undefined) {
	window.dnoa_internal = {};
}

/// <summary>Instantiates an object that provides string manipulation services for URIs.</summary>
window.dnoa_internal.Uri = function(url) {
	this.originalUri = url.toString();

	this.toString = function() {
		return this.originalUri;
	};

	this.getAuthority = function() {
		var authority = this.getScheme() + "://" + this.getHost();
		return authority;
	};

	this.getHost = function() {
		var hostStartIdx = this.originalUri.indexOf("://") + 3;
		var hostEndIndex = this.originalUri.indexOf("/", hostStartIdx);
		if (hostEndIndex < 0) { hostEndIndex = this.originalUri.length; }
		var host = this.originalUri.substr(hostStartIdx, hostEndIndex - hostStartIdx);
		return host;
	};

	this.getScheme = function() {
		var schemeStartIdx = this.indexOf("://");
		return this.originalUri.substr(this.originalUri, schemeStartIdx);
	};

	this.trimFragment = function() {
		var hashmark = this.originalUri.indexOf('#');
		if (hashmark >= 0) {
			return new window.dnoa_internal.Uri(this.originalUri.substr(0, hashmark));
		}
		return this;
	};

	this.appendQueryVariable = function(name, value) {
		var pair = encodeURI(name) + "=" + encodeURI(value);
		if (this.originalUri.indexOf('?') >= 0) {
			this.originalUri = this.originalUri + "&" + pair;
		} else {
			this.originalUri = this.originalUri + "?" + pair;
		}
	};

	function KeyValuePair(key, value) {
		this.key = key;
		this.value = value;
	}

	this.pairs = [];

	var queryBeginsAt = this.originalUri.indexOf('?');
	if (queryBeginsAt >= 0) {
		this.queryString = this.originalUri.substr(queryBeginsAt + 1);
		var queryStringPairs = this.queryString.split('&');

		for (var i = 0; i < queryStringPairs.length; i++) {
			var equalsAt = queryStringPairs[i].indexOf('=');
			left = (equalsAt >= 0) ? queryStringPairs[i].substring(0, equalsAt) : null;
			right = (equalsAt >= 0) ? queryStringPairs[i].substring(equalsAt + 1) : queryStringPairs[i];
			this.pairs.push(new KeyValuePair(unescape(left), unescape(right)));
		}
	}

	this.getQueryArgValue = function(key) {
		for (var i = 0; i < this.pairs.length; i++) {
			if (this.pairs[i].key == key) {
				return this.pairs[i].value;
			}
		}
	};

	this.getPairs = function() {
		return this.pairs;
	};

	this.containsQueryArg = function(key) {
		return this.getQueryArgValue(key);
	};

	this.getUriWithoutQueryOrFragement = function() {
		var queryBeginsAt = this.originalUri.indexOf('?');
		if (queryBeginsAt >= 0) {
			return this.originalUri.substring(0, queryBeginsAt);
		} else {
			var fragmentBeginsAt = this.originalUri.indexOf('#');
			if (fragmentBeginsAt >= 0) {
				return this.originalUri.substring(0, fragmentBeginsAt);
			} else {
				return this.originalUri;
			}
		}
	};

	this.indexOf = function(args) {
		return this.originalUri.indexOf(args);
	};

	return this;
};

/// <summary>Creates a hidden iframe.</summary>
window.dnoa_internal.createHiddenIFrame = function() {
	var iframe = document.createElement("iframe");
	if (!window.openid_visible_iframe) {
		iframe.setAttribute("width", 0);
		iframe.setAttribute("height", 0);
		iframe.setAttribute("style", "display: none");
		iframe.setAttribute("border", 0);
	}

	return iframe;
};

/// <summary>Redirects the current window/frame to the given URI, 
/// either using a GET or a POST as required by the length of the URL.</summary>
window.dnoa_internal.GetOrPost = function(uri) {
	var maxGetLength = 2 * 1024; // keep in sync with DotNetOpenAuth.Messaging.Channel.IndirectMessageGetToPostThreshold
	uri = new window.dnoa_internal.Uri(uri);

	if (uri.toString().length <= maxGetLength) {
		window.location = uri.toString();
	} else {
		trace("Preparing to POST: " + uri.toString());
		var iframe = window.dnoa_internal.createHiddenIFrame();
		document.body.appendChild(iframe);
		var doc = iframe.ownerDocument;
		var form = doc.createElement('form');
		form.action = uri.getUriWithoutQueryOrFragement();
		form.method = "POST";
		form.target = "_top";
		for (var i = 0; i < uri.getPairs().length; i++) {
			var input = doc.createElement('input');
			input.type = 'hidden';
			input.name = uri.getPairs()[i].key;
			input.value = uri.getPairs()[i].value;
			trace(input.name + " = " + input.value);
			form.appendChild(input);
		}
		doc.body.appendChild(form);
		form.submit();
	}
};