tuitio/IdentityServer.Application/Services/UserService.cs

46 lines
1.5 KiB
C#

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
{
private readonly ISecurityStore _securityStore;
private readonly IIdentityRepository _identityRepository;
private readonly ITokenService _tokenService;
public UserService(ISecurityStore securityStore, IIdentityRepository identityRepository, ITokenService tokenService)
{
_securityStore = securityStore;
_identityRepository = identityRepository;
_tokenService = tokenService;
}
public async Task<Token> Authenticate(string userName, string password)
{
var user = await _identityRepository.GetAppUser(userName, password);
if (user == null)
return null;
var tokenRaw = _tokenService.GenerateTokenRaw(user);
var currentDate = DateTime.Now;
var token = new Token() { Raw = tokenRaw, ValidFrom = currentDate, ValidUntil = currentDate.AddMonths(12) };
_securityStore.SetToken(token, user.UserId);
return token;
}
public TokenCore Authorize(string token)
{
var tokenCore = _securityStore.ValidateAndGetTokenCore(token);
if (tokenCore == null)
return null;
return tokenCore;
}
}
}