Skip to content

Commit

Permalink
[fix] credentials builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Asfiroth committed Oct 16, 2024
1 parent 5cbcd8a commit 0264755
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions example/Dimo.Example.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
var challenge = await dimoClient.AuthenticationService.GenerateChallengeAsync(
clientId: "<your client id>",
domain: "<your domain>",
address: "<your address>",
address: "<your address>"
);
var signedChallenge = await dimoClient.AuthenticationService.SignChallengeAsync(
Expand All @@ -37,8 +37,8 @@
domain: "<your domain>",
state: challenge.State,
signature: signedChallenge
);*/

);
*/
// uncomment the following code to get data from the rest services.
/*var tokenId = 12345; // token id from the device you want to get data from
Expand Down
8 changes: 7 additions & 1 deletion src/Dimo.Client/DimoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ public DimoClient(

if (credentials != null)
{
collection.AddSingleton(credentials);
collection.Configure<ClientCredentials>(options =>
{
options.ClientId = credentials.ClientId;
options.PrivateKey = credentials.PrivateKey;
options.Domain = credentials.Domain;
options.Address = credentials.Address;
});
}

_provider = collection.BuildServiceProvider();
Expand Down
13 changes: 10 additions & 3 deletions src/Dimo.Client/Services/Authentication/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ internal sealed class AuthenticationService : IAuthenticationService

public AuthenticationService(
IHttpClientFactory httpClientFactory,
ClientCredentials credentials,
IOptions<ClientCredentials> options)
{
_httpClientFactory = httpClientFactory;
_credentials = credentials ?? options.Value;
_credentials = options.Value;
}

public async Task<SignatureChallenge> GenerateChallengeAsync(string clientId, string domain, string address,
Expand Down Expand Up @@ -103,7 +102,7 @@ public async Task<Auth> GetTokenAsync(string clientId, string domain, string pri

public async Task<Auth> GetTokenAsync(CancellationToken cancellationToken = default)
{
if (_credentials == null)
if (!ValidateCredentials())
{
throw new ArgumentNullException(nameof(_credentials), "Client credentials are not set. Have you set them?");
}
Expand All @@ -113,5 +112,13 @@ public async Task<Auth> GetTokenAsync(CancellationToken cancellationToken = defa
var auth = await SubmitChallengeAsync(_credentials.ClientId, _credentials.Domain, challenge.State, signature, cancellationToken);
return auth;
}

private bool ValidateCredentials()
{
return _credentials != null && !string.IsNullOrWhiteSpace(_credentials.ClientId) &&
!string.IsNullOrWhiteSpace(_credentials.Domain) &&
!string.IsNullOrWhiteSpace(_credentials.PrivateKey) &&
!string.IsNullOrWhiteSpace(_credentials.Address);
}
}
}

0 comments on commit 0264755

Please sign in to comment.