Beispiel #1
0
        public virtual ActionResult Callback(string code)
        {
            var user = UserManager.FindById(User.Identity.GetUserId());

            WebServerClient client = GetClient();
            if (client == null)
            {
                return Redirect("../Devices/Index");
            }
            ViewBag.Message = String.Format("Started Callback with Code: {0}<br />", code);
            try
            {
                IAuthorizationState authorisation = client.ProcessUserAuthorization();
                if (authorisation == null)
                {
                    ViewBag.Message = "Device not authorised!";
                    return Redirect("../Devices/Index");
                }
                DeviceType deviceType = deviceService.FindDeviceTypeByName(DeviceName());
                if (deviceType == null)
                {
                    ViewBag.Message = "Could not find " + DeviceName() + " connectivity settings!";
                    return Redirect("../Devices/Index");
                }

                Device device = new Device();

                device.ConstructionFactory(
                    authorisation.AccessToken,
                    authorisation.RefreshToken,
                    deviceType.Id,
                    authorisation.AccessTokenExpirationUtc,
                    user.Id);

                // if a device of this type already exisits for this user, update it
                Device deviceToUpdate = deviceService.FindDevice(user.Id, DeviceName()); // mock user ID for now - should be getting this from session?

                if (deviceToUpdate != null)
                {
                    deviceToUpdate.refreshToken = device.refreshToken;
                    deviceToUpdate.tokenExpiration = device.tokenExpiration;
                    deviceToUpdate.accessToken = device.accessToken;
                    deviceToUpdate.deviceTypeId = device.deviceTypeId;
                }
                else
                {
                    db.Devices.Add(device);
                }
                db.SaveChanges();
                return Redirect("../Devices/Index");
                // return RedirectToAction("Index"); // redirect to list of actions
            }
            catch (Exception e)
            {
                ViewBag.Message += "Exception accessing the code. " + e.Message;
            }

            return Redirect("../Devices/Index");
        }
Beispiel #2
0
        /**
         * Due to issues with the call to WebServerClient.ProcessUserAuthorization()
         *  the Jawbone device must have its oven Callback function, rather than using DeviceAPI's.
         *
         */
        public override ActionResult Callback(string code)
        {
            var user = UserManager.FindById(User.Identity.GetUserId());

            WebServerClient jawbone = GetClient();

            if (jawbone == null)
            {
                ViewBag.Message += "Got token<br />";
                return Redirect("../Devices/Index");
            }
            ViewBag.Message = String.Format("Started Callback with Code: {0}  ", code);
            try
            {
                HttpWebRequest request = GetRequest(code);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                JavaScriptSerializer js = new JavaScriptSerializer();
                var authorisation = js.Deserialize<dynamic>(reader.ReadToEnd());

                reader.Close();
                dataStream.Close();
                response.Close();

                ViewBag.Message += "Authorisation not null<br />";
                ViewBag.InitialAccessToken = authorisation["access_token"];
                Session["InitialAccessToken"] = authorisation["access_token"];
                ViewBag.InitialRefreshToken = authorisation["refresh_token"];
                Session["InitialRefreshToken"] = authorisation["refresh_token"];

                string token = authorisation["access_token"];
                ViewBag.ExtraAccessToken = token;
                ViewBag.ExtraRefreshToken = authorisation["refresh_token"];
                ViewBag.AccessTokenExpiration = DateTime.Now.AddSeconds(authorisation["expires_in"]);

                if (authorisation == null)
                {
                    ViewBag.Message = "Device not authorised!";
                    return Redirect("../Devices/Index");
                }

                DeviceType deviceType = deviceService.FindDeviceTypeByName(DeviceName());
                if(deviceType == null)
                {
                    ViewBag.Message = "Could not find jawbone connectivity settings!";
                    return Redirect("../Devices/Index");
                }

                Device device = new Device();

                device.ConstructionFactory(
                    authorisation["access_token"],
                    authorisation["refresh_token"],
                    deviceType.Id,
                    DateTime.Now.AddSeconds(authorisation["expires_in"]),
                    user.Id);

                Device temp = deviceService.FindDevice(user.Id, DeviceName()); // mock user ID for now - should be getting this from session?

                db.Devices.Add(device);
                db.SaveChanges();
                return Redirect("../Devices/Index"); // redirect to list of actions
            }
            catch (Exception e)
            {
                ViewBag.Message += "Exception accessing the code. " + e.Message;
            }
            return Redirect("../Devices/Index");
        }