-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathEmailProvider.cs
193 lines (163 loc) · 7.08 KB
/
EmailProvider.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Firebase.Auth.Requests;
using System.Linq;
using System.Threading.Tasks;
namespace Firebase.Auth.Providers
{
public class EmailProvider : FirebaseAuthProvider
{
private SignupNewUser signupNewUser;
private SetAccountInfo setAccountInfo;
private GetAccountInfo getAccountInfo;
private VerifyPassword verifyPassword;
private ResetPassword resetPassword;
private SetAccountLink linkAccount;
public override FirebaseProviderType ProviderType => FirebaseProviderType.EmailAndPassword;
internal override void Initialize(FirebaseAuthConfig config)
{
base.Initialize(config);
this.signupNewUser = new SignupNewUser(this.config);
this.setAccountInfo = new SetAccountInfo(this.config);
this.getAccountInfo = new GetAccountInfo(this.config);
this.verifyPassword = new VerifyPassword(this.config);
this.resetPassword = new ResetPassword(this.config);
this.linkAccount = new SetAccountLink(config);
}
public static AuthCredential GetCredential(string email, string password)
{
return new EmailCredential
{
ProviderType = FirebaseProviderType.EmailAndPassword,
Email = email,
Password = password
};
}
public Task ResetEmailPasswordAsync(string email)
{
var request = new ResetPasswordRequest
{
Email = email
};
return this.resetPassword.ExecuteAsync(request);
}
public Task<UserCredential> SignInUserAsync(string email, string password)
{
return this.SignInWithCredentialAsync(GetCredential(email, password));
}
public async Task<UserCredential> SignUpUserAsync(string email, string password, string displayName)
{
var authCredential = GetCredential(email, password);
var signupResponse = await this.signupNewUser.ExecuteAsync(new SignupNewUserRequest
{
Email = email,
Password = password,
ReturnSecureToken = true
}).ConfigureAwait(false);
var credential = new FirebaseCredential
{
ExpiresIn = signupResponse.ExpiresIn,
IdToken = signupResponse.IdToken,
RefreshToken = signupResponse.RefreshToken,
ProviderType = FirebaseProviderType.EmailAndPassword
};
// set display name if available
if (!string.IsNullOrWhiteSpace(displayName))
{
var setResponse = await this.setAccountInfo.ExecuteAsync(new SetAccountDisplayName
{
DisplayName = displayName,
IdToken = signupResponse.IdToken,
ReturnSecureToken = true
}).ConfigureAwait(false);
var setUser = new UserInfo
{
DisplayName = setResponse.DisplayName,
Email = setResponse.Email,
IsEmailVerified = setResponse.EmailVerified,
Uid = setResponse.LocalId,
IsAnonymous = false
};
return new UserCredential(new User(this.config, setUser, credential), authCredential, OperationType.SignIn);
}
var getUser = await this.GetUserInfoAsync(signupResponse.IdToken).ConfigureAwait(false);
return new UserCredential(new User(this.config, getUser, credential), authCredential, OperationType.SignIn);
}
protected internal override async Task<UserCredential> SignInWithCredentialAsync(AuthCredential credential)
{
var ec = (EmailCredential)credential;
var response = await this.verifyPassword.ExecuteAsync(new VerifyPasswordRequest
{
Email = ec.Email,
Password = ec.Password,
ReturnSecureToken = true
}).ConfigureAwait(false);
var user = await this.GetUserInfoAsync(response.IdToken).ConfigureAwait(false);
var fc = new FirebaseCredential
{
ExpiresIn = response.ExpiresIn,
IdToken = response.IdToken,
RefreshToken = response.RefreshToken,
ProviderType = FirebaseProviderType.EmailAndPassword
};
return new UserCredential(new User(this.config, user, fc), ec, OperationType.SignIn);
}
protected internal override async Task<UserCredential> LinkWithCredentialAsync(string token, AuthCredential credential)
{
var c = (EmailCredential)credential;
var request = new SetAccountLinkRequest
{
IdToken = token,
Email = c.Email,
Password = c.Password,
ReturnSecureToken = true
};
SetAccountLinkResponse link;
try
{
link = await this.linkAccount.ExecuteAsync(request).ConfigureAwait(false);
}
catch (FirebaseAuthException e) when (e.Reason == AuthErrorReason.EmailExists)
{
throw new FirebaseAuthWithCredentialException("The email address is already in use by another account.", credential, AuthErrorReason.EmailExists);
}
var getResult = await this.getAccountInfo.ExecuteAsync(new IdTokenRequest { IdToken = link.IdToken }).ConfigureAwait(false);
var u = getResult.Users[0];
var info = new UserInfo
{
DisplayName = u.DisplayName,
Email = u.Email,
IsEmailVerified = u.EmailVerified,
FederatedId = u.ProviderUserInfo?.FirstOrDefault(info => info.FederatedId != null)?.FederatedId,
Uid = u.LocalId,
PhotoUrl = u.PhotoUrl,
IsAnonymous = false
};
var fc = new FirebaseCredential
{
ExpiresIn = link.ExpiresIn,
IdToken = link.IdToken,
ProviderType = credential.ProviderType,
RefreshToken = link.RefreshToken
};
return new UserCredential(new User(this.config, info, fc), credential, OperationType.Link);
}
private async Task<UserInfo> GetUserInfoAsync(string idToken)
{
var getResponse = await this.getAccountInfo.ExecuteAsync(new IdTokenRequest { IdToken = idToken }).ConfigureAwait(false);
var user = getResponse.Users[0];
return new UserInfo
{
DisplayName = user.DisplayName,
Email = user.Email,
IsEmailVerified = user.EmailVerified,
Uid = user.LocalId,
PhotoUrl = user.PhotoUrl,
IsAnonymous = false
};
}
internal class EmailCredential : AuthCredential
{
public string Email { get; set; }
public string Password { get; set; }
}
}
}