AW-3569 Added posibility to login with username/password
This commit is contained in:
		| @@ -7,8 +7,11 @@ | |||||||
|         public string BasePath { get; set; }  |         public string BasePath { get; set; }  | ||||||
|         public string DiscoveryEndpointUrl { get; set; }  |         public string DiscoveryEndpointUrl { get; set; }  | ||||||
|         public string RedirectUri { get; set; }  |         public string RedirectUri { get; set; }  | ||||||
|  |         public string GrantClientId { get; set; }  | ||||||
|         public string ClientId { get; set; }  |         public string ClientId { get; set; }  | ||||||
|         public string ClientSecret { get; set; } |         public string ClientSecret { get; set; } | ||||||
|  |         public string Username { get; set; } | ||||||
|  |         public string Password { get; set; } | ||||||
|         public string[] Scopes { get; set; }  |         public string[] Scopes { get; set; }  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -53,9 +53,16 @@ namespace FarmmapsApi.Services | |||||||
|                 throw new AuthenticationException("Already seems to be authenticated"); |                 throw new AuthenticationException("Already seems to be authenticated"); | ||||||
|  |  | ||||||
|             var disco = await _openIdConnectService.GetDiscoveryDocumentAsync(); |             var disco = await _openIdConnectService.GetDiscoveryDocumentAsync(); | ||||||
|             var token = await _openIdConnectService.GetTokenClientCredentialsAsync(disco.TokenEndpoint, |  | ||||||
|                 _configuration.ClientId, _configuration.ClientSecret); |  | ||||||
|  |  | ||||||
|  |             var usePasswordGrant = string.IsNullOrEmpty(_configuration.ClientId) || | ||||||
|  |                                    string.IsNullOrEmpty(_configuration.ClientSecret); | ||||||
|  |              | ||||||
|  |             var token = usePasswordGrant ?  | ||||||
|  |                 await _openIdConnectService.GetTokenUsernamePasswordAsync(disco.TokenEndpoint, | ||||||
|  |                         _configuration.GrantClientId, _configuration.Username, _configuration.Password) : | ||||||
|  |                 await _openIdConnectService.GetTokenClientCredentialsAsync(disco.TokenEndpoint, | ||||||
|  |                 _configuration.ClientId, _configuration.ClientSecret); | ||||||
|  |              | ||||||
|             if (token.IsError) |             if (token.IsError) | ||||||
|                 throw new AuthenticationException(token.Error); |                 throw new AuthenticationException(token.Error); | ||||||
|  |  | ||||||
| @@ -314,7 +321,6 @@ namespace FarmmapsApi.Services | |||||||
|             farmmapsUploader.UploadSessionData += data => location = data.UploadUri; |             farmmapsUploader.UploadSessionData += data => location = data.UploadUri; | ||||||
|  |  | ||||||
|             var progress = await farmmapsUploader.UploadAsync(); |             var progress = await farmmapsUploader.UploadAsync(); | ||||||
|  |  | ||||||
|             return new UploadResults(progress, location); |             return new UploadResults(progress, location); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
| using System.Net.Http; | using System.Net.Http; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using FarmmapsApi.Models; | using FarmmapsApi.Models; | ||||||
|  | using IdentityModel; | ||||||
| using IdentityModel.Client; | using IdentityModel.Client; | ||||||
|  |  | ||||||
| namespace FarmmapsApi.Services | namespace FarmmapsApi.Services | ||||||
| @@ -40,6 +41,19 @@ namespace FarmmapsApi.Services | |||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         public async Task<TokenResponse> GetTokenUsernamePasswordAsync(string tokenEndpointUrl, string grantClientId, string username, string password) | ||||||
|  |         { | ||||||
|  |             return await _httpClient.RequestPasswordTokenAsync(new PasswordTokenRequest() | ||||||
|  |             { | ||||||
|  |                 Address = tokenEndpointUrl, | ||||||
|  |                 UserName = username, | ||||||
|  |                 Password = password, | ||||||
|  |                 ClientId = grantClientId, | ||||||
|  |                 GrantType = OidcConstants.GrantTypes.Password, | ||||||
|  |                 Scope = string.Join(" ", _configuration.Scopes) | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |          | ||||||
|         public async Task<TokenResponse> RefreshTokensAsync(string tokenEndpointUrl, string refreshToken) |         public async Task<TokenResponse> RefreshTokensAsync(string tokenEndpointUrl, string refreshToken) | ||||||
|         { |         { | ||||||
|             return await _httpClient.RequestRefreshTokenAsync(new RefreshTokenRequest() |             return await _httpClient.RequestRefreshTokenAsync(new RefreshTokenRequest() | ||||||
|   | |||||||
| @@ -17,8 +17,5 @@ | |||||||
|     <ItemGroup> |     <ItemGroup> | ||||||
|         <ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" /> |         <ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" /> | ||||||
|     </ItemGroup> |     </ItemGroup> | ||||||
| 	<ItemGroup> |  | ||||||
| 		<Folder Include="Data" /> |  | ||||||
| 	</ItemGroup> |  | ||||||
|  |  | ||||||
| </Project> | </Project> | ||||||
|   | |||||||
| @@ -1,23 +1,26 @@ | |||||||
| { | { | ||||||
|   //There are three farmmaps environments, uncomment the environemnt you want to use with this sample client |   //There are three farmmaps environments, uncomment the environemnt you want to use with this sample client | ||||||
|  |  | ||||||
|   ////test environment |   //test environment | ||||||
|   //"Authority": "https://accounts.test.farmmaps.eu/", | //  "Authority": "https://accounts.test.farmmaps.eu/", | ||||||
|   //"Endpoint": "https://test.farmmaps.eu/", | //  "Endpoint": "https://test.farmmaps.eu/", | ||||||
|   //"BasePath": "api/v1", | //  "BasePath": "api/v1", | ||||||
|   //"DiscoveryEndpointUrl": "https://accounts.test.farmmaps.eu/.well-known/openid-configuration", | //  "DiscoveryEndpointUrl": "https://accounts.test.farmmaps.eu/.well-known/openid-configuration", | ||||||
|  | //  "GrantClientId": "farmmapstesteu", | ||||||
|  |  | ||||||
|   ////acceptance environment |   ////acceptance environment | ||||||
|   //"Authority": "https://accounts.acc.farmmaps.eu/", |   //"Authority": "https://accounts.acc.farmmaps.eu/", | ||||||
|   //"Endpoint": "https://acc.farmmaps.eu/", |   //"Endpoint": "https://acc.farmmaps.eu/", | ||||||
|   //"BasePath": "api/v1", |   //"BasePath": "api/v1", | ||||||
|   //"DiscoveryEndpointUrl": "https://accounts.acc.farmmaps.eu/.well-known/openid-configuration", |   //"DiscoveryEndpointUrl": "https://accounts.acc.farmmaps.eu/.well-known/openid-configuration", | ||||||
|  |   //"GrantClientId": "farmmapsacceu", | ||||||
|  |  | ||||||
|   //production environment |   //production environment | ||||||
|   "authority": "https://accounts.farmmaps.eu/", |   "authority": "https://accounts.farmmaps.eu/", | ||||||
|   "endpoint": "https://farmmaps.eu/", |   "endpoint": "https://farmmaps.eu/", | ||||||
|   "basepath": "api/v1", |   "basepath": "api/v1", | ||||||
|   "discoveryendpointurl": "https://accounts.farmmaps.eu/.well-known/openid-configuration", |   "discoveryendpointurl": "https://accounts.farmmaps.eu/.well-known/openid-configuration", | ||||||
|  |   "GrantClientId": "farmmaps", | ||||||
|  |  | ||||||
|   //overige info |   //overige info | ||||||
|   "RedirectUri": "http://example.nl/api", |   "RedirectUri": "http://example.nl/api", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user