Example #1
0
        public void ExternalSignin()
        {
            var user = new ExternalUserBindingModel {
                MobilePhone    = "0924",
                GivenName      = "GivenName",
                Name           = "Name",
                ProviderId     = AccountProvider.Facebook,
                NameIdentifier = "NameIdentifier",
                Surname        = "Surname",
                Email          = "*****@*****.**",
                //DeviceId = "27dc93ce-3190-4dc1-9fd1-b43a4081a16d",
                //DeviceName = "DeviceName",
                //DeviceType = "DeviceType"
            };

            var sccountProfile = new AccountProfileResult {
                Id        = 3,
                AccountId = 21,
                LinkedId  = "*****@*****.**",
                TypeId    = 2,
                StatusId  = Status.Active
            };

            var result = _accountService.ExternalSigninAsync(user, sccountProfile).GetAwaiter().GetResult();

            Console.WriteLine(result.Code);
            Console.WriteLine(result.Data);

            Assert.IsTrue(result.Code == 200);
            Assert.IsTrue(result.Data != null);
        }
Example #2
0
        public async Task <IServiceResult> ExternalSignupAsync(ExternalUserBindingModel externalUser)
        {
            var username   = _randomMaker.NewNumber();
            var duplicated = await FirstAsync(new AccountGetFirstSchema { Username = username });

            while (duplicated != null)
            {
                username   = _randomMaker.NewNumber();
                duplicated = await FirstAsync(new AccountGetFirstSchema { Username = username });
            }

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) {
                try {
                    var now = DateTime.UtcNow;

                    var account = new AccountAddSchema {
                        ProviderId = externalUser.ProviderId.ToInt(),
                        Username   = username,
                        CreatedAt  = now,
                        StatusId   = Status.Active.ToInt()
                    };
                    var accountId = await AddAsync(account);

                    var accountDevice = new AccountDeviceAddSchema {
                        AccountId  = accountId,
                        DeviceId   = externalUser.DeviceId,
                        DeviceName = externalUser.DeviceName,
                        DeviceType = externalUser.DeviceType,
                        CreatedAt  = now,
                        StatusId   = Status.Active.ToInt()
                    };
                    var deviceId = await _accountDeviceService.AddAsync(accountDevice);

                    var accountProfile = new AccountProfileAddSchema {
                        AccountId = accountId,
                        TypeId    = AccountProfileType.Email.ToInt(),
                        LinkedId  = externalUser.Email,
                        CreatedAt = now,
                        StatusId  = Status.Active.ToInt()
                    };
                    await _accountProfileService.AddAsync(accountProfile);

                    transaction.Complete();
                    var token = _jwtHandler.Bearer(new Account(accountId, deviceId, username, now).ToClaimsIdentity());
                    return(DataTransferer.Ok(token));
                }
                catch (Exception ex) {
                    var errmsg = "Something went wrong.";
                    Log.Error(ex, errmsg);
                    return(null);
                }
            }
        }
        private ExternalUserBindingModel GetExternalUser(IEnumerable <Claim> claims)
        {
            var result         = new ExternalUserBindingModel();
            var objectAccessor = ObjectAccessor.Create(result);

            foreach (var item in claims)
            {
                var claim = claims.FirstOrDefault(x => x.Type == item.Value);
                objectAccessor[item.Type] = claim?.Value;
                if (result.ProviderId == AccountProvider.Clipboardy)
                {
                    result.ProviderId = claim.Issuer.ToProvider();
                }
            }
            return(result);
        }
Example #4
0
        public void ExternalSignup()
        {
            var user = new ExternalUserBindingModel {
                MobilePhone    = "0924",
                GivenName      = "GivenName",
                Name           = "Name",
                ProviderId     = AccountProvider.Facebook,
                NameIdentifier = "NameIdentifier",
                Surname        = "Surname",
                Email          = "*****@*****.**",
                //DeviceId = Guid.NewGuid().ToString(),
                //DeviceName = "DeviceName",
                //DeviceType = "DeviceType"
            };
            var result = _accountService.ExternalSignupAsync(user).GetAwaiter().GetResult();

            Console.WriteLine(result.Code);
            Console.WriteLine(result.Data);

            Assert.IsTrue(result.Code == 200);
            Assert.IsTrue(result.Data != null);
        }
Example #5
0
        public async Task <IServiceResult> ExternalSigninAsync(ExternalUserBindingModel externalUser, AccountProfileResult accountProfile)
        {
            if (accountProfile == null && accountProfile.AccountId.HasValue)
            {
                return(DataTransferer.DefectiveEntry());
            }

            if (accountProfile.StatusId != Status.Active)
            {
                return(DataTransferer.UserIsNotActive());
            }

            var now = DateTime.UtcNow;

            var accountQuery = new AccountGetFirstSchema {
                Id       = accountProfile.AccountId,
                StatusId = Status.Active.ToInt()
            };
            var account = await FirstAsync(accountQuery);

            if (account == null)
            {
                return(DataTransferer.UserNotFound());
            }

            // todo: check the account
            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) {
                try {
                    var accountDeviceQuery = new AccountDeviceGetFirstSchema {
                        AccountId = account.Id,
                        DeviceId  = externalUser.DeviceId
                    };
                    var accountDevice = await _accountDeviceService.FirstAsync(accountDeviceQuery);

                    if (accountDevice == null)
                    {
                        return(DataTransferer.DeviceIdNotFound());
                    }

                    int deviceId;
                    if (accountDevice != null)
                    {
                        deviceId = accountDevice.Id.Value;
                        // set a new token
                        await _accountDeviceService.UpdateAsync(new AccountDeviceUpdateSchema {
                            Id = accountDevice.Id.Value
                        });
                    }
                    else
                    {
                        // create new device for account
                        deviceId = await _accountDeviceService.AddAsync(new AccountDeviceAddSchema {
                            AccountId  = account.Id,
                            DeviceId   = externalUser.DeviceId,
                            DeviceName = externalUser.DeviceName,
                            DeviceType = externalUser.DeviceType,
                            CreatedAt  = now,
                            StatusId   = Status.Active.ToInt()
                        });
                    }

                    // clean forgot password tokens
                    //account.Id.Value, transaction
                    await _accountProfileService.CleanForgotPasswordTokensAsync(account.Id.Value);

                    //if(accountProfileCleanTokens.StatusCode != 200) {
                    //    Log.Error($"Can't update 'ForgotPasswordTokens' to NULL for AccountId={account.Id}");
                    //    return DataTransferer.SomethingWentWrong();
                    //}

                    // set last signed in at
                    var accountUpdate = new AccountUpdateSchema {
                        Id             = account.Id.Value,
                        LastSignedinAt = now
                    };
                    await UpdateAsync(accountUpdate);

                    if (accountUpdate.StatusCode != 200)
                    {
                        Log.Error($"Can't update 'LastSignedinAt' after a successfully signing in for AccountId={account.Id}");
                    }

                    transaction.Complete();
                    var token = _jwtHandler.Bearer(new Account(account.Id.Value, deviceId, account.Username, now).ToClaimsIdentity());
                    return(DataTransferer.Ok(token));
                }
                catch (Exception ex) {
                    Log.Error(ex, ex.Source);
                    return(DataTransferer.InternalServerError(ex));
                }
            }
        }