I'm developing a small WPF Application which will sit on top of a Local Git Repo.
It's a Line-Of-Business app to allow some business users to edit some files via a GUI and then push them back up to GitHub
I'm using the LibGit2Sharp libraries to handle all the git commands, but I'd also like to force the users to login to the application using their GitHub credentials everytime the application is loaded.
I've been looking at the OAuth2 libraries & some existing nuget packages (http://johnnycode.com/GitHubOAuth2Client/) but they all seem to revolve around a web based app, and require client id's and secret keys.
Can anyone point me in the direction of a simple API that will take a username & password and return a simple success/fail for the login attempt or am I stuck writing my own wrapper around the GitHub API
I ended up solving this with a simple REST call to the older v2 API passing UserName & Password in the auth headers while requesting a specific repo. Then checking the permissions that came back on the requested Repo.
private class Permissions
{
public bool Admin { get; set; }
public bool Push { get; set; }
public bool Pull { get; set; }
}
private class GitHubRepo
{
public Permissions Permissions { get; set; }
public string Name { get; set; }
}
public bool Authenticate(string userName, string password)
{
try
{
var apiUrl = "https://api.github.com/";
var resource = "repos/myAccount/";
var repoName = "myRepo";
var client = new RestClient
{
BaseUrl = apiUrl,
Authenticator = new HttpBasicAuthenticator(userName, password)
};
var request = new RestRequest {Resource = string.Join("", resource, repoName)};
var response = client.Execute<List<GitHubRepo>>(request);
if ((int) response.StatusCode >= 200 && (int) response.StatusCode < 209)
{
if (response.Data.Any(r => r.Name == repoName))
{
var repo = response.Data.Single(r => r.Name == repoName);
if (repo.Permissions.Admin || (repo.Permissions.Pull && repo.Permissions.Push))
{
return true;
}
}
}
return false;
}
catch
{
return false;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments