-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5f1e46
commit a47f36b
Showing
6 changed files
with
93 additions
and
176 deletions.
There are no files selected for viewing
67 changes: 17 additions & 50 deletions
67
dotnet/Vaas/src/Vaas/Authentication/ClientCredentialsGrantAuthenticator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Vaas.Authentication; | ||
|
||
public class ClientCredentialsGrantAuthenticator : IAuthenticator | ||
public class ClientCredentialsGrantAuthenticator( | ||
string clientId, | ||
string clientSecret, | ||
Uri? tokenEndpoint = null, | ||
HttpClient? httpClient = null, | ||
ISystemClock? systemClock = null | ||
) : TokenReceiver(tokenEndpoint, httpClient, systemClock), IAuthenticator | ||
{ | ||
private readonly TokenReceiver _tokenReceiver; | ||
private string ClientId { get; } = clientId; | ||
private string ClientSecret { get; } = clientSecret; | ||
|
||
private string ClientId { get; } | ||
private string ClientSecret { get; } | ||
|
||
public ClientCredentialsGrantAuthenticator( | ||
string clientId, | ||
string clientSecret, | ||
Uri? tokenUrl = null, | ||
HttpClient? httpClient = null, | ||
ISystemClock? systemClock = null | ||
) | ||
protected override FormUrlEncodedContent TokenRequestToForm() | ||
{ | ||
_tokenReceiver = new ClientCredentialsTokenReceiver( | ||
this, | ||
tokenUrl, | ||
httpClient, | ||
systemClock | ||
return new FormUrlEncodedContent( | ||
new List<KeyValuePair<string, string>> | ||
{ | ||
new("client_id", ClientId), | ||
new("client_secret", ClientSecret ?? throw new InvalidOperationException()), | ||
new("grant_type", "client_credentials"), | ||
} | ||
); | ||
ClientId = clientId; | ||
ClientSecret = clientSecret; | ||
} | ||
|
||
public async Task<string> GetTokenAsync(CancellationToken cancellationToken) | ||
{ | ||
return await _tokenReceiver.GetTokenAsync(cancellationToken); | ||
} | ||
|
||
private class ClientCredentialsTokenReceiver( | ||
IAuthenticator authenticator, | ||
Uri? tokenUrl = null, | ||
HttpClient? httpClient = null, | ||
ISystemClock? systemClock = null | ||
) : TokenReceiver(authenticator, tokenUrl, httpClient, systemClock) | ||
{ | ||
protected override FormUrlEncodedContent TokenRequestToForm() | ||
{ | ||
var authenticator = (ClientCredentialsGrantAuthenticator)Authenticator; | ||
return new FormUrlEncodedContent( | ||
new List<KeyValuePair<string, string>> | ||
{ | ||
new("client_id", authenticator.ClientId), | ||
new( | ||
"client_secret", | ||
authenticator.ClientSecret ?? throw new InvalidOperationException() | ||
), | ||
new("grant_type", "client_credentials"), | ||
} | ||
); | ||
} | ||
} | ||
} |
77 changes: 20 additions & 57 deletions
77
dotnet/Vaas/src/Vaas/Authentication/ResourceOwnerPasswordGrantAuthenticator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Vaas.Authentication; | ||
|
||
public class ResourceOwnerPasswordGrantAuthenticator : IAuthenticator | ||
public class ResourceOwnerPasswordGrantAuthenticator( | ||
string clientId, | ||
string userName, | ||
string password, | ||
Uri? tokenEndpoint = null, | ||
HttpClient? httpClient = null, | ||
ISystemClock? systemClock = null | ||
) : TokenReceiver(tokenEndpoint, httpClient, systemClock), IAuthenticator | ||
{ | ||
private readonly TokenReceiver _tokenReceiver; | ||
private string ClientId { get; } = clientId; | ||
private string UserName { get; } = userName; | ||
private string Password { get; } = password; | ||
|
||
private string ClientId { get; } | ||
private string UserName { get; } | ||
private string Password { get; } | ||
|
||
public ResourceOwnerPasswordGrantAuthenticator( | ||
string clientId, | ||
string userName, | ||
string password, | ||
Uri? tokenUrl = null, | ||
HttpClient? httpClient = null, | ||
ISystemClock? systemClock = null | ||
) | ||
protected override FormUrlEncodedContent TokenRequestToForm() | ||
{ | ||
_tokenReceiver = new ResourceOwnerPasswordTokenReceiver( | ||
this, | ||
tokenUrl, | ||
httpClient, | ||
systemClock | ||
return new FormUrlEncodedContent( | ||
new List<KeyValuePair<string, string>> | ||
{ | ||
new("client_id", ClientId), | ||
new("username", UserName ?? throw new InvalidOperationException()), | ||
new("password", Password ?? throw new InvalidOperationException()), | ||
new("grant_type", "password"), | ||
} | ||
); | ||
ClientId = clientId; | ||
UserName = userName; | ||
Password = password; | ||
} | ||
|
||
public async Task<string> GetTokenAsync(CancellationToken cancellationToken) | ||
{ | ||
return await _tokenReceiver.GetTokenAsync(cancellationToken); | ||
} | ||
|
||
private class ResourceOwnerPasswordTokenReceiver( | ||
IAuthenticator authenticator, | ||
Uri? tokenUrl = null, | ||
HttpClient? httpClient = null, | ||
ISystemClock? systemClock = null | ||
) : TokenReceiver(authenticator, tokenUrl, httpClient, systemClock) | ||
{ | ||
protected override FormUrlEncodedContent TokenRequestToForm() | ||
{ | ||
var authenticator = (ResourceOwnerPasswordGrantAuthenticator)Authenticator; | ||
return new FormUrlEncodedContent( | ||
new List<KeyValuePair<string, string>> | ||
{ | ||
new("client_id", authenticator.ClientId), | ||
new( | ||
"username", | ||
authenticator.UserName ?? throw new InvalidOperationException() | ||
), | ||
new( | ||
"password", | ||
authenticator.Password ?? throw new InvalidOperationException() | ||
), | ||
new("grant_type", "password"), | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters