diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs index e0aae67..755b21e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs @@ -14,6 +14,16 @@ namespace DamageAssesment.Api.UsersAccess.Controllers { this.userAccessProvider = userAccessProvider; } + [HttpPost("dadeschooltoken")] + public async Task DadeSchoolAuthenticateAsync(string username, string password) + { + var result = await userAccessProvider.DadeSchoolAuthenticateAsync(username, password); + if (result.IsSuccess) + { + return Ok(result.TokenResponse); + } + return Unauthorized(result.ErrorMessage); + } [Authorize(Policy = "Dadeschools")] [HttpPost("token/{employecode}")] public async Task AuthenticateAsync(string employecode) diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs index ea64376..6a70a5b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Interfaces/IUsersAccessProvider.cs @@ -11,6 +11,7 @@ namespace DamageAssesment.Api.UsersAccess.Interfaces public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> DeleteUserAsync(int Id); public Task<(bool IsSuccess, IEnumerable Roles, string ErrorMessage)> GetRolesAsync(); public Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)> AuthenticateAsync(string employeCode); + public Task<(bool IsSuccess, Models.DadeSchoolToken TokenResponse, string ErrorMessage)> DadeSchoolAuthenticateAsync(string username, string password); public Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)>RefreshTokenAsync(TokenResponse tokenResponse); public void seedData(); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Models/DadeSchoolToken.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Models/DadeSchoolToken.cs new file mode 100644 index 0000000..6b88ea5 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Models/DadeSchoolToken.cs @@ -0,0 +1,10 @@ +namespace DamageAssesment.Api.UsersAccess.Models +{ + public class DadeSchoolToken + { + public string access_token { get; set; } + public int expires_in { get; set; } + public string token_type { get; set; } + public string scope { get; set; } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs index a599053..939ca4e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/UserAccessProvider.cs @@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using System.Data; +using Newtonsoft.Json; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; @@ -19,9 +20,10 @@ namespace DamageAssesment.Api.UsersAccess.Providers private readonly IMapper mapper; //private readonly IEmployeeServiceProvider employeeServiceProvider; private readonly JwtSettings jwtSettings; - private readonly ITokenServiceProvider tokenServiceProvider; + private readonly ITokenServiceProvider tokenServiceProvider; + private readonly IConfiguration configuration; - public UsersAccessProvider(IOptions options, ITokenServiceProvider tokenServiceProvider, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger logger, IMapper mapper) + public UsersAccessProvider(IConfiguration configuration,IOptions options, ITokenServiceProvider tokenServiceProvider, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger logger, IMapper mapper) { this.userAccessDbContext = userAccessDbContext; //this.employeeServiceProvider = employeeServiceProvider; @@ -29,7 +31,8 @@ namespace DamageAssesment.Api.UsersAccess.Providers this.mapper = mapper; jwtSettings = options.Value; this.tokenServiceProvider = tokenServiceProvider; - // seedData(); + this.configuration = configuration; + // seedData(); } public void seedData() @@ -187,7 +190,35 @@ namespace DamageAssesment.Api.UsersAccess.Providers return (false, null, ex.Message); } } + public async Task<(bool IsSuccess, DadeSchoolToken TokenResponse, string ErrorMessage)> DadeSchoolAuthenticateAsync(string username, string password) + { + try + { + var client = new HttpClient(); + var request = new HttpRequestMessage(HttpMethod.Post, configuration.GetValue("Dadeschools:TokenUrl")); + var collection = new List>(); + collection.Add(new("client_id", configuration.GetValue("Dadeschools:TokenClientId"))); + collection.Add(new("client_secret", configuration.GetValue("Dadeschools:TokenClientSecret"))); + collection.Add(new("scope", configuration.GetValue("Dadeschools:scope"))); + collection.Add(new("grant_type", configuration.GetValue("Dadeschools:grant_type"))); + collection.Add(new("username", username)); + collection.Add(new("password", password)); + var content = new FormUrlEncodedContent(collection); + request.Content = content; + var response = await client.SendAsync(request); + var responseString = await response.Content.ReadAsStringAsync(); + if (response.IsSuccessStatusCode) + { + return (true, JsonConvert.DeserializeObject(responseString), ""); + } + return (false, null, responseString); + } + catch (Exception ex) + { + return (false, null, ex.Message); + } + } public async Task<(bool IsSuccess, TokenResponse TokenResponse, string ErrorMessage)> AuthenticateAsync(string employecode) { diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json index 150b8f3..e506d8d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/appsettings.json @@ -21,6 +21,10 @@ "TokenUrl": "https://dev-graph.dadeschools.net/connect/token", "ClientId": "dmapi", "ClientSecret": "bfce2c8d-2064-4a02-b19d-7f1d42b16eae", + "TokenClientId": "damage_assessment_postman", + "TokenClientSecret": "e4774164-f018-44c9-b9d2-3a29fc21db3c", + "scope": "openid profile", + "grant_type": "password", "Name": "Dadeschools Identity Server" }, "Scopes": [