Merge lp:~mandel/ubuntuone-windows-installer/refactor_sso into lp:ubuntuone-windows-installer/beta
- refactor_sso
- Merge into beta
Proposed by
Manuel de la Peña
Status: | Merged |
---|---|
Approved by: | Rick McBride |
Approved revision: | 88 |
Merged at revision: | 90 |
Proposed branch: | lp:~mandel/ubuntuone-windows-installer/refactor_sso |
Merge into: | lp:ubuntuone-windows-installer/beta |
Prerequisite: | lp:~mandel/ubuntuone-windows-installer/re_enable_keyring_tests |
Diff against target: |
2731 lines (+2118/-397) 21 files modified
src/Canonical.Ubuntu.SSO.Tests/Canonical.Ubuntu.SSO.Tests.csproj (+7/-0) src/Canonical.Ubuntu.SSO.Tests/ColonSeparatedSSOCredentialsEncoderFixture.cs (+69/-0) src/Canonical.Ubuntu.SSO.Tests/JsonSSOCredentialsEncoderFixture.cs (+29/-0) src/Canonical.Ubuntu.SSO.Tests/SSOLoginProcessorFixture.cs (+98/-0) src/Canonical.Ubuntu.SSO/Canonical.Ubuntu.SSO.csproj (+11/-1) src/Canonical.Ubuntu.SSO/ColonSeparatedSSOCredentialsEncoder.cs (+60/-0) src/Canonical.Ubuntu.SSO/DPAPIDataProtector.cs (+0/-1) src/Canonical.Ubuntu.SSO/IOAuth.cs (+122/-0) src/Canonical.Ubuntu.SSO/ISSOCredentialsEncoder.cs (+46/-0) src/Canonical.Ubuntu.SSO/JsonSSOCredentialsEncoder.cs (+82/-0) src/Canonical.Ubuntu.SSO/OAuth.cs (+391/-0) src/Canonical.Ubuntu.SSO/OAuthBase.cs (+0/-358) src/Canonical.Ubuntu.SSO/SSOException.cs (+57/-0) src/Canonical.Ubuntu.SSO/SSOLoginException.cs (+57/-0) src/Canonical.Ubuntu.SSO/SSOLoginProcessor.cs (+154/-1) src/Canonical.UbuntuOne.Client/Notification/NotificationIconPresenter.cs (+25/-36) src/Canonical.UbuntuOne.Common/Canonical.UbuntuOne.Common.csproj (+4/-0) src/Canonical.UbuntuOne.Common/Net/HttpWebRequest.cs (+505/-0) src/Canonical.UbuntuOne.Common/Net/HttpWebRequestFactory.cs (+59/-0) src/Canonical.UbuntuOne.Common/Net/IHttpWebRequest.cs (+297/-0) src/Canonical.UbuntuOne.Common/Net/IHttpWebRequestFactory.cs (+45/-0) |
To merge this branch: | bzr merge lp:~mandel/ubuntuone-windows-installer/refactor_sso |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Rick McBride (community) | Approve | ||
John Lenton (community) | Approve | ||
Review via email:
|
Commit message
Description of the change
SSO has been refactored to make it more testeable and robust.
To post a comment you must log in.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
John Lenton (chipaca) : | # |
review:
Approve
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Rick McBride (rmcbride) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/Canonical.Ubuntu.SSO.Tests/Canonical.Ubuntu.SSO.Tests.csproj' | |||
2 | --- src/Canonical.Ubuntu.SSO.Tests/Canonical.Ubuntu.SSO.Tests.csproj 2010-10-05 09:58:34 +0000 | |||
3 | +++ src/Canonical.Ubuntu.SSO.Tests/Canonical.Ubuntu.SSO.Tests.csproj 2010-10-05 09:58:35 +0000 | |||
4 | @@ -48,14 +48,21 @@ | |||
5 | 48 | <Reference Include="System.Xml" /> | 48 | <Reference Include="System.Xml" /> |
6 | 49 | </ItemGroup> | 49 | </ItemGroup> |
7 | 50 | <ItemGroup> | 50 | <ItemGroup> |
8 | 51 | <Compile Include="ColonSeparatedSSOCredentialsEncoderFixture.cs" /> | ||
9 | 52 | <Compile Include="JsonSSOCredentialsEncoderFixture.cs" /> | ||
10 | 51 | <Compile Include="KeyringFixture.cs" /> | 53 | <Compile Include="KeyringFixture.cs" /> |
11 | 52 | <Compile Include="Properties\AssemblyInfo.cs" /> | 54 | <Compile Include="Properties\AssemblyInfo.cs" /> |
12 | 55 | <Compile Include="SSOLoginProcessorFixture.cs" /> | ||
13 | 53 | </ItemGroup> | 56 | </ItemGroup> |
14 | 54 | <ItemGroup> | 57 | <ItemGroup> |
15 | 55 | <ProjectReference Include="..\Canonical.Ubuntu.SSO\Canonical.Ubuntu.SSO.csproj"> | 58 | <ProjectReference Include="..\Canonical.Ubuntu.SSO\Canonical.Ubuntu.SSO.csproj"> |
16 | 56 | <Project>{9460A771-2589-45DA-9618-9FE8BB7D16E8}</Project> | 59 | <Project>{9460A771-2589-45DA-9618-9FE8BB7D16E8}</Project> |
17 | 57 | <Name>Canonical.Ubuntu.SSO</Name> | 60 | <Name>Canonical.Ubuntu.SSO</Name> |
18 | 58 | </ProjectReference> | 61 | </ProjectReference> |
19 | 62 | <ProjectReference Include="..\Canonical.UbuntuOne.Common\Canonical.UbuntuOne.Common.csproj"> | ||
20 | 63 | <Project>{11353FF8-8E5A-488E-9CB1-873DADD232B9}</Project> | ||
21 | 64 | <Name>Canonical.UbuntuOne.Common</Name> | ||
22 | 65 | </ProjectReference> | ||
23 | 59 | </ItemGroup> | 66 | </ItemGroup> |
24 | 60 | <ItemGroup> | 67 | <ItemGroup> |
25 | 61 | <Folder Include="Service\" /> | 68 | <Folder Include="Service\" /> |
26 | 62 | 69 | ||
27 | === added file 'src/Canonical.Ubuntu.SSO.Tests/ColonSeparatedSSOCredentialsEncoderFixture.cs' | |||
28 | --- src/Canonical.Ubuntu.SSO.Tests/ColonSeparatedSSOCredentialsEncoderFixture.cs 1970-01-01 00:00:00 +0000 | |||
29 | +++ src/Canonical.Ubuntu.SSO.Tests/ColonSeparatedSSOCredentialsEncoderFixture.cs 2010-10-05 09:58:35 +0000 | |||
30 | @@ -0,0 +1,69 @@ | |||
31 | 1 | /* Copyright 2010 Canonical Ltd. | ||
32 | 2 | * | ||
33 | 3 | * This file is part of UbuntuOne on Windows. | ||
34 | 4 | * | ||
35 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
36 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
37 | 7 | * as published by the Free Software Foundation. | ||
38 | 8 | * | ||
39 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
40 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
41 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
42 | 12 | * GNU Lesser General Public License for more details. | ||
43 | 13 | * | ||
44 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
45 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
46 | 16 | * | ||
47 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
48 | 18 | */ | ||
49 | 19 | using NUnit.Framework; | ||
50 | 20 | |||
51 | 21 | namespace Canonical.Ubuntu.SSO.Tests | ||
52 | 22 | { | ||
53 | 23 | [TestFixture] | ||
54 | 24 | public class ColonSeparatedSSOCredentialsEncoderFixture | ||
55 | 25 | { | ||
56 | 26 | #region Variables | ||
57 | 27 | |||
58 | 28 | private ColonSeparatedSSOCredentialsEncoder _encoder; | ||
59 | 29 | |||
60 | 30 | #endregion | ||
61 | 31 | |||
62 | 32 | #region Setup & TearDown | ||
63 | 33 | |||
64 | 34 | [SetUp] | ||
65 | 35 | public void Setup() | ||
66 | 36 | { | ||
67 | 37 | _encoder = new ColonSeparatedSSOCredentialsEncoder(); | ||
68 | 38 | } | ||
69 | 39 | #endregion | ||
70 | 40 | |||
71 | 41 | #region Tests | ||
72 | 42 | |||
73 | 43 | [TestCase("firstToken:firstTokenSecret:firstConsumer:firstConsumerSecret", "firstToken", "firstTokenSecret", "firstConsumer", "firstConsumerSecret")] | ||
74 | 44 | [TestCase("ad180jjd733klru7:hdhd0244k9j7ao03:dpf43f3p2l4k3l03:djr9rjt0jd78jf88%26", "ad180jjd733klru7", "hdhd0244k9j7ao03", "dpf43f3p2l4k3l03", "djr9rjt0jd78jf88%26")] | ||
75 | 45 | public void EncodeTest(string expected, string token, string tokenSecret, string consumerKey, string consumerSecret) | ||
76 | 46 | { | ||
77 | 47 | Assert.AreEqual(expected, _encoder.Encode(token, tokenSecret, consumerKey, consumerSecret)); | ||
78 | 48 | } | ||
79 | 49 | |||
80 | 50 | [TestCase("firstToken:firstTokenSecret:firstConsumer:firstConsumerSecret", "firstToken", "firstTokenSecret", "firstConsumer", "firstConsumerSecret")] | ||
81 | 51 | [TestCase("ad180jjd733klru7:hdhd0244k9j7ao03:dpf43f3p2l4k3l03:djr9rjt0jd78jf88%26", "ad180jjd733klru7", "hdhd0244k9j7ao03", "dpf43f3p2l4k3l03", "djr9rjt0jd78jf88%26")] | ||
82 | 52 | public void DecodeTest(string secret, string expectedToken, string expectedTokenSecret, string expectedConsumerKey, | ||
83 | 53 | string expectedConsumerSecret) | ||
84 | 54 | { | ||
85 | 55 | string token; | ||
86 | 56 | string tokenSecret; | ||
87 | 57 | string consumerKey; | ||
88 | 58 | string consumerSecret; | ||
89 | 59 | _encoder.Decode(secret, out token, out tokenSecret, out consumerKey, out consumerSecret); | ||
90 | 60 | Assert.AreEqual(expectedToken, token); | ||
91 | 61 | Assert.AreEqual(expectedTokenSecret, tokenSecret); | ||
92 | 62 | Assert.AreEqual(expectedConsumerKey, consumerKey); | ||
93 | 63 | Assert.AreEqual(expectedConsumerSecret, consumerSecret); | ||
94 | 64 | } | ||
95 | 65 | |||
96 | 66 | #endregion | ||
97 | 67 | |||
98 | 68 | } | ||
99 | 69 | } | ||
100 | 0 | 70 | ||
101 | === added file 'src/Canonical.Ubuntu.SSO.Tests/JsonSSOCredentialsEncoderFixture.cs' | |||
102 | --- src/Canonical.Ubuntu.SSO.Tests/JsonSSOCredentialsEncoderFixture.cs 1970-01-01 00:00:00 +0000 | |||
103 | +++ src/Canonical.Ubuntu.SSO.Tests/JsonSSOCredentialsEncoderFixture.cs 2010-10-05 09:58:35 +0000 | |||
104 | @@ -0,0 +1,29 @@ | |||
105 | 1 | /* Copyright 2010 Canonical Ltd. | ||
106 | 2 | * | ||
107 | 3 | * This file is part of UbuntuOne on Windows. | ||
108 | 4 | * | ||
109 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
110 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
111 | 7 | * as published by the Free Software Foundation. | ||
112 | 8 | * | ||
113 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
114 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
115 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
116 | 12 | * GNU Lesser General Public License for more details. | ||
117 | 13 | * | ||
118 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
119 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
120 | 16 | * | ||
121 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
122 | 18 | */ | ||
123 | 19 | using System; | ||
124 | 20 | using System.Collections.Generic; | ||
125 | 21 | using System.Linq; | ||
126 | 22 | using System.Text; | ||
127 | 23 | |||
128 | 24 | namespace Canonical.Ubuntu.SSO.Tests | ||
129 | 25 | { | ||
130 | 26 | class JsonSSOCredentialsEncoderFixture | ||
131 | 27 | { | ||
132 | 28 | } | ||
133 | 29 | } | ||
134 | 0 | 30 | ||
135 | === added file 'src/Canonical.Ubuntu.SSO.Tests/SSOLoginProcessorFixture.cs' | |||
136 | --- src/Canonical.Ubuntu.SSO.Tests/SSOLoginProcessorFixture.cs 1970-01-01 00:00:00 +0000 | |||
137 | +++ src/Canonical.Ubuntu.SSO.Tests/SSOLoginProcessorFixture.cs 2010-10-05 09:58:35 +0000 | |||
138 | @@ -0,0 +1,98 @@ | |||
139 | 1 | /* Copyright 2010 Canonical Ltd. | ||
140 | 2 | * | ||
141 | 3 | * This file is part of UbuntuOne on Windows. | ||
142 | 4 | * | ||
143 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
144 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
145 | 7 | * as published by the Free Software Foundation. | ||
146 | 8 | * | ||
147 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
148 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
149 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
150 | 12 | * GNU Lesser General Public License for more details. | ||
151 | 13 | * | ||
152 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
153 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
154 | 16 | * | ||
155 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
156 | 18 | */ | ||
157 | 19 | using Canonical.Ubuntu.SSO.Service; | ||
158 | 20 | using Canonical.UbuntuOne.Common.Net; | ||
159 | 21 | using NUnit.Framework; | ||
160 | 22 | using Rhino.Mocks; | ||
161 | 23 | |||
162 | 24 | namespace Canonical.Ubuntu.SSO.Tests | ||
163 | 25 | { | ||
164 | 26 | [TestFixture] | ||
165 | 27 | public class SSOLoginProcessorFixture | ||
166 | 28 | { | ||
167 | 29 | #region Variables | ||
168 | 30 | |||
169 | 31 | private MockRepository _mocks; | ||
170 | 32 | private Authentications _auth; | ||
171 | 33 | private IKeyring _keyring; | ||
172 | 34 | private ISSOCredentialsEncoder _encode; | ||
173 | 35 | private IOAuth _oauth; | ||
174 | 36 | private IHttpWebRequestFactory _requestFactory; | ||
175 | 37 | private IHttpWebRequest _request; | ||
176 | 38 | private SSOLoginProcessor _processor; | ||
177 | 39 | |||
178 | 40 | #endregion | ||
179 | 41 | |||
180 | 42 | #region Setup | ||
181 | 43 | |||
182 | 44 | [SetUp] | ||
183 | 45 | public void Setup() | ||
184 | 46 | { | ||
185 | 47 | _mocks = new MockRepository(); | ||
186 | 48 | _auth = _mocks.DynamicMock<Authentications>(); | ||
187 | 49 | _keyring = _mocks.DynamicMock<IKeyring>(); | ||
188 | 50 | _encode = _mocks.DynamicMock<ISSOCredentialsEncoder>(); | ||
189 | 51 | _oauth = _mocks.DynamicMock<IOAuth>(); | ||
190 | 52 | _requestFactory = _mocks.DynamicMock<IHttpWebRequestFactory>(); | ||
191 | 53 | _request = _mocks.DynamicMock<IHttpWebRequest>(); | ||
192 | 54 | _processor = new SSOLoginProcessor | ||
193 | 55 | { | ||
194 | 56 | Authentications = _auth, | ||
195 | 57 | HttpWebRequestFactory = _requestFactory, | ||
196 | 58 | Keyring = _keyring, | ||
197 | 59 | OAuth = _oauth, | ||
198 | 60 | SSOCredentialsEncoder = _encode | ||
199 | 61 | }; | ||
200 | 62 | } | ||
201 | 63 | |||
202 | 64 | #endregion | ||
203 | 65 | |||
204 | 66 | #region Tests | ||
205 | 67 | |||
206 | 68 | [TestCase("manuel@canonical.com", "Passwd", "UbuntuOne")] | ||
207 | 69 | [TestCase("mandel@themacaque.com", "Test", "Gwibber")] | ||
208 | 70 | [ExpectedException(typeof(SSOLoginException))] | ||
209 | 71 | public void LoginCredentialsExceptionTest(string email, string password, string tokenName) | ||
210 | 72 | { | ||
211 | 73 | Assert.Ignore("Not yet implemented"); | ||
212 | 74 | } | ||
213 | 75 | |||
214 | 76 | [Test] | ||
215 | 77 | [ExpectedException(typeof(SSOLoginException))] | ||
216 | 78 | public void LoginPingExceptionTest() | ||
217 | 79 | { | ||
218 | 80 | Assert.Ignore("Not yet implemented"); | ||
219 | 81 | } | ||
220 | 82 | |||
221 | 83 | [Test] | ||
222 | 84 | public void LoginNoSecretTest() | ||
223 | 85 | { | ||
224 | 86 | Assert.Ignore("Not yet implemented"); | ||
225 | 87 | } | ||
226 | 88 | |||
227 | 89 | [Test] | ||
228 | 90 | public void LoginSecretTest() | ||
229 | 91 | { | ||
230 | 92 | Assert.Ignore("Not yet implemented"); | ||
231 | 93 | } | ||
232 | 94 | |||
233 | 95 | #endregion | ||
234 | 96 | |||
235 | 97 | } | ||
236 | 98 | } | ||
237 | 0 | 99 | ||
238 | === modified file 'src/Canonical.Ubuntu.SSO/Canonical.Ubuntu.SSO.csproj' | |||
239 | --- src/Canonical.Ubuntu.SSO/Canonical.Ubuntu.SSO.csproj 2010-10-05 09:58:34 +0000 | |||
240 | +++ src/Canonical.Ubuntu.SSO/Canonical.Ubuntu.SSO.csproj 2010-10-05 09:58:35 +0000 | |||
241 | @@ -35,6 +35,10 @@ | |||
242 | 35 | <SpecificVersion>False</SpecificVersion> | 35 | <SpecificVersion>False</SpecificVersion> |
243 | 36 | <HintPath>..\..\lib\log4net\log4net.dll</HintPath> | 36 | <HintPath>..\..\lib\log4net\log4net.dll</HintPath> |
244 | 37 | </Reference> | 37 | </Reference> |
245 | 38 | <Reference Include="Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> | ||
246 | 39 | <SpecificVersion>False</SpecificVersion> | ||
247 | 40 | <HintPath>..\..\lib\JsonNet\Newtonsoft.Json.dll</HintPath> | ||
248 | 41 | </Reference> | ||
249 | 38 | <Reference Include="System" /> | 42 | <Reference Include="System" /> |
250 | 39 | <Reference Include="System.Core"> | 43 | <Reference Include="System.Core"> |
251 | 40 | <RequiredTargetFramework>3.5</RequiredTargetFramework> | 44 | <RequiredTargetFramework>3.5</RequiredTargetFramework> |
252 | @@ -54,6 +58,7 @@ | |||
253 | 54 | <Compile Include="..\Version.cs"> | 58 | <Compile Include="..\Version.cs"> |
254 | 55 | <Link>Properties\Version.cs</Link> | 59 | <Link>Properties\Version.cs</Link> |
255 | 56 | </Compile> | 60 | </Compile> |
256 | 61 | <Compile Include="ColonSeparatedSSOCredentialsEncoder.cs" /> | ||
257 | 57 | <Compile Include="CredentialsDeniedEventArgs.cs" /> | 62 | <Compile Include="CredentialsDeniedEventArgs.cs" /> |
258 | 58 | <Compile Include="CredentialsErrorEventArgs.cs" /> | 63 | <Compile Include="CredentialsErrorEventArgs.cs" /> |
259 | 59 | <Compile Include="CredentialsFoundEventArgs.cs" /> | 64 | <Compile Include="CredentialsFoundEventArgs.cs" /> |
260 | @@ -61,11 +66,14 @@ | |||
261 | 61 | <Compile Include="IDataProtector.cs" /> | 66 | <Compile Include="IDataProtector.cs" /> |
262 | 62 | <Compile Include="ILoginOrRegisterView.cs" /> | 67 | <Compile Include="ILoginOrRegisterView.cs" /> |
263 | 63 | <Compile Include="ILoginView.cs" /> | 68 | <Compile Include="ILoginView.cs" /> |
264 | 69 | <Compile Include="IOAuth.cs" /> | ||
265 | 64 | <Compile Include="IRegistryKey.cs" /> | 70 | <Compile Include="IRegistryKey.cs" /> |
266 | 71 | <Compile Include="ISSOCredentialsEncoder.cs" /> | ||
267 | 65 | <Compile Include="ISSOLoginProcessor.cs" /> | 72 | <Compile Include="ISSOLoginProcessor.cs" /> |
268 | 73 | <Compile Include="JsonSSOCredentialsEncoder.cs" /> | ||
269 | 66 | <Compile Include="Keyring.cs" /> | 74 | <Compile Include="Keyring.cs" /> |
270 | 67 | <Compile Include="LoginCredentialsEventArgs.cs" /> | 75 | <Compile Include="LoginCredentialsEventArgs.cs" /> |
272 | 68 | <Compile Include="OAuthBase.cs" /> | 76 | <Compile Include="OAuth.cs" /> |
273 | 69 | <Compile Include="RegistryKeyWrapper.cs" /> | 77 | <Compile Include="RegistryKeyWrapper.cs" /> |
274 | 70 | <Compile Include="Service\Account.cs" /> | 78 | <Compile Include="Service\Account.cs" /> |
275 | 71 | <Compile Include="Service\AccountDiff.cs" /> | 79 | <Compile Include="Service\AccountDiff.cs" /> |
276 | @@ -109,6 +117,8 @@ | |||
277 | 109 | <Compile Include="IKeyring.cs" /> | 117 | <Compile Include="IKeyring.cs" /> |
278 | 110 | <Compile Include="ISSOCredentialsProvider.cs" /> | 118 | <Compile Include="ISSOCredentialsProvider.cs" /> |
279 | 111 | <Compile Include="Properties\AssemblyInfo.cs" /> | 119 | <Compile Include="Properties\AssemblyInfo.cs" /> |
280 | 120 | <Compile Include="SSOException.cs" /> | ||
281 | 121 | <Compile Include="SSOLoginException.cs" /> | ||
282 | 112 | <Compile Include="SSOLoginProcessor.cs" /> | 122 | <Compile Include="SSOLoginProcessor.cs" /> |
283 | 113 | </ItemGroup> | 123 | </ItemGroup> |
284 | 114 | <ItemGroup> | 124 | <ItemGroup> |
285 | 115 | 125 | ||
286 | === added file 'src/Canonical.Ubuntu.SSO/ColonSeparatedSSOCredentialsEncoder.cs' | |||
287 | --- src/Canonical.Ubuntu.SSO/ColonSeparatedSSOCredentialsEncoder.cs 1970-01-01 00:00:00 +0000 | |||
288 | +++ src/Canonical.Ubuntu.SSO/ColonSeparatedSSOCredentialsEncoder.cs 2010-10-05 09:58:35 +0000 | |||
289 | @@ -0,0 +1,60 @@ | |||
290 | 1 | /* Copyright 2010 Canonical Ltd. | ||
291 | 2 | * | ||
292 | 3 | * This file is part of UbuntuOne on Windows. | ||
293 | 4 | * | ||
294 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
295 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
296 | 7 | * as published by the Free Software Foundation. | ||
297 | 8 | * | ||
298 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
299 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
300 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
301 | 12 | * GNU Lesser General Public License for more details. | ||
302 | 13 | * | ||
303 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
304 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
305 | 16 | * | ||
306 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
307 | 18 | */ | ||
308 | 19 | namespace Canonical.Ubuntu.SSO | ||
309 | 20 | { | ||
310 | 21 | /// <summary> | ||
311 | 22 | /// Encode implementation that encodes the oauth token using colons as separators. | ||
312 | 23 | /// </summary> | ||
313 | 24 | public class ColonSeparatedSSOCredentialsEncoder : ISSOCredentialsEncoder | ||
314 | 25 | { | ||
315 | 26 | #region Implementation of ISSOCredentialsEncoder | ||
316 | 27 | |||
317 | 28 | /// <summary> | ||
318 | 29 | /// Encodes oauth credentials so that they can be stored in a keyring. | ||
319 | 30 | /// </summary> | ||
320 | 31 | /// <param name="token">The oauth token.</param> | ||
321 | 32 | /// <param name="tokenSecret">The oauth secret.</param> | ||
322 | 33 | /// <param name="consumerKey">The oauth consumer key.</param> | ||
323 | 34 | /// <param name="consumerSecret">The oauth consumer secret.</param> | ||
324 | 35 | /// <returns>a stirng that represents the oauth token.</returns> | ||
325 | 36 | public string Encode(string token, string tokenSecret, string consumerKey, string consumerSecret) | ||
326 | 37 | { | ||
327 | 38 | return string.Format("{0}:{1}:{2}:{3}", token, tokenSecret, consumerKey, consumerSecret); | ||
328 | 39 | } | ||
329 | 40 | |||
330 | 41 | /// <summary> | ||
331 | 42 | /// Decodes the oauth credentials from a single string. | ||
332 | 43 | /// </summary> | ||
333 | 44 | /// <param name="secret">The secret that was stored in the keyring.</param> | ||
334 | 45 | /// <param name="token">Out var where the ouath token will be returned.</param> | ||
335 | 46 | /// <param name="tokenSecret">Out var where the oauth token secret will be returned.</param> | ||
336 | 47 | /// <param name="consumerKey">Out var where the oauth consumer key will be returned.</param> | ||
337 | 48 | /// <param name="consumerSecret">Out var where the oauth consumer secret will be returned.</param> | ||
338 | 49 | public void Decode(string secret, out string token, out string tokenSecret, out string consumerKey, out string consumerSecret) | ||
339 | 50 | { | ||
340 | 51 | var data = secret.Split(":".ToCharArray(),4); | ||
341 | 52 | token = data[0]; | ||
342 | 53 | tokenSecret = data[1]; | ||
343 | 54 | consumerKey = data[2]; | ||
344 | 55 | consumerSecret = data[3]; | ||
345 | 56 | } | ||
346 | 57 | |||
347 | 58 | #endregion | ||
348 | 59 | } | ||
349 | 60 | } | ||
350 | 0 | 61 | ||
351 | === modified file 'src/Canonical.Ubuntu.SSO/DPAPIDataProtector.cs' | |||
352 | --- src/Canonical.Ubuntu.SSO/DPAPIDataProtector.cs 2010-10-05 09:58:34 +0000 | |||
353 | +++ src/Canonical.Ubuntu.SSO/DPAPIDataProtector.cs 2010-10-05 09:58:35 +0000 | |||
354 | @@ -17,7 +17,6 @@ | |||
355 | 17 | * | 17 | * |
356 | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> |
357 | 19 | */ | 19 | */ |
358 | 20 | |||
359 | 21 | using System.Security.Cryptography; | 20 | using System.Security.Cryptography; |
360 | 22 | using System.Text; | 21 | using System.Text; |
361 | 23 | using log4net; | 22 | using log4net; |
362 | 24 | 23 | ||
363 | === added file 'src/Canonical.Ubuntu.SSO/IOAuth.cs' | |||
364 | --- src/Canonical.Ubuntu.SSO/IOAuth.cs 1970-01-01 00:00:00 +0000 | |||
365 | +++ src/Canonical.Ubuntu.SSO/IOAuth.cs 2010-10-05 09:58:35 +0000 | |||
366 | @@ -0,0 +1,122 @@ | |||
367 | 1 | /* Copyright 2010 Canonical Ltd. | ||
368 | 2 | * | ||
369 | 3 | * This file is part of UbuntuOne on Windows. | ||
370 | 4 | * | ||
371 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
372 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
373 | 7 | * as published by the Free Software Foundation. | ||
374 | 8 | * | ||
375 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
376 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
377 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
378 | 12 | * GNU Lesser General Public License for more details. | ||
379 | 13 | * | ||
380 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
381 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
382 | 16 | * | ||
383 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
384 | 18 | */ | ||
385 | 19 | using System; | ||
386 | 20 | using System.Collections.Generic; | ||
387 | 21 | using System.Security.Cryptography; | ||
388 | 22 | |||
389 | 23 | namespace Canonical.Ubuntu.SSO | ||
390 | 24 | { | ||
391 | 25 | public interface IOAuth | ||
392 | 26 | { | ||
393 | 27 | /// <summary> | ||
394 | 28 | /// Generate the signature base that is used to produce the signature | ||
395 | 29 | /// </summary> | ||
396 | 30 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
397 | 31 | /// <param name="consumerKey">The consumer key</param> | ||
398 | 32 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
399 | 33 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
400 | 34 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
401 | 35 | /// <param name="nonce"></param> | ||
402 | 36 | /// <param name="signatureType">The signature type. To use the default values use <see cref="OAuth.SignatureTypes">OAuth.SignatureTypes</see>.</param> | ||
403 | 37 | /// <param name="timeStamp"></param> | ||
404 | 38 | /// <param name="normalizedUrl"></param> | ||
405 | 39 | /// <param name="normalizedRequestParameters"></param> | ||
406 | 40 | /// <returns>The signature base</returns> | ||
407 | 41 | string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, | ||
408 | 42 | string httpMethod, string timeStamp, string nonce, string signatureType, out string normalizedUrl, | ||
409 | 43 | out string normalizedRequestParameters); | ||
410 | 44 | |||
411 | 45 | /// <summary> | ||
412 | 46 | /// Generate the signature value based on the given signature base and hash algorithm | ||
413 | 47 | /// </summary> | ||
414 | 48 | /// <param name="signatureBase">The signature based as produced by the GenerateSignatureBase method or by any other means</param> | ||
415 | 49 | /// <param name="hash">The hash algorithm used to perform the hashing. If the hashing algorithm requires initialization or a key it should be set prior to calling this method</param> | ||
416 | 50 | /// <returns>A base64 string of the hash value</returns> | ||
417 | 51 | string GenerateSignatureUsingHash(string signatureBase, HashAlgorithm hash); | ||
418 | 52 | |||
419 | 53 | /// <summary> | ||
420 | 54 | /// Generates a signature using the HMAC-SHA1 algorithm | ||
421 | 55 | /// </summary> | ||
422 | 56 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
423 | 57 | /// <param name="consumerKey">The consumer key</param> | ||
424 | 58 | /// <param name="consumerSecret">The consumer seceret</param> | ||
425 | 59 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
426 | 60 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
427 | 61 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
428 | 62 | /// <param name="timeStamp"></param> | ||
429 | 63 | /// <param name="nonce"></param> | ||
430 | 64 | /// <param name="normalizedUrl"></param> | ||
431 | 65 | /// <param name="normalizedRequestParameters"></param> | ||
432 | 66 | /// <returns>A base64 string of the hash value</returns> | ||
433 | 67 | string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, | ||
434 | 68 | string tokenSecret, string httpMethod, string timeStamp, string nonce, out string normalizedUrl, | ||
435 | 69 | out string normalizedRequestParameters); | ||
436 | 70 | |||
437 | 71 | /// <summary> | ||
438 | 72 | /// Generates a signature using the specified signatureType | ||
439 | 73 | /// </summary> | ||
440 | 74 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
441 | 75 | /// <param name="consumerKey">The consumer key</param> | ||
442 | 76 | /// <param name="consumerSecret">The consumer seceret</param> | ||
443 | 77 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
444 | 78 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
445 | 79 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
446 | 80 | /// <param name="nonce"></param> | ||
447 | 81 | /// <param name="signatureType">The type of signature to use</param> | ||
448 | 82 | /// <param name="timeStamp"></param> | ||
449 | 83 | /// <param name="normalizedUrl"></param> | ||
450 | 84 | /// <param name="normalizedRequestParameters"></param> | ||
451 | 85 | /// <returns>A base64 string of the hash value</returns> | ||
452 | 86 | string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, | ||
453 | 87 | string tokenSecret, string httpMethod, string timeStamp, string nonce, | ||
454 | 88 | OAuth.SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters); | ||
455 | 89 | |||
456 | 90 | /// <summary> | ||
457 | 91 | /// Returns a key value pair that contains a hedaer that can be used to authenticate a web requet. | ||
458 | 92 | /// </summary> | ||
459 | 93 | /// <param name="url">The uri with the web resource.</param> | ||
460 | 94 | /// <param name="realm">The real that was used for the oauth.</param> | ||
461 | 95 | /// <param name="consumerKey">The oauth consumer key.</param> | ||
462 | 96 | /// <param name="consumerSecret">The oauth consumer secret.</param> | ||
463 | 97 | /// <param name="token">The oauth token.</param> | ||
464 | 98 | /// <param name="tokenSecret">The aoauth token secret.</param> | ||
465 | 99 | /// <param name="httpMethod">The http method used for the request.</param> | ||
466 | 100 | /// <param name="timeStamp">A time stamp.</param> | ||
467 | 101 | /// <param name="nonce">A once</param> | ||
468 | 102 | /// <param name="signatureType">Teht ype of signature that will be used.</param> | ||
469 | 103 | /// <param name="normUrl"></param> | ||
470 | 104 | /// <param name="normParams"></param> | ||
471 | 105 | /// <returns>A key value pair where the key is the name of the header and the value is the data of the header.</returns> | ||
472 | 106 | KeyValuePair<string, string> GenerateHeaderWithSignature(Uri url, string realm, string consumerKey, | ||
473 | 107 | string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, | ||
474 | 108 | string nonce, OAuth.SignatureTypes signatureType, out string normUrl, out string normParams); | ||
475 | 109 | |||
476 | 110 | /// <summary> | ||
477 | 111 | /// Generate the timestamp for the signature | ||
478 | 112 | /// </summary> | ||
479 | 113 | /// <returns></returns> | ||
480 | 114 | string GenerateTimeStamp(); | ||
481 | 115 | |||
482 | 116 | /// <summary> | ||
483 | 117 | /// Generate a nonce | ||
484 | 118 | /// </summary> | ||
485 | 119 | /// <returns></returns> | ||
486 | 120 | string GenerateNonce(); | ||
487 | 121 | } | ||
488 | 122 | } | ||
489 | 0 | \ No newline at end of file | 123 | \ No newline at end of file |
490 | 1 | 124 | ||
491 | === added file 'src/Canonical.Ubuntu.SSO/ISSOCredentialsEncoder.cs' | |||
492 | --- src/Canonical.Ubuntu.SSO/ISSOCredentialsEncoder.cs 1970-01-01 00:00:00 +0000 | |||
493 | +++ src/Canonical.Ubuntu.SSO/ISSOCredentialsEncoder.cs 2010-10-05 09:58:35 +0000 | |||
494 | @@ -0,0 +1,46 @@ | |||
495 | 1 | /* Copyright 2010 Canonical Ltd. | ||
496 | 2 | * | ||
497 | 3 | * This file is part of UbuntuOne on Windows. | ||
498 | 4 | * | ||
499 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
500 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
501 | 7 | * as published by the Free Software Foundation. | ||
502 | 8 | * | ||
503 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
504 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
505 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
506 | 12 | * GNU Lesser General Public License for more details. | ||
507 | 13 | * | ||
508 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
509 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
510 | 16 | * | ||
511 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
512 | 18 | */ | ||
513 | 19 | namespace Canonical.Ubuntu.SSO | ||
514 | 20 | { | ||
515 | 21 | /// <summary> | ||
516 | 22 | /// Interface to be implemented by those object that know how to encode a oauth token so that it is saved as a single string. | ||
517 | 23 | /// </summary> | ||
518 | 24 | public interface ISSOCredentialsEncoder | ||
519 | 25 | { | ||
520 | 26 | /// <summary> | ||
521 | 27 | /// When implemented will encode oauth credentials so that they can be stored in a keyring. | ||
522 | 28 | /// </summary> | ||
523 | 29 | /// <param name="token">The oauth token.</param> | ||
524 | 30 | /// <param name="tokenSecret">The oauth secret.</param> | ||
525 | 31 | /// <param name="consumerKey">The oauth consumer key.</param> | ||
526 | 32 | /// <param name="consumerSecret">The oauth consumer secret.</param> | ||
527 | 33 | /// <returns>a stirng that represents the oauth token.</returns> | ||
528 | 34 | string Encode(string token, string tokenSecret, string consumerKey, string consumerSecret); | ||
529 | 35 | |||
530 | 36 | /// <summary> | ||
531 | 37 | /// When implemented decodes the oauth credentials from a single string. | ||
532 | 38 | /// </summary> | ||
533 | 39 | /// <param name="secret">The secret that was stored in the keyring.</param> | ||
534 | 40 | /// <param name="token">Out var where the ouath token will be returned.</param> | ||
535 | 41 | /// <param name="tokenSecret">Out var where the oauth token secret will be returned.</param> | ||
536 | 42 | /// <param name="consumerKey">Out var where the oauth consumer key will be returned.</param> | ||
537 | 43 | /// <param name="consumerSecret">Out var where the oauth consumer secret will be returned.</param> | ||
538 | 44 | void Decode(string secret, out string token, out string tokenSecret, out string consumerKey, out string consumerSecret); | ||
539 | 45 | } | ||
540 | 46 | } | ||
541 | 0 | 47 | ||
542 | === added file 'src/Canonical.Ubuntu.SSO/JsonSSOCredentialsEncoder.cs' | |||
543 | --- src/Canonical.Ubuntu.SSO/JsonSSOCredentialsEncoder.cs 1970-01-01 00:00:00 +0000 | |||
544 | +++ src/Canonical.Ubuntu.SSO/JsonSSOCredentialsEncoder.cs 2010-10-05 09:58:35 +0000 | |||
545 | @@ -0,0 +1,82 @@ | |||
546 | 1 | /* Copyright 2010 Canonical Ltd. | ||
547 | 2 | * | ||
548 | 3 | * This file is part of UbuntuOne on Windows. | ||
549 | 4 | * | ||
550 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
551 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
552 | 7 | * as published by the Free Software Foundation. | ||
553 | 8 | * | ||
554 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
555 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
556 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
557 | 12 | * GNU Lesser General Public License for more details. | ||
558 | 13 | * | ||
559 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
560 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
561 | 16 | * | ||
562 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
563 | 18 | */ | ||
564 | 19 | using System; | ||
565 | 20 | using Newtonsoft.Json.Linq; | ||
566 | 21 | |||
567 | 22 | namespace Canonical.Ubuntu.SSO | ||
568 | 23 | { | ||
569 | 24 | public class JsonSSOCredentialsEncoder : ISSOCredentialsEncoder | ||
570 | 25 | { | ||
571 | 26 | #region Implementation of ISSOCredentialsEncoder | ||
572 | 27 | |||
573 | 28 | /// <summary> | ||
574 | 29 | /// When implemented will encode oauth credentials so that they can be stored in a keyring. | ||
575 | 30 | /// </summary> | ||
576 | 31 | /// <param name="token">The oauth token.</param> | ||
577 | 32 | /// <param name="tokenSecret">The oauth secret.</param> | ||
578 | 33 | /// <param name="consumerKey">The oauth consumer key.</param> | ||
579 | 34 | /// <param name="consumerSecret">The oauth consumer secret.</param> | ||
580 | 35 | /// <returns>a stirng that represents the oauth token.</returns> | ||
581 | 36 | public string Encode(string token, string tokenSecret, string consumerKey, string consumerSecret) | ||
582 | 37 | { | ||
583 | 38 | var jobject = new JObject(); | ||
584 | 39 | jobject["consumer_key"] = ""; | ||
585 | 40 | jobject["consumer_secret"] = ""; | ||
586 | 41 | jobject["token"] = ""; | ||
587 | 42 | jobject["token_secret"] = ""; | ||
588 | 43 | return jobject.ToString(); | ||
589 | 44 | } | ||
590 | 45 | |||
591 | 46 | /// <summary> | ||
592 | 47 | /// When implemented decodes the oauth credentials from a single string. | ||
593 | 48 | /// </summary> | ||
594 | 49 | /// <param name="secret">The secret that was stored in the keyring.</param> | ||
595 | 50 | /// <param name="token">Out var where the ouath token will be returned.</param> | ||
596 | 51 | /// <param name="tokenSecret">Out var where the oauth token secret will be returned.</param> | ||
597 | 52 | /// <param name="consumerKey">Out var where the oauth consumer key will be returned.</param> | ||
598 | 53 | /// <param name="consumerSecret">Out var where the oauth consumer secret will be returned.</param> | ||
599 | 54 | public void Decode(string secret, out string token, out string tokenSecret, out string consumerKey, out string consumerSecret) | ||
600 | 55 | { | ||
601 | 56 | var credentials = JObject.Parse(secret); | ||
602 | 57 | try | ||
603 | 58 | { | ||
604 | 59 | consumerKey = (string)credentials["consumer_key"]; | ||
605 | 60 | consumerSecret = (string)credentials["consumer_secret"]; | ||
606 | 61 | token = (string)credentials["token"]; | ||
607 | 62 | tokenSecret = (string)credentials["token_secret"]; | ||
608 | 63 | |||
609 | 64 | } | ||
610 | 65 | catch (InvalidCastException e) | ||
611 | 66 | { | ||
612 | 67 | // could happen when the json is not formatted as we expected. | ||
613 | 68 | throw new SSOLoginException("Error when retrieving credentials.", e); | ||
614 | 69 | } | ||
615 | 70 | if (string.IsNullOrEmpty(consumerKey) | ||
616 | 71 | || string.IsNullOrEmpty(consumerSecret) | ||
617 | 72 | || string.IsNullOrEmpty(token) | ||
618 | 73 | || string.IsNullOrEmpty(tokenSecret)) | ||
619 | 74 | { | ||
620 | 75 | // none of them can be null or empty. | ||
621 | 76 | throw new SSOException("Credentials could not be parsed."); | ||
622 | 77 | } | ||
623 | 78 | } | ||
624 | 79 | |||
625 | 80 | #endregion | ||
626 | 81 | } | ||
627 | 82 | } | ||
628 | 0 | 83 | ||
629 | === added file 'src/Canonical.Ubuntu.SSO/OAuth.cs' | |||
630 | --- src/Canonical.Ubuntu.SSO/OAuth.cs 1970-01-01 00:00:00 +0000 | |||
631 | +++ src/Canonical.Ubuntu.SSO/OAuth.cs 2010-10-05 09:58:35 +0000 | |||
632 | @@ -0,0 +1,391 @@ | |||
633 | 1 | using System; | ||
634 | 2 | using System.Security.Cryptography; | ||
635 | 3 | using System.Collections.Generic; | ||
636 | 4 | using System.Text; | ||
637 | 5 | using System.Web; | ||
638 | 6 | |||
639 | 7 | namespace Canonical.Ubuntu.SSO | ||
640 | 8 | { | ||
641 | 9 | public class OAuth : IOAuth | ||
642 | 10 | { | ||
643 | 11 | |||
644 | 12 | /// <summary> | ||
645 | 13 | /// Provides a predefined set of algorithms that are supported officially by the protocol | ||
646 | 14 | /// </summary> | ||
647 | 15 | public enum SignatureTypes | ||
648 | 16 | { | ||
649 | 17 | HMACSHA1, | ||
650 | 18 | PLAINTEXT, | ||
651 | 19 | RSASHA1 | ||
652 | 20 | } | ||
653 | 21 | |||
654 | 22 | /// <summary> | ||
655 | 23 | /// Provides an internal structure to sort the query parameter | ||
656 | 24 | /// </summary> | ||
657 | 25 | protected class QueryParameter | ||
658 | 26 | { | ||
659 | 27 | private readonly string _name = null; | ||
660 | 28 | private readonly string _value = null; | ||
661 | 29 | |||
662 | 30 | public QueryParameter(string name, string value) | ||
663 | 31 | { | ||
664 | 32 | _name = name; | ||
665 | 33 | _value = value; | ||
666 | 34 | } | ||
667 | 35 | |||
668 | 36 | public string Name | ||
669 | 37 | { | ||
670 | 38 | get { return _name; } | ||
671 | 39 | } | ||
672 | 40 | |||
673 | 41 | public string Value | ||
674 | 42 | { | ||
675 | 43 | get { return _value; } | ||
676 | 44 | } | ||
677 | 45 | } | ||
678 | 46 | |||
679 | 47 | /// <summary> | ||
680 | 48 | /// Comparer class used to perform the sorting of the query parameters | ||
681 | 49 | /// </summary> | ||
682 | 50 | protected class QueryParameterComparer : IComparer<QueryParameter> | ||
683 | 51 | { | ||
684 | 52 | |||
685 | 53 | #region IComparer<QueryParameter> Members | ||
686 | 54 | |||
687 | 55 | public int Compare(QueryParameter x, QueryParameter y) | ||
688 | 56 | { | ||
689 | 57 | return x.Name == y.Name ? string.Compare(x.Value, y.Value) | ||
690 | 58 | : string.Compare(x.Name, y.Name); | ||
691 | 59 | } | ||
692 | 60 | |||
693 | 61 | #endregion | ||
694 | 62 | } | ||
695 | 63 | |||
696 | 64 | protected const string OAuthVersion = "1.0"; | ||
697 | 65 | protected const string OAuthParameterPrefix = "oauth_"; | ||
698 | 66 | |||
699 | 67 | // | ||
700 | 68 | // List of know and used oauth parameters' names | ||
701 | 69 | // | ||
702 | 70 | protected const string OAuthConsumerKeyKey = "oauth_consumer_key"; | ||
703 | 71 | protected const string OAuthCallbackKey = "oauth_callback"; | ||
704 | 72 | protected const string OAuthVersionKey = "oauth_version"; | ||
705 | 73 | protected const string OAuthSignatureMethodKey = "oauth_signature_method"; | ||
706 | 74 | protected const string OAuthSignatureKey = "oauth_signature"; | ||
707 | 75 | protected const string OAuthTimestampKey = "oauth_timestamp"; | ||
708 | 76 | protected const string OAuthNonceKey = "oauth_nonce"; | ||
709 | 77 | protected const string OAuthTokenKey = "oauth_token"; | ||
710 | 78 | protected const string OAuthTokenSecretKey = "oauth_token_secret"; | ||
711 | 79 | |||
712 | 80 | protected const string HMACSHA1SignatureType = "HMAC-SHA1"; | ||
713 | 81 | protected const string PlainTextSignatureType = "PLAINTEXT"; | ||
714 | 82 | protected const string RSASHA1SignatureType = "RSA-SHA1"; | ||
715 | 83 | |||
716 | 84 | protected Random Random = new Random(); | ||
717 | 85 | |||
718 | 86 | protected string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; | ||
719 | 87 | |||
720 | 88 | /// <summary> | ||
721 | 89 | /// Helper function to compute a hash value | ||
722 | 90 | /// </summary> | ||
723 | 91 | /// <param name="hashAlgorithm">The hashing algoirhtm used. If that algorithm needs some initialization, like HMAC and its derivatives, they should be initialized prior to passing it to this function</param> | ||
724 | 92 | /// <param name="data">The data to hash</param> | ||
725 | 93 | /// <returns>a Base64 string of the hash value</returns> | ||
726 | 94 | private static string ComputeHash(HashAlgorithm hashAlgorithm, string data) | ||
727 | 95 | { | ||
728 | 96 | if (hashAlgorithm == null) | ||
729 | 97 | { | ||
730 | 98 | throw new ArgumentNullException("hashAlgorithm"); | ||
731 | 99 | } | ||
732 | 100 | |||
733 | 101 | if (string.IsNullOrEmpty(data)) | ||
734 | 102 | { | ||
735 | 103 | throw new ArgumentNullException("data"); | ||
736 | 104 | } | ||
737 | 105 | |||
738 | 106 | var dataBuffer = Encoding.ASCII.GetBytes(data); | ||
739 | 107 | var hashBytes = hashAlgorithm.ComputeHash(dataBuffer); | ||
740 | 108 | |||
741 | 109 | return Convert.ToBase64String(hashBytes); | ||
742 | 110 | } | ||
743 | 111 | |||
744 | 112 | /// <summary> | ||
745 | 113 | /// Internal function to cut out all non oauth query string parameters (all parameters not begining with "oauth_") | ||
746 | 114 | /// </summary> | ||
747 | 115 | /// <param name="parameters">The query string part of the Url</param> | ||
748 | 116 | /// <returns>A list of QueryParameter each containing the parameter name and value</returns> | ||
749 | 117 | private static List<QueryParameter> GetQueryParameters(string parameters) | ||
750 | 118 | { | ||
751 | 119 | if (parameters.StartsWith("?")) | ||
752 | 120 | { | ||
753 | 121 | parameters = parameters.Remove(0, 1); | ||
754 | 122 | } | ||
755 | 123 | |||
756 | 124 | var result = new List<QueryParameter>(); | ||
757 | 125 | |||
758 | 126 | if (!string.IsNullOrEmpty(parameters)) | ||
759 | 127 | { | ||
760 | 128 | string[] p = parameters.Split('&'); | ||
761 | 129 | foreach (string s in p) | ||
762 | 130 | { | ||
763 | 131 | if (!string.IsNullOrEmpty(s) && !s.StartsWith(OAuthParameterPrefix)) | ||
764 | 132 | { | ||
765 | 133 | if (s.IndexOf('=') > -1) | ||
766 | 134 | { | ||
767 | 135 | string[] temp = s.Split('='); | ||
768 | 136 | result.Add(new QueryParameter(temp[0], temp[1])); | ||
769 | 137 | } | ||
770 | 138 | else | ||
771 | 139 | { | ||
772 | 140 | result.Add(new QueryParameter(s, string.Empty)); | ||
773 | 141 | } | ||
774 | 142 | } | ||
775 | 143 | } | ||
776 | 144 | } | ||
777 | 145 | |||
778 | 146 | return result; | ||
779 | 147 | } | ||
780 | 148 | |||
781 | 149 | /// <summary> | ||
782 | 150 | /// This is a different Url Encode implementation since the default .NET one outputs the percent encoding in lower case. | ||
783 | 151 | /// While this is not a problem with the percent encoding spec, it is used in upper case throughout OAuth | ||
784 | 152 | /// </summary> | ||
785 | 153 | /// <param name="value">The value to Url encode</param> | ||
786 | 154 | /// <returns>Returns a Url encoded string</returns> | ||
787 | 155 | protected string UrlEncode(string value) | ||
788 | 156 | { | ||
789 | 157 | var result = new StringBuilder(); | ||
790 | 158 | |||
791 | 159 | foreach (char symbol in value) | ||
792 | 160 | { | ||
793 | 161 | if (UnreservedChars.IndexOf(symbol) != -1) | ||
794 | 162 | { | ||
795 | 163 | result.Append(symbol); | ||
796 | 164 | } | ||
797 | 165 | else | ||
798 | 166 | { | ||
799 | 167 | result.Append('%' + String.Format("{0:X2}", (int)symbol)); | ||
800 | 168 | } | ||
801 | 169 | } | ||
802 | 170 | |||
803 | 171 | return result.ToString(); | ||
804 | 172 | } | ||
805 | 173 | |||
806 | 174 | /// <summary> | ||
807 | 175 | /// Normalizes the request parameters according to the spec | ||
808 | 176 | /// </summary> | ||
809 | 177 | /// <param name="parameters">The list of parameters already sorted</param> | ||
810 | 178 | /// <returns>a string representing the normalized parameters</returns> | ||
811 | 179 | protected string NormalizeRequestParameters(IList<QueryParameter> parameters) | ||
812 | 180 | { | ||
813 | 181 | var sb = new StringBuilder(); | ||
814 | 182 | QueryParameter p; | ||
815 | 183 | for (var i = 0; i < parameters.Count; i++) | ||
816 | 184 | { | ||
817 | 185 | p = parameters[i]; | ||
818 | 186 | sb.AppendFormat("{0}={1}", p.Name, p.Value); | ||
819 | 187 | |||
820 | 188 | if (i < parameters.Count - 1) | ||
821 | 189 | { | ||
822 | 190 | sb.Append("&"); | ||
823 | 191 | } | ||
824 | 192 | } | ||
825 | 193 | |||
826 | 194 | return sb.ToString(); | ||
827 | 195 | } | ||
828 | 196 | |||
829 | 197 | /// <summary> | ||
830 | 198 | /// Generate the signature base that is used to produce the signature | ||
831 | 199 | /// </summary> | ||
832 | 200 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
833 | 201 | /// <param name="consumerKey">The consumer key</param> | ||
834 | 202 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
835 | 203 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
836 | 204 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
837 | 205 | /// <param name="nonce"></param> | ||
838 | 206 | /// <param name="signatureType">The signature type. To use the default values use <see cref="OAuth.SignatureTypes">OAuth.SignatureTypes</see>.</param> | ||
839 | 207 | /// <param name="timeStamp"></param> | ||
840 | 208 | /// <param name="normalizedUrl"></param> | ||
841 | 209 | /// <param name="normalizedRequestParameters"></param> | ||
842 | 210 | /// <returns>The signature base</returns> | ||
843 | 211 | public string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, string signatureType, out string normalizedUrl, out string normalizedRequestParameters) | ||
844 | 212 | { | ||
845 | 213 | if (token == null) | ||
846 | 214 | { | ||
847 | 215 | token = string.Empty; | ||
848 | 216 | } | ||
849 | 217 | |||
850 | 218 | if (tokenSecret == null) | ||
851 | 219 | { | ||
852 | 220 | tokenSecret = string.Empty; | ||
853 | 221 | } | ||
854 | 222 | |||
855 | 223 | if (string.IsNullOrEmpty(consumerKey)) | ||
856 | 224 | { | ||
857 | 225 | throw new ArgumentNullException("consumerKey"); | ||
858 | 226 | } | ||
859 | 227 | |||
860 | 228 | if (string.IsNullOrEmpty(httpMethod)) | ||
861 | 229 | { | ||
862 | 230 | throw new ArgumentNullException("httpMethod"); | ||
863 | 231 | } | ||
864 | 232 | |||
865 | 233 | if (string.IsNullOrEmpty(signatureType)) | ||
866 | 234 | { | ||
867 | 235 | throw new ArgumentNullException("signatureType"); | ||
868 | 236 | } | ||
869 | 237 | |||
870 | 238 | List<QueryParameter> parameters = GetQueryParameters(url.Query); | ||
871 | 239 | parameters.Add(new QueryParameter(OAuthVersionKey, OAuthVersion)); | ||
872 | 240 | parameters.Add(new QueryParameter(OAuthNonceKey, nonce)); | ||
873 | 241 | parameters.Add(new QueryParameter(OAuthTimestampKey, timeStamp)); | ||
874 | 242 | parameters.Add(new QueryParameter(OAuthSignatureMethodKey, signatureType)); | ||
875 | 243 | parameters.Add(new QueryParameter(OAuthConsumerKeyKey, consumerKey)); | ||
876 | 244 | |||
877 | 245 | if (!string.IsNullOrEmpty(token)) | ||
878 | 246 | { | ||
879 | 247 | parameters.Add(new QueryParameter(OAuthTokenKey, token)); | ||
880 | 248 | } | ||
881 | 249 | |||
882 | 250 | parameters.Sort(new QueryParameterComparer()); | ||
883 | 251 | |||
884 | 252 | normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host); | ||
885 | 253 | if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443))) | ||
886 | 254 | { | ||
887 | 255 | normalizedUrl += ":" + url.Port; | ||
888 | 256 | } | ||
889 | 257 | normalizedUrl += url.AbsolutePath; | ||
890 | 258 | normalizedRequestParameters = NormalizeRequestParameters(parameters); | ||
891 | 259 | |||
892 | 260 | var signatureBase = new StringBuilder(); | ||
893 | 261 | signatureBase.AppendFormat("{0}&", httpMethod.ToUpper()); | ||
894 | 262 | signatureBase.AppendFormat("{0}&", UrlEncode(normalizedUrl)); | ||
895 | 263 | signatureBase.AppendFormat("{0}", UrlEncode(normalizedRequestParameters)); | ||
896 | 264 | |||
897 | 265 | return signatureBase.ToString(); | ||
898 | 266 | } | ||
899 | 267 | |||
900 | 268 | /// <summary> | ||
901 | 269 | /// Generate the signature value based on the given signature base and hash algorithm | ||
902 | 270 | /// </summary> | ||
903 | 271 | /// <param name="signatureBase">The signature based as produced by the GenerateSignatureBase method or by any other means</param> | ||
904 | 272 | /// <param name="hash">The hash algorithm used to perform the hashing. If the hashing algorithm requires initialization or a key it should be set prior to calling this method</param> | ||
905 | 273 | /// <returns>A base64 string of the hash value</returns> | ||
906 | 274 | public string GenerateSignatureUsingHash(string signatureBase, HashAlgorithm hash) | ||
907 | 275 | { | ||
908 | 276 | return ComputeHash(hash, signatureBase); | ||
909 | 277 | } | ||
910 | 278 | |||
911 | 279 | /// <summary> | ||
912 | 280 | /// Generates a signature using the HMAC-SHA1 algorithm | ||
913 | 281 | /// </summary> | ||
914 | 282 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
915 | 283 | /// <param name="consumerKey">The consumer key</param> | ||
916 | 284 | /// <param name="consumerSecret">The consumer seceret</param> | ||
917 | 285 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
918 | 286 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
919 | 287 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
920 | 288 | /// <param name="timeStamp"></param> | ||
921 | 289 | /// <param name="nonce"></param> | ||
922 | 290 | /// <param name="normalizedUrl"></param> | ||
923 | 291 | /// <param name="normalizedRequestParameters"></param> | ||
924 | 292 | /// <returns>A base64 string of the hash value</returns> | ||
925 | 293 | public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, out string normalizedUrl, out string normalizedRequestParameters) | ||
926 | 294 | { | ||
927 | 295 | return GenerateSignature(url, consumerKey, consumerSecret, token, tokenSecret, httpMethod, timeStamp, nonce, SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters); | ||
928 | 296 | } | ||
929 | 297 | |||
930 | 298 | /// <summary> | ||
931 | 299 | /// Generates a signature using the specified signatureType | ||
932 | 300 | /// </summary> | ||
933 | 301 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
934 | 302 | /// <param name="consumerKey">The consumer key</param> | ||
935 | 303 | /// <param name="consumerSecret">The consumer seceret</param> | ||
936 | 304 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
937 | 305 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
938 | 306 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
939 | 307 | /// <param name="nonce"></param> | ||
940 | 308 | /// <param name="signatureType">The type of signature to use</param> | ||
941 | 309 | /// <param name="timeStamp"></param> | ||
942 | 310 | /// <param name="normalizedUrl"></param> | ||
943 | 311 | /// <param name="normalizedRequestParameters"></param> | ||
944 | 312 | /// <returns>A base64 string of the hash value</returns> | ||
945 | 313 | public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters) | ||
946 | 314 | { | ||
947 | 315 | normalizedUrl = null; | ||
948 | 316 | normalizedRequestParameters = null; | ||
949 | 317 | |||
950 | 318 | switch (signatureType) | ||
951 | 319 | { | ||
952 | 320 | case SignatureTypes.PLAINTEXT: | ||
953 | 321 | return HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret)); | ||
954 | 322 | case SignatureTypes.HMACSHA1: | ||
955 | 323 | var signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters); | ||
956 | 324 | |||
957 | 325 | var hmacsha1 = new HMACSHA1(); | ||
958 | 326 | hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret))); | ||
959 | 327 | |||
960 | 328 | return GenerateSignatureUsingHash(signatureBase, hmacsha1); | ||
961 | 329 | case SignatureTypes.RSASHA1: | ||
962 | 330 | throw new NotImplementedException(); | ||
963 | 331 | default: | ||
964 | 332 | throw new ArgumentException("Unknown signature type", "signatureType"); | ||
965 | 333 | } | ||
966 | 334 | } | ||
967 | 335 | |||
968 | 336 | /// <summary> | ||
969 | 337 | /// Returns a key value pair that contains a hedaer that can be used to authenticate a web requet. | ||
970 | 338 | /// </summary> | ||
971 | 339 | /// <param name="url">The uri with the web resource.</param> | ||
972 | 340 | /// <param name="realm">The real that was used for the oauth.</param> | ||
973 | 341 | /// <param name="consumerKey">The oauth consumer key.</param> | ||
974 | 342 | /// <param name="consumerSecret">The oauth consumer secret.</param> | ||
975 | 343 | /// <param name="token">The oauth token.</param> | ||
976 | 344 | /// <param name="tokenSecret">The aoauth token secret.</param> | ||
977 | 345 | /// <param name="httpMethod">The http method used for the request.</param> | ||
978 | 346 | /// <param name="timeStamp">A time stamp.</param> | ||
979 | 347 | /// <param name="nonce">A once</param> | ||
980 | 348 | /// <param name="signatureType">Teht ype of signature that will be used.</param> | ||
981 | 349 | /// <returns>A key value pair where the key is the name of the header and the value is the data of the header.</returns> | ||
982 | 350 | public KeyValuePair<string, string> GenerateHeaderWithSignature(Uri url, string realm, string consumerKey, | ||
983 | 351 | string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, | ||
984 | 352 | string nonce, SignatureTypes signatureType, out string normUrl, out string normParams) | ||
985 | 353 | { | ||
986 | 354 | var signature = GenerateSignature(url, | ||
987 | 355 | consumerKey, consumerSecret, token, tokenSecret, | ||
988 | 356 | httpMethod, timeStamp, nonce, SignatureTypes.HMACSHA1, | ||
989 | 357 | out normUrl, out normParams); | ||
990 | 358 | |||
991 | 359 | // Construct the OAuth header | ||
992 | 360 | var headerData = normParams + "&" + HttpUtility.UrlEncode("oauth_signature") + "=" + HttpUtility.UrlEncode(signature); | ||
993 | 361 | headerData = headerData.Replace("&", @""","); | ||
994 | 362 | headerData = headerData.Replace("=", @"="""); | ||
995 | 363 | headerData += @""""; | ||
996 | 364 | headerData = ((string.IsNullOrEmpty(realm)) ? "OAuth realm=\"\"," : | ||
997 | 365 | string.Format("OAuth realm=\"{0}\",", realm)) + headerData; | ||
998 | 366 | return new KeyValuePair<string, string>("Authorization", headerData); | ||
999 | 367 | } | ||
1000 | 368 | |||
1001 | 369 | /// <summary> | ||
1002 | 370 | /// Generate the timestamp for the signature | ||
1003 | 371 | /// </summary> | ||
1004 | 372 | /// <returns></returns> | ||
1005 | 373 | public virtual string GenerateTimeStamp() | ||
1006 | 374 | { | ||
1007 | 375 | // Default implementation of UNIX time of the current UTC time | ||
1008 | 376 | TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); | ||
1009 | 377 | return Convert.ToInt64(ts.TotalSeconds).ToString(); | ||
1010 | 378 | } | ||
1011 | 379 | |||
1012 | 380 | /// <summary> | ||
1013 | 381 | /// Generate a nonce | ||
1014 | 382 | /// </summary> | ||
1015 | 383 | /// <returns></returns> | ||
1016 | 384 | public virtual string GenerateNonce() | ||
1017 | 385 | { | ||
1018 | 386 | // Just a simple implementation of a random number between 123400 and 9999999 | ||
1019 | 387 | return Random.Next(123400, 9999999).ToString(); | ||
1020 | 388 | } | ||
1021 | 389 | |||
1022 | 390 | } | ||
1023 | 391 | } | ||
1024 | 0 | \ No newline at end of file | 392 | \ No newline at end of file |
1025 | 1 | 393 | ||
1026 | === removed file 'src/Canonical.Ubuntu.SSO/OAuthBase.cs' | |||
1027 | --- src/Canonical.Ubuntu.SSO/OAuthBase.cs 2010-10-05 09:58:34 +0000 | |||
1028 | +++ src/Canonical.Ubuntu.SSO/OAuthBase.cs 1970-01-01 00:00:00 +0000 | |||
1029 | @@ -1,358 +0,0 @@ | |||
1030 | 1 | using System; | ||
1031 | 2 | using System.Security.Cryptography; | ||
1032 | 3 | using System.Collections.Generic; | ||
1033 | 4 | using System.Text; | ||
1034 | 5 | using System.Web; | ||
1035 | 6 | |||
1036 | 7 | namespace OAuth | ||
1037 | 8 | { | ||
1038 | 9 | public class OAuthBase | ||
1039 | 10 | { | ||
1040 | 11 | |||
1041 | 12 | /// <summary> | ||
1042 | 13 | /// Provides a predefined set of algorithms that are supported officially by the protocol | ||
1043 | 14 | /// </summary> | ||
1044 | 15 | public enum SignatureTypes | ||
1045 | 16 | { | ||
1046 | 17 | HMACSHA1, | ||
1047 | 18 | PLAINTEXT, | ||
1048 | 19 | RSASHA1 | ||
1049 | 20 | } | ||
1050 | 21 | |||
1051 | 22 | /// <summary> | ||
1052 | 23 | /// Provides an internal structure to sort the query parameter | ||
1053 | 24 | /// </summary> | ||
1054 | 25 | protected class QueryParameter | ||
1055 | 26 | { | ||
1056 | 27 | private readonly string _name = null; | ||
1057 | 28 | private readonly string _value = null; | ||
1058 | 29 | |||
1059 | 30 | public QueryParameter(string name, string value) | ||
1060 | 31 | { | ||
1061 | 32 | _name = name; | ||
1062 | 33 | _value = value; | ||
1063 | 34 | } | ||
1064 | 35 | |||
1065 | 36 | public string Name | ||
1066 | 37 | { | ||
1067 | 38 | get { return _name; } | ||
1068 | 39 | } | ||
1069 | 40 | |||
1070 | 41 | public string Value | ||
1071 | 42 | { | ||
1072 | 43 | get { return _value; } | ||
1073 | 44 | } | ||
1074 | 45 | } | ||
1075 | 46 | |||
1076 | 47 | /// <summary> | ||
1077 | 48 | /// Comparer class used to perform the sorting of the query parameters | ||
1078 | 49 | /// </summary> | ||
1079 | 50 | protected class QueryParameterComparer : IComparer<QueryParameter> | ||
1080 | 51 | { | ||
1081 | 52 | |||
1082 | 53 | #region IComparer<QueryParameter> Members | ||
1083 | 54 | |||
1084 | 55 | public int Compare(QueryParameter x, QueryParameter y) | ||
1085 | 56 | { | ||
1086 | 57 | return x.Name == y.Name ? string.Compare(x.Value, y.Value) | ||
1087 | 58 | : string.Compare(x.Name, y.Name); | ||
1088 | 59 | } | ||
1089 | 60 | |||
1090 | 61 | #endregion | ||
1091 | 62 | } | ||
1092 | 63 | |||
1093 | 64 | protected const string OAuthVersion = "1.0"; | ||
1094 | 65 | protected const string OAuthParameterPrefix = "oauth_"; | ||
1095 | 66 | |||
1096 | 67 | // | ||
1097 | 68 | // List of know and used oauth parameters' names | ||
1098 | 69 | // | ||
1099 | 70 | protected const string OAuthConsumerKeyKey = "oauth_consumer_key"; | ||
1100 | 71 | protected const string OAuthCallbackKey = "oauth_callback"; | ||
1101 | 72 | protected const string OAuthVersionKey = "oauth_version"; | ||
1102 | 73 | protected const string OAuthSignatureMethodKey = "oauth_signature_method"; | ||
1103 | 74 | protected const string OAuthSignatureKey = "oauth_signature"; | ||
1104 | 75 | protected const string OAuthTimestampKey = "oauth_timestamp"; | ||
1105 | 76 | protected const string OAuthNonceKey = "oauth_nonce"; | ||
1106 | 77 | protected const string OAuthTokenKey = "oauth_token"; | ||
1107 | 78 | protected const string OAuthTokenSecretKey = "oauth_token_secret"; | ||
1108 | 79 | |||
1109 | 80 | protected const string HMACSHA1SignatureType = "HMAC-SHA1"; | ||
1110 | 81 | protected const string PlainTextSignatureType = "PLAINTEXT"; | ||
1111 | 82 | protected const string RSASHA1SignatureType = "RSA-SHA1"; | ||
1112 | 83 | |||
1113 | 84 | protected Random Random = new Random(); | ||
1114 | 85 | |||
1115 | 86 | protected string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; | ||
1116 | 87 | |||
1117 | 88 | /// <summary> | ||
1118 | 89 | /// Helper function to compute a hash value | ||
1119 | 90 | /// </summary> | ||
1120 | 91 | /// <param name="hashAlgorithm">The hashing algoirhtm used. If that algorithm needs some initialization, like HMAC and its derivatives, they should be initialized prior to passing it to this function</param> | ||
1121 | 92 | /// <param name="data">The data to hash</param> | ||
1122 | 93 | /// <returns>a Base64 string of the hash value</returns> | ||
1123 | 94 | private static string ComputeHash(HashAlgorithm hashAlgorithm, string data) | ||
1124 | 95 | { | ||
1125 | 96 | if (hashAlgorithm == null) | ||
1126 | 97 | { | ||
1127 | 98 | throw new ArgumentNullException("hashAlgorithm"); | ||
1128 | 99 | } | ||
1129 | 100 | |||
1130 | 101 | if (string.IsNullOrEmpty(data)) | ||
1131 | 102 | { | ||
1132 | 103 | throw new ArgumentNullException("data"); | ||
1133 | 104 | } | ||
1134 | 105 | |||
1135 | 106 | var dataBuffer = Encoding.ASCII.GetBytes(data); | ||
1136 | 107 | var hashBytes = hashAlgorithm.ComputeHash(dataBuffer); | ||
1137 | 108 | |||
1138 | 109 | return Convert.ToBase64String(hashBytes); | ||
1139 | 110 | } | ||
1140 | 111 | |||
1141 | 112 | /// <summary> | ||
1142 | 113 | /// Internal function to cut out all non oauth query string parameters (all parameters not begining with "oauth_") | ||
1143 | 114 | /// </summary> | ||
1144 | 115 | /// <param name="parameters">The query string part of the Url</param> | ||
1145 | 116 | /// <returns>A list of QueryParameter each containing the parameter name and value</returns> | ||
1146 | 117 | private static List<QueryParameter> GetQueryParameters(string parameters) | ||
1147 | 118 | { | ||
1148 | 119 | if (parameters.StartsWith("?")) | ||
1149 | 120 | { | ||
1150 | 121 | parameters = parameters.Remove(0, 1); | ||
1151 | 122 | } | ||
1152 | 123 | |||
1153 | 124 | var result = new List<QueryParameter>(); | ||
1154 | 125 | |||
1155 | 126 | if (!string.IsNullOrEmpty(parameters)) | ||
1156 | 127 | { | ||
1157 | 128 | string[] p = parameters.Split('&'); | ||
1158 | 129 | foreach (string s in p) | ||
1159 | 130 | { | ||
1160 | 131 | if (!string.IsNullOrEmpty(s) && !s.StartsWith(OAuthParameterPrefix)) | ||
1161 | 132 | { | ||
1162 | 133 | if (s.IndexOf('=') > -1) | ||
1163 | 134 | { | ||
1164 | 135 | string[] temp = s.Split('='); | ||
1165 | 136 | result.Add(new QueryParameter(temp[0], temp[1])); | ||
1166 | 137 | } | ||
1167 | 138 | else | ||
1168 | 139 | { | ||
1169 | 140 | result.Add(new QueryParameter(s, string.Empty)); | ||
1170 | 141 | } | ||
1171 | 142 | } | ||
1172 | 143 | } | ||
1173 | 144 | } | ||
1174 | 145 | |||
1175 | 146 | return result; | ||
1176 | 147 | } | ||
1177 | 148 | |||
1178 | 149 | /// <summary> | ||
1179 | 150 | /// This is a different Url Encode implementation since the default .NET one outputs the percent encoding in lower case. | ||
1180 | 151 | /// While this is not a problem with the percent encoding spec, it is used in upper case throughout OAuth | ||
1181 | 152 | /// </summary> | ||
1182 | 153 | /// <param name="value">The value to Url encode</param> | ||
1183 | 154 | /// <returns>Returns a Url encoded string</returns> | ||
1184 | 155 | protected string UrlEncode(string value) | ||
1185 | 156 | { | ||
1186 | 157 | var result = new StringBuilder(); | ||
1187 | 158 | |||
1188 | 159 | foreach (char symbol in value) | ||
1189 | 160 | { | ||
1190 | 161 | if (UnreservedChars.IndexOf(symbol) != -1) | ||
1191 | 162 | { | ||
1192 | 163 | result.Append(symbol); | ||
1193 | 164 | } | ||
1194 | 165 | else | ||
1195 | 166 | { | ||
1196 | 167 | result.Append('%' + String.Format("{0:X2}", (int)symbol)); | ||
1197 | 168 | } | ||
1198 | 169 | } | ||
1199 | 170 | |||
1200 | 171 | return result.ToString(); | ||
1201 | 172 | } | ||
1202 | 173 | |||
1203 | 174 | /// <summary> | ||
1204 | 175 | /// Normalizes the request parameters according to the spec | ||
1205 | 176 | /// </summary> | ||
1206 | 177 | /// <param name="parameters">The list of parameters already sorted</param> | ||
1207 | 178 | /// <returns>a string representing the normalized parameters</returns> | ||
1208 | 179 | protected string NormalizeRequestParameters(IList<QueryParameter> parameters) | ||
1209 | 180 | { | ||
1210 | 181 | var sb = new StringBuilder(); | ||
1211 | 182 | QueryParameter p; | ||
1212 | 183 | for (var i = 0; i < parameters.Count; i++) | ||
1213 | 184 | { | ||
1214 | 185 | p = parameters[i]; | ||
1215 | 186 | sb.AppendFormat("{0}={1}", p.Name, p.Value); | ||
1216 | 187 | |||
1217 | 188 | if (i < parameters.Count - 1) | ||
1218 | 189 | { | ||
1219 | 190 | sb.Append("&"); | ||
1220 | 191 | } | ||
1221 | 192 | } | ||
1222 | 193 | |||
1223 | 194 | return sb.ToString(); | ||
1224 | 195 | } | ||
1225 | 196 | |||
1226 | 197 | /// <summary> | ||
1227 | 198 | /// Generate the signature base that is used to produce the signature | ||
1228 | 199 | /// </summary> | ||
1229 | 200 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
1230 | 201 | /// <param name="consumerKey">The consumer key</param> | ||
1231 | 202 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
1232 | 203 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
1233 | 204 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
1234 | 205 | /// <param name="nonce"></param> | ||
1235 | 206 | /// <param name="signatureType">The signature type. To use the default values use <see cref="OAuthBase.SignatureTypes">OAuthBase.SignatureTypes</see>.</param> | ||
1236 | 207 | /// <param name="timeStamp"></param> | ||
1237 | 208 | /// <param name="normalizedUrl"></param> | ||
1238 | 209 | /// <param name="normalizedRequestParameters"></param> | ||
1239 | 210 | /// <returns>The signature base</returns> | ||
1240 | 211 | public string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, string signatureType, out string normalizedUrl, out string normalizedRequestParameters) | ||
1241 | 212 | { | ||
1242 | 213 | if (token == null) | ||
1243 | 214 | { | ||
1244 | 215 | token = string.Empty; | ||
1245 | 216 | } | ||
1246 | 217 | |||
1247 | 218 | if (tokenSecret == null) | ||
1248 | 219 | { | ||
1249 | 220 | tokenSecret = string.Empty; | ||
1250 | 221 | } | ||
1251 | 222 | |||
1252 | 223 | if (string.IsNullOrEmpty(consumerKey)) | ||
1253 | 224 | { | ||
1254 | 225 | throw new ArgumentNullException("consumerKey"); | ||
1255 | 226 | } | ||
1256 | 227 | |||
1257 | 228 | if (string.IsNullOrEmpty(httpMethod)) | ||
1258 | 229 | { | ||
1259 | 230 | throw new ArgumentNullException("httpMethod"); | ||
1260 | 231 | } | ||
1261 | 232 | |||
1262 | 233 | if (string.IsNullOrEmpty(signatureType)) | ||
1263 | 234 | { | ||
1264 | 235 | throw new ArgumentNullException("signatureType"); | ||
1265 | 236 | } | ||
1266 | 237 | |||
1267 | 238 | List<QueryParameter> parameters = GetQueryParameters(url.Query); | ||
1268 | 239 | parameters.Add(new QueryParameter(OAuthVersionKey, OAuthVersion)); | ||
1269 | 240 | parameters.Add(new QueryParameter(OAuthNonceKey, nonce)); | ||
1270 | 241 | parameters.Add(new QueryParameter(OAuthTimestampKey, timeStamp)); | ||
1271 | 242 | parameters.Add(new QueryParameter(OAuthSignatureMethodKey, signatureType)); | ||
1272 | 243 | parameters.Add(new QueryParameter(OAuthConsumerKeyKey, consumerKey)); | ||
1273 | 244 | |||
1274 | 245 | if (!string.IsNullOrEmpty(token)) | ||
1275 | 246 | { | ||
1276 | 247 | parameters.Add(new QueryParameter(OAuthTokenKey, token)); | ||
1277 | 248 | } | ||
1278 | 249 | |||
1279 | 250 | parameters.Sort(new QueryParameterComparer()); | ||
1280 | 251 | |||
1281 | 252 | normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host); | ||
1282 | 253 | if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443))) | ||
1283 | 254 | { | ||
1284 | 255 | normalizedUrl += ":" + url.Port; | ||
1285 | 256 | } | ||
1286 | 257 | normalizedUrl += url.AbsolutePath; | ||
1287 | 258 | normalizedRequestParameters = NormalizeRequestParameters(parameters); | ||
1288 | 259 | |||
1289 | 260 | var signatureBase = new StringBuilder(); | ||
1290 | 261 | signatureBase.AppendFormat("{0}&", httpMethod.ToUpper()); | ||
1291 | 262 | signatureBase.AppendFormat("{0}&", UrlEncode(normalizedUrl)); | ||
1292 | 263 | signatureBase.AppendFormat("{0}", UrlEncode(normalizedRequestParameters)); | ||
1293 | 264 | |||
1294 | 265 | return signatureBase.ToString(); | ||
1295 | 266 | } | ||
1296 | 267 | |||
1297 | 268 | /// <summary> | ||
1298 | 269 | /// Generate the signature value based on the given signature base and hash algorithm | ||
1299 | 270 | /// </summary> | ||
1300 | 271 | /// <param name="signatureBase">The signature based as produced by the GenerateSignatureBase method or by any other means</param> | ||
1301 | 272 | /// <param name="hash">The hash algorithm used to perform the hashing. If the hashing algorithm requires initialization or a key it should be set prior to calling this method</param> | ||
1302 | 273 | /// <returns>A base64 string of the hash value</returns> | ||
1303 | 274 | public string GenerateSignatureUsingHash(string signatureBase, HashAlgorithm hash) | ||
1304 | 275 | { | ||
1305 | 276 | return ComputeHash(hash, signatureBase); | ||
1306 | 277 | } | ||
1307 | 278 | |||
1308 | 279 | /// <summary> | ||
1309 | 280 | /// Generates a signature using the HMAC-SHA1 algorithm | ||
1310 | 281 | /// </summary> | ||
1311 | 282 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
1312 | 283 | /// <param name="consumerKey">The consumer key</param> | ||
1313 | 284 | /// <param name="consumerSecret">The consumer seceret</param> | ||
1314 | 285 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
1315 | 286 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
1316 | 287 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
1317 | 288 | /// <param name="timeStamp"></param> | ||
1318 | 289 | /// <param name="nonce"></param> | ||
1319 | 290 | /// <param name="normalizedUrl"></param> | ||
1320 | 291 | /// <param name="normalizedRequestParameters"></param> | ||
1321 | 292 | /// <returns>A base64 string of the hash value</returns> | ||
1322 | 293 | public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, out string normalizedUrl, out string normalizedRequestParameters) | ||
1323 | 294 | { | ||
1324 | 295 | return GenerateSignature(url, consumerKey, consumerSecret, token, tokenSecret, httpMethod, timeStamp, nonce, SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters); | ||
1325 | 296 | } | ||
1326 | 297 | |||
1327 | 298 | /// <summary> | ||
1328 | 299 | /// Generates a signature using the specified signatureType | ||
1329 | 300 | /// </summary> | ||
1330 | 301 | /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> | ||
1331 | 302 | /// <param name="consumerKey">The consumer key</param> | ||
1332 | 303 | /// <param name="consumerSecret">The consumer seceret</param> | ||
1333 | 304 | /// <param name="token">The token, if available. If not available pass null or an empty string</param> | ||
1334 | 305 | /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param> | ||
1335 | 306 | /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> | ||
1336 | 307 | /// <param name="nonce"></param> | ||
1337 | 308 | /// <param name="signatureType">The type of signature to use</param> | ||
1338 | 309 | /// <param name="timeStamp"></param> | ||
1339 | 310 | /// <param name="normalizedUrl"></param> | ||
1340 | 311 | /// <param name="normalizedRequestParameters"></param> | ||
1341 | 312 | /// <returns>A base64 string of the hash value</returns> | ||
1342 | 313 | public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters) | ||
1343 | 314 | { | ||
1344 | 315 | normalizedUrl = null; | ||
1345 | 316 | normalizedRequestParameters = null; | ||
1346 | 317 | |||
1347 | 318 | switch (signatureType) | ||
1348 | 319 | { | ||
1349 | 320 | case SignatureTypes.PLAINTEXT: | ||
1350 | 321 | return HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret)); | ||
1351 | 322 | case SignatureTypes.HMACSHA1: | ||
1352 | 323 | var signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters); | ||
1353 | 324 | |||
1354 | 325 | var hmacsha1 = new HMACSHA1(); | ||
1355 | 326 | hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret))); | ||
1356 | 327 | |||
1357 | 328 | return GenerateSignatureUsingHash(signatureBase, hmacsha1); | ||
1358 | 329 | case SignatureTypes.RSASHA1: | ||
1359 | 330 | throw new NotImplementedException(); | ||
1360 | 331 | default: | ||
1361 | 332 | throw new ArgumentException("Unknown signature type", "signatureType"); | ||
1362 | 333 | } | ||
1363 | 334 | } | ||
1364 | 335 | |||
1365 | 336 | /// <summary> | ||
1366 | 337 | /// Generate the timestamp for the signature | ||
1367 | 338 | /// </summary> | ||
1368 | 339 | /// <returns></returns> | ||
1369 | 340 | public virtual string GenerateTimeStamp() | ||
1370 | 341 | { | ||
1371 | 342 | // Default implementation of UNIX time of the current UTC time | ||
1372 | 343 | TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); | ||
1373 | 344 | return Convert.ToInt64(ts.TotalSeconds).ToString(); | ||
1374 | 345 | } | ||
1375 | 346 | |||
1376 | 347 | /// <summary> | ||
1377 | 348 | /// Generate a nonce | ||
1378 | 349 | /// </summary> | ||
1379 | 350 | /// <returns></returns> | ||
1380 | 351 | public virtual string GenerateNonce() | ||
1381 | 352 | { | ||
1382 | 353 | // Just a simple implementation of a random number between 123400 and 9999999 | ||
1383 | 354 | return Random.Next(123400, 9999999).ToString(); | ||
1384 | 355 | } | ||
1385 | 356 | |||
1386 | 357 | } | ||
1387 | 358 | } | ||
1388 | 359 | \ No newline at end of file | 0 | \ No newline at end of file |
1389 | 360 | 1 | ||
1390 | === added file 'src/Canonical.Ubuntu.SSO/SSOException.cs' | |||
1391 | --- src/Canonical.Ubuntu.SSO/SSOException.cs 1970-01-01 00:00:00 +0000 | |||
1392 | +++ src/Canonical.Ubuntu.SSO/SSOException.cs 2010-10-05 09:58:35 +0000 | |||
1393 | @@ -0,0 +1,57 @@ | |||
1394 | 1 | /* Copyright 2010 Canonical Ltd. | ||
1395 | 2 | * | ||
1396 | 3 | * This file is part of UbuntuOne on Windows. | ||
1397 | 4 | * | ||
1398 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
1399 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
1400 | 7 | * as published by the Free Software Foundation. | ||
1401 | 8 | * | ||
1402 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
1403 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1404 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1405 | 12 | * GNU Lesser General Public License for more details. | ||
1406 | 13 | * | ||
1407 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
1408 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
1409 | 16 | * | ||
1410 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
1411 | 18 | */ | ||
1412 | 19 | using System; | ||
1413 | 20 | |||
1414 | 21 | namespace Canonical.Ubuntu.SSO | ||
1415 | 22 | { | ||
1416 | 23 | /// <summary> | ||
1417 | 24 | /// Root exception class for all those SSO problems. | ||
1418 | 25 | /// </summary> | ||
1419 | 26 | public class SSOException : Exception | ||
1420 | 27 | { | ||
1421 | 28 | /// <summary> | ||
1422 | 29 | /// Creates a new instance of the class with no message or inner exception. | ||
1423 | 30 | /// </summary> | ||
1424 | 31 | public SSOException() | ||
1425 | 32 | { | ||
1426 | 33 | |||
1427 | 34 | } | ||
1428 | 35 | |||
1429 | 36 | /// <summary> | ||
1430 | 37 | /// Creates a new instance of the class with the given message. | ||
1431 | 38 | /// </summary> | ||
1432 | 39 | /// <param name="message">The message to be carried by the exception.</param> | ||
1433 | 40 | public SSOException(string message) | ||
1434 | 41 | : base(message) | ||
1435 | 42 | { | ||
1436 | 43 | |||
1437 | 44 | } | ||
1438 | 45 | |||
1439 | 46 | /// <summary> | ||
1440 | 47 | /// Creates a new instance of the class with the given message and inner exception. | ||
1441 | 48 | /// </summary> | ||
1442 | 49 | /// <param name="message">The message to be carried by the exception.</param> | ||
1443 | 50 | /// <param name="e">The inner exception.</param> | ||
1444 | 51 | public SSOException(string message, Exception e) | ||
1445 | 52 | : base(message, e) | ||
1446 | 53 | { | ||
1447 | 54 | |||
1448 | 55 | } | ||
1449 | 56 | } | ||
1450 | 57 | } | ||
1451 | 0 | 58 | ||
1452 | === added file 'src/Canonical.Ubuntu.SSO/SSOLoginException.cs' | |||
1453 | --- src/Canonical.Ubuntu.SSO/SSOLoginException.cs 1970-01-01 00:00:00 +0000 | |||
1454 | +++ src/Canonical.Ubuntu.SSO/SSOLoginException.cs 2010-10-05 09:58:35 +0000 | |||
1455 | @@ -0,0 +1,57 @@ | |||
1456 | 1 | /* Copyright 2010 Canonical Ltd. | ||
1457 | 2 | * | ||
1458 | 3 | * This file is part of UbuntuOne on Windows. | ||
1459 | 4 | * | ||
1460 | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
1461 | 6 | * it under the terms of the GNU Lesser General Public License version | ||
1462 | 7 | * as published by the Free Software Foundation. | ||
1463 | 8 | * | ||
1464 | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
1465 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1466 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1467 | 12 | * GNU Lesser General Public License for more details. | ||
1468 | 13 | * | ||
1469 | 14 | * You should have received a copy of the GNU Lesser General Public License | ||
1470 | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
1471 | 16 | * | ||
1472 | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
1473 | 18 | */ | ||
1474 | 19 | using System; | ||
1475 | 20 | |||
1476 | 21 | namespace Canonical.Ubuntu.SSO | ||
1477 | 22 | { | ||
1478 | 23 | /// <summary> | ||
1479 | 24 | /// Exception thrown when there are errors during a login. | ||
1480 | 25 | /// </summary> | ||
1481 | 26 | public class SSOLoginException : SSOException | ||
1482 | 27 | { | ||
1483 | 28 | /// <summary> | ||
1484 | 29 | /// Creates a new instance of the class with no message or inner exception. | ||
1485 | 30 | /// </summary> | ||
1486 | 31 | public SSOLoginException() | ||
1487 | 32 | { | ||
1488 | 33 | |||
1489 | 34 | } | ||
1490 | 35 | |||
1491 | 36 | /// <summary> | ||
1492 | 37 | /// Creates a new instance of the class with the given message. | ||
1493 | 38 | /// </summary> | ||
1494 | 39 | /// <param name="message">The message to be carried by the exception.</param> | ||
1495 | 40 | public SSOLoginException(string message) | ||
1496 | 41 | : base(message) | ||
1497 | 42 | { | ||
1498 | 43 | |||
1499 | 44 | } | ||
1500 | 45 | |||
1501 | 46 | /// <summary> | ||
1502 | 47 | /// Creates a new instance of the class with the given message and inner exception. | ||
1503 | 48 | /// </summary> | ||
1504 | 49 | /// <param name="message">The message to be carried by the exception.</param> | ||
1505 | 50 | /// <param name="e">The inner exception.</param> | ||
1506 | 51 | public SSOLoginException(string message, Exception e) | ||
1507 | 52 | : base(message, e) | ||
1508 | 53 | { | ||
1509 | 54 | |||
1510 | 55 | } | ||
1511 | 56 | } | ||
1512 | 57 | } | ||
1513 | 0 | 58 | ||
1514 | === modified file 'src/Canonical.Ubuntu.SSO/SSOLoginProcessor.cs' | |||
1515 | --- src/Canonical.Ubuntu.SSO/SSOLoginProcessor.cs 2010-09-20 11:21:19 +0000 | |||
1516 | +++ src/Canonical.Ubuntu.SSO/SSOLoginProcessor.cs 2010-10-05 09:58:35 +0000 | |||
1517 | @@ -18,6 +18,10 @@ | |||
1518 | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> |
1519 | 19 | */ | 19 | */ |
1520 | 20 | using System; | 20 | using System; |
1521 | 21 | using System.Net; | ||
1522 | 22 | using Canonical.Ubuntu.SSO.Service; | ||
1523 | 23 | using Canonical.UbuntuOne.Common.Net; | ||
1524 | 24 | using Newtonsoft.Json.Linq; | ||
1525 | 21 | 25 | ||
1526 | 22 | namespace Canonical.Ubuntu.SSO | 26 | namespace Canonical.Ubuntu.SSO |
1527 | 23 | { | 27 | { |
1528 | @@ -27,6 +31,126 @@ | |||
1529 | 27 | /// </summary> | 31 | /// </summary> |
1530 | 28 | public class SSOLoginProcessor : ISSOLoginProcessor | 32 | public class SSOLoginProcessor : ISSOLoginProcessor |
1531 | 29 | { | 33 | { |
1532 | 34 | private const string ApplicationTokenName = "Ubuntu One @ {0} (Windows)"; | ||
1533 | 35 | internal const string KeyringName = "Default"; | ||
1534 | 36 | internal const string ApplicationName = "UbuntuOne"; | ||
1535 | 37 | |||
1536 | 38 | #region DI properties | ||
1537 | 39 | |||
1538 | 40 | /// <summary> | ||
1539 | 41 | /// Gets and sets the Authentications rest service that is used to authenticate the user. | ||
1540 | 42 | /// </summary> | ||
1541 | 43 | public Authentications Authentications { get; set; } | ||
1542 | 44 | |||
1543 | 45 | /// <summary> | ||
1544 | 46 | /// Gets and sets the keyring that will be used to store the secrets. | ||
1545 | 47 | /// </summary> | ||
1546 | 48 | public IKeyring Keyring { get; set; } | ||
1547 | 49 | |||
1548 | 50 | /// <summary> | ||
1549 | 51 | /// Gets and sets the credential encoder that will be used to encode the oauth credentials | ||
1550 | 52 | /// before they are saved in the keyring. | ||
1551 | 53 | /// </summary> | ||
1552 | 54 | public ISSOCredentialsEncoder SSOCredentialsEncoder { get; set; } | ||
1553 | 55 | |||
1554 | 56 | /// <summary> | ||
1555 | 57 | /// Gets and sets the object that will take care of the oauth operations. | ||
1556 | 58 | /// </summary> | ||
1557 | 59 | public IOAuth OAuth { get; set; } | ||
1558 | 60 | |||
1559 | 61 | public IHttpWebRequestFactory HttpWebRequestFactory { get; set; } | ||
1560 | 62 | |||
1561 | 63 | #endregion | ||
1562 | 64 | |||
1563 | 65 | #region Helper methods} | ||
1564 | 66 | |||
1565 | 67 | /// <summary> | ||
1566 | 68 | /// Creates an oauth request that can access a protected web resource. | ||
1567 | 69 | /// </summary> | ||
1568 | 70 | /// <param name="uri">The uri where the resource is found.</param> | ||
1569 | 71 | /// <param name="httpMethod">The request berb to be used.</param> | ||
1570 | 72 | /// <param name="consumerKey">Consumer key from oauth.</param> | ||
1571 | 73 | /// <param name="consumerSecret">Consumer secret from oauth.</param> | ||
1572 | 74 | /// <param name="token">Token from oauth.</param> | ||
1573 | 75 | /// <param name="tokenSecret">Token secret from oauth.</param> | ||
1574 | 76 | /// <returns>A webrequest that can be used to access the resource.</returns> | ||
1575 | 77 | public IHttpWebRequest MakeRequest(string uri, string httpMethod, string consumerKey, string consumerSecret, string token, string tokenSecret) | ||
1576 | 78 | { | ||
1577 | 79 | // Form the full REST request url | ||
1578 | 80 | var url = new Uri(uri); | ||
1579 | 81 | string normUrl; | ||
1580 | 82 | string normParams; | ||
1581 | 83 | |||
1582 | 84 | // get oauth header | ||
1583 | 85 | var authHeader = OAuth.GenerateHeaderWithSignature(url, string.Empty, consumerKey, consumerSecret, | ||
1584 | 86 | token, tokenSecret, httpMethod, OAuth.GenerateTimeStamp(), OAuth.GenerateNonce(), SSO.OAuth.SignatureTypes.HMACSHA1, | ||
1585 | 87 | out normUrl, out normParams); | ||
1586 | 88 | |||
1587 | 89 | var request = HttpWebRequestFactory.Create(url); | ||
1588 | 90 | request.Headers.Add(authHeader.Key, authHeader.Value); | ||
1589 | 91 | return request; | ||
1590 | 92 | } | ||
1591 | 93 | |||
1592 | 94 | /// <summary> | ||
1593 | 95 | /// Pings the Ubuntu One server so that the SSO tokens are added to them and they know how to identify the user. | ||
1594 | 96 | /// </summary> | ||
1595 | 97 | /// <param name="email">The email used by the user to register to u1.</param> | ||
1596 | 98 | /// <param name="consumerKey">The oauth consumer key.</param> | ||
1597 | 99 | /// <param name="consumerSecret">The oauth consumer secret.</param> | ||
1598 | 100 | /// <param name="token">The oauth token.</param> | ||
1599 | 101 | /// <param name="tokenSecret">The oauth secret.</param> | ||
1600 | 102 | private void PingUbuntuOneServer(string email, string consumerKey, string consumerSecret, string token, string tokenSecret) | ||
1601 | 103 | { | ||
1602 | 104 | // ping the service to make it download the tokens | ||
1603 | 105 | var pingRequest = MakeRequest(Constants.PingUrl + email, "GET", consumerKey, consumerSecret, | ||
1604 | 106 | token, tokenSecret); | ||
1605 | 107 | try | ||
1606 | 108 | { | ||
1607 | 109 | pingRequest.GetResponse(); | ||
1608 | 110 | } | ||
1609 | 111 | catch (Exception e) | ||
1610 | 112 | { | ||
1611 | 113 | throw new SSOException("Credentials could not be sent to Ubuntu One servers", e); | ||
1612 | 114 | } | ||
1613 | 115 | } | ||
1614 | 116 | |||
1615 | 117 | /// <summary> | ||
1616 | 118 | /// Allows to parse the json that is returned by the rest service to extract that credentials. | ||
1617 | 119 | /// </summary> | ||
1618 | 120 | /// <param name="credentialsJson">Json that contains the credentials info.</param> | ||
1619 | 121 | /// <param name="consumerKey">Out var where the oauth consumer key will be returned.</param> | ||
1620 | 122 | /// <param name="consumerSecret">Out var where the oauth consumer secret will be returned.</param> | ||
1621 | 123 | /// <param name="token">Out var where the oauth token secret will be returned.</param> | ||
1622 | 124 | /// <param name="tokenSecret">Out var where the oauth token secret will be returned.</param> | ||
1623 | 125 | private static void ParseCredentials(string credentialsJson, out string consumerKey, out string consumerSecret, out string token, | ||
1624 | 126 | out string tokenSecret) | ||
1625 | 127 | { | ||
1626 | 128 | var credentials = JObject.Parse(credentialsJson); | ||
1627 | 129 | try | ||
1628 | 130 | { | ||
1629 | 131 | consumerKey = (string)credentials["consumer_key"]; | ||
1630 | 132 | consumerSecret = (string)credentials["consumer_secret"]; | ||
1631 | 133 | token = (string)credentials["token"]; | ||
1632 | 134 | tokenSecret = (string)credentials["token_secret"]; | ||
1633 | 135 | |||
1634 | 136 | } | ||
1635 | 137 | catch (InvalidCastException e) | ||
1636 | 138 | { | ||
1637 | 139 | // could happen when the json is not formatted as we expected. | ||
1638 | 140 | throw new SSOLoginException("Error when retrieving credentials.", e); | ||
1639 | 141 | } | ||
1640 | 142 | if (string.IsNullOrEmpty(consumerKey) | ||
1641 | 143 | || string.IsNullOrEmpty(consumerSecret) | ||
1642 | 144 | || string.IsNullOrEmpty(token) | ||
1643 | 145 | || string.IsNullOrEmpty(tokenSecret)) | ||
1644 | 146 | { | ||
1645 | 147 | // none of them can be null or empty. | ||
1646 | 148 | throw new SSOLoginException("Credentials have missing values.."); | ||
1647 | 149 | } | ||
1648 | 150 | |||
1649 | 151 | } | ||
1650 | 152 | #endregion | ||
1651 | 153 | |||
1652 | 30 | #region Implementation of ISSOLoginProcessor | 154 | #region Implementation of ISSOLoginProcessor |
1653 | 31 | 155 | ||
1654 | 32 | /// <summary> | 156 | /// <summary> |
1655 | @@ -61,7 +185,36 @@ | |||
1656 | 61 | /// <returns>A string with the sso credentials.</returns> | 185 | /// <returns>A string with the sso credentials.</returns> |
1657 | 62 | public string Login(string email, string password, string tokenName) | 186 | public string Login(string email, string password, string tokenName) |
1658 | 63 | { | 187 | { |
1660 | 64 | throw new NotImplementedException(); | 188 | var secret = Keyring.GetSecretByName(KeyringName, ApplicationName); |
1661 | 189 | if (secret == null) | ||
1662 | 190 | { | ||
1663 | 191 | try | ||
1664 | 192 | { | ||
1665 | 193 | // credentials are not present in the keyring, therefore we will have to get them from the | ||
1666 | 194 | // rest service | ||
1667 | 195 | string token; | ||
1668 | 196 | string tokenSecret; | ||
1669 | 197 | string consumerKey; | ||
1670 | 198 | string consumerSecret; | ||
1671 | 199 | |||
1672 | 200 | // the rest server returns json, we need to parse it. | ||
1673 | 201 | ParseCredentials(Authentications.Authenticate(email, password, tokenName), | ||
1674 | 202 | out consumerKey, out consumerSecret, out token, out tokenSecret); | ||
1675 | 203 | secret = SSOCredentialsEncoder.Encode(token, tokenSecret, consumerKey, consumerSecret); | ||
1676 | 204 | // ping the service to make it download the tokens | ||
1677 | 205 | PingUbuntuOneServer(email, consumerKey, consumerKey, token, tokenSecret); | ||
1678 | 206 | Keyring.CreateSecret(KeyringName, ApplicationName, secret); | ||
1679 | 207 | } | ||
1680 | 208 | catch (WebException e) | ||
1681 | 209 | { | ||
1682 | 210 | throw new SSOLoginException("Unable to login", e); | ||
1683 | 211 | } | ||
1684 | 212 | catch (Exception e) | ||
1685 | 213 | { | ||
1686 | 214 | throw new SSOLoginException("Unable to login", e); | ||
1687 | 215 | } | ||
1688 | 216 | } | ||
1689 | 217 | return secret; | ||
1690 | 65 | } | 218 | } |
1691 | 66 | 219 | ||
1692 | 67 | /// <summary> | 220 | /// <summary> |
1693 | 68 | 221 | ||
1694 | === modified file 'src/Canonical.UbuntuOne.Client/Notification/NotificationIconPresenter.cs' | |||
1695 | --- src/Canonical.UbuntuOne.Client/Notification/NotificationIconPresenter.cs 2010-10-05 09:58:34 +0000 | |||
1696 | +++ src/Canonical.UbuntuOne.Client/Notification/NotificationIconPresenter.cs 2010-10-05 09:58:35 +0000 | |||
1697 | @@ -1,25 +1,25 @@ | |||
1715 | 1 | // Copyright 2010 Canonical Ltd. | 1 | /* Copyright 2010 Canonical Ltd. |
1716 | 2 | // | 2 | * |
1717 | 3 | // This file is part of UbuntuOne on Windows. | 3 | * This file is part of UbuntuOne on Windows. |
1718 | 4 | // | 4 | * |
1719 | 5 | // UbuntuOne on Windows is free software: you can redistribute it and/or modify | 5 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify |
1720 | 6 | // it under the terms of the GNU Lesser General Public License version | 6 | * it under the terms of the GNU Lesser General Public License version |
1721 | 7 | // as published by the Free Software Foundation. | 7 | * as published by the Free Software Foundation. |
1722 | 8 | // | 8 | * |
1723 | 9 | // Ubuntu One on Windows is distributed in the hope that it will be useful, | 9 | * Ubuntu One on Windows is distributed in the hope that it will be useful, |
1724 | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1725 | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1726 | 12 | // GNU Lesser General Public License for more details. | 12 | * GNU Lesser General Public License for more details. |
1727 | 13 | // | 13 | * |
1728 | 14 | // You should have received a copy of the GNU Lesser General Public License | 14 | * You should have received a copy of the GNU Lesser General Public License |
1729 | 15 | // along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | 15 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. |
1730 | 16 | // | 16 | * |
1731 | 17 | // Authors: Manuel de la Peña <manuel.delapena@canonical.com> | 17 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> |
1732 | 18 | */ | ||
1733 | 18 | using System; | 19 | using System; |
1734 | 19 | using System.Diagnostics; | 20 | using System.Diagnostics; |
1735 | 20 | using System.IO; | 21 | using System.IO; |
1736 | 21 | using System.Net; | 22 | using System.Net; |
1737 | 22 | using System.Web; | ||
1738 | 23 | using System.Windows; | 23 | using System.Windows; |
1739 | 24 | using Canonical.Ubuntu.SSO; | 24 | using Canonical.Ubuntu.SSO; |
1740 | 25 | using Canonical.Ubuntu.SSO.Service; | 25 | using Canonical.Ubuntu.SSO.Service; |
1741 | @@ -29,7 +29,6 @@ | |||
1742 | 29 | using Canonical.UbuntuOne.Common.Utils; | 29 | using Canonical.UbuntuOne.Common.Utils; |
1743 | 30 | using log4net; | 30 | using log4net; |
1744 | 31 | using Newtonsoft.Json.Linq; | 31 | using Newtonsoft.Json.Linq; |
1745 | 32 | using OAuth; | ||
1746 | 33 | 32 | ||
1747 | 34 | namespace Canonical.UbuntuOne.Client.Notification | 33 | namespace Canonical.UbuntuOne.Client.Notification |
1748 | 35 | { | 34 | { |
1749 | @@ -148,33 +147,23 @@ | |||
1750 | 148 | 147 | ||
1751 | 149 | #region Helper methods | 148 | #region Helper methods |
1752 | 150 | 149 | ||
1754 | 151 | public HttpWebRequest MakeRequest(string uri, string consumerKey, string consumerSecret, string toke, string tokenSecret) | 150 | public HttpWebRequest MakeRequest(string uri, string consumerKey, string consumerSecret, string token, string tokenSecret) |
1755 | 152 | { | 151 | { |
1756 | 153 | // Form the full REST request url | 152 | // Form the full REST request url |
1757 | 154 | Uri url = new Uri(uri); | 153 | Uri url = new Uri(uri); |
1758 | 155 | 154 | ||
1759 | 156 | // Instantiate OAuthBase and declare variables | 155 | // Instantiate OAuthBase and declare variables |
1763 | 157 | var oAuth = new OAuthBase(); | 156 | var oAuth = new OAuth(); |
1764 | 158 | var nonce = oAuth.GenerateNonce(); | 157 | |
1762 | 159 | var timeStamp = oAuth.GenerateTimeStamp(); | ||
1765 | 160 | var normUrl = string.Empty; | 158 | var normUrl = string.Empty; |
1766 | 161 | var normParams = string.Empty; | 159 | var normParams = string.Empty; |
1767 | 162 | var strRequest = string.Empty; | ||
1768 | 163 | 160 | ||
1773 | 164 | // Create an OAuth signature | 161 | var authHeader = oAuth.GenerateHeaderWithSignature(url, string.Empty, consumerKey, consumerSecret, |
1774 | 165 | string signature = oAuth.GenerateSignature(url, | 162 | token, tokenSecret, "GET", oAuth.GenerateTimeStamp(), oAuth.GenerateNonce(), OAuth.SignatureTypes.HMACSHA1, |
1771 | 166 | consumerKey, consumerSecret, toke, tokenSecret, | ||
1772 | 167 | "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, | ||
1775 | 168 | out normUrl, out normParams); | 163 | out normUrl, out normParams); |
1783 | 169 | 164 | ||
1777 | 170 | // Construct the OAuth authenticated REST url | ||
1778 | 171 | strRequest = normParams + "&" + HttpUtility.UrlEncode("oauth_signature") + "=" + HttpUtility.UrlEncode(signature); | ||
1779 | 172 | strRequest = strRequest.Replace("&", @""","); | ||
1780 | 173 | strRequest = strRequest.Replace("=", @"="""); | ||
1781 | 174 | strRequest += @""""; | ||
1782 | 175 | strRequest = @"OAuth realm=""""," + strRequest; | ||
1784 | 176 | var request = WebRequest.Create(normUrl) as HttpWebRequest; | 165 | var request = WebRequest.Create(normUrl) as HttpWebRequest; |
1786 | 177 | request.Headers.Add("Authorization", strRequest); | 166 | request.Headers.Add(authHeader.Key, authHeader.Value); |
1787 | 178 | return request; | 167 | return request; |
1788 | 179 | } | 168 | } |
1789 | 180 | 169 | ||
1790 | 181 | 170 | ||
1791 | === modified file 'src/Canonical.UbuntuOne.Common/Canonical.UbuntuOne.Common.csproj' | |||
1792 | --- src/Canonical.UbuntuOne.Common/Canonical.UbuntuOne.Common.csproj 2010-08-30 17:30:31 +0000 | |||
1793 | +++ src/Canonical.UbuntuOne.Common/Canonical.UbuntuOne.Common.csproj 2010-10-05 09:58:35 +0000 | |||
1794 | @@ -57,6 +57,10 @@ | |||
1795 | 57 | <Compile Include="Container\ObjectsContainer.cs" /> | 57 | <Compile Include="Container\ObjectsContainer.cs" /> |
1796 | 58 | <Compile Include="Container\SpringContainer.cs" /> | 58 | <Compile Include="Container\SpringContainer.cs" /> |
1797 | 59 | <Compile Include="Container\UnsatisfiedDependencyException.cs" /> | 59 | <Compile Include="Container\UnsatisfiedDependencyException.cs" /> |
1798 | 60 | <Compile Include="Net\HttpWebRequest.cs" /> | ||
1799 | 61 | <Compile Include="Net\HttpWebRequestFactory.cs" /> | ||
1800 | 62 | <Compile Include="Net\IHttpWebRequestFactory.cs" /> | ||
1801 | 63 | <Compile Include="Net\IHttpWebRequest.cs" /> | ||
1802 | 60 | <Compile Include="OperationContracts\IEventNotifier.cs" /> | 64 | <Compile Include="OperationContracts\IEventNotifier.cs" /> |
1803 | 61 | <Compile Include="Security\IAuthentication.cs" /> | 65 | <Compile Include="Security\IAuthentication.cs" /> |
1804 | 62 | <Compile Include="Threading\IDispatcher.cs" /> | 66 | <Compile Include="Threading\IDispatcher.cs" /> |
1805 | 63 | 67 | ||
1806 | === added directory 'src/Canonical.UbuntuOne.Common/Net' | |||
1807 | === added file 'src/Canonical.UbuntuOne.Common/Net/HttpWebRequest.cs' | |||
1808 | --- src/Canonical.UbuntuOne.Common/Net/HttpWebRequest.cs 1970-01-01 00:00:00 +0000 | |||
1809 | +++ src/Canonical.UbuntuOne.Common/Net/HttpWebRequest.cs 2010-10-05 09:58:35 +0000 | |||
1810 | @@ -0,0 +1,505 @@ | |||
1811 | 1 | /* | ||
1812 | 2 | * Copyright 2010 Canonical Ltd. | ||
1813 | 3 | * | ||
1814 | 4 | * This file is part of UbuntuOne on Windows. | ||
1815 | 5 | * | ||
1816 | 6 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
1817 | 7 | * it under the terms of the GNU Lesser General Public License version | ||
1818 | 8 | * as published by the Free Software Foundation. | ||
1819 | 9 | * | ||
1820 | 10 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
1821 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1822 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1823 | 13 | * GNU Lesser General Public License for more details. | ||
1824 | 14 | * | ||
1825 | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
1826 | 16 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
1827 | 17 | * | ||
1828 | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
1829 | 19 | */ | ||
1830 | 20 | using System; | ||
1831 | 21 | using System.IO; | ||
1832 | 22 | using System.Net; | ||
1833 | 23 | using System.Net.Cache; | ||
1834 | 24 | using System.Net.Security; | ||
1835 | 25 | using System.Security.Cryptography.X509Certificates; | ||
1836 | 26 | using System.Security.Principal; | ||
1837 | 27 | using RealHttpRequest = System.Net.HttpWebRequest; | ||
1838 | 28 | |||
1839 | 29 | namespace Canonical.UbuntuOne.Common.Net | ||
1840 | 30 | { | ||
1841 | 31 | /// <summary> | ||
1842 | 32 | /// Wrapper around the System.Net.HttpWebRequest. | ||
1843 | 33 | /// </summary> | ||
1844 | 34 | internal class HttpWebRequest : IHttpWebRequest | ||
1845 | 35 | { | ||
1846 | 36 | #region Variables | ||
1847 | 37 | |||
1848 | 38 | private RealHttpRequest _request; | ||
1849 | 39 | |||
1850 | 40 | #endregion | ||
1851 | 41 | |||
1852 | 42 | #region Constructors | ||
1853 | 43 | |||
1854 | 44 | /// <summary> | ||
1855 | 45 | /// Internal constructor that can only be used by a httpweb request factory. | ||
1856 | 46 | /// </summary> | ||
1857 | 47 | /// <param name="httpWebRequest"></param> | ||
1858 | 48 | internal HttpWebRequest(RealHttpRequest httpWebRequest) | ||
1859 | 49 | { | ||
1860 | 50 | _request = httpWebRequest; | ||
1861 | 51 | } | ||
1862 | 52 | |||
1863 | 53 | #endregion | ||
1864 | 54 | |||
1865 | 55 | #region Implementation of IHttpWebRequest | ||
1866 | 56 | |||
1867 | 57 | /// <summary> | ||
1868 | 58 | /// Gets and sets value of the Accept HTTP header. The default value is null. | ||
1869 | 59 | /// </summary> | ||
1870 | 60 | public string Accept | ||
1871 | 61 | { | ||
1872 | 62 | get { return _request.Accept; } | ||
1873 | 63 | set { _request.Accept = value; } | ||
1874 | 64 | } | ||
1875 | 65 | |||
1876 | 66 | /// <summary> | ||
1877 | 67 | /// Gets and sets the uri that responds to the request. | ||
1878 | 68 | /// </summary> | ||
1879 | 69 | public Uri Address | ||
1880 | 70 | { | ||
1881 | 71 | get { return _request.Address; } | ||
1882 | 72 | } | ||
1883 | 73 | |||
1884 | 74 | /// <summary> | ||
1885 | 75 | /// Gets and sets if the request should automatically follow redirection responses | ||
1886 | 76 | /// </summary> | ||
1887 | 77 | public bool AllowAutoRedirect | ||
1888 | 78 | { | ||
1889 | 79 | get { return _request.AllowAutoRedirect; } | ||
1890 | 80 | set { _request.AllowAutoRedirect = value; } | ||
1891 | 81 | } | ||
1892 | 82 | |||
1893 | 83 | /// <summary> | ||
1894 | 84 | /// Enables buffering of the data sent to the Internet resource | ||
1895 | 85 | /// </summary> | ||
1896 | 86 | public bool AllowWriteStreamBuffering | ||
1897 | 87 | { | ||
1898 | 88 | get { return _request.AllowWriteStreamBuffering; } | ||
1899 | 89 | set { _request.AllowWriteStreamBuffering = value; } | ||
1900 | 90 | } | ||
1901 | 91 | |||
1902 | 92 | /// <summary> | ||
1903 | 93 | /// Gets and sets the auhentication lavel used. | ||
1904 | 94 | /// </summary> | ||
1905 | 95 | public AuthenticationLevel AuthenticationLevel | ||
1906 | 96 | { | ||
1907 | 97 | get { return _request.AuthenticationLevel; } | ||
1908 | 98 | set { _request.AuthenticationLevel = value; } | ||
1909 | 99 | } | ||
1910 | 100 | |||
1911 | 101 | /// <summary> | ||
1912 | 102 | /// Gets and sets the type of decompression that is used. | ||
1913 | 103 | /// </summary> | ||
1914 | 104 | public DecompressionMethods AutomaticDecompression | ||
1915 | 105 | { | ||
1916 | 106 | get { return _request.AutomaticDecompression; } | ||
1917 | 107 | set { _request.AutomaticDecompression = value; } | ||
1918 | 108 | } | ||
1919 | 109 | |||
1920 | 110 | /// <summary> | ||
1921 | 111 | /// Gets or sets the cache policy used by the request. | ||
1922 | 112 | /// </summary> | ||
1923 | 113 | public RequestCachePolicy CachePolicy | ||
1924 | 114 | { | ||
1925 | 115 | get { return _request.CachePolicy; } | ||
1926 | 116 | set { _request.CachePolicy = value; } | ||
1927 | 117 | } | ||
1928 | 118 | |||
1929 | 119 | /// <summary> | ||
1930 | 120 | /// Gets and sets the certificates used by the request. | ||
1931 | 121 | /// </summary> | ||
1932 | 122 | public X509CertificateCollection ClientCertificates | ||
1933 | 123 | { | ||
1934 | 124 | get { return _request.ClientCertificates; } | ||
1935 | 125 | set { _request.ClientCertificates = value; } | ||
1936 | 126 | } | ||
1937 | 127 | |||
1938 | 128 | /// <summary> | ||
1939 | 129 | /// Gets and sets the Connection HTTP header | ||
1940 | 130 | /// </summary> | ||
1941 | 131 | public string Connection | ||
1942 | 132 | { | ||
1943 | 133 | get { return _request.Connection; } | ||
1944 | 134 | set { _request.Connection = value; } | ||
1945 | 135 | } | ||
1946 | 136 | |||
1947 | 137 | /// <summary> | ||
1948 | 138 | /// Gets and sets the name of the connection group for this request | ||
1949 | 139 | /// </summary> | ||
1950 | 140 | public string ConnectionGroupName | ||
1951 | 141 | { | ||
1952 | 142 | get { return _request.ConnectionGroupName; } | ||
1953 | 143 | set { _request.ConnectionGroupName = value; } | ||
1954 | 144 | } | ||
1955 | 145 | |||
1956 | 146 | /// <summary> | ||
1957 | 147 | /// Gets and sets the number of bytes of data to send to the Internet resource. | ||
1958 | 148 | /// </summary> | ||
1959 | 149 | public long ContentLength | ||
1960 | 150 | { | ||
1961 | 151 | get { return _request.ContentLength; } | ||
1962 | 152 | set { _request.ContentLength = value; } | ||
1963 | 153 | } | ||
1964 | 154 | |||
1965 | 155 | /// <summary> | ||
1966 | 156 | /// Gets and sets the content type of the request. | ||
1967 | 157 | /// </summary> | ||
1968 | 158 | public string ContentType | ||
1969 | 159 | { | ||
1970 | 160 | get { return _request.ContentType; } | ||
1971 | 161 | set { _request.ContentType = value; } | ||
1972 | 162 | } | ||
1973 | 163 | |||
1974 | 164 | /// <summary> | ||
1975 | 165 | /// Gets and sets the delegate that implements the callback method that | ||
1976 | 166 | /// executes when an HTTP Continue response is returned from the Internet resource. | ||
1977 | 167 | /// </summary> | ||
1978 | 168 | public HttpContinueDelegate ContinueDelegate | ||
1979 | 169 | { | ||
1980 | 170 | get { return _request.ContinueDelegate; } | ||
1981 | 171 | set { _request.ContinueDelegate = value; } | ||
1982 | 172 | } | ||
1983 | 173 | |||
1984 | 174 | /// <summary> | ||
1985 | 175 | /// Gets and sets cookies container associated with this request. | ||
1986 | 176 | /// </summary> | ||
1987 | 177 | public CookieContainer CookieContainer | ||
1988 | 178 | { | ||
1989 | 179 | get { return _request.CookieContainer; } | ||
1990 | 180 | set { _request.CookieContainer = value; } | ||
1991 | 181 | } | ||
1992 | 182 | |||
1993 | 183 | /// <summary> | ||
1994 | 184 | /// Gets and sets the credentials used with the request. | ||
1995 | 185 | /// </summary> | ||
1996 | 186 | public ICredentials Credentials | ||
1997 | 187 | { | ||
1998 | 188 | get { return _request.Credentials; } | ||
1999 | 189 | set { _request.Credentials = value; } | ||
2000 | 190 | } | ||
2001 | 191 | |||
2002 | 192 | /// <summary> | ||
2003 | 193 | /// Gets and sets the data header of the request. | ||
2004 | 194 | /// </summary> | ||
2005 | 195 | public DateTime Date | ||
2006 | 196 | { | ||
2007 | 197 | get { throw new NotImplementedException(); } | ||
2008 | 198 | set { throw new NotImplementedException(); } | ||
2009 | 199 | } | ||
2010 | 200 | |||
2011 | 201 | /// <summary> | ||
2012 | 202 | /// Gets and sets the Expect HTTP header. | ||
2013 | 203 | /// </summary> | ||
2014 | 204 | public string Expect | ||
2015 | 205 | { | ||
2016 | 206 | get { return _request.Expect; } | ||
2017 | 207 | set { _request.Expect = value; } | ||
2018 | 208 | } | ||
2019 | 209 | |||
2020 | 210 | /// <summary> | ||
2021 | 211 | /// Gets if a response has been received. | ||
2022 | 212 | /// </summary> | ||
2023 | 213 | public bool HaveResponse | ||
2024 | 214 | { | ||
2025 | 215 | get { return _request.HaveResponse; } | ||
2026 | 216 | } | ||
2027 | 217 | |||
2028 | 218 | /// <summary> | ||
2029 | 219 | /// Gets and sets the headers of the request, | ||
2030 | 220 | /// </summary> | ||
2031 | 221 | public WebHeaderCollection Headers | ||
2032 | 222 | { | ||
2033 | 223 | get { return _request.Headers; } | ||
2034 | 224 | set { _request.Headers = value; } | ||
2035 | 225 | } | ||
2036 | 226 | |||
2037 | 227 | /// <summary> | ||
2038 | 228 | /// Gets and sets the Host header. | ||
2039 | 229 | /// </summary> | ||
2040 | 230 | public string Host | ||
2041 | 231 | { | ||
2042 | 232 | get { throw new NotImplementedException(); } | ||
2043 | 233 | set { throw new NotImplementedException(); } | ||
2044 | 234 | } | ||
2045 | 235 | |||
2046 | 236 | /// <summary> | ||
2047 | 237 | /// A DateTime that contains the contents of the If-Modified-Since HTTP header. | ||
2048 | 238 | /// </summary> | ||
2049 | 239 | public DateTime IfModifiedSince | ||
2050 | 240 | { | ||
2051 | 241 | get { return _request.IfModifiedSince; } | ||
2052 | 242 | set { _request.IfModifiedSince = value; } | ||
2053 | 243 | } | ||
2054 | 244 | |||
2055 | 245 | /// <summary> | ||
2056 | 246 | /// Gets and sets the the impersonation level. | ||
2057 | 247 | /// </summary> | ||
2058 | 248 | public TokenImpersonationLevel ImpersonationLevel | ||
2059 | 249 | { | ||
2060 | 250 | get { return _request.ImpersonationLevel; } | ||
2061 | 251 | set { _request.ImpersonationLevel = value; } | ||
2062 | 252 | } | ||
2063 | 253 | |||
2064 | 254 | /// <summary> | ||
2065 | 255 | /// Gets and sets if the request should have a Connection HTTP header. | ||
2066 | 256 | /// </summary> | ||
2067 | 257 | public bool KeepAlive | ||
2068 | 258 | { | ||
2069 | 259 | get { return _request.KeepAlive; } | ||
2070 | 260 | set { _request.KeepAlive = value; } | ||
2071 | 261 | } | ||
2072 | 262 | |||
2073 | 263 | /// <summary> | ||
2074 | 264 | /// Gets and sets the max number of redirections. | ||
2075 | 265 | /// </summary> | ||
2076 | 266 | public int MaximumAutomaticRedirections | ||
2077 | 267 | { | ||
2078 | 268 | get { return _request.MaximumAutomaticRedirections; } | ||
2079 | 269 | set { _request.MaximumAutomaticRedirections = value; } | ||
2080 | 270 | } | ||
2081 | 271 | |||
2082 | 272 | /// <summary> | ||
2083 | 273 | /// Gets and sets The length, in kilobytes (1024 bytes), of the response headers. | ||
2084 | 274 | /// </summary> | ||
2085 | 275 | public int MaximumResponseHeadersLength | ||
2086 | 276 | { | ||
2087 | 277 | get { return _request.MaximumResponseHeadersLength; } | ||
2088 | 278 | set { _request.MaximumResponseHeadersLength = value; } | ||
2089 | 279 | } | ||
2090 | 280 | |||
2091 | 281 | /// <summary> | ||
2092 | 282 | /// The media type of the request. | ||
2093 | 283 | /// </summary> | ||
2094 | 284 | public string MediaType | ||
2095 | 285 | { | ||
2096 | 286 | get { return _request.MediaType; } | ||
2097 | 287 | set { _request.MediaType = value; } | ||
2098 | 288 | } | ||
2099 | 289 | |||
2100 | 290 | /// <summary> | ||
2101 | 291 | /// Gets and sets the request method. | ||
2102 | 292 | /// </summary> | ||
2103 | 293 | public string Method | ||
2104 | 294 | { | ||
2105 | 295 | get { return _request.Method; } | ||
2106 | 296 | set { _request.Method = value; } | ||
2107 | 297 | } | ||
2108 | 298 | |||
2109 | 299 | /// <summary> | ||
2110 | 300 | /// Gets and sets if the request should be pipelined | ||
2111 | 301 | /// </summary> | ||
2112 | 302 | public bool Pipelined | ||
2113 | 303 | { | ||
2114 | 304 | get { return _request.Pipelined; } | ||
2115 | 305 | set { _request.Pipelined = value; } | ||
2116 | 306 | } | ||
2117 | 307 | |||
2118 | 308 | /// <summary> | ||
2119 | 309 | /// Gets and sets if an HTTP Authorization header with requests after authentication has taken place; | ||
2120 | 310 | /// </summary> | ||
2121 | 311 | public bool PreAuthenticate | ||
2122 | 312 | { | ||
2123 | 313 | get { return _request.PreAuthenticate; } | ||
2124 | 314 | set { _request.PreAuthenticate = value; } | ||
2125 | 315 | } | ||
2126 | 316 | |||
2127 | 317 | /// <summary> | ||
2128 | 318 | /// Gets and sets the protol version used. | ||
2129 | 319 | /// </summary> | ||
2130 | 320 | public Version ProtocolVersion | ||
2131 | 321 | { | ||
2132 | 322 | get { return _request.ProtocolVersion; } | ||
2133 | 323 | set { _request.ProtocolVersion = value; } | ||
2134 | 324 | } | ||
2135 | 325 | |||
2136 | 326 | /// <summary> | ||
2137 | 327 | /// Gets and sets the proxy information. | ||
2138 | 328 | /// </summary> | ||
2139 | 329 | public IWebProxy Proxy | ||
2140 | 330 | { | ||
2141 | 331 | get { return _request.Proxy; } | ||
2142 | 332 | set { _request.Proxy = value; } | ||
2143 | 333 | } | ||
2144 | 334 | |||
2145 | 335 | /// <summary> | ||
2146 | 336 | /// Gets and sets the number of milliseconds before the writing or reading times out.. | ||
2147 | 337 | /// </summary> | ||
2148 | 338 | public int ReadWriteTimeout | ||
2149 | 339 | { | ||
2150 | 340 | get { return _request.ReadWriteTimeout; } | ||
2151 | 341 | set { _request.ReadWriteTimeout = value; } | ||
2152 | 342 | } | ||
2153 | 343 | |||
2154 | 344 | /// <summary> | ||
2155 | 345 | /// Gets and sets the Referer HTTP header. | ||
2156 | 346 | /// </summary> | ||
2157 | 347 | public string Referer | ||
2158 | 348 | { | ||
2159 | 349 | get { return _request.Referer; } | ||
2160 | 350 | set { _request.Referer = value; } | ||
2161 | 351 | } | ||
2162 | 352 | |||
2163 | 353 | /// <summary> | ||
2164 | 354 | /// Gets the resource uri used by the request. | ||
2165 | 355 | /// </summary> | ||
2166 | 356 | public Uri RequestUri | ||
2167 | 357 | { | ||
2168 | 358 | get { return _request.RequestUri; } | ||
2169 | 359 | } | ||
2170 | 360 | |||
2171 | 361 | /// <summary> | ||
2172 | 362 | /// Ges and sets if the request should be sent in chuckes. | ||
2173 | 363 | /// </summary> | ||
2174 | 364 | public bool SendChunked | ||
2175 | 365 | { | ||
2176 | 366 | get { return _request.SendChunked; } | ||
2177 | 367 | set { _request.SendChunked = value; } | ||
2178 | 368 | } | ||
2179 | 369 | |||
2180 | 370 | /// <summary> | ||
2181 | 371 | /// Gets the service point that represents the internet resource. | ||
2182 | 372 | /// </summary> | ||
2183 | 373 | public ServicePoint ServicePoint | ||
2184 | 374 | { | ||
2185 | 375 | get { return _request.ServicePoint; } | ||
2186 | 376 | } | ||
2187 | 377 | |||
2188 | 378 | /// <summary> | ||
2189 | 379 | /// Gets and sets the number of milliseconds to wait before the request times out. | ||
2190 | 380 | /// </summary> | ||
2191 | 381 | public int Timeout | ||
2192 | 382 | { | ||
2193 | 383 | get { return _request.Timeout; } | ||
2194 | 384 | set { _request.Timeout = value; } | ||
2195 | 385 | } | ||
2196 | 386 | |||
2197 | 387 | /// <summary> | ||
2198 | 388 | /// Gets and sets the Transfer-encoding HTTP header. | ||
2199 | 389 | /// </summary> | ||
2200 | 390 | public string TransferEncoding | ||
2201 | 391 | { | ||
2202 | 392 | get { return _request.TransferEncoding; } | ||
2203 | 393 | set { _request.TransferEncoding = value; } | ||
2204 | 394 | } | ||
2205 | 395 | |||
2206 | 396 | /// <summary> | ||
2207 | 397 | /// Gets and sets if the authenticated connection is kept opened. | ||
2208 | 398 | /// </summary> | ||
2209 | 399 | public bool UnsafeAuthenticatedConnectionSharing | ||
2210 | 400 | { | ||
2211 | 401 | get { return _request.UnsafeAuthenticatedConnectionSharing; } | ||
2212 | 402 | set { _request.UnsafeAuthenticatedConnectionSharing = value; } | ||
2213 | 403 | } | ||
2214 | 404 | |||
2215 | 405 | /// <summary> | ||
2216 | 406 | /// Gets and sets if the default credentials are used. | ||
2217 | 407 | /// </summary> | ||
2218 | 408 | public bool UseDefaultCredentials | ||
2219 | 409 | { | ||
2220 | 410 | get { return _request.UseDefaultCredentials; } | ||
2221 | 411 | set { _request.UseDefaultCredentials = value; } | ||
2222 | 412 | } | ||
2223 | 413 | |||
2224 | 414 | /// <summary> | ||
2225 | 415 | /// Gets or sets the User-agent HTTP header; | ||
2226 | 416 | /// </summary> | ||
2227 | 417 | public string UserAgent | ||
2228 | 418 | { | ||
2229 | 419 | get { return _request.UserAgent; } | ||
2230 | 420 | set { _request.UserAgent = value; } | ||
2231 | 421 | } | ||
2232 | 422 | |||
2233 | 423 | /// <summary> | ||
2234 | 424 | /// Cancels a request to a resource | ||
2235 | 425 | /// </summary> | ||
2236 | 426 | public void Abort() | ||
2237 | 427 | { | ||
2238 | 428 | _request.Abort(); | ||
2239 | 429 | } | ||
2240 | 430 | |||
2241 | 431 | /// <summary> | ||
2242 | 432 | /// Begins an asynchronous request for a Stream object to use to write data. | ||
2243 | 433 | /// </summary> | ||
2244 | 434 | /// <param name="callback">The delegate to execute.</param> | ||
2245 | 435 | /// <param name="state">Teh state object of the request. </param> | ||
2246 | 436 | /// <returns>An async result.</returns> | ||
2247 | 437 | public IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) | ||
2248 | 438 | { | ||
2249 | 439 | return _request.BeginGetRequestStream(callback, state); | ||
2250 | 440 | } | ||
2251 | 441 | |||
2252 | 442 | /// <summary> | ||
2253 | 443 | /// Begins an asynchronous request to an Internet resource. | ||
2254 | 444 | /// </summary> | ||
2255 | 445 | /// <param name="callback">Teh deleaget.</param> | ||
2256 | 446 | /// <param name="state">Teh state object fo the response.</param> | ||
2257 | 447 | /// <returns>an asyn result.</returns> | ||
2258 | 448 | public IAsyncResult BeginGetResponse(AsyncCallback callback, object state) | ||
2259 | 449 | { | ||
2260 | 450 | return _request.BeginGetResponse(callback, state); | ||
2261 | 451 | } | ||
2262 | 452 | |||
2263 | 453 | /// <summary> | ||
2264 | 454 | /// Ends an asynchronous request for a Stream object to use to write data. | ||
2265 | 455 | /// </summary> | ||
2266 | 456 | /// <param name="asyncResult">The result from the async request.</param> | ||
2267 | 457 | /// <returns>Teh stream from teh request.</returns> | ||
2268 | 458 | public Stream EndGetRequestStream(IAsyncResult asyncResult) | ||
2269 | 459 | { | ||
2270 | 460 | return EndGetRequestStream(asyncResult); | ||
2271 | 461 | } | ||
2272 | 462 | |||
2273 | 463 | /// <summary> | ||
2274 | 464 | /// Ends an asynchronous request for a Stream object to use to write data and outputs | ||
2275 | 465 | /// the TransportContext associated with the stream. | ||
2276 | 466 | /// </summary> | ||
2277 | 467 | /// <param name="asyncResult">The async result from the request stream.</param> | ||
2278 | 468 | /// <param name="context">The context of the request.</param> | ||
2279 | 469 | /// <returns>The request stream.</returns> | ||
2280 | 470 | public Stream EndGetRequestStream(IAsyncResult asyncResult, out TransportContext context) | ||
2281 | 471 | { | ||
2282 | 472 | return _request.EndGetRequestStream(asyncResult, out context); | ||
2283 | 473 | } | ||
2284 | 474 | |||
2285 | 475 | /// <summary> | ||
2286 | 476 | /// Ends an asynchronous request to an Internet resource. | ||
2287 | 477 | /// </summary> | ||
2288 | 478 | /// <param name="asyncResult">The async result from the asyn response.</param> | ||
2289 | 479 | /// <returns>Teh response frm the internet resource.</returns> | ||
2290 | 480 | public WebResponse EndGetResponse(IAsyncResult asyncResult) | ||
2291 | 481 | { | ||
2292 | 482 | return _request.EndGetResponse(asyncResult); | ||
2293 | 483 | } | ||
2294 | 484 | |||
2295 | 485 | /// <summary> | ||
2296 | 486 | /// Gets a Stream object to use to write request data. | ||
2297 | 487 | /// </summary> | ||
2298 | 488 | /// <returns>Teh stream to which write the request.</returns> | ||
2299 | 489 | public Stream GetRequestStream() | ||
2300 | 490 | { | ||
2301 | 491 | return _request.GetRequestStream(); | ||
2302 | 492 | } | ||
2303 | 493 | |||
2304 | 494 | /// <summary> | ||
2305 | 495 | /// Returns a response from an Internet resource. | ||
2306 | 496 | /// </summary> | ||
2307 | 497 | /// <returns>Teh response from the resource.</returns> | ||
2308 | 498 | public WebResponse GetResponse() | ||
2309 | 499 | { | ||
2310 | 500 | return _request.GetResponse(); | ||
2311 | 501 | } | ||
2312 | 502 | |||
2313 | 503 | #endregion | ||
2314 | 504 | } | ||
2315 | 505 | } | ||
2316 | 0 | 506 | ||
2317 | === added file 'src/Canonical.UbuntuOne.Common/Net/HttpWebRequestFactory.cs' | |||
2318 | --- src/Canonical.UbuntuOne.Common/Net/HttpWebRequestFactory.cs 1970-01-01 00:00:00 +0000 | |||
2319 | +++ src/Canonical.UbuntuOne.Common/Net/HttpWebRequestFactory.cs 2010-10-05 09:58:35 +0000 | |||
2320 | @@ -0,0 +1,59 @@ | |||
2321 | 1 | /* | ||
2322 | 2 | * Copyright 2010 Canonical Ltd. | ||
2323 | 3 | * | ||
2324 | 4 | * This file is part of UbuntuOne on Windows. | ||
2325 | 5 | * | ||
2326 | 6 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
2327 | 7 | * it under the terms of the GNU Lesser General Public License version | ||
2328 | 8 | * as published by the Free Software Foundation. | ||
2329 | 9 | * | ||
2330 | 10 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
2331 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2332 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2333 | 13 | * GNU Lesser General Public License for more details. | ||
2334 | 14 | * | ||
2335 | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
2336 | 16 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
2337 | 17 | * | ||
2338 | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
2339 | 19 | */ | ||
2340 | 20 | using System; | ||
2341 | 21 | using System.Net; | ||
2342 | 22 | using RealHttpRequest = System.Net.HttpWebRequest; | ||
2343 | 23 | |||
2344 | 24 | namespace Canonical.UbuntuOne.Common.Net | ||
2345 | 25 | { | ||
2346 | 26 | /// <summary> | ||
2347 | 27 | /// Class that creates IHttpWebRequest that can be used to request an internet resource. | ||
2348 | 28 | /// </summary> | ||
2349 | 29 | internal class HttpWebRequestFactory : IHttpWebRequestFactory | ||
2350 | 30 | { | ||
2351 | 31 | #region Implementation of IHttpWebRequestFactory | ||
2352 | 32 | |||
2353 | 33 | /// <summary> | ||
2354 | 34 | /// Creates a new webrequest that will be used to request an internet resource that is identified | ||
2355 | 35 | /// by the given uri. | ||
2356 | 36 | /// </summary> | ||
2357 | 37 | /// <param name="uri">A string with the uri that identifies the internet resource.</param> | ||
2358 | 38 | /// <returns>An IHttpWebRequest that can be used to request an internet resource.</returns> | ||
2359 | 39 | public IHttpWebRequest Create(string uri) | ||
2360 | 40 | { | ||
2361 | 41 | var realRequest = WebRequest.Create(uri) as RealHttpRequest; | ||
2362 | 42 | return new HttpWebRequest(realRequest); | ||
2363 | 43 | } | ||
2364 | 44 | |||
2365 | 45 | /// <summary> | ||
2366 | 46 | /// Creates a webrequest that will be used to request an internet resource that is identified by | ||
2367 | 47 | /// the given uri. | ||
2368 | 48 | /// </summary> | ||
2369 | 49 | /// <param name="uri">An object that identifies the web request.</param> | ||
2370 | 50 | /// <returns>An HttpWebRequest that can be used to request an internet resource.</returns> | ||
2371 | 51 | public IHttpWebRequest Create(Uri uri) | ||
2372 | 52 | { | ||
2373 | 53 | var realRequest = WebRequest.Create(uri) as RealHttpRequest; | ||
2374 | 54 | return new HttpWebRequest(realRequest); | ||
2375 | 55 | } | ||
2376 | 56 | |||
2377 | 57 | #endregion | ||
2378 | 58 | } | ||
2379 | 59 | } | ||
2380 | 0 | 60 | ||
2381 | === added file 'src/Canonical.UbuntuOne.Common/Net/IHttpWebRequest.cs' | |||
2382 | --- src/Canonical.UbuntuOne.Common/Net/IHttpWebRequest.cs 1970-01-01 00:00:00 +0000 | |||
2383 | +++ src/Canonical.UbuntuOne.Common/Net/IHttpWebRequest.cs 2010-10-05 09:58:35 +0000 | |||
2384 | @@ -0,0 +1,297 @@ | |||
2385 | 1 | /* | ||
2386 | 2 | * Copyright 2010 Canonical Ltd. | ||
2387 | 3 | * | ||
2388 | 4 | * This file is part of UbuntuOne on Windows. | ||
2389 | 5 | * | ||
2390 | 6 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
2391 | 7 | * it under the terms of the GNU Lesser General Public License version | ||
2392 | 8 | * as published by the Free Software Foundation. | ||
2393 | 9 | * | ||
2394 | 10 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
2395 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2396 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2397 | 13 | * GNU Lesser General Public License for more details. | ||
2398 | 14 | * | ||
2399 | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
2400 | 16 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
2401 | 17 | * | ||
2402 | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
2403 | 19 | */ | ||
2404 | 20 | using System; | ||
2405 | 21 | using System.IO; | ||
2406 | 22 | using System.Net; | ||
2407 | 23 | using System.Net.Cache; | ||
2408 | 24 | using System.Net.Security; | ||
2409 | 25 | using System.Security.Cryptography.X509Certificates; | ||
2410 | 26 | using System.Security.Principal; | ||
2411 | 27 | |||
2412 | 28 | namespace Canonical.UbuntuOne.Common.Net | ||
2413 | 29 | { | ||
2414 | 30 | /// <summary> | ||
2415 | 31 | /// Helper interface that wrapps the HttpWebRequest class from System.Net | ||
2416 | 32 | /// </summary> | ||
2417 | 33 | public interface IHttpWebRequest | ||
2418 | 34 | { | ||
2419 | 35 | /// <summary> | ||
2420 | 36 | /// Gets and sets value of the Accept HTTP header. The default value is null. | ||
2421 | 37 | /// </summary> | ||
2422 | 38 | string Accept { get; set; } | ||
2423 | 39 | |||
2424 | 40 | /// <summary> | ||
2425 | 41 | /// Gets and sets the uri that responds to the request. | ||
2426 | 42 | /// </summary> | ||
2427 | 43 | Uri Address { get; } | ||
2428 | 44 | |||
2429 | 45 | /// <summary> | ||
2430 | 46 | /// Gets and sets if the request should automatically follow redirection responses | ||
2431 | 47 | /// </summary> | ||
2432 | 48 | bool AllowAutoRedirect { get; set; } | ||
2433 | 49 | |||
2434 | 50 | /// <summary> | ||
2435 | 51 | /// Enables buffering of the data sent to the Internet resource | ||
2436 | 52 | /// </summary> | ||
2437 | 53 | bool AllowWriteStreamBuffering { get; set; } | ||
2438 | 54 | |||
2439 | 55 | /// <summary> | ||
2440 | 56 | /// Gets and sets the auhentication lavel used. | ||
2441 | 57 | /// </summary> | ||
2442 | 58 | AuthenticationLevel AuthenticationLevel { get; set; } | ||
2443 | 59 | |||
2444 | 60 | /// <summary> | ||
2445 | 61 | /// Gets and sets the type of decompression that is used. | ||
2446 | 62 | /// </summary> | ||
2447 | 63 | DecompressionMethods AutomaticDecompression { get; set; } | ||
2448 | 64 | |||
2449 | 65 | /// <summary> | ||
2450 | 66 | /// Gets or sets the cache policy used by the request. | ||
2451 | 67 | /// </summary> | ||
2452 | 68 | RequestCachePolicy CachePolicy { get; set; } | ||
2453 | 69 | |||
2454 | 70 | /// <summary> | ||
2455 | 71 | /// Gets and sets the certificates used by the request. | ||
2456 | 72 | /// </summary> | ||
2457 | 73 | X509CertificateCollection ClientCertificates { get; set; } | ||
2458 | 74 | |||
2459 | 75 | /// <summary> | ||
2460 | 76 | /// Gets and sets the Connection HTTP header | ||
2461 | 77 | /// </summary> | ||
2462 | 78 | string Connection { get; set; } | ||
2463 | 79 | |||
2464 | 80 | /// <summary> | ||
2465 | 81 | /// Gets and sets the name of the connection group for this request | ||
2466 | 82 | /// </summary> | ||
2467 | 83 | string ConnectionGroupName { get; set; } | ||
2468 | 84 | |||
2469 | 85 | /// <summary> | ||
2470 | 86 | /// Gets and sets the number of bytes of data to send to the Internet resource. | ||
2471 | 87 | /// </summary> | ||
2472 | 88 | long ContentLength { get; set; } | ||
2473 | 89 | |||
2474 | 90 | /// <summary> | ||
2475 | 91 | /// Gets and sets the content type of the request. | ||
2476 | 92 | /// </summary> | ||
2477 | 93 | string ContentType { get; set; } | ||
2478 | 94 | |||
2479 | 95 | /// <summary> | ||
2480 | 96 | /// Gets and sets the delegate that implements the callback method that | ||
2481 | 97 | /// executes when an HTTP Continue response is returned from the Internet resource. | ||
2482 | 98 | /// </summary> | ||
2483 | 99 | HttpContinueDelegate ContinueDelegate { get; set; } | ||
2484 | 100 | |||
2485 | 101 | /// <summary> | ||
2486 | 102 | /// Gets and sets cookies container associated with this request. | ||
2487 | 103 | /// </summary> | ||
2488 | 104 | CookieContainer CookieContainer { get; set; } | ||
2489 | 105 | |||
2490 | 106 | /// <summary> | ||
2491 | 107 | /// Gets and sets the credentials used with the request. | ||
2492 | 108 | /// </summary> | ||
2493 | 109 | ICredentials Credentials { get; set; } | ||
2494 | 110 | |||
2495 | 111 | /// <summary> | ||
2496 | 112 | /// Gets and sets the data header of the request. | ||
2497 | 113 | /// </summary> | ||
2498 | 114 | DateTime Date { get; set; } | ||
2499 | 115 | |||
2500 | 116 | /// <summary> | ||
2501 | 117 | /// Gets and sets the Expect HTTP header. | ||
2502 | 118 | /// </summary> | ||
2503 | 119 | string Expect { get; set; } | ||
2504 | 120 | |||
2505 | 121 | /// <summary> | ||
2506 | 122 | /// Gets if a response has been received. | ||
2507 | 123 | /// </summary> | ||
2508 | 124 | bool HaveResponse { get; } | ||
2509 | 125 | |||
2510 | 126 | /// <summary> | ||
2511 | 127 | /// Gets and sets the headers of the request, | ||
2512 | 128 | /// </summary> | ||
2513 | 129 | WebHeaderCollection Headers { get; set; } | ||
2514 | 130 | |||
2515 | 131 | /// <summary> | ||
2516 | 132 | /// Gets and sets the Host header. | ||
2517 | 133 | /// </summary> | ||
2518 | 134 | string Host { get; set; } | ||
2519 | 135 | |||
2520 | 136 | /// <summary> | ||
2521 | 137 | /// A DateTime that contains the contents of the If-Modified-Since HTTP header. | ||
2522 | 138 | /// </summary> | ||
2523 | 139 | DateTime IfModifiedSince { get; set; } | ||
2524 | 140 | |||
2525 | 141 | /// <summary> | ||
2526 | 142 | /// Gets and sets the the impersonation level. | ||
2527 | 143 | /// </summary> | ||
2528 | 144 | TokenImpersonationLevel ImpersonationLevel { get; set; } | ||
2529 | 145 | |||
2530 | 146 | /// <summary> | ||
2531 | 147 | /// Gets and sets if the request should have a Connection HTTP header. | ||
2532 | 148 | /// </summary> | ||
2533 | 149 | bool KeepAlive { get; set; } | ||
2534 | 150 | |||
2535 | 151 | /// <summary> | ||
2536 | 152 | /// Gets and sets the max number of redirections. | ||
2537 | 153 | /// </summary> | ||
2538 | 154 | int MaximumAutomaticRedirections { get; set; } | ||
2539 | 155 | |||
2540 | 156 | /// <summary> | ||
2541 | 157 | /// Gets and sets The length, in kilobytes (1024 bytes), of the response headers. | ||
2542 | 158 | /// </summary> | ||
2543 | 159 | int MaximumResponseHeadersLength { get; set; } | ||
2544 | 160 | |||
2545 | 161 | /// <summary> | ||
2546 | 162 | /// The media type of the request. | ||
2547 | 163 | /// </summary> | ||
2548 | 164 | string MediaType { get; set; } | ||
2549 | 165 | |||
2550 | 166 | /// <summary> | ||
2551 | 167 | /// Gets and sets the request method. | ||
2552 | 168 | /// </summary> | ||
2553 | 169 | string Method { get; set; } | ||
2554 | 170 | |||
2555 | 171 | /// <summary> | ||
2556 | 172 | /// Gets and sets if the request should be pipelined | ||
2557 | 173 | /// </summary> | ||
2558 | 174 | bool Pipelined { get; set; } | ||
2559 | 175 | |||
2560 | 176 | /// <summary> | ||
2561 | 177 | /// Gets and sets if an HTTP Authorization header with requests after authentication has taken place; | ||
2562 | 178 | /// </summary> | ||
2563 | 179 | bool PreAuthenticate { get; set; } | ||
2564 | 180 | |||
2565 | 181 | /// <summary> | ||
2566 | 182 | /// Gets and sets the protol version used. | ||
2567 | 183 | /// </summary> | ||
2568 | 184 | Version ProtocolVersion { get; set; } | ||
2569 | 185 | |||
2570 | 186 | /// <summary> | ||
2571 | 187 | /// Gets and sets the proxy information. | ||
2572 | 188 | /// </summary> | ||
2573 | 189 | IWebProxy Proxy { get; set; } | ||
2574 | 190 | |||
2575 | 191 | /// <summary> | ||
2576 | 192 | /// Gets and sets the number of milliseconds before the writing or reading times out.. | ||
2577 | 193 | /// </summary> | ||
2578 | 194 | int ReadWriteTimeout { get; set; } | ||
2579 | 195 | |||
2580 | 196 | /// <summary> | ||
2581 | 197 | /// Gets and sets the Referer HTTP header. | ||
2582 | 198 | /// </summary> | ||
2583 | 199 | string Referer { get; set; } | ||
2584 | 200 | |||
2585 | 201 | /// <summary> | ||
2586 | 202 | /// Gets the resource uri used by the request. | ||
2587 | 203 | /// </summary> | ||
2588 | 204 | Uri RequestUri { get; } | ||
2589 | 205 | |||
2590 | 206 | /// <summary> | ||
2591 | 207 | /// Ges and sets if the request should be sent in chuckes. | ||
2592 | 208 | /// </summary> | ||
2593 | 209 | bool SendChunked { get; set; } | ||
2594 | 210 | |||
2595 | 211 | /// <summary> | ||
2596 | 212 | /// Gets the service point that represents the internet resource. | ||
2597 | 213 | /// </summary> | ||
2598 | 214 | ServicePoint ServicePoint { get; } | ||
2599 | 215 | |||
2600 | 216 | /// <summary> | ||
2601 | 217 | /// Gets and sets the number of milliseconds to wait before the request times out. | ||
2602 | 218 | /// </summary> | ||
2603 | 219 | int Timeout { get; set; } | ||
2604 | 220 | |||
2605 | 221 | /// <summary> | ||
2606 | 222 | /// Gets and sets the Transfer-encoding HTTP header. | ||
2607 | 223 | /// </summary> | ||
2608 | 224 | string TransferEncoding { get; set; } | ||
2609 | 225 | |||
2610 | 226 | /// <summary> | ||
2611 | 227 | /// Gets and sets if the authenticated connection is kept opened. | ||
2612 | 228 | /// </summary> | ||
2613 | 229 | bool UnsafeAuthenticatedConnectionSharing { get; set; } | ||
2614 | 230 | |||
2615 | 231 | /// <summary> | ||
2616 | 232 | /// Gets and sets if the default credentials are used. | ||
2617 | 233 | /// </summary> | ||
2618 | 234 | bool UseDefaultCredentials { get; set; } | ||
2619 | 235 | |||
2620 | 236 | /// <summary> | ||
2621 | 237 | /// Gets or sets the User-agent HTTP header; | ||
2622 | 238 | /// </summary> | ||
2623 | 239 | string UserAgent { get; set; } | ||
2624 | 240 | |||
2625 | 241 | /// <summary> | ||
2626 | 242 | /// Cancels a request to a resource | ||
2627 | 243 | /// </summary> | ||
2628 | 244 | void Abort(); | ||
2629 | 245 | |||
2630 | 246 | /// <summary> | ||
2631 | 247 | /// Begins an asynchronous request for a Stream object to use to write data. | ||
2632 | 248 | /// </summary> | ||
2633 | 249 | /// <param name="callback">The delegate to execute.</param> | ||
2634 | 250 | /// <param name="state">Teh state object of the request. </param> | ||
2635 | 251 | /// <returns>An async result.</returns> | ||
2636 | 252 | IAsyncResult BeginGetRequestStream(AsyncCallback callback,Object state); | ||
2637 | 253 | |||
2638 | 254 | /// <summary> | ||
2639 | 255 | /// Begins an asynchronous request to an Internet resource. | ||
2640 | 256 | /// </summary> | ||
2641 | 257 | /// <param name="callback">Teh deleaget.</param> | ||
2642 | 258 | /// <param name="state">Teh state object fo the response.</param> | ||
2643 | 259 | /// <returns>an asyn result.</returns> | ||
2644 | 260 | IAsyncResult BeginGetResponse(AsyncCallback callback,Object state); | ||
2645 | 261 | |||
2646 | 262 | /// <summary> | ||
2647 | 263 | /// Ends an asynchronous request for a Stream object to use to write data. | ||
2648 | 264 | /// </summary> | ||
2649 | 265 | /// <param name="asyncResult">The result from the async request.</param> | ||
2650 | 266 | /// <returns>Teh stream from teh request.</returns> | ||
2651 | 267 | Stream EndGetRequestStream(IAsyncResult asyncResult); | ||
2652 | 268 | |||
2653 | 269 | /// <summary> | ||
2654 | 270 | /// Ends an asynchronous request for a Stream object to use to write data and outputs | ||
2655 | 271 | /// the TransportContext associated with the stream. | ||
2656 | 272 | /// </summary> | ||
2657 | 273 | /// <param name="asyncResult">The async result from the request stream.</param> | ||
2658 | 274 | /// <param name="context">The context of the request.</param> | ||
2659 | 275 | /// <returns>The request stream.</returns> | ||
2660 | 276 | Stream EndGetRequestStream(IAsyncResult asyncResult,out TransportContext context); | ||
2661 | 277 | |||
2662 | 278 | /// <summary> | ||
2663 | 279 | /// Ends an asynchronous request to an Internet resource. | ||
2664 | 280 | /// </summary> | ||
2665 | 281 | /// <param name="asyncResult">The async result from the asyn response.</param> | ||
2666 | 282 | /// <returns>Teh response frm the internet resource.</returns> | ||
2667 | 283 | WebResponse EndGetResponse(IAsyncResult asyncResult); | ||
2668 | 284 | |||
2669 | 285 | /// <summary> | ||
2670 | 286 | /// Gets a Stream object to use to write request data. | ||
2671 | 287 | /// </summary> | ||
2672 | 288 | /// <returns>Teh stream to which write the request.</returns> | ||
2673 | 289 | Stream GetRequestStream(); | ||
2674 | 290 | |||
2675 | 291 | /// <summary> | ||
2676 | 292 | /// Returns a response from an Internet resource. | ||
2677 | 293 | /// </summary> | ||
2678 | 294 | /// <returns>Teh response from the resource.</returns> | ||
2679 | 295 | WebResponse GetResponse(); | ||
2680 | 296 | } | ||
2681 | 297 | } | ||
2682 | 0 | 298 | ||
2683 | === added file 'src/Canonical.UbuntuOne.Common/Net/IHttpWebRequestFactory.cs' | |||
2684 | --- src/Canonical.UbuntuOne.Common/Net/IHttpWebRequestFactory.cs 1970-01-01 00:00:00 +0000 | |||
2685 | +++ src/Canonical.UbuntuOne.Common/Net/IHttpWebRequestFactory.cs 2010-10-05 09:58:35 +0000 | |||
2686 | @@ -0,0 +1,45 @@ | |||
2687 | 1 | /* | ||
2688 | 2 | * Copyright 2010 Canonical Ltd. | ||
2689 | 3 | * | ||
2690 | 4 | * This file is part of UbuntuOne on Windows. | ||
2691 | 5 | * | ||
2692 | 6 | * UbuntuOne on Windows is free software: you can redistribute it and/or modify | ||
2693 | 7 | * it under the terms of the GNU Lesser General Public License version | ||
2694 | 8 | * as published by the Free Software Foundation. | ||
2695 | 9 | * | ||
2696 | 10 | * Ubuntu One on Windows is distributed in the hope that it will be useful, | ||
2697 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2698 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2699 | 13 | * GNU Lesser General Public License for more details. | ||
2700 | 14 | * | ||
2701 | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
2702 | 16 | * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>. | ||
2703 | 17 | * | ||
2704 | 18 | * Authors: Manuel de la Peña <manuel.delapena@canonical.com> | ||
2705 | 19 | */ | ||
2706 | 20 | using System; | ||
2707 | 21 | |||
2708 | 22 | namespace Canonical.UbuntuOne.Common.Net | ||
2709 | 23 | { | ||
2710 | 24 | /// <summary> | ||
2711 | 25 | /// Simple interface to be implemented by those objects that are able to create web requests. | ||
2712 | 26 | /// </summary> | ||
2713 | 27 | public interface IHttpWebRequestFactory | ||
2714 | 28 | { | ||
2715 | 29 | /// <summary> | ||
2716 | 30 | /// Creates a new webrequest that will be used to request an internet resource that is identified | ||
2717 | 31 | /// by the given uri. | ||
2718 | 32 | /// </summary> | ||
2719 | 33 | /// <param name="uri">A string with the uri that identifies the internet resource.</param> | ||
2720 | 34 | /// <returns>An IHttpWebRequest that can be used to request an internet resource.</returns> | ||
2721 | 35 | IHttpWebRequest Create(string uri); | ||
2722 | 36 | |||
2723 | 37 | /// <summary> | ||
2724 | 38 | /// Creates a webrequest that will be used to request an internet resource that is identified by | ||
2725 | 39 | /// the given uri. | ||
2726 | 40 | /// </summary> | ||
2727 | 41 | /// <param name="uri">An object that identifies the web request.</param> | ||
2728 | 42 | /// <returns>An HttpWebRequest that can be used to request an internet resource.</returns> | ||
2729 | 43 | IHttpWebRequest Create(Uri uri); | ||
2730 | 44 | } | ||
2731 | 45 | } |