tuitio/IdentityServer.Application/Services/UserService.cs

47 lines
1.6 KiB
C#
Raw Normal View History

2020-12-20 03:06:43 +02:00
using IdentityServer.Application.Stores;
using IdentityServer.Domain.Models;
using IdentityServer.Domain.Repositories;
using System;
using System.Threading.Tasks;
namespace IdentityServer.Application.Services
{
internal class UserService : IUserService
2020-12-20 03:06:43 +02:00
{
private readonly ISecurityStore _securityStore;
private readonly IIdentityRepository _identityRepository;
private readonly ITokenService _tokenService;
2020-12-20 03:06:43 +02:00
public UserService(ISecurityStore securityStore, IIdentityRepository identityRepository, ITokenService tokenService)
2020-12-20 03:06:43 +02:00
{
_securityStore = securityStore;
_identityRepository = identityRepository;
_tokenService = tokenService;
2020-12-20 03:06:43 +02:00
}
public async Task<Token> Authenticate(string userName, string password)
{
2021-11-13 16:04:04 +02:00
var user = await _identityRepository.GetUser(userName, password);
2020-12-20 03:06:43 +02:00
if (user == null)
return null;
var tokenRaw = _tokenService.GenerateTokenRaw(user);
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);
2021-11-13 16:04:04 +02:00
await _identityRepository.UpdateUserAfterAuthentication(user, token);
2020-12-20 03:06:43 +02:00
return token;
}
public TokenCore Authorize(string token)
2020-12-20 03:06:43 +02:00
{
var tokenCore = _securityStore.ValidateAndGetTokenCore(token);
if (tokenCore == null)
return null;
2020-12-20 03:06:43 +02:00
return tokenCore;
2020-12-20 03:06:43 +02:00
}
}
}