-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.cs
More file actions
169 lines (146 loc) · 7.38 KB
/
AuthService.cs
File metadata and controls
169 lines (146 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using ECommerceAPI.Application.Abstractions.Services;
using ECommerceAPI.Application.Abstractions.Token;
using ECommerceAPI.Application.DTOs;
using ECommerceAPI.Application.DTOs.Facebook;
using ECommerceAPI.Application.DTOs.User;
using ECommerceAPI.Application.Exceptions;
using ECommerceAPI.Application.Helpers;
using ECommerceAPI.Domain.Entities.Identity;
using Google.Apis.Auth;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using System.Security.Authentication;
using System.Text.Json;
namespace ECommerceAPI.Persistence.Services
{
public class AuthService : IAuthService
{
readonly HttpClient _httpClient;
readonly IConfiguration _configuration;
readonly ITokenHandler _tokenHandler;
readonly UserManager<AppUser> _userManager;
readonly SignInManager<AppUser> _signInManager;
readonly IUserService _userService;
readonly IMailService _mailService;
public AuthService(IHttpClientFactory httpClientFactory, IConfiguration configuration, UserManager<AppUser> userManager, ITokenHandler tokenHandler, SignInManager<AppUser> signInManager, IUserService userService, IMailService mailService)
{
_httpClient = httpClientFactory.CreateClient();
_configuration = configuration;
_userManager = userManager;
_tokenHandler = tokenHandler;
_signInManager = signInManager;
_userService = userService;
_mailService = mailService;
}
async Task<Token> CreateUserExternalAsync(AppUser user, string email, string name, UserLoginInfo info, int accessTokenLifeTime)
{
bool result = user != null;
if (user == null)
{
user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
user = new()
{
Id = Guid.NewGuid().ToString(),
Email = email,
UserName = email,
NameSurname = name,
};
var identityResult = await _userManager.CreateAsync(user);
result = identityResult.Succeeded;
}
}
if (result)
{
await _userManager.AddLoginAsync(user, info);
Token token = _tokenHandler.CreateAccessToken(user, accessTokenLifeTime);
await _userService.UpdateRefreshTokenAsync(token.RefreshToken, token.Expiration, 900, user);
return token;
}
throw new Exception("Invalid external authentication");
}
public async Task<Token> FacebookLoginAsync(string authToken, int accessTokenLifetime)
{
string accessTokenResponse = await _httpClient.GetStringAsync($"https://graph.facebook.com/oauth/access_token?client_id={_configuration["ExternalLoginSettings:Facebook:ClientID"]}&client_secret={_configuration["ExternalLoginSettings:Facebook:ClientSecret"]}&grant_type=client_credentials");
var fbAccessTokenResponse = JsonSerializer.Deserialize<FacebookAccessTokenResponse>(accessTokenResponse);
var userAccessTokenValidation = await _httpClient.GetStringAsync($"https://graph.facebook.com/debug_token?input_token={authToken}&access_token={fbAccessTokenResponse?.AccessToken}");
var validation = JsonSerializer.Deserialize<FacebookAccessTokenValidation>(userAccessTokenValidation);
if (validation?.Data.IsValid != null)
{
string userInfoResponse = await _httpClient.GetStringAsync($"https://graph.facebook.com/me?fields=email,name&access_token={authToken}");
var fbUserInfo = JsonSerializer.Deserialize<FacebookUserInfoResponse>(userInfoResponse);
var info = new UserLoginInfo("FACEBOOK", validation.Data.UserId, "FACEBOOK");
var user = await _userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey);
return await CreateUserExternalAsync(user, fbUserInfo.Email, fbUserInfo.Name, info, accessTokenLifetime);
}
throw new Exception("Invalid external authentication");
}
public async Task<Token> GoogleLoginAsync(string idToken, int accessTokenLifetime)
{
var settings = new GoogleJsonWebSignature.ValidationSettings()
{
Audience = new List<string> { _configuration["ExternalLoginSettings:Google:ClientID"] }
};
var payload = await GoogleJsonWebSignature.ValidateAsync(idToken, settings);
var info = new UserLoginInfo("GOOGLE", payload.Subject, "GOOGLE");
var user = await _userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey);
return await CreateUserExternalAsync(user, payload.Email, payload.Name, info, accessTokenLifetime);
}
public async Task<Token> LoginAsync(string userNameOrEmail, string password, int accessTokenLifetime)
{
var user = await _userManager.FindByNameAsync(userNameOrEmail);
if (user == null)
user = await _userManager.FindByEmailAsync(userNameOrEmail);
if (user == null)
throw new UserNotFoundException();
var result = await _signInManager.CheckPasswordSignInAsync(user, password, false);
if (result.Succeeded)
{
Token token = _tokenHandler.CreateAccessToken(user, accessTokenLifetime);
await _userService.UpdateRefreshTokenAsync(token.RefreshToken, token.Expiration, 9000, user);
return token;
}
throw new AuthenticationException();
}
public async Task<Token> RefreshTokenLoginAsync(string refreshToken)
{
AppUser? user = _userManager.Users.FirstOrDefault(u => u.RefreshToken == refreshToken);
if (user != null && user.RefreshTokenEndDate > DateTime.UtcNow)
{
Token token = _tokenHandler.CreateAccessToken(user, 900);
await _userService.UpdateRefreshTokenAsync(token.RefreshToken, token.Expiration, 900, user);
return token;
}
throw new UserNotFoundException();
}
public async Task PasswordResetAsnyc(string email)
{
AppUser user = await _userManager.FindByEmailAsync(email);
if (user != null)
{
string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user);
resetToken = resetToken.UrlEncode();
await _mailService.SendPasswordResetMailAsync(email, user.Id, resetToken);
}
}
public async Task<VerifyUserResetTokenResponse> VerifyResetTokenAsync(string resetToken, string userId)
{
AppUser user = await _userManager.FindByIdAsync(userId);
if (user != null)
{
resetToken = resetToken.UrlDecode();
var state = await _userManager.VerifyUserTokenAsync(user, _userManager.Options.Tokens.PasswordResetTokenProvider, "ResetPassword", resetToken);
return new()
{
State = state,
Email = user.Email
};
}
return new()
{
State = false
};
}
}
}