Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
Conflicts:
	Raven.Tests.Silverlight/Generate.cs
	Raven.Tests/Querying/UsingDynamicQueryWithRemoteServer.cs
	Raven.Tests/Suggestions/Person.cs
	Raven.Tests/Suggestions/SuggestionsHelper.cs
  • Loading branch information
ayende committed Feb 8, 2013
2 parents ab7aca1 + a66615d commit 76f3b74
Show file tree
Hide file tree
Showing 429 changed files with 17,935 additions and 6,778 deletions.
104 changes: 104 additions & 0 deletions Bundles/Raven.Bundles.Tests/Authorization/Bugs/Matthew.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
extern alias client;
using Raven.Client.Exceptions;
using client::Raven.Bundles.Authorization.Model;
using System.Collections.Generic;
using Raven.Client;
using Xunit;

namespace Raven.Bundles.Tests.Authorization.Bugs
{
public class Matthew : AuthorizationTest
{
[Fact]
public void AuthorizationDemo_Works()
{
// Arrange
using (IDocumentSession session = store.OpenSession())
{
session.Store(
new AuthorizationRole
{
Id = "Authorization/Roles/Nurses",
Permissions =
{
new OperationPermission
{
Allow = true,
Operation = "Appointment/Schedule",
Tags = new List<string> {"Patient"}
}
}
});

// Allow doctors to authorize hospitalizations
session.Store(
new AuthorizationRole
{
Id = "Authorization/Roles/Doctors",
Permissions =
{
new OperationPermission
{
Allow = true,
Operation = "Hospitalization/Authorize",
Tags = new List<string> {"Patient"}
}
}
});
// Associate Patient with clinic
var maryMallon = new Patient {Id = "Patients/MaryMallon"};
session.Store(maryMallon);
client::Raven.Client.Authorization.AuthorizationClientExtensions.SetAuthorizationFor(session, maryMallon,
new DocumentAuthorization
{
Tags =
{
"Clinics/Kirya",
"Patient"
}
});

// Associate Doctor with clinic
session.Store(
new AuthorizationUser
{
Id = "Authorization/Users/DrHowser",
Name = "Doogie Howser",
Roles = {"Authorization/Roles/Doctors"},
Permissions =
{
new OperationPermission
{
Allow = true,
Operation = "Patient/View",
Tags = new List<string> {"Clinics/Kirya"}
},
}
});
session.SaveChanges();
}


// Assert
using (IDocumentSession session = store.OpenSession())
{
client::Raven.Client.Authorization.AuthorizationClientExtensions.SecureFor(session,
"Authorization/Users/NotDrHowser",
"Hospitalization/Authorize");
var readVetoException = Assert.Throws<ReadVetoException>(() => session.Load<Patient>("Patients/MaryMallon"));
Assert.Contains(
"Could not find user: Authorization/Users/NotDrHowser for secured document: Patients/MaryMallon",
readVetoException.Message);
}
}
}

public class Patient
{
public string Id { get; set; }

public void AuthorizeHospitalization()
{
}
}
}
1 change: 1 addition & 0 deletions Bundles/Raven.Bundles.Tests/Raven.Bundles.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Authorization\Bugs\Jalchr.cs" />
<Compile Include="Authorization\Bugs\Kwal.cs" />
<Compile Include="Authorization\Bugs\LoadingSavedInfo.cs" />
<Compile Include="Authorization\Bugs\Matthew.cs" />
<Compile Include="Authorization\Bugs\WhenUsingMultiTenancy.cs" />
<Compile Include="Authorization\Bugs\WithChangingOfUser.cs" />
<Compile Include="Authorization\CanAskAuthQuestions.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern alias database;
using System.Threading;
using Raven.Abstractions.Data;
using Raven.Client.Extensions;
using Raven.Tests.Bundles.Replication;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading;
using Raven.Abstractions.Data;
using Raven.Client.Extensions;
using Raven.Json.Linq;
using Raven.Tests.Bundles.Replication;
using Xunit;
Expand Down
2 changes: 1 addition & 1 deletion DefaultConfigs/RavenDb.exe.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<runtime>
<loadFromRemoteSources enabled="true"/>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Analyzers"/>
<probing privatePath="Analyzers;Plugins"/>
</assemblyBinding>
</runtime>
</configuration>
2 changes: 1 addition & 1 deletion DefaultConfigs/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

<probing privatePath="Analyzers"/>
<probing privatePath="Analyzers;Plugins"/>

</assemblyBinding>

Expand Down
30 changes: 13 additions & 17 deletions Raven.Abstractions/Connection/HttpRavenRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,13 @@ public HttpRavenRequest(string url, string method, Action<RavenConnectionStringO

private HttpWebRequest CreateRequest()
{
var request = (HttpWebRequest) System.Net.WebRequest.Create(url);
var request = (HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = method;
if (method == "POST" || method == "PUT")
request.Headers["Content-Encoding"] = "gzip";
request.Headers["Accept-Encoding"] = "deflate,gzip";
request.ContentType = "application/json; charset=utf-8";

if (connectionStringOptions.Credentials != null)
request.Credentials = connectionStringOptions.Credentials;
else
request.UseDefaultCredentials = true;

configureRequest(connectionStringOptions, request);
return request;
}
Expand All @@ -65,8 +60,8 @@ public void Write(Stream streamToWrite)
{
postedStream = streamToWrite;
using (var stream = WebRequest.GetRequestStream())
using(var countingStream = new CountingStream(stream, l => NumberOfBytesWrittenCompressed = l))
using(var commpressedStream = new GZipStream(countingStream, CompressionMode.Compress))
using (var countingStream = new CountingStream(stream, l => NumberOfBytesWrittenCompressed = l))
using (var commpressedStream = new GZipStream(countingStream, CompressionMode.Compress))
using (var countingStream2 = new CountingStream(commpressedStream, l => NumberOfBytesWrittenUncompressed = l))
{
streamToWrite.CopyTo(countingStream2);
Expand Down Expand Up @@ -128,13 +123,13 @@ public T ExecuteRequest<T>()
{
T result = default(T);
SendRequestToServer(response =>
{
using (var stream = response.GetResponseStreamWithHttpDecompression())
using (var reader = new StreamReader(stream))
{
result = reader.JsonDeserialization<T>();
}
});
{
using (var stream = response.GetResponseStreamWithHttpDecompression())
using (var reader = new StreamReader(stream))
{
result = reader.JsonDeserialization<T>();
}
});
return result;
}

Expand Down Expand Up @@ -188,7 +183,8 @@ private void SendRequestToServer(Action<WebResponse> action)
if (response == null)
throw;

if (response.StatusCode != HttpStatusCode.Unauthorized)
if (response.StatusCode != HttpStatusCode.Unauthorized &&
response.StatusCode != HttpStatusCode.PreconditionFailed)
{
using (var streamReader = new StreamReader(response.GetResponseStreamWithHttpDecompression()))
{
Expand Down Expand Up @@ -235,7 +231,7 @@ private void RecreateWebRequest()
if (postedStream != null)
{
postedStream.Position = 0;
using (var stream = newWebRequest.GetRequestStream())
using (var stream = newWebRequest.GetRequestStream())
using (var compressedData = new GZipStream(stream, CompressionMode.Compress))
{
postedStream.CopyTo(compressedData);
Expand Down
62 changes: 29 additions & 33 deletions Raven.Abstractions/Connection/HttpRavenRequestFactory.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net;
using Raven.Abstractions.Data;
using Raven.Abstractions.OAuth;

namespace Raven.Abstractions.Connection
{
public class HttpRavenRequestFactory
{
public int? RequestTimeoutInMs { get; set; }

private bool RefreshOauthToken(RavenConnectionStringOptions options, WebResponse response)
readonly ConcurrentDictionary<string, SecuredAuthenticator> authenticators = new ConcurrentDictionary<string, SecuredAuthenticator>();

public void ConfigureRequest(RavenConnectionStringOptions options, WebRequest request)
{
var oauthSource = response.Headers["OAuth-Source"];
if (string.IsNullOrEmpty(oauthSource))
return false;
if (RequestTimeoutInMs.HasValue)
request.Timeout = RequestTimeoutInMs.Value;


var authRequest = PrepareOAuthRequest(options, oauthSource);
using (var authResponse = authRequest.GetResponse())
using (var stream = authResponse.GetResponseStreamWithHttpDecompression())
using (var reader = new StreamReader(stream))
if (options.ApiKey == null)
{
options.CurrentOAuthToken = "Bearer " + reader.ReadToEnd();
request.Credentials = options.Credentials ?? CredentialCache.DefaultNetworkCredentials;
return;
}
return true;
}

private HttpWebRequest PrepareOAuthRequest(RavenConnectionStringOptions options, string oauthSource)
{
var authRequest = (HttpWebRequest) WebRequest.Create(oauthSource);
authRequest.Credentials = options.Credentials;
authRequest.Headers["Accept-Encoding"] = "deflate,gzip";
authRequest.Accept = "application/json;charset=UTF-8";

authRequest.Headers["grant_type"] = "client_credentials";
var value = authenticators.GetOrAdd(options.ApiKey, s => new SecuredAuthenticator(s));

if (string.IsNullOrEmpty(options.ApiKey) == false)
authRequest.Headers["Api-Key"] = options.ApiKey;

return authRequest;
value.ConfigureRequest(this, new WebRequestEventArgs
{
Request = request
});
}

public void ConfigureRequest(RavenConnectionStringOptions options, WebRequest request)
public HttpRavenRequest Create(string url, string method, RavenConnectionStringOptions connectionStringOptions)
{
request.Credentials = options.Credentials ?? CredentialCache.DefaultNetworkCredentials;

if (RequestTimeoutInMs.HasValue)
request.Timeout = RequestTimeoutInMs.Value;

if (string.IsNullOrEmpty(options.CurrentOAuthToken) == false)
request.Headers["Authorization"] = options.CurrentOAuthToken;
return new HttpRavenRequest(url, method, ConfigureRequest, HandleUnauthorizedResponse, connectionStringOptions);
}

public HttpRavenRequest Create(string url, string method, RavenConnectionStringOptions connectionStringOptions)
private bool HandleUnauthorizedResponse(RavenConnectionStringOptions options, WebResponse webResponse)
{
return new HttpRavenRequest(url, method, ConfigureRequest, RefreshOauthToken, connectionStringOptions);
if (options.ApiKey == null)
return false;

var value = authenticators.GetOrAdd(options.ApiKey, s => new SecuredAuthenticator(s));

var oauthSource = options.Url + "/OAuth/API-Key";

var result = value.DoOAuthRequest(oauthSource);
return result != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Raven.Client.Silverlight.Connection;
#endif

namespace Raven.Client.Connection
namespace Raven.Abstractions.Connection
{
/// <summary>
/// Event arguments for the event of creating a <see cref="WebRequest"/>
Expand Down
2 changes: 1 addition & 1 deletion Raven.Abstractions/Data/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static Constants()
{
{"Text", "The encryption is correct."}
};
InDatabaseKeyVerificationDocumentContents.EnsureSnapshot();
InDatabaseKeyVerificationDocumentContents.EnsureCannotBeChangeAndEnableSnapshotting();
}

public const string RavenClientPrimaryServerUrl = "Raven-Client-Primary-Server-Url";
Expand Down
2 changes: 1 addition & 1 deletion Raven.Abstractions/Data/Facet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Facet
public List<string> Ranges { get; set; }
public int? MaxResults { get; set; }
public FacetTermSortMode TermSortMode { get; set; }
public bool InclueRemainingTerms { get; set; }
public bool IncludeRemainingTerms { get; set; }

public Facet()
{
Expand Down
Loading

0 comments on commit 76f3b74

Please sign in to comment.