Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jon-UID2-3680-test-coverage-v4-base64-tokens #62

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/UID2.Client.Test/BidstreamClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ private void SmokeTestForBidstream(IdentityScope identityScope, TokenVersion tok
DecryptAndAssertSuccess(advertisingToken, tokenVersion);
}

[Theory]
[InlineData(IdentityScope.UID2)]
[InlineData(IdentityScope.EUID)]
private void CanDecryptV4TokenEncodedAsBase64(IdentityScope identityScope)
{
Refresh(KeyBidstreamResponse(new[] { MASTER_KEY, SITE_KEY }, identityScope));

var now = DateTime.UtcNow;
var advertisingTokenBase64Url = AdvertisingTokenBuilder.Builder().WithVersion(TokenVersion.V4).WithScope(identityScope).WithEstablished(now.AddMonths(-4)).WithGenerated(now.AddDays(-1)).WithExpiry(now.AddDays(2)).Build();

var tokenAsBinary = UID2Base64UrlCoder.Decode(advertisingTokenBase64Url);
var advertisingTokenBase64 = Convert.ToBase64String(tokenAsBinary);
Assert.True(advertisingTokenBase64.Contains("="));
Assert.True(advertisingTokenBase64.Contains("/"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a slim chance that there may not be / or + char? is it better to assert that advertisingTokenBase64Url must contain - and _ char first (if not, generate a new token value again) and then we are assured that we are testing the Bae64url->base64 conversion with these asserts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some code to regenerate in a loop until it finds all these base64 chars. Tested a few times, but in practice I've never seen it generate a token that doesn't have all of these chars.

Assert.True(advertisingTokenBase64.Contains("+"));

DecryptAndAssertSuccess(advertisingTokenBase64, TokenVersion.V4);
}


private void DecryptAndAssertSuccess(string advertisingToken, TokenVersion tokenVersion)
{
var res = _client.DecryptTokenIntoRawUid(advertisingToken, null);
Expand Down