Skip to content

Commit

Permalink
.NET: Always send metadata; don't fail if auth token not present (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
geel9 committed Jul 5, 2022
1 parent 51be399 commit da6609d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
12 changes: 6 additions & 6 deletions dotnet/Trinsic/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
/// <returns></returns>
public async Task<string> SignInAsync(SignInRequest request) {
if (string.IsNullOrWhiteSpace(request.EcosystemId)) request.EcosystemId = Options.DefaultEcosystem;
var response = await Client.SignInAsync(request);
var response = await Client.SignInAsync(request, await BuildMetadataAsync(request));

var authToken = Base64Url.Encode(response.Profile.ToByteArray());

Expand All @@ -65,7 +65,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
/// <returns></returns>
public string SignIn(SignInRequest request) {
if (string.IsNullOrWhiteSpace(request.EcosystemId)) request.EcosystemId = Options.DefaultEcosystem;
var response = Client.SignIn(request);
var response = Client.SignIn(request, BuildMetadata(request));

var authToken = Base64Url.Encode(response.Profile.ToByteArray());

Expand Down Expand Up @@ -127,7 +127,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
if (string.IsNullOrWhiteSpace(request.EcosystemId))
request.EcosystemId = Options.DefaultEcosystem;

var response = await Client.LoginAsync(request);
var response = await Client.LoginAsync(request, await BuildMetadataAsync(request));

if (response.ResponseCase == LoginResponse.ResponseOneofCase.Profile)
{
Expand All @@ -146,7 +146,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
if (string.IsNullOrWhiteSpace(request.EcosystemId))
request.EcosystemId = Options.DefaultEcosystem;

var response = Client.Login(request);
var response = Client.Login(request, BuildMetadata(request));

return response;
}
Expand All @@ -169,7 +169,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
ConfirmationCodeHashed = hashed.Digest
};

var response = await Client.LoginConfirmAsync(request);
var response = await Client.LoginConfirmAsync(request, await BuildMetadataAsync(request));

if (response?.Profile == null)
return null;
Expand Down Expand Up @@ -202,7 +202,7 @@ internal AccountService(ITokenProvider tokenProvider, IOptions<ServiceOptions> o
ConfirmationCodeHashed = hashed.Digest
};

var response = Client.LoginConfirm(request);
var response = Client.LoginConfirm(request, BuildMetadata(request));

if (response?.Profile == null)
return null;
Expand Down
9 changes: 2 additions & 7 deletions dotnet/Trinsic/ProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ internal ProviderService(ITokenProvider tokenProvider, IOptions<ServiceOptions>
public async Task<(Ecosystem ecosystem, string authToken)> CreateEcosystemAsync(CreateEcosystemRequest request) {
request.Details ??= new();

var response = !string.IsNullOrWhiteSpace(request.Name) || !string.IsNullOrWhiteSpace(request.Details?.Email)
? await Client.CreateEcosystemAsync(request, await BuildMetadataAsync(request))
: await Client.CreateEcosystemAsync(request);

var response = await Client.CreateEcosystemAsync(request, await BuildMetadataAsync(request));
var authToken = Base64Url.Encode(response.Profile.ToByteArray());

if (!response.Profile.Protection?.Enabled ?? true)
Expand All @@ -59,9 +56,7 @@ internal ProviderService(ITokenProvider tokenProvider, IOptions<ServiceOptions>
public (Ecosystem ecosystem, string authToken) CreateEcosystem(CreateEcosystemRequest request) {
request.Details ??= new();

var response = !string.IsNullOrWhiteSpace(request.Name) || !string.IsNullOrWhiteSpace(request.Details?.Email)
? Client.CreateEcosystem(request, BuildMetadata(request))
: Client.CreateEcosystem(request);
var response = Client.CreateEcosystem(request, BuildMetadata(request));
var authToken = Base64Url.Encode(response.Profile.ToByteArray());

if (!response.Profile.Protection?.Enabled ?? true)
Expand Down
22 changes: 16 additions & 6 deletions dotnet/Trinsic/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ public abstract class ServiceBase
var authToken = string.IsNullOrWhiteSpace(Options.AuthToken)
? await TokenProvider.GetAsync()
: Options.AuthToken;
if (authToken is null) throw new("Cannot call authenticated endpoint before signing in");

// Return empty metadata if no auth token
if (authToken is null)
{
return new();
}

var profile = AccountProfile.Parser.ParseFrom(Base64Url.DecodeBytes(authToken));

return new() {
{"Authorization", await _securityProvider.GetAuthHeaderAsync(profile, request)}
{ "Authorization", await _securityProvider.GetAuthHeaderAsync(profile, request) }
};
}

Expand All @@ -93,15 +98,20 @@ public abstract class ServiceBase
/// </summary>
/// <returns></returns>
protected Metadata BuildMetadata(IMessage request) {
var authToken = string.IsNullOrWhiteSpace(Options.AuthToken)
? TokenProvider.Get()
var authToken = string.IsNullOrWhiteSpace(Options.AuthToken)
? TokenProvider.Get()
: Options.AuthToken;
if (authToken is null) throw new("Cannot call authenticated endpoint before signing in");

// Return empty metadata if no auth token
if (authToken is null)
{
return new();
}

var profile = AccountProfile.Parser.ParseFrom(Base64Url.DecodeBytes(authToken));

return new() {
{"Authorization", _securityProvider.GetAuthHeader(profile, request)}
{ "Authorization", _securityProvider.GetAuthHeader(profile, request) }
};
}
}
37 changes: 37 additions & 0 deletions dotnet/Trinsic/Trinsic.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da6609d

Please sign in to comment.