added new endpoint to generate dade school token

This commit is contained in:
Vijay Uppu 2023-11-13 15:13:25 -05:00
parent 3b07723ebe
commit 9c97f08289
5 changed files with 59 additions and 2 deletions

View File

@ -14,6 +14,16 @@ namespace DamageAssesment.Api.UsersAccess.Controllers
{ {
this.userAccessProvider = userAccessProvider; this.userAccessProvider = userAccessProvider;
} }
[HttpPost("dadeschooltoken")]
public async Task<ActionResult> 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")] [Authorize(Policy = "Dadeschools")]
[HttpPost("token/{employecode}")] [HttpPost("token/{employecode}")]
public async Task<ActionResult> AuthenticateAsync(string employecode) public async Task<ActionResult> AuthenticateAsync(string employecode)

View File

@ -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, Models.User User, string ErrorMessage)> DeleteUserAsync(int Id);
public Task<(bool IsSuccess, IEnumerable<Models.Role> Roles, string ErrorMessage)> GetRolesAsync(); public Task<(bool IsSuccess, IEnumerable<Models.Role> Roles, string ErrorMessage)> GetRolesAsync();
public Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)> AuthenticateAsync(string employeCode); 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 Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)>RefreshTokenAsync(TokenResponse tokenResponse);
public void seedData(); public void seedData();
} }

View File

@ -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; }
}
}

View File

@ -5,6 +5,7 @@ using DamageAssesment.Api.UsersAccess.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System.Data; using System.Data;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
@ -20,8 +21,9 @@ namespace DamageAssesment.Api.UsersAccess.Providers
//private readonly IEmployeeServiceProvider employeeServiceProvider; //private readonly IEmployeeServiceProvider employeeServiceProvider;
private readonly JwtSettings jwtSettings; private readonly JwtSettings jwtSettings;
private readonly ITokenServiceProvider tokenServiceProvider; private readonly ITokenServiceProvider tokenServiceProvider;
private readonly IConfiguration configuration;
public UsersAccessProvider(IOptions<JwtSettings> options, ITokenServiceProvider tokenServiceProvider, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger<UsersAccessProvider> logger, IMapper mapper) public UsersAccessProvider(IConfiguration configuration,IOptions<JwtSettings> options, ITokenServiceProvider tokenServiceProvider, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger<UsersAccessProvider> logger, IMapper mapper)
{ {
this.userAccessDbContext = userAccessDbContext; this.userAccessDbContext = userAccessDbContext;
//this.employeeServiceProvider = employeeServiceProvider; //this.employeeServiceProvider = employeeServiceProvider;
@ -29,7 +31,8 @@ namespace DamageAssesment.Api.UsersAccess.Providers
this.mapper = mapper; this.mapper = mapper;
jwtSettings = options.Value; jwtSettings = options.Value;
this.tokenServiceProvider = tokenServiceProvider; this.tokenServiceProvider = tokenServiceProvider;
// seedData(); this.configuration = configuration;
// seedData();
} }
public void seedData() public void seedData()
@ -239,6 +242,35 @@ namespace DamageAssesment.Api.UsersAccess.Providers
return (false, null, "Credentials are required to authenticate."); return (false, null, "Credentials are required to authenticate.");
} }
} }
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<string>("Dadeschools:TokenUrl"));
var collection = new List<KeyValuePair<string, string>>();
collection.Add(new("client_id", configuration.GetValue<string>("Dadeschools:TokenClientId")));
collection.Add(new("client_secret", configuration.GetValue<string>("Dadeschools:TokenClientSecret")));
collection.Add(new("scope", configuration.GetValue<string>("Dadeschools:scope")));
collection.Add(new("grant_type", configuration.GetValue<string>("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<DadeSchoolToken>(responseString), "");
}
return (false, null, responseString);
}
catch (Exception ex)
{
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.Role> Roles, string ErrorMessage)> GetRolesAsync() public async Task<(bool IsSuccess, IEnumerable<Models.Role> Roles, string ErrorMessage)> GetRolesAsync()
{ {
try try

View File

@ -21,6 +21,10 @@
"TokenUrl": "https://dev-graph.dadeschools.net/connect/token", "TokenUrl": "https://dev-graph.dadeschools.net/connect/token",
"ClientId": "dmapi", "ClientId": "dmapi",
"ClientSecret": "bfce2c8d-2064-4a02-b19d-7f1d42b16eae", "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" "Name": "Dadeschools Identity Server"
}, },
"Scopes": [ "Scopes": [