2020-12-20 03:06:43 +02:00
|
|
|
|
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}";
|
2020-12-25 01:08:20 +02:00
|
|
|
|
|
|
|
|
|
var currentDate = DateTime.Now;
|
|
|
|
|
var token = new Token() { Raw = tokenRaw, ValidFrom = currentDate, ValidUntil = currentDate.AddMonths(12) };
|
2020-12-24 04:55:45 +02:00
|
|
|
|
_securityStore.SetToken(token, user.UserId);
|
2020-12-20 03:06:43 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|