49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using IdentityServer.Application.Stores;
|
|
using IdentityServer.Domain.Entities;
|
|
using IdentityServer.Domain.Models;
|
|
using IdentityServer.Domain.Repositories;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IdentityServer.Application.Services
|
|
{
|
|
public class UserService : IUserService
|
|
{
|
|
private readonly ISecurityStore _securityStore;
|
|
private readonly IIdentityRepository _identityRepository;
|
|
|
|
public UserService(ISecurityStore securityStore, IIdentityRepository identityRepository)
|
|
{
|
|
_securityStore = securityStore;
|
|
_identityRepository = identityRepository;
|
|
}
|
|
|
|
public async Task<Token> Authenticate(string userName, string password)
|
|
{
|
|
var user = await _identityRepository.GetAppUser(userName, password);
|
|
if (user == null)
|
|
return null;
|
|
|
|
var tokenRaw = $"{Guid.NewGuid()}-{Guid.NewGuid()}-{user.UserId}";
|
|
|
|
var currentDate = DateTime.Now;
|
|
var token = new Token() { Raw = tokenRaw, ValidFrom = currentDate, ValidUntil = currentDate.AddMonths(12) };
|
|
_securityStore.SetToken(token, user.UserId);
|
|
|
|
return token;
|
|
}
|
|
|
|
public async Task<AppUser> Authorize(string token)
|
|
{
|
|
var tokenValidation = _securityStore.ValidateToken(token);
|
|
if (tokenValidation.Success)
|
|
{
|
|
var user = await _identityRepository.GetAppUser(tokenValidation.UserId);
|
|
return user;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|