Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Allow DSN without private key
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed Apr 5, 2018
1 parent 0e651f7 commit 43b8e4a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/app/SharpRaven/Dsn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Dsn(string dsn)
{
this.uri = new Uri(dsn);
this.privateKey = GetPrivateKey(this.uri);
this.publicKey = GetPublicKey(this.uri);
this.publicKey = GetPublicKey(this.uri) ?? throw new ArgumentException("A publicKey is required.", nameof(dsn));
this.port = this.uri.Port;
this.projectID = GetProjectID(this.uri);
this.path = GetPath(this.uri);
Expand Down Expand Up @@ -176,13 +176,14 @@ private static string GetPath(Uri uri)


/// <summary>
/// Get a private key from a Dsn uri.
/// Get a private key or null if no private key defined.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private static string GetPrivateKey(Uri uri)
{
return uri.UserInfo.Split(':')[1];
var parts = uri.UserInfo.Split(':');
return parts.Length == 2 ? parts[1] : null;
}


Expand All @@ -205,7 +206,8 @@ private static string GetProjectID(Uri uri)
/// <returns></returns>
private static string GetPublicKey(Uri uri)
{
return uri.UserInfo.Split(':')[0];
var publicKey = uri.UserInfo.Split(':')[0];
return publicKey != string.Empty ? publicKey : null;
}
}
}
4 changes: 2 additions & 2 deletions src/app/SharpRaven/Utilities/PacketBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public static string CreateAuthenticationHeader(Dsn dsn)
+ ", sentry_client={1}"
+ ", sentry_timestamp={2}"
+ ", sentry_key={3}"
+ ", sentry_secret={4}",
+ "{4}",
SentryVersion,
UserAgent,
(long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds,
dsn.PublicKey,
dsn.PrivateKey);
dsn.PrivateKey != null ? ", sentry_secret=" + dsn.PrivateKey : null);
}
}
}
28 changes: 28 additions & 0 deletions src/tests/SharpRaven.UnitTests/DsnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public void Constructor_ValidHttpsUri_SentryUriHasHttpsScheme()
}


[Test]
public void Constructor_ValidDsnWithoutPrivateKey_PrivateKeyGetterReturnsNull()
{
var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey);

Assert.That(dsn.PrivateKey, Is.Null);
}


[Test]
public void Constructor_ValidDsnWithoutPrivateKey_PublicKeyIsParsed()
{
var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey);

Assert.AreEqual("7d6466e66155431495bdb4036ba9a04b", dsn.PublicKey);
}


[Test]
public void Constructor_ValidHttpsUri_UriIsEqualToDsn()
{
Expand All @@ -83,6 +101,16 @@ public void Constructor_ValidHttpsUri_UriIsEqualToDsn()
}


[Test]
public void Constructor_ValidDsnWithoutPrivateKey_UriIsEqualToDsn()
{
var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey);

Assert.That(dsn.Uri, Is.Not.Null);
Assert.That(dsn.Uri.ToString(), Is.EqualTo(TestHelper.DsnUriWithoutPrivateKey));
}


[Test]
public void Constructor_ValidHttpUri_SentryUriHasHttpScheme()
{
Expand Down
16 changes: 15 additions & 1 deletion src/tests/SharpRaven.UnitTests/Utilities/PacketBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace SharpRaven.UnitTests.Utilities
public class PacketBuilderTests
{
[Test]
public void CreateAuthenticationHeader_ReturnsCorrectAuthenticationHeader()
public void CreateAuthenticationHeader_DsnWithSecret_ReturnsCorrectAuthenticationHeader()
{
const string expected =
@"^Sentry sentry_version=[\d], sentry_client=SharpRaven/[\d\.]+, sentry_timestamp=\d+, sentry_key=7d6466e66155431495bdb4036ba9a04b, sentry_secret=4c1cfeab7ebd4c1cb9e18008173a3630$";
Expand All @@ -50,5 +50,19 @@ public void CreateAuthenticationHeader_ReturnsCorrectAuthenticationHeader()

Assert.That(authenticationHeader, Is.StringMatching(expected));
}

[Test]
public void CreateAuthenticationHeader_DsnWithoutSecret_ReturnsCorrectAuthenticationHeader()
{
const string expected =
@"^Sentry sentry_version=[\d], sentry_client=SharpRaven/[\d\.]+, sentry_timestamp=\d+, sentry_key=7d6466e66155431495bdb4036ba9a04b$";

var dsn = new Dsn(
"https://7d6466e66155431495bdb4036ba9a04b@app.getsentry.com/3739");

var authenticationHeader = PacketBuilder.CreateAuthenticationHeader(dsn);

Assert.That(authenticationHeader, Is.StringMatching(expected));
}
}
}
3 changes: 3 additions & 0 deletions src/tests/SharpRaven.UnitTests/Utilities/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static class TestHelper
public const string DsnUri =
"https://7d6466e66155431495bdb4036ba9a04b:4c1cfeab7ebd4c1cb9e18008173a3630@app.getsentry.com/3739";

public const string DsnUriWithoutPrivateKey =
"https://7d6466e66155431495bdb4036ba9a04b@app.getsentry.com/3739";


public static Exception GetException()
{
Expand Down

0 comments on commit 43b8e4a

Please sign in to comment.