Example #1
0
        public HttpResponseMessage CheckAuthenicationSession(int siteId, string code)
        {
            var response          = new HttpResponseMessage();
            var authCheckResponse = new AuthCodeCheckResponse();

            var deviceData = JsonConvert.DeserializeObject <DeviceData>(this.Request.GetHeader("X-Rock-DeviceData"));

            var rockContext = new RockContext();
            var remoteAuthenticationSessionService = new RemoteAuthenticationSessionService(rockContext);

            // Get client Ip address
            var clientIp = GetClientIp(Request);

            var expireDateTime = RockDateTime.Now.AddMinutes(_codeReusePeriodInMinutes);

            // Check for validated account
            var validatedSession = remoteAuthenticationSessionService.Queryable()
                                   .Include(s => s.AuthorizedPersonAlias.Person)
                                   .Where(s => s.Code == code &&
                                          s.SiteId == siteId &&
                                          s.SessionStartDateTime < expireDateTime &&
                                          s.AuthorizedPersonAliasId.HasValue &&
                                          s.DeviceUniqueIdentifier == deviceData.DeviceIdentifier &&
                                          s.ClientIpAddress == clientIp)
                                   .OrderByDescending(s => s.SessionStartDateTime)
                                   .FirstOrDefault();

            if (validatedSession != null)
            {
                // Mark the auth session as ended
                validatedSession.SessionEndDateTime = RockDateTime.Now;
                rockContext.SaveChanges();

                authCheckResponse.CurrentPerson  = TvHelper.GetTvPerson(validatedSession.AuthorizedPersonAlias.Person);
                authCheckResponse.IsAuthenciated = true;

                // Link personal device
                var tvDeviceTypeValueId   = DefinedValueCache.Get(SystemGuid.DefinedValue.PERSONAL_DEVICE_TYPE_TV).Id;
                var personalDeviceService = new PersonalDeviceService(rockContext);
                var personalDevice        = personalDeviceService.Queryable()
                                            .Where(a => a.DeviceUniqueIdentifier == deviceData.DeviceIdentifier && a.PersonalDeviceTypeValueId == tvDeviceTypeValueId && a.SiteId == siteId)
                                            .FirstOrDefault();

                if (personalDevice != null)
                {
                    personalDevice.PersonAliasId = validatedSession.AuthorizedPersonAliasId;
                }
            }
            else
            {
                authCheckResponse.IsAuthenciated = false;
            }



            rockContext.SaveChanges();

            // Return
            response.Content    = new StringContent(authCheckResponse.ToJson(), System.Text.Encoding.UTF8, "application/json");
            response.StatusCode = HttpStatusCode.OK;
            return(response);
        }
Example #2
0
        public IHttpActionResult GetLaunchPacket()
        {
            // Read site Id from the request header
            var siteId = this.Request.GetHeader("X-Rock-App-Id").AsIntegerOrNull();

            // Get device data from the request header
            // Get device data
            var deviceData = JsonConvert.DeserializeObject <DeviceData>(this.Request.GetHeader("X-Rock-DeviceData"));

            if (deviceData == null)
            {
                StatusCode(HttpStatusCode.InternalServerError);
            }

            if (!siteId.HasValue)
            {
                return(NotFound());
            }

            var site = SiteCache.Get(siteId.Value);

            // If the site was not found then return 404
            if (site == null)
            {
                return(NotFound());
            }

            // Return the launch packet
            try
            {
                var rockContext = new RockContext();
                var person      = GetPerson(rockContext);

                var launchPacket = new AppleLaunchPacket();
                launchPacket.EnablePageViews = site.EnablePageViews;

                if (person != null)
                {
                    var principal = ControllerContext.Request.GetUserPrincipal();

                    launchPacket.CurrentPerson           = TvHelper.GetTvPerson(person);
                    launchPacket.CurrentPerson.AuthToken = TvHelper.GetAuthenticationTokenFromUsername(principal.Identity.Name);

                    UserLoginService.UpdateLastLogin(principal.Identity.Name);
                }

                // Get or create the personal device.
                var tvDeviceTypeValueId   = DefinedValueCache.Get(SystemGuid.DefinedValue.PERSONAL_DEVICE_TYPE_TV).Id;
                var personalDeviceService = new PersonalDeviceService(rockContext);
                var personalDevice        = personalDeviceService.Queryable()
                                            .Where(a => a.DeviceUniqueIdentifier == deviceData.DeviceIdentifier && a.PersonalDeviceTypeValueId == tvDeviceTypeValueId && a.SiteId == site.Id)
                                            .FirstOrDefault();

                if (personalDevice == null)
                {
                    personalDevice = new PersonalDevice
                    {
                        DeviceUniqueIdentifier    = deviceData.DeviceIdentifier,
                        PersonalDeviceTypeValueId = tvDeviceTypeValueId,
                        SiteId               = site.Id,
                        PersonAliasId        = person?.PrimaryAliasId,
                        NotificationsEnabled = true,
                        Manufacturer         = deviceData.Manufacturer,
                        Model            = deviceData.Model,
                        Name             = deviceData.Name,
                        IsActive         = true,
                        LastSeenDateTime = RockDateTime.Now,
                        DeviceVersion    = deviceData.Version
                    };

                    personalDeviceService.Add(personalDevice);
                    rockContext.SaveChanges();
                }
                else
                {
                    // A change is determined as one of the following:
                    // 1) A change in Name, Manufacturer, Model, or NotificationsEnabled.
                    // 2) Device not being active.
                    // 3) Not seen in 24 hours.
                    // 4) Signed in with a different person.
                    var hasDeviceChanged = !personalDevice.IsActive ||
                                           personalDevice.Name != deviceData.Name ||
                                           personalDevice.Manufacturer != deviceData.Manufacturer ||
                                           personalDevice.Model != deviceData.Model ||
                                           !personalDevice.LastSeenDateTime.HasValue ||
                                           personalDevice.LastSeenDateTime.Value.AddDays(1) < RockDateTime.Now ||
                                           (person != null && personalDevice.PersonAliasId != person.PrimaryAliasId);

                    if (hasDeviceChanged)
                    {
                        personalDevice.IsActive         = true;
                        personalDevice.Manufacturer     = deviceData.Manufacturer;
                        personalDevice.Model            = deviceData.Model;
                        personalDevice.Name             = deviceData.Name;
                        personalDevice.LastSeenDateTime = RockDateTime.Now;

                        // Update the person tied to the device, but never blank it out.
                        if (person != null && personalDevice.PersonAliasId != person.PrimaryAliasId)
                        {
                            personalDevice.PersonAliasId = person.PrimaryAliasId;
                        }

                        rockContext.SaveChanges();
                    }
                }

                launchPacket.PersonalDeviceGuid = personalDevice.Guid;
                launchPacket.HomepageGuid       = site.DefaultPage.Guid;

                launchPacket.RockVersion = VersionInfo.VersionInfo.GetRockProductVersionNumber();

                return(Ok(launchPacket));
            }
            catch (Exception)
            {
                // Ooops...
                return(StatusCode(HttpStatusCode.InternalServerError));
            }
        }