public ActionResult Authenticate(SourceControlProvider provider) { //Authentication string username = Request["username"] ?? string.Empty; string password = Request["password"] ?? string.Empty; string repositoryUrl = Request["repositoryurl"] ?? string.Empty; string filename = Request["filename"] ?? string.Empty; string message = string.Empty; //Commit message bool success = true; string extraData = string.Empty; if (username.IsEmpty() || password.IsEmpty() || provider.ToString().IsEmpty() || repositoryUrl.IsEmpty()) { message = "Please make sure Username, Password are not empty"; success = false; } if (success) { UserWidgetDataDetails userData = new UserWidgetDataDetails(); userData.Username = username.Trim(); userData.Password = SecretsHelper.Encrypt(password.Trim(), SecretsHelper.EncryptionKey); userData.Provider = provider; userData.RepositoryUrl = repositoryUrl.Trim(); userData.AccessToken = string.Empty; if (provider == SourceControlProvider.SVN) { SVN svn = new SVN(); svn.SaveLoginDetails(CurrentUser, userData, GeminiContext); } else if (provider == SourceControlProvider.GitHub) { GitHub github = new GitHub(); github.SaveLoginDetails(CurrentUser, userData, GeminiContext); extraData = string.Format("https://github.com/login/oauth/authorize?client_id={0}&redirect_uri={1}apps/saucery/github/authenticate?state={2}&scope=repo", username, UserContext.Url, CurrentUser.Entity.Id); } else if (provider == SourceControlProvider.TFS2012) { TFS2012 tfs2012 = new TFS2012(); tfs2012.SaveLoginDetails(CurrentUser, userData, GeminiContext); } else if (provider == SourceControlProvider.TFS2010) { TFS2010 tfs2010 = new TFS2010(); tfs2010.SaveLoginDetails(CurrentUser, userData, GeminiContext); } else if (provider == SourceControlProvider.Git) { Git git = new Git(); git.SaveLoginDetails(CurrentUser, userData, GeminiContext); } else if (provider == SourceControlProvider.Bitbucket) { Bitbucket bitbucket = new Bitbucket(); bitbucket.SaveLoginDetails(CurrentUser, userData, GeminiContext); } } return(JsonSuccess(new { success = success, message = message, extraData = extraData })); }
public ActionResult Authenticate(int issueId) { //Authentication string username = Request["username"] ?? string.Empty; string password = Request["password"] ?? string.Empty; string repositoryUrl = Request["repositoryurl"] ?? string.Empty; string message = string.Empty; bool success = true; string dataView = string.Empty; if (username.IsEmpty() || password.IsEmpty() || repositoryUrl.IsEmpty()) { message = "Please make sure Username, Password and Url are not empty"; success = false; } if (success) { UserWidgetDataDetails userData = new UserWidgetDataDetails(); userData.Username = username.Trim(); userData.Password = SecretsHelper.Encrypt(password.Trim(), SecretsHelper.EncryptionKey); userData.RepositoryUrl = repositoryUrl.Trim(); SaveLoginDetails(CurrentUser, userData, GeminiContext); TFSPicker tfsPicker = new TFSPicker(); try { ItemWidgetArguments args = new ItemWidgetArguments(UserContext, GeminiContext, Cache, System.Web.HttpContext.Current.Request, CurrentIssue); tfsPicker.AuthenticateUser(args); UserWidgetDataDetails loginDetails = tfsPicker.getLoginDetails(); TFSPicker.ConnectByImplementingCredentialsProvider connect = new TFSPicker.ConnectByImplementingCredentialsProvider(); ICredentials iCred = new NetworkCredential(loginDetails.Username, loginDetails.Password); connect.setLoginDetails(loginDetails.Username, loginDetails.Password, loginDetails.RepositoryUrl); connect.GetCredentials(new Uri(loginDetails.RepositoryUrl), iCred); TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(loginDetails.RepositoryUrl)); configurationServer.Credentials = iCred; if (TFSPicker.IsBasicAuth) { configurationServer.ClientCredentials = new TfsClientCredentials(new BasicAuthCredential(iCred)); } else { configurationServer.ClientCredentials = new TfsClientCredentials(new WindowsCredential(iCred)); } try { configurationServer.EnsureAuthenticated(); } catch { System.Threading.Thread.Sleep(1000); configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(loginDetails.RepositoryUrl)); configurationServer.Credentials = iCred; if (TFSPicker.IsBasicAuth) { configurationServer.ClientCredentials = new TfsClientCredentials(new BasicAuthCredential(iCred)); } else { configurationServer.ClientCredentials = new TfsClientCredentials(new WindowsCredential(iCred)); } configurationServer.EnsureAuthenticated(); } } catch (Exception ex) { var logindetails = GeminiContext.UserWidgetStore.Get <UserWidgetDataDetails>(CurrentUser.Entity.Id, Constants.AppId, Constants.ControlId); if (logindetails != null) { GeminiContext.UserWidgetStore.Delete(logindetails.Id); } success = false; message = ex.Message; GeminiApp.LogException(new Exception(ex.Message) { Source = "TFS Picker" }, false); return(JsonSuccess(new { success = success, message = message })); } tfsPicker.setLoginDetails(userData.Username, password.Trim(), userData.RepositoryUrl); WidgetResult result = new WidgetResult(); List <string> tfsDetails = new List <string>(); IssueWidgetData <List <string> > data = GeminiContext.IssueWidgetStore.Get <List <string> >(issueId, Constants.AppId, Constants.ControlId); if (data != null && data.Value != null && data.Value.Count > 0) { tfsDetails = data.Value; } List <WorkItem> details = new List <WorkItem>(); foreach (var tfs in tfsDetails) { try { string url; UserWidgetDataDetails loginDetails = tfsPicker.getLoginDetails(); if (Username.IsEmpty()) { Username = loginDetails.Username; } if (Password.IsEmpty()) { Password = loginDetails.Password; } if (RepositoryUrl.IsEmpty()) { RepositoryUrl = loginDetails.RepositoryUrl; } var item = GetItem(tfs, out url); if (item != null) { details.Add(item); } } catch (Exception ex) { success = false; message = ex.Message; GeminiApp.LogException(new Exception(ex.Message) { Source = "TFS Picker" }, false); } } } return(JsonSuccess(new { success = success, message = message })); }