public async Task <UserResponse> GetUserProfile()
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            UserProfileApi userProfileApi = new UserProfileApi();

            userProfileApi.Configuration.AccessToken = userAccessToken;
            try
            {
                dynamic user = await userProfileApi.GetUserProfileAsync();

                UserResponse response = new UserResponse();
                response.firstName = user.firstName;
                response.lastName  = user.lastName;

                return(response);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
        [Route("api/forge/callback/oauth")] // see Web.Config FORGE_CALLBACK_URL variable
        public async Task <HttpResponseMessage> OAuthCallback(string code, string state)
        {
            ThreeLeggedApi    oauth  = new ThreeLeggedApi();
            DynamicDictionary bearer = await oauth.GettokenAsync(ConfigVariables.FORGE_CLIENT_ID, ConfigVariables.FORGE_CLIENT_SECRET, oAuthConstants.AUTHORIZATION_CODE, code, ConfigVariables.FORGE_CALLBACK_URL);

            // the local_id of the requester machine should be provised on the original
            // login call as passed as state
            if (!string.IsNullOrWhiteSpace(state))
            {
                bearer.Dictionary.Add("local_id", state);
            }

            // the respose come with expires_in in minutes, so let's also store the absolute time
            bearer.Dictionary.Add("expires_at", DateTime.UtcNow.AddSeconds((long)bearer.Dictionary["expires_in"]));

            // at this point we can store the access & refresh token on a database and return
            // the respective DB unique ID, that way the application can refresh the token in
            // and the client will not see it.
            string sessionIdUnprotected = await OAuthDB.RegisterUser(bearer);

            // and encrypt the database ID to send to the user
            string sessionIdProtected = Convert.ToBase64String(System.Web.Security.MachineKey.Protect(Encoding.UTF8.GetBytes(sessionIdUnprotected)));

            // return to user
            HttpResponseMessage res = Request.CreateResponse(System.Net.HttpStatusCode.OK);

            res.Content = new StringContent(sessionIdProtected, Encoding.UTF8, "text/plain");

            return(res);
        }
Example #3
0
        public async Task <IList <Item> > GetTopFoldersAsync(string hubId, string projectId)
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            IList <Item> nodes = new List <Item>();

            ProjectsApi projectsApi = new ProjectsApi();

            projectsApi.Configuration.AccessToken = userAccessToken;
            var folders = await projectsApi.GetProjectTopFoldersAsync(hubId, projectId);

            foreach (KeyValuePair <string, dynamic> folderInfo in new DynamicDictionaryItems(folders.data))
            {
                Item projectNode = new Item(folderInfo.Value.links.self.href, folderInfo.Value.attributes.displayName, "folders");
                nodes.Add(projectNode);
            }

            return(nodes);
        }
        public async Task <bool> GetIsSessionValid()
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(false);
            }

            return(await OAuthDB.IsSessionIdValid(sessionId, localId));
        }
Example #5
0
        public async Task <HttpResponseMessage> Get()
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            if (_httpClient == null)
            {
                _httpClient = new HttpClient(
                    // this should avoid HttpClient seaching for proxy settings
                    new HttpClientHandler()
                {
                    UseProxy = false,
                    Proxy    = null
                }, true);
                _httpClient.BaseAddress = new Uri(FORGE_BASE_URL);
                ServicePointManager.DefaultConnectionLimit = int.MaxValue;
            }

            string url         = Request.RequestUri.AbsolutePath.Replace(PROXY_ROUTE, string.Empty);
            string resourceUrl = url + Request.RequestUri.Query;

            try
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, resourceUrl);

                // add our Access Token
                request.Headers.Add("Authorization", "Bearer " + userAccessToken);

                HttpResponseMessage response = await _httpClient.SendAsync(request,
                                                                           // this ResponseHeadersRead force the SendAsync to return
                                                                           // as soon as the header is ready, faster
                                                                           HttpCompletionOption.ResponseHeadersRead);

                return(response);
            }
            catch
            {
                return(new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError));
            }
        }
Example #6
0
        public async Task <IList <Item> > GetHubs()
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            HubsApi hubsApi = new HubsApi();

            hubsApi.Configuration.AccessToken = userAccessToken;

            IList <Item> nodes = new List <Item>();
            var          hubs  = await hubsApi.GetHubsAsync();

            foreach (KeyValuePair <string, dynamic> hubInfo in new DynamicDictionaryItems(hubs.data))
            {
                string hubType = "hubs";
                switch ((string)hubInfo.Value.attributes.extension.type)
                {
                case "hubs:autodesk.core:Hub":
                    hubType = "hubs";
                    break;

                case "hubs:autodesk.a360:PersonalHub":
                    hubType = "personalhub";
                    break;

                case "hubs:autodesk.bim360:Account":
                    hubType = "bim360hubs";
                    break;
                }
                Item item = new Item(hubInfo.Value.links.self.href, hubInfo.Value.attributes.name, hubType);
                nodes.Add(item);
            }

            return(nodes);
        }
Example #7
0
        protected async void Page_Load(object sender, EventArgs e)
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return;
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return;
            }

            string urn        = Page.Request.QueryString["urn"];
            string proxyRoute = string.Format("{0}/api/forge/viewerproxy/", Request.Url.GetLeftPart(UriPartial.Authority));

            string script = string.Format("<script>showModel('{0}', '{1}');</script>", urn, proxyRoute);

            ClientScript.RegisterStartupScript(GetType(), "ShowModel", script);
        }
Example #8
0
        public async Task <IList <Item> > GetProjectsAsync(string hubId)
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            IList <Item> nodes = new List <Item>();

            ProjectsApi projectsApi = new ProjectsApi();

            projectsApi.Configuration.AccessToken = userAccessToken;
            var projects = await projectsApi.GetHubProjectsAsync(hubId);

            foreach (KeyValuePair <string, dynamic> projectInfo in new DynamicDictionaryItems(projects.data))
            {
                string projectType = "projects";
                switch ((string)projectInfo.Value.attributes.extension.type)
                {
                case "projects:autodesk.core:Project":
                    projectType = "a360projects";
                    break;

                case "projects:autodesk.bim360:Project":
                    projectType = "bim360projects";
                    break;
                }
                Item projectNode = new Item(projectInfo.Value.links.self.href, projectInfo.Value.attributes.name, projectType);
                nodes.Add(projectNode);
            }

            return(nodes);
        }
Example #9
0
        public async Task <IList <Item> > GetItemVersionsAsync(string projectId, string itemId)
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            IList <Item> versionsList = new List <Item>();

            ItemsApi itemsApi = new ItemsApi();

            itemsApi.Configuration.AccessToken = userAccessToken;
            var versions = await itemsApi.GetItemVersionsAsync(projectId, itemId);

            foreach (KeyValuePair <string, dynamic> version in new DynamicDictionaryItems(versions.data))
            {
                DateTime versionDate = version.Value.attributes.lastModifiedTime;

                string urn = string.Empty;
                try { urn = (string)version.Value.relationships.derivatives.data.id; }
                catch { urn = "not_available"; } // some BIM 360 versions don't have viewable

                Item itemNode = new Item("/versions/" + urn, versionDate.ToString("dd/MM/yy HH:mm:ss"), "versions");

                versionsList.Add(itemNode);
            }

            return(versionsList);
        }
Example #10
0
        public async Task <IList <Item> > GetFolderContentsAsync(string projectId, string folderId)
        {
            string sessionId, localId;

            if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId))
            {
                return(null);
            }
            if (!await OAuthDB.IsSessionIdValid(sessionId, localId))
            {
                return(null);
            }
            string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId);

            IList <Item> folderItems = new List <Item>();

            FoldersApi folderApi = new FoldersApi();

            folderApi.Configuration.AccessToken = userAccessToken;
            var folderContents = await folderApi.GetFolderContentsAsync(projectId, folderId);

            foreach (KeyValuePair <string, dynamic> folderContentItem in new DynamicDictionaryItems(folderContents.data))
            {
                string displayName = folderContentItem.Value.attributes.displayName;
                if (string.IsNullOrWhiteSpace(displayName))
                {
                    continue;
                }

                Item itemNode = new Item(folderContentItem.Value.links.self.href, displayName, (string)folderContentItem.Value.type);

                folderItems.Add(itemNode);
            }

            return(folderItems);
        }