public async Task <PostChangePhoneResponse> ChangeAsync( string country, string oldPhone, string newPhone, string password, string ipAddress, string userAgent, CancellationToken ct) { var identityTypes = new[] { IdentityType.PhoneAndPassword }; var identity = await _identitiesService.GetByKeyAndTypesAsync(oldPhone, identityTypes, ct); if (identity == null) { return(new PostChangePhoneResponse(true)); } var profile = await _profilesService.GetAsync(identity.ProfileId, ct); if (profile == null) { return(new PostChangePhoneResponse(true)); } var isPasswordCorrect = _identitiesService.IsPasswordCorrect(identity, password); if (!isPasswordCorrect) { return(new PostChangePhoneResponse(true)); } var newIdentity = new Identities.Models.Identity { Key = newPhone }; await _identitiesService.UpdateAsync(identity, newIdentity, ct); var tokenId = await _phoneConfirmationService.SendMessageAsync(country, newPhone, ipAddress, userAgent, ct); return(new PostChangePhoneResponse(tokenId)); }
public async Task <PostChangePasswordResponse> ChangeAsync( string country, string key, string oldPassword, string newPassword, CancellationToken ct) { var identityTypes = IdentityTypeExtensions.TypesWithPassword; var phoneIdentityType = new[] { IdentityType.PhoneAndPassword }; var identity = await _identitiesService.GetVerifiedByKeyAndTypesAsync(key, identityTypes, ct) ?? await _identitiesService.GetVerifiedByKeyAndTypesAsync(key.GetPhoneWithoutPrefixes(country), phoneIdentityType, ct); if (identity == null) { return(new PostChangePasswordResponse(true)); } var profile = await _profilesService.GetAsync(identity.ProfileId, ct); if (profile == null) { return(new PostChangePasswordResponse(true)); } var isPasswordCorrect = _identitiesService.IsPasswordCorrect(identity, oldPassword); if (!isPasswordCorrect) { return(new PostChangePasswordResponse(true)); } await _identitiesService.ChangePasswordByProfileIdAsync(profile.Id, newPassword, ct); return(new PostChangePasswordResponse(false)); }
public async Task <PostAuthorizeResponse> AuthorizeAsync( string country, string key, string password, string responseType, string redirectUri, string state, string ipAddress, string userAgent, List <string> scopes, string audience, CancellationToken ct) { var identityTypes = IdentityTypeExtensions.TypesWithPassword; var phoneIdentityType = new[] { IdentityType.PhoneAndPassword }; var identity = await _identitiesService.GetVerifiedByKeyAndTypesAsync(key, identityTypes, ct) ?? await _identitiesService.GetVerifiedByKeyAndTypesAsync(key.GetPhoneWithoutPrefixes(country), phoneIdentityType, ct); if (identity == null) { return(new PostAuthorizeResponse(redirectUri, true)); } var profile = await _profilesService.GetAsync(identity.ProfileId, ct); if (profile == null) { return(new PostAuthorizeResponse(redirectUri, true)); } var isPasswordCorrect = _identitiesService.IsPasswordCorrect(identity, password); if (!isPasswordCorrect) { return(new PostAuthorizeResponse(redirectUri, true)); } var claims = await _claimsService.GetByScopesAsync(scopes, profile, ct); switch (responseType) { case ResponseType.Code: { var code = _codesService.Create(profile, claims); var callbackUri = _callbackUriService.GetByCode(redirectUri, state, code); return(new PostAuthorizeResponse(callbackUri, false)); } case ResponseType.Token: { var accessToken = _accessTokensService.Create(audience, claims); var refreshToken = await _refreshTokensService.CreateAsync(claims, profile, ipAddress, userAgent, ct); var callbackUri = _callbackUriService.GetByTokens(redirectUri, state, accessToken, refreshToken); return(new PostAuthorizeResponse(callbackUri, false)); } default: throw new ArgumentOutOfRangeException(responseType); } }