Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MacProxyCredentialProvider.cs « MacPlatform « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 423d92b043417efc7bfdf861862259aead911f16 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// MacProxyCredentialsProvider.cs
//
// Author:
//       Bojan Rajkovic <bojan.rajkovic@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Net;

using MonoDevelop.Core;
using MonoDevelop.Core.Web;
using MonoDevelop.Ide;

using AppKit;
using CoreGraphics;
using Foundation;
using MonoDevelop.MacInterop;
using Security;

namespace MonoDevelop.MacIntegration
{
	class MacProxyCredentialProvider : ICredentialProvider
	{
		static nint NSAlertFirstButtonReturn = 1000;
		object guiLock = new object();

		public ICredentials GetCredentials (Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
		{
			// if looking for proxy credentials, we care about the proxy's URL, not the request URL
			if (credentialType == CredentialType.ProxyCredentials) {
				var proxyUri = proxy.GetProxy (uri);
				if (proxyUri != null)
					uri = proxyUri;
			}

			lock (guiLock) {
				// If this is the first attempt, return any stored credentials. If they fail, we'll be called again.
				if (!retrying) {
					var creds = GetExistingCredentials (uri, credentialType);
					if (creds != null)
						return creds;
				}

				return GetCredentialsFromUser (uri, proxy, credentialType);
			}
		}

		static ICredentials GetSystemProxyCredentials (Uri uri)
		{
			var kind = SecProtocol.Http;
			if (uri.Scheme == "http")
				kind = SecProtocol.HttpProxy;
			else if (uri.Scheme == "https")
				kind = SecProtocol.HttpsProxy;

			//TODO: get username from SystemConfiguration APIs so we don't trigger a double auth prompt
			var existing = Keychain.FindInternetUserNameAndPassword (uri, kind);
			if (existing != null && existing.Item1 != null && existing.Item2 != null)
				return new NetworkCredential (existing.Item1, existing.Item2);

			return null;
		}

		static ICredentials GetExistingCredentials (Uri uri, CredentialType credentialType)
		{
			if (credentialType == CredentialType.ProxyCredentials) {
				var proxyCreds = GetSystemProxyCredentials (uri);
				if (proxyCreds != null)
					return proxyCreds;
			}

			var rootUri = new Uri (uri.GetComponents (UriComponents.SchemeAndServer, UriFormat.SafeUnescaped));
			var existing =
				Keychain.FindInternetUserNameAndPassword (uri) ??
				Keychain.FindInternetUserNameAndPassword (rootUri);

			return existing != null ? new NetworkCredential (existing.Item1, existing.Item2) : null;
		}

		static ICredentials GetCredentialsFromUser (Uri uri, IWebProxy proxy, CredentialType credentialType)
		{
			NetworkCredential result = null;

			Runtime.RunInMainThread (() => {

				using (var ns = new NSAutoreleasePool ()) {
					var message = credentialType == CredentialType.ProxyCredentials
						? GettextCatalog.GetString (
							"{0} needs credentials to access the proxy server {1}.",
							BrandingService.ApplicationName,
							uri.Host
						)
						: GettextCatalog.GetString (
							"{0} needs credentials to access {1}.",
							BrandingService.ApplicationName,
							uri.Host
						);

					using (var alert = new NSAlert ()) {
						alert.MessageText = GettextCatalog.GetString ("Credentials Required");
						alert.InformativeText = message;

						var okButton = alert.AddButton (GettextCatalog.GetString ("OK"));
						var cancelButton = alert.AddButton (GettextCatalog.GetString ("Cancel"));

						alert.Icon = MacPlatformService.ApplicationIcon;

						var view = new NSView (new CGRect (0, 0, 313, 91));

						var usernameLabel = new NSTextField (new CGRect (17, 55, 71, 17)) {
							Identifier = "usernameLabel",
							StringValue = "Username:",
							Alignment = NSTextAlignment.Right,
							Editable = false,
							Bordered = false,
							DrawsBackground = false,
							Bezeled = false,
							Selectable = false,
						};
						view.AddSubview (usernameLabel);

						var usernameInput = new NSTextField (new CGRect (93, 52, 200, 22));
						view.AddSubview (usernameInput);

						var passwordLabel = new NSTextField (new CGRect (22, 23, 66, 17)) {
							StringValue = "Password:",
							Alignment = NSTextAlignment.Right,
							Editable = false,
							Bordered = false,
							DrawsBackground = false,
							Bezeled = false,
							Selectable = false,
						};
						view.AddSubview (passwordLabel);

						var passwordInput = new NSSecureTextField (new CGRect (93, 20, 200, 22));
						view.AddSubview (passwordInput);

						using (var alertDelegate = new PasswordAlertWindowDelegate (usernameInput, passwordInput, cancelButton, okButton)) {
							alert.AccessoryView = view;
							MonoDevelop.Components.IdeTheme.ApplyTheme (alert.Window);
							alert.Window.WeakDelegate = alertDelegate;
							alert.Window.InitialFirstResponder = usernameInput;
							alert.Window.ReleasedWhenClosed = true;
							if (alert.RunModal () == NSAlertFirstButtonReturn) {
								var username = usernameInput.StringValue;
								var password = passwordInput.StringValue;
								result = new NetworkCredential (username, password);
							}
							alert.Window.Close ();
						}
					}
				}
			}).Wait ();

			// store the obtained credentials in the keychain
			// but don't store for the root url since it may have other credentials
			if (result != null)
				Keychain.AddInternetPassword (uri, result.UserName, result.Password);

			return result;
		}

		class PasswordAlertWindowDelegate : NSWindowDelegate
		{
			readonly WeakReference<NSTextField> weakUsernameInput;
			readonly WeakReference<NSTextField> weakPasswordInput;
			readonly WeakReference<NSButton> weakCancelButton;
			readonly WeakReference<NSButton> weakOkButton;

			public PasswordAlertWindowDelegate (NSTextField usernameInput, NSTextField passwordInput, NSButton cancelButton, NSButton okButton)
			{
				weakUsernameInput = new WeakReference<NSTextField> (usernameInput);
				weakPasswordInput = new WeakReference<NSTextField> (passwordInput);
				weakCancelButton = new WeakReference<NSButton> (cancelButton);
				weakOkButton = new WeakReference<NSButton> (okButton);
			}

			public override void DidBecomeKey (NSNotification notification)
			{
				if (!weakUsernameInput.TryGetTarget (out var usernameInput) ||
					!weakPasswordInput.TryGetTarget (out var passwordInput) ||
					!weakCancelButton.TryGetTarget (out var cancelButton) ||
					!weakOkButton.TryGetTarget (out var okButton))
					return;
				// The NSAlert defines the keyviewloop after it is displayed so the tab order is defined
				// here otherwise once the focus is on the OK and Cancel buttons it is not possible to tab
				// to the username and password NSTextFields.
				usernameInput.NextKeyView = passwordInput;
				passwordInput.NextKeyView = cancelButton;
				okButton.NextKeyView = usernameInput;
			}
		}
	}
}