// Find contact by vipps email/phone number
        protected virtual string ByEmailOrPhoneNumber(VippsUserInfo vippsInfo)
        {
            var contacts = _vippsCommerceService
                           .FindCustomerContacts(vippsInfo.Email, vippsInfo.PhoneNumber)
                           .ToArray();

            if (contacts.Length == 1)
            {
                var contact = contacts.First();
                if (!_vippsLoginSanityCheck.IsValidContact(contact, vippsInfo))
                {
                    var message = "Existing contact does not pass verification.";
                    Logger.Warning($"Vipps.Login: {message}. Subject Guid: {vippsInfo.Sub}");
                    throw new VippsLoginSanityCheckException(message);
                }

                return(GetLoginEmailFromContact(contact));
            }

            if (contacts.Length > 1)
            {
                var message = "Multiple accounts found matching this Vipps UserInfo";
                Logger.Warning($"Vipps.Login: {message}. Subject Guid: {vippsInfo.Sub}");
                throw new VippsLoginDuplicateAccountException(message);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public virtual void MapVippsContactFields(
            CustomerContact contact,
            VippsUserInfo userInfo
            )
        {
            if (contact == null)
            {
                throw new ArgumentNullException(nameof(contact));
            }
            if (userInfo == null)
            {
                throw new ArgumentNullException(nameof(userInfo));
            }

            contact.Email = userInfo.Email;
            if (!string.IsNullOrWhiteSpace(userInfo.GivenName))
            {
                contact.FirstName = userInfo.GivenName;
            }
            if (!string.IsNullOrWhiteSpace(userInfo.FamilyName))
            {
                contact.LastName = userInfo.FamilyName;
            }
            if (!string.IsNullOrWhiteSpace(userInfo.Name))
            {
                contact.FullName = userInfo.Name;
            }
            if (userInfo.BirthDate.HasValue && userInfo.BirthDate.Value > DateTime.MinValue)
            {
                contact.BirthDate = userInfo.BirthDate;
            }
        }
        // Check if we're trying to link an existing account
        protected virtual string ByLinkAccount(
            SecurityTokenValidatedNotification <OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context,
            VippsUserInfo vippsUserInfo)
        {
            var props = context.AuthenticationTicket.Properties.Dictionary;

            if (props.ContainsKey(VippsConstants.LinkAccount) &&
                Guid.TryParse(props[VippsConstants.LinkAccount], out var linkAccountToken))
            {
                // Do not allow linking to multiple accounts
                string message;
                if (BySubjectGuid(vippsUserInfo) != null)
                {
                    message =
                        "This Vipps account is already linked to an account. Please remove the connection before making a new one.";
                    Logger.Error($"Vipps.Login: {message}");
                    throw new VippsLoginLinkAccountException(message, true);
                }

                var accountToLink = _vippsCommerceService.FindCustomerContactByLinkAccountToken(linkAccountToken);
                if (accountToLink == null)
                {
                    message = "Could not find account to link to.";
                    Logger.Error($"Vipps.Login: {message}");
                    throw new VippsLoginLinkAccountException(message);
                }

                return(GetLoginEmailFromContact(accountToLink));
            }

            return(null);
        }
Ejemplo n.º 4
0
        public void SyncInfoShouldSyncAddressInfo()
        {
            var contact = A.Fake <CustomerContact>();

            var userInfo = new VippsUserInfo()
            {
                Sub       = Guid.NewGuid(),
                Addresses = new[] { new VippsAddress(), new VippsAddress() }
            };
            var identity     = new ClaimsIdentity();
            var loginService = A.Fake <IVippsLoginService>();

            A.CallTo(() => loginService.GetVippsUserInfo(A <IIdentity> ._))
            .Returns(userInfo);

            var mapper  = A.Fake <IVippsLoginMapper>();
            var service = new VippsLoginCommerceService(
                loginService,
                mapper,
                A.Fake <IVippsLoginDataLoader>(), A.Fake <ICustomerContactService>());

            service.SyncInfo(identity, contact, new VippsSyncOptions
            {
                SyncContactInfo   = false,
                SyncAddresses     = true,
                ShouldSaveContact = false,
            });

            A.CallTo(() => mapper.MapAddress(null, CustomerAddressTypeEnum.Billing, null, String.Empty))
            .WithAnyArguments()
            .MustHaveHappenedTwiceExactly();
        }
Ejemplo n.º 5
0
        public void SyncInfoShouldNotSyncUserInfo()
        {
            var userinfo = new VippsUserInfo()
            {
                Addresses = Enumerable.Empty <VippsAddress>()
            };
            var loginService = A.Fake <IVippsLoginService>();

            A.CallTo(() => loginService.GetVippsUserInfo(A <ClaimsIdentity> ._)).Returns(userinfo);
            var mapper  = A.Fake <IVippsLoginMapper>();
            var service = new VippsLoginCommerceService(
                loginService,
                mapper,
                A.Fake <IVippsLoginDataLoader>(),
                A.Fake <ICustomerContactService>());

            service.SyncInfo(new ClaimsIdentity(), new CustomerContact(), new VippsSyncOptions
            {
                SyncContactInfo   = false,
                SyncAddresses     = false,
                ShouldSaveContact = false,
            });

            A.CallTo(() => mapper.MapVippsAddressFields(A <CustomerAddress> ._, A <VippsAddress> ._)).MustNotHaveHappened();
        }
Ejemplo n.º 6
0
        public bool IsValidContact(CustomerContact contact, VippsUserInfo userInfo)
        {
            if (contact == null || userInfo == null)
            {
                return(false);
            }

            return
                ((userInfo.GivenName ?? string.Empty).Equals(contact.FirstName, StringComparison.InvariantCultureIgnoreCase) &&
                 (userInfo.FamilyName ?? string.Empty).Equals(contact.LastName, StringComparison.InvariantCultureIgnoreCase));
        }
        // Check if we already have a contact for this sub
        protected virtual string BySubjectGuid(VippsUserInfo vippsInfo)
        {
            if (vippsInfo?.Sub == null)
            {
                return(null);
            }

            var customerContact = _vippsCommerceService
                                  .FindCustomerContact(vippsInfo.Sub);

            return(customerContact == null ? null : GetLoginEmailFromContact(customerContact));
        }
Ejemplo n.º 8
0
        public void IsValidContactFalseIfFirstAndLastNameDoNotMatch()
        {
            var sanityCheck = new VippsLoginSanityCheck();

            var firstName = "firstName";
            var lastName  = "lastName";
            var contact   = new CustomerContact
            {
                FirstName = firstName,
                LastName  = lastName
            };
            var userInfo = new VippsUserInfo
            {
                GivenName  = "xzxczcxz",
                FamilyName = "asdasdds"
            };

            Assert.False(sanityCheck.IsValidContact(contact, userInfo));
        }
Ejemplo n.º 9
0
        public void IsValidContactTrueIfFirstAndLastNameMatch()
        {
            var sanityCheck = new VippsLoginSanityCheck();

            var firstName = "firstName";
            var lastName  = "lastName";
            var contact   = new CustomerContact
            {
                FirstName = firstName,
                LastName  = lastName
            };
            var userInfo = new VippsUserInfo
            {
                GivenName  = firstName,
                FamilyName = lastName
            };

            Assert.True(sanityCheck.IsValidContact(contact, userInfo));
        }