added new endpoint to generate dade school token

This commit is contained in:
Vijay Uppu 2023-11-13 15:42:45 -05:00
parent 126da500a1
commit eb0df19522
5 changed files with 59 additions and 3 deletions

View File

@ -14,6 +14,16 @@ namespace DamageAssesment.Api.UsersAccess.Controllers
{
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")]
[HttpPost("token/{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, IEnumerable<Models.Role> 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();
}

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

@ -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<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.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<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, TokenResponse TokenResponse, string ErrorMessage)> AuthenticateAsync(string employecode)
{

View File

@ -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": [