Ejemplo n.º 1
0
            public ActionResult Authenticate(string code, int state)
            {
                GitHub gitHub = new GitHub(); //Creates a new GitHub object

                UserWidgetData <List <UserWidgetDataDetails> > userDataRaw = GeminiContext.UserWidgetStore.Get <List <UserWidgetDataDetails> >(CurrentUser.Entity.Id, Constants.AppId, Constants.ControlId);

                if (userDataRaw != null)
                {
                    var data = userDataRaw.Value.Find(f => f.Provider == SourceControlProvider.GitHub && f.AccessToken.IsEmpty());

                    // Need to check that state is the same as we've sent otherwise ABORT (cross-site request forgery attacks) !
                    if (!code.IsEmpty() && CurrentUser.Entity.Id == state)
                    {
                        if (data != null)
                        {
                            var password = SecretsHelper.Decrypt(data.Password, SecretsHelper.EncryptionKey);

                            try
                            {
                                var response = gitHub.GetResponse(string.Format("https://github.com/login/oauth/access_token?client_id={0}&client_secret={1}&code={2}&state={3}", data.Username, password, code, state), RestSharp.Method.GET);

                                if (response != null)
                                {
                                    var token = response.Content.FromJson <AuthenticateToken>();

                                    if (token.access_token.IsNullOrWhiteSpace())
                                    {
                                        GeminiApp.LogException(new Exception(response.Content.FromJson <Error>().error)
                                        {
                                            Source = "GitHub Authentication"
                                        }, false);
                                        gitHub.DeleteLoginDetails(CurrentUser, data, GeminiContext);
                                        //If request fails we need to make sure we delete the record associated with this authentication request from DB. Otherwise we'll have several records with empty access token
                                    }
                                    else
                                    {
                                        data.AccessToken = token.access_token;
                                        gitHub.SaveLoginDetails(CurrentUser, data, GeminiContext);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                GeminiApp.LogException(ex, false);
                            }
                        }
                    }
                    else
                    {
                        GeminiApp.LogException(new UnauthorizedAccessException("Code/State invalid")
                        {
                            Source = SourceControlProvider.GitHub.ToString()
                        }, false);

                        gitHub.DeleteLoginDetails(CurrentUser, data, GeminiContext);
                    }
                }

                return(Redirect(CurrentProject.HomePageUrl));
            }
Ejemplo n.º 2
0
        public void SaveLoginDetails(UserDto user, UserWidgetDataDetails userData, GeminiContext gemini)
        {
            UserWidgetData <UserWidgetDataDetails> userDataRaw = gemini.UserWidgetStore.Get <UserWidgetDataDetails>(user.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userDataRaw == null)
            {
                var data = new UserWidgetData <UserWidgetDataDetails>();

                data.Value = new UserWidgetDataDetails();

                data.Value = userData;

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, data.Value);
            }
            else
            {
                userDataRaw.Value.Password = userData.Password;

                userDataRaw.Value.Username = userData.Username;

                userDataRaw.Value.RepositoryUrl = userData.RepositoryUrl;

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, userDataRaw.Value);
            }
        }
        public ActionResult SaveDocument(string projectCode, int projectId, int issueId, string documentId)
        {
            if (NoSettings())
            {
                return(Redirect("~/configure"));
            }

            UserWidgetData <LucidChartUser> userData = GeminiContext.UserWidgetStore.Get <LucidChartUser>(CurrentUser.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userData == null || userData.IsNew)
            {
                return(Authenticate(string.Empty, string.Empty, string.Format("apps/lucidchart/editdocument/{0}/{1}/{2}", projectCode, projectId, issueId)));
            }

            string name = new LucidChartsConsumer(UserContext.Url, userData.Value).GetDocumentDescription(documentId).Name;

            byte[] img = new LucidChartsConsumer(UserContext.Url, userData.Value).GetDocumentImage(documentId, 0, 500, false);

            byte[] thumb = new LucidChartsConsumer(UserContext.Url, userData.Value).GetDocumentImage(documentId, 0, 128, true);

            IssueWidgetData <List <LucidChartData> > data = GeminiContext.IssueWidgetStore.Get <List <LucidChartData> >(issueId, Constants.AppId, Constants.ControlId);

            if (data == null || data.IsNew)
            {
                data = new IssueWidgetData <List <LucidChartData> >();

                data.IssueId = issueId;

                data.AppId = Constants.AppId;

                data.ControlId = Constants.ControlId;

                data.Value = new List <LucidChartData>();
            }

            LucidChartData doc = data.Value.Find(d => string.Compare(d.DocumentId, documentId, true) == 0);

            if (doc == null)
            {
                data.Value.Add(new LucidChartData()
                {
                    DocumentId = documentId, Image = img, ThumnailImage = thumb, DocumentName = name
                });
            }
            else
            {
                doc.DocumentName = name;

                doc.Image = img;

                doc.ThumnailImage = thumb;
            }

            GeminiContext.IssueWidgetStore.Save <List <LucidChartData> >(data.IssueId, data.AppId, data.ControlId, data.Value);

            string redirectURL = Countersoft.Gemini.Infrastructure.Helpers.NavigationHelper.GetIssuePageUrl(projectId, projectCode, issueId, UserContext.Card.Id, UserContext.Url);

            return(Redirect(redirectURL));
        }
Ejemplo n.º 4
0
        public void SaveLoginDetails(UserDto user, UserWidgetDataDetails userData, GeminiContext gemini)
        {
            UserWidgetData <List <UserWidgetDataDetails> > userDataRaw = gemini.UserWidgetStore.Get <List <UserWidgetDataDetails> >(user.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userDataRaw == null)
            {
                var data = new UserWidgetData <List <UserWidgetDataDetails> >();

                data.Value = new List <UserWidgetDataDetails>();

                data.Value.Add(userData);

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, data.Value);
            }
            else
            {
                /**
                 * We need to make sure there are NO github Authentication records with an empty access token otherwise when we receive the access_token back from them we will not be able to associate that request to the right
                 * Repository with an empty access token
                 * check if there is already an entry for github with empty access token, if yes then delete it
                 */
                var incompleteAuthentication = userDataRaw.Value.Find(f => f.Provider == userData.Provider && f.AccessToken.IsEmpty());

                if (incompleteAuthentication != null)
                {
                    //var index = userDataRaw.Value.FindIndex(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider && f.AccessToken.IsEmpty());
                    DeleteLoginDetails(user, incompleteAuthentication, gemini);

                    userDataRaw = gemini.UserWidgetStore.Get <List <UserWidgetDataDetails> >(user.Entity.Id, Constants.AppId, Constants.ControlId);
                }

                var tmpUser = userDataRaw.Value.Find(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);

                // If a password for this rep already exist, update the details only
                if (tmpUser != null)
                {
                    var index = userDataRaw.Value.FindIndex(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);

                    userDataRaw.Value[index].Username = userData.Username;

                    userDataRaw.Value[index].Password = userData.Password;

                    userDataRaw.Value[index].AccessToken = userData.AccessToken;
                }
                else
                {
                    // Add a new user authentication for this user
                    userDataRaw.Value.Add(userData);
                }

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, userDataRaw.Value);
            }
        }
Ejemplo n.º 5
0
        public bool AuthenticateUser(IssueDto args)
        {
            UserWidgetData <UserWidgetDataDetails> userDataRaw = GeminiContext.UserWidgetStore.Get <UserWidgetDataDetails>(CurrentUser.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userDataRaw == null)
            {
                return(false);
            }

            Username = userDataRaw.Value.Username;

            Password = SecretsHelper.Decrypt(userDataRaw.Value.Password, SecretsHelper.EncryptionKey);

            RepositoryUrl = userDataRaw.Value.RepositoryUrl;

            return(true);
        }
Ejemplo n.º 6
0
        public void DeleteLoginDetails(UserDto user, UserWidgetDataDetails userData, GeminiContext gemini)
        {
            UserWidgetData <List <UserWidgetDataDetails> > userDataRaw = gemini.UserWidgetStore.Get <List <UserWidgetDataDetails> >(user.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userDataRaw != null)
            {
                var tmpUser = userDataRaw.Value.FindAll(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);

                // If a password for this rep already exist, update the details only
                if (tmpUser != null)
                {
                    var index = userDataRaw.Value.FindIndex(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);

                    userDataRaw.Value.RemoveAt(index);
                }

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, userDataRaw.Value);
            }
        }
        public ActionResult ViewDocument(string projectCode, int projectId, int issueId, string documentId)
        {
            if (NoSettings())
            {
                return(Redirect("~/configure"));
            }

            UserWidgetData <LucidChartUser> userData = GeminiContext.UserWidgetStore.Get <LucidChartUser>(CurrentUser.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userData == null || userData.IsNew)
            {
                return(Authenticate(string.Empty, string.Empty, string.Format("apps/lucidchart/viewdocument/{0}/{1}/{2}/{3}", projectCode, projectId, issueId, documentId)));
            }

            HttpWebRequest request = new LucidChartsConsumer(UserContext.Url, userData.Value).EditDocument(documentId, projectCode, projectId, issueId);

            string queryString = request.Headers["Authorization"].Replace(",", "&").Replace("\"", "").Replace("OAuth ", "");

            return(Redirect(string.Concat(request.RequestUri.ToString(), "&", queryString)));
        }
Ejemplo n.º 8
0
        public bool AuthenticateUser(UserDto user, string repositoryUrl, GeminiContext gemini)
        {
            UserWidgetData <List <UserWidgetDataDetails> > userDataRaw = gemini.UserWidgetStore.Get <List <UserWidgetDataDetails> >(user.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userDataRaw == null)
            {
                return(false);
            }

            var data = userDataRaw.Value.Find(f => f.RepositoryUrl == repositoryUrl && f.Provider == SourceControlProvider.Git);

            if (data == null)
            {
                return(false);
            }

            Username = data.Username;

            Password = SecretsHelper.Decrypt(data.Password, SecretsHelper.EncryptionKey);

            return(true);
        }
Ejemplo n.º 9
0
        public void SaveLoginDetails(UserDto user, UserWidgetDataDetails userData, GeminiContext gemini)
        {
            UserWidgetData <List <UserWidgetDataDetails> > userDataRaw = gemini.UserWidgetStore.Get <List <UserWidgetDataDetails> >(user.Entity.Id, Constants.AppId, Constants.ControlId);

            if (userDataRaw == null)
            {
                var data = new UserWidgetData <List <UserWidgetDataDetails> >();

                data.Value = new List <UserWidgetDataDetails>();

                data.Value.Add(userData);

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, data.Value);
            }
            else
            {
                var tmpUser = userDataRaw.Value.Find(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);

                // If a password for this rep already exist, update the details only
                if (tmpUser != null)
                {
                    var index = userDataRaw.Value.FindIndex(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);

                    userDataRaw.Value[index].Password = userData.Password;

                    userDataRaw.Value[index].Username = userData.Username;
                }
                else
                {
                    // Add a new user authentication for this user
                    userDataRaw.Value.Add(userData);
                }

                gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, userDataRaw.Value);
            }
        }