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)); }
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); var allUserDataRaw = gemini.UserWidgetStore.GetAll().Where(f => f.AppId == Constants.AppId && f.ControlId == Constants.ControlId); if (allUserDataRaw == null || allUserDataRaw.Count() == 0) { return(false); } var data = allUserDataRaw.Select(f => f.Value.FromJson <List <UserWidgetDataDetails> >()).First().Find(s => s.Provider == SourceControlProvider.GitHub && s.RepositoryUrl == repositoryUrl); if (data == null) { return(false); } if (data.AccessToken.IsEmpty()) { return(false); } Username = data.Username; Password = SecretsHelper.Decrypt(data.Password, SecretsHelper.EncryptionKey); AccessToken = data.AccessToken; return(true); }
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); }
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); }