Skip to content

Commit f559bb4

Browse files
committed
1 parent 6e0a856 commit f559bb4

File tree

114 files changed

+1498
-711
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+1498
-711
lines changed
2.68 KB
Loading
970 Bytes
Loading

Artifacts/DevDefinedOAuthTitle.png

5.35 KB
Loading

Changes.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
31st July 2009
2+
--------------
3+
4+
Fixed bug that allowed oauth_token_secret to be transmitted from the consumer to the provider.
5+
Fixed bug that meant provider would not work with plain text signature method, unless oauth_token_secret was sent by the client.
6+
Fixed the ExampleConsumerSite and ExampleProviderSite to implement a working OAuth 1.0a exchange of tokens etc.
7+
Apply General code cleanup.
8+
19
30th July 2009
210
--------------
311

DevDefined.OAuth.Tests/Consumer/GoogleIntegrationTests.cs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.IO;
2929
using System.Net;
3030
using System.Security.Cryptography.X509Certificates;
31+
using System.Web;
3132
using DevDefined.OAuth.Consumer;
3233
using DevDefined.OAuth.Framework;
3334
using NUnit.Framework;
@@ -124,13 +125,13 @@ public void RequestContacts()
124125
Assert.IsTrue(html.Contains("Authorized") || html.Contains("successfully granted"));
125126

126127
int index = html.IndexOf("verification code:");
127-
128+
128129
Assert.IsTrue(index > 0);
129130

130131
int startIndex = html.IndexOf("<B>", index, StringComparison.InvariantCultureIgnoreCase);
131132
int endIndex = html.IndexOf("</B>", startIndex + 1, StringComparison.InvariantCultureIgnoreCase);
132133

133-
verificationCode = html.Substring(startIndex+3, endIndex-(startIndex+3));
134+
verificationCode = html.Substring(startIndex + 3, endIndex - (startIndex + 3));
134135
}
135136

136137
// this will implicitly set AccessToken on the current session...
@@ -155,6 +156,78 @@ public void RequestContacts()
155156
}
156157
}
157158

159+
160+
[Test]
161+
public void DenyCallback()
162+
{
163+
// this test does a full end-to-end integration (request token, user authoriazation, exchanging request token
164+
// for an access token and then using then access token to retrieve some data).
165+
166+
// the access token is directly associated with a google user, by them logging in and granting access
167+
// for your request - thus the client is never exposed to the users credentials (not even their login).
168+
169+
var consumerContext = new OAuthConsumerContext
170+
{
171+
ConsumerKey = "weitu.googlepages.com",
172+
SignatureMethod = SignatureMethod.RsaSha1,
173+
Key = certificate.PrivateKey
174+
};
175+
176+
var consumer = new OAuthSession(consumerContext, "https://www.google.com/accounts/OAuthGetRequestToken",
177+
"https://www.google.com/accounts/accounts/OAuthAuthorizeToken",
178+
"https://www.google.com/accounts/OAuthGetAccessToken ", "http://localhost:1897/callback.aspx")
179+
.WithQueryParameters(new { scope = "https://www.google.com/m8/feeds" })
180+
.RequiresCallbackConfirmation();
181+
182+
using (With.NoCertificateValidation())
183+
{
184+
IToken requestToken = consumer.GetRequestToken();
185+
186+
string userAuthorize = consumer.GetUserAuthorizationUrlForToken(requestToken, null);
187+
188+
string verificationCode;
189+
190+
using (var ie = new IE(userAuthorize))
191+
{
192+
Link overrideLink = ie.Link("overridelink");
193+
if (overrideLink.Exists) overrideLink.Click();
194+
195+
if (ie.Form("gaia_loginform").Exists)
196+
{
197+
ie.TextField("Email").Value = "[email protected]";
198+
ie.TextField("Passwd").Value = "oauth_password";
199+
ie.Form("gaia_loginform").Submit();
200+
}
201+
ie.Button("deny").Click();
202+
203+
Console.WriteLine(ie.Url);
204+
205+
verificationCode = new OAuthContextBuilder().FromUri("GET", ie.Uri).Verifier;
206+
}
207+
208+
// this will implicitly set AccessToken on the current session...
209+
210+
IToken accessToken = consumer.ExchangeRequestTokenForAccessToken(requestToken, verificationCode);
211+
212+
try
213+
{
214+
string responseText = consumer.Request().Get().ForUrl("https://www.google.com/m8/feeds/contacts/default/base").ToString();
215+
216+
Assert.IsTrue(responseText.Contains("[email protected]"));
217+
}
218+
catch (WebException webEx)
219+
{
220+
var response = (HttpWebResponse) webEx.Response;
221+
using (var reader = new StreamReader(response.GetResponseStream()))
222+
{
223+
Console.WriteLine(reader.ReadToEnd());
224+
}
225+
Assert.Fail();
226+
}
227+
}
228+
}
229+
230+
158231
[Test]
159232
public void RequestTokenForRsaSha1()
160233
{

DevDefined.OAuth.Tests/Consumer/OAuthSessionTests.cs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,12 @@
2727
using DevDefined.OAuth.Consumer;
2828
using DevDefined.OAuth.Framework;
2929
using NUnit.Framework;
30-
using Rhino.Mocks;
3130

3231
namespace DevDefined.OAuth.Tests.Consumer
3332
{
3433
[TestFixture]
3534
public class OAuthSessionTests
3635
{
37-
[Test]
38-
public void GetUserAuthorizationUriForTokenWithCallback()
39-
{
40-
var session = new OAuthSession(new OAuthConsumerContext(), "http://localhost/request",
41-
"http://localhost/userauth", "http://localhost/access");
42-
string actual = session.GetUserAuthorizationUrlForToken(new TokenBase {Token = "token"},
43-
"http://localhost/callback");
44-
Assert.AreEqual(
45-
"http://localhost/userauth?oauth_token=token&oauth_callback=http%3A%2F%2Flocalhost%2Fcallback", actual);
46-
}
47-
4836
[Test]
4937
public void GetRequestTokenForConsumerWithCallbackUrl()
5038
{
@@ -53,39 +41,50 @@ public void GetRequestTokenForConsumerWithCallbackUrl()
5341
var session = new OAuthSession(consumerContext, "http://localhost/request",
5442
"http://localhost/userauth", "http://localhost/access", "http://localhost/callback");
5543

56-
var description = session.BuildRequestTokenContext("POST").GetRequestDescription();
44+
RequestDescription description = session.BuildRequestTokenContext("POST").GetRequestDescription();
5745

5846
Assert.IsTrue(description.Body.Contains("oauth_callback=http%3A%2F%2Flocalhost%2Fcallback"));
5947
}
6048

6149
[Test]
6250
public void GetRequestTokenForConsumerWithoutCallbackUrl()
6351
{
64-
var consumerContext = new OAuthConsumerContext { ConsumerKey = "key" };
52+
var consumerContext = new OAuthConsumerContext {ConsumerKey = "key"};
6553

6654
var session = new OAuthSession(consumerContext, "http://localhost/request",
6755
"http://localhost/userauth", "http://localhost/access");
6856

69-
var description = session.BuildRequestTokenContext("POST").GetRequestDescription();
57+
RequestDescription description = session.BuildRequestTokenContext("POST").GetRequestDescription();
7058

7159
Assert.IsTrue(description.Body.Contains("oauth_callback=oob"));
7260
}
7361

7462
[Test]
7563
public void GetRequestTokenForMethodGetDoesNotPopulateBody()
7664
{
77-
var consumerContext = new OAuthConsumerContext { ConsumerKey = "key" };
65+
var consumerContext = new OAuthConsumerContext {ConsumerKey = "key"};
7866

7967
var session = new OAuthSession(consumerContext, "http://localhost/request",
8068
"http://localhost/userauth", "http://localhost/access");
8169

82-
var description = session.BuildRequestTokenContext("GET").GetRequestDescription();
70+
RequestDescription description = session.BuildRequestTokenContext("GET").GetRequestDescription();
8371

8472
Assert.IsNull(description.Body);
8573
Assert.IsNull(description.ContentType);
8674
Assert.AreEqual("GET", description.Method);
8775
}
8876

77+
[Test]
78+
public void GetUserAuthorizationUriForTokenWithCallback()
79+
{
80+
var session = new OAuthSession(new OAuthConsumerContext(), "http://localhost/request",
81+
"http://localhost/userauth", "http://localhost/access");
82+
string actual = session.GetUserAuthorizationUrlForToken(new TokenBase {Token = "token"},
83+
"http://localhost/callback");
84+
Assert.AreEqual(
85+
"http://localhost/userauth?oauth_token=token&oauth_callback=http%3A%2F%2Flocalhost%2Fcallback", actual);
86+
}
87+
8988
[Test]
9089
public void GetUserAuthorizationUriForTokenWithoutCallback()
9190
{
@@ -94,5 +93,59 @@ public void GetUserAuthorizationUriForTokenWithoutCallback()
9493
string actual = session.GetUserAuthorizationUrlForToken(new TokenBase {Token = "token"}, null);
9594
Assert.AreEqual("http://localhost/userauth?oauth_token=token", actual);
9695
}
96+
97+
[Test]
98+
public void TokenSecretNotIncludedInAuthorizationHeaderForPostRequestWithUseAuthorizationHeaders()
99+
{
100+
var session = new OAuthSession(new OAuthConsumerContext {ConsumerKey = "consumer", UseHeaderForOAuthParameters = true}, "http://localhost/request",
101+
"http://localhost/userauth", "http://localhost/access");
102+
103+
var accessToken = new TokenBase {ConsumerKey = "consumer", Token = "token", TokenSecret = "secret"};
104+
105+
RequestDescription description = session
106+
.Request(accessToken)
107+
.Post()
108+
.ForUrl("http://localhost/")
109+
.SignWithToken()
110+
.GetRequestDescription();
111+
112+
Assert.IsFalse(description.Headers["Authorization"].Contains(Parameters.OAuth_Token_Secret));
113+
}
114+
115+
[Test]
116+
public void TokenSecretNotIncludedInBodyParametersForPostRequest()
117+
{
118+
var session = new OAuthSession(new OAuthConsumerContext {ConsumerKey = "consumer"}, "http://localhost/request",
119+
"http://localhost/userauth", "http://localhost/access");
120+
121+
var accessToken = new TokenBase {ConsumerKey = "consumer", Token = "token", TokenSecret = "secret"};
122+
123+
RequestDescription description = session
124+
.Request(accessToken)
125+
.Post()
126+
.ForUrl("http://localhost/")
127+
.SignWithToken()
128+
.GetRequestDescription();
129+
130+
Assert.IsFalse(description.Body.Contains(Parameters.OAuth_Token_Secret));
131+
}
132+
133+
[Test]
134+
public void TokenSecretNotIncludedInQueryParametersForGetRequest()
135+
{
136+
var session = new OAuthSession(new OAuthConsumerContext {ConsumerKey = "consumer"}, "http://localhost/request",
137+
"http://localhost/userauth", "http://localhost/access");
138+
139+
var accessToken = new TokenBase {ConsumerKey = "consumer", Token = "token", TokenSecret = "secret"};
140+
141+
RequestDescription description = session
142+
.Request(accessToken)
143+
.Get()
144+
.ForUrl("http://localhost/")
145+
.SignWithToken()
146+
.GetRequestDescription();
147+
148+
Assert.IsFalse(description.Url.ToString().Contains(Parameters.OAuth_Token_Secret));
149+
}
97150
}
98151
}

DevDefined.OAuth.Tests/Consumer/TermIeConsumerIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void MakeAuthenticatedCallForTokenRsaSha1WithPostAndHeaders()
9292
session.AccessToken = new TokenBase {ConsumerKey = "key", Token = "accesskey", TokenSecret = "accesssecret"};
9393
session.ConsumerContext.UseHeaderForOAuthParameters = true;
9494

95-
var context = session.Request().Post().ForUrl("http://term.ie/oauth/example/echo_api.php")
95+
IConsumerRequest context = session.Request().Post().ForUrl("http://term.ie/oauth/example/echo_api.php")
9696
.WithFormParameters(new {success = "true"})
9797
.SignWithToken();
9898

DevDefined.OAuth.Tests/DevDefined.OAuth.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>

DevDefined.OAuth.Tests/Framework/OAuthProblemReportTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,63 @@ public void FormatVersionRangeReport()
100100

101101
Assert.AreEqual("oauth_problem=version_rejected&oauth_acceptable_versions=1.0-2.0", report.ToString());
102102
}
103+
104+
[Test]
105+
public void PopulateFromFormattedMissingParameterReport()
106+
{
107+
string formatted = "oauth_problem=parameter_absent&oauth_parameters_absent=oauth_nonce";
108+
109+
var report = new OAuthProblemReport(formatted);
110+
111+
Assert.AreEqual(OAuthProblems.ParameterAbset, report.Problem);
112+
Assert.Contains(Parameters.OAuth_Nonce, report.ParametersAbsent);
113+
}
114+
115+
[Test]
116+
public void PopulateFromFormattedRejectedParameterReport()
117+
{
118+
string formatted = "oauth_problem=parameter_rejected&oauth_parameters_rejected=oauth_timestamp";
119+
120+
var report = new OAuthProblemReport(formatted);
121+
122+
Assert.AreEqual(OAuthProblems.ParameterRejected, report.Problem);
123+
Assert.Contains(Parameters.OAuth_Timestamp, report.ParametersRejected);
124+
}
125+
126+
[Test]
127+
public void PopulateFromFormattedReportWithAdvice()
128+
{
129+
string formatted =
130+
"oauth_problem=consumer_key_refused&oauth_problem_advice=The%20supplied%20consumer%20key%20has%20been%20black-listed%20due%20to%20complaints.";
131+
132+
var report = new OAuthProblemReport(formatted);
133+
134+
Assert.AreEqual(report.Problem, OAuthProblems.ConsumerKeyRefused);
135+
Assert.AreEqual("The supplied consumer key has been black-listed due to complaints.", report.ProblemAdvice);
136+
}
137+
138+
[Test]
139+
public void PopulateFromFormattedTimestampRangeReport()
140+
{
141+
string formatted = "oauth_problem=timestamp_refused&oauth_acceptable_timestamps=1199098800-1230721200";
142+
143+
var report = new OAuthProblemReport(formatted);
144+
145+
Assert.AreEqual(OAuthProblems.TimestampRefused, report.Problem);
146+
Assert.AreEqual(new DateTime(2008, 1, 1), report.AcceptableTimeStampsFrom);
147+
Assert.AreEqual(new DateTime(2009, 1, 1), report.AcceptableTimeStampsTo);
148+
}
149+
150+
[Test]
151+
public void PopulateFromFormattedVersionRangeReport()
152+
{
153+
string formatted = "oauth_problem=version_rejected&oauth_acceptable_versions=1.0-2.0";
154+
155+
var report = new OAuthProblemReport(formatted);
156+
157+
Assert.AreEqual(OAuthProblems.VersionRejected, report.Problem);
158+
Assert.AreEqual("1.0", report.AcceptableVersionFrom);
159+
Assert.AreEqual("2.0", report.AcceptableVersionTo);
160+
}
103161
}
104162
}

DevDefined.OAuth.Tests/Provider/Inspectors/TimestampRangeInspectorTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public void OutsideAfterRange()
4242
() => new DateTime(2008, 1, 1, 12, 0, 0));
4343

4444
var context = new OAuthContext
45-
{
46-
Timestamp = new DateTime(2008, 1, 1, 13, 0, 1).Epoch().ToString()
47-
};
45+
{
46+
Timestamp = new DateTime(2008, 1, 1, 13, 0, 1).Epoch().ToString()
47+
};
4848

4949
inspector.InspectContext(ProviderPhase.GrantRequestToken, context);
5050
}
@@ -57,9 +57,9 @@ public void OutsideBeforeRange()
5757
() => new DateTime(2008, 1, 1, 12, 0, 0));
5858

5959
var context = new OAuthContext
60-
{
61-
Timestamp = new DateTime(2008, 1, 1, 10, 59, 59).Epoch().ToString()
62-
};
60+
{
61+
Timestamp = new DateTime(2008, 1, 1, 10, 59, 59).Epoch().ToString()
62+
};
6363

6464
inspector.InspectContext(ProviderPhase.GrantRequestToken, context);
6565
}
@@ -71,9 +71,9 @@ public void WithAfterRange()
7171
() => new DateTime(2008, 1, 1, 12, 0, 0));
7272

7373
var context = new OAuthContext
74-
{
75-
Timestamp = new DateTime(2008, 1, 1, 13, 0, 0).Epoch().ToString()
76-
};
74+
{
75+
Timestamp = new DateTime(2008, 1, 1, 13, 0, 0).Epoch().ToString()
76+
};
7777

7878
inspector.InspectContext(ProviderPhase.GrantRequestToken, context);
7979
}
@@ -85,9 +85,9 @@ public void WithinBeforeRange()
8585
() => new DateTime(2008, 1, 1, 12, 0, 0));
8686

8787
var context = new OAuthContext
88-
{
89-
Timestamp = new DateTime(2008, 1, 1, 11, 0, 0).Epoch().ToString()
90-
};
88+
{
89+
Timestamp = new DateTime(2008, 1, 1, 11, 0, 0).Epoch().ToString()
90+
};
9191

9292
inspector.InspectContext(ProviderPhase.GrantRequestToken, context);
9393
}

0 commit comments

Comments
 (0)