Skip to content

Commit

Permalink
ability to extract all token details, even for error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jon8787 committed May 24, 2024
1 parent 6ac53b1 commit 00050f9
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 78 deletions.
39 changes: 39 additions & 0 deletions src/SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,45 @@ static void ExampleBidStreamClient()
return;
}

var tokenDetails = client.DecryptTokenDetails(_advertisingToken, _domain);

//masterIv = { System.Convert.ToBase64String(tokenDetails.masterIv)}
Console.WriteLine(
$@"
DecryptionStatus = {tokenDetails.decryptionStatus};
identityScope = {tokenDetails.identityScope}
identityType = {tokenDetails.identityType}
tokenVersion = {tokenDetails.tokenVersion}
masterKeyId = {tokenDetails.masterKeyId}
expiry = {tokenDetails.expiry}
generated = {tokenDetails.generated}
operatorSiteId = {tokenDetails.operatorSiteId}
operatorType = {tokenDetails.operatorType}
operatorVersion = {tokenDetails.operatorVersion}
operatorKeyId = {tokenDetails.operatorKeyId}
siteKeyId = {tokenDetails.siteKeyId}
siteId = {tokenDetails.siteId}
publisherId = {tokenDetails.publisherId}
publisherKeyId = {tokenDetails.publisherKeyId}
privacyBits.CSTG = {tokenDetails.privacyBits.IsClientSideGenerated}
privacyBits.OptedOut= {tokenDetails.privacyBits.IsOptedOut}
established = {tokenDetails.established}
refreshed = {tokenDetails.refreshed}
idLength = {tokenDetails.idLength}
idString = {tokenDetails.idString}
");



return;
var result = client.DecryptTokenIntoRawUid(_advertisingToken, _domain);
Console.WriteLine($"DecryptedSuccess={result.Success} Status={result.Status}");
Console.WriteLine($"UID={result.Uid}");
Expand Down
5 changes: 5 additions & 0 deletions src/UID2.Client/BidstreamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public DecryptionResponse DecryptTokenIntoRawUid(string token, string domainOrAp
return DecryptTokenIntoRawUid(token, domainOrAppNameFromBidRequest, DateTime.UtcNow);
}

public TokenDetails DecryptTokenDetails(string token, string domainOrAppNameFromBidRequest)
{
return _tokenHelper.DecryptTokenDetails(token, DateTime.UtcNow, domainOrAppNameFromBidRequest, ClientType.Bidstream);
}

internal DecryptionResponse DecryptTokenIntoRawUid(string token, string domainOrAppNameFromBidRequest, DateTime utcNow)
{
return _tokenHelper.Decrypt(token, utcNow, domainOrAppNameFromBidRequest, ClientType.Bidstream);
Expand Down
2 changes: 1 addition & 1 deletion src/UID2.Client/IdentityScope.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace UID2.Client
{
internal enum IdentityScope
public enum IdentityScope
{
UID2 = 0,
EUID = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/UID2.Client/PrivacyBits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace UID2.Client
{
internal class PrivacyBits
public class PrivacyBits
{
// Bit 0 is legacy and is no longer in use
private const int BitClientSideGenerated = 1;
Expand Down
24 changes: 20 additions & 4 deletions src/UID2.Client/TokenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,45 @@ internal TokenHelper(string endpoint, string authKey, string secretKey)
_uid2ClientHelper = new Uid2ClientHelper(endpoint, authKey, secretKey);
}

internal static DecryptionResponse TokenDetailsToDecryptionResponse(TokenDetails tokenDetails)
{
string uid = tokenDetails.idString; //todo, some errors should blank this out
int siteKeySiteId = 0; //todo

return new DecryptionResponse(tokenDetails.decryptionStatus, uid, tokenDetails.established, tokenDetails.siteId, siteKeySiteId, tokenDetails.identityType, tokenDetails.tokenVersion,
tokenDetails.privacyBits?.IsClientSideGenerated, tokenDetails.expiry);
}

internal DecryptionResponse Decrypt(string token, DateTime now, string domainOrAppNameFromBidRequest, ClientType clientType)
{
var tokenDetails = DecryptTokenDetails(token, now, domainOrAppNameFromBidRequest, clientType);
return TokenDetailsToDecryptionResponse(tokenDetails);
}

internal TokenDetails DecryptTokenDetails(string token, DateTime now, string domainOrAppNameFromBidRequest, ClientType clientType)
{
var container = Volatile.Read(ref _container);
if (container == null)
{
return DecryptionResponse.MakeError(DecryptionStatus.NotInitialized);
return new TokenDetails(DecryptionStatus.NotInitialized);
}

if (!container.IsValid(now))
{
return DecryptionResponse.MakeError(DecryptionStatus.KeysNotSynced);
return new TokenDetails(DecryptionStatus.KeysNotSynced);
}

try
{
return UID2Encryption.Decrypt(token, container, now, domainOrAppNameFromBidRequest, container.IdentityScope, clientType);
return UID2Encryption.DecryptTokenDetails(token, container, now, domainOrAppNameFromBidRequest, container.IdentityScope, clientType);
}
catch (Exception)
{
return DecryptionResponse.MakeError(DecryptionStatus.InvalidPayload);
return new TokenDetails(DecryptionStatus.InvalidPayload);
}
}


internal EncryptionDataResponse Encrypt(string rawUid, DateTime now)
{
var container = Volatile.Read(ref _container);
Expand Down
3 changes: 2 additions & 1 deletion src/UID2.Client/UID2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private DecryptionResponse Decrypt(string token, DateTime now, string domainOrAp

try
{
return UID2Encryption.Decrypt(token, container, now, domainOrAppNameFromBidRequest, _identityScope, clientType);
var tokenDetails = UID2Encryption.DecryptTokenDetails(token, container, now, domainOrAppNameFromBidRequest, _identityScope, clientType);
return TokenHelper.TokenDetailsToDecryptionResponse(tokenDetails);
}
catch (Exception)
{
Expand Down
Loading

0 comments on commit 00050f9

Please sign in to comment.