-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from mjcheetham/gh-oauthenv
Allow GitHub OAuth params to be overridden at runtime
- Loading branch information
Showing
3 changed files
with
65 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,75 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
using System; | ||
using System.Net.Http; | ||
using Microsoft.Git.CredentialManager; | ||
using Microsoft.Git.CredentialManager.Authentication.OAuth; | ||
|
||
namespace GitHub | ||
{ | ||
public class GitHubOAuth2Client : OAuth2Client | ||
{ | ||
private static readonly string ClientId = "0120e057bd645470c1ed"; | ||
private static readonly string ClientSecret = "18867509d956965542b521a529a79bb883344c90"; | ||
private static readonly Uri RedirectUri = new Uri("http://localhost/"); | ||
|
||
public GitHubOAuth2Client(HttpClient httpClient, Uri baseUri) | ||
: base(httpClient, CreateEndpoints(baseUri), ClientId, RedirectUri, ClientSecret) { } | ||
public GitHubOAuth2Client(HttpClient httpClient, ISettings settings, Uri baseUri) | ||
: base(httpClient, CreateEndpoints(baseUri), | ||
GetClientId(settings), GetRedirectUri(settings), GetClientSecret(settings)) { } | ||
|
||
private static OAuth2ServerEndpoints CreateEndpoints(Uri baseUri) | ||
{ | ||
Uri authEndpoint = new Uri(baseUri, "/login/oauth/authorize"); | ||
Uri tokenEndpoint = new Uri(baseUri, "/login/oauth/access_token"); | ||
Uri authEndpoint = new Uri(baseUri, GitHubConstants.OAuthAuthorizationEndpointRelativeUri); | ||
Uri tokenEndpoint = new Uri(baseUri, GitHubConstants.OAuthTokenEndpointRelativeUri); | ||
|
||
Uri deviceAuthEndpoint = null; | ||
if (GitHubConstants.IsOAuthDeviceAuthSupported) | ||
{ | ||
deviceAuthEndpoint = new Uri(baseUri, "/login/oauth/authorize/device"); | ||
deviceAuthEndpoint = new Uri(baseUri, GitHubConstants.OAuthDeviceEndpointRelativeUri); | ||
} | ||
|
||
return new OAuth2ServerEndpoints(authEndpoint, tokenEndpoint) | ||
{ | ||
DeviceAuthorizationEndpoint = deviceAuthEndpoint | ||
}; | ||
} | ||
|
||
private static string GetClientId(ISettings settings) | ||
{ | ||
// Check for developer override value | ||
if (settings.TryGetSetting( | ||
GitHubConstants.EnvironmentVariables.DevOAuthClientId, | ||
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.DevOAuthClientId, | ||
out string clientId)) | ||
{ | ||
return clientId; | ||
} | ||
|
||
return GitHubConstants.OAuthClientId; | ||
} | ||
|
||
private static Uri GetRedirectUri(ISettings settings) | ||
{ | ||
// Check for developer override value | ||
if (settings.TryGetSetting( | ||
GitHubConstants.EnvironmentVariables.DevOAuthRedirectUri, | ||
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.DevOAuthRedirectUri, | ||
out string redirectUriStr) && Uri.TryCreate(redirectUriStr, UriKind.Absolute, out Uri redirectUri)) | ||
{ | ||
return redirectUri; | ||
} | ||
|
||
return GitHubConstants.OAuthRedirectUri; | ||
} | ||
|
||
private static string GetClientSecret(ISettings settings) | ||
{ | ||
// Check for developer override value | ||
if (settings.TryGetSetting( | ||
GitHubConstants.EnvironmentVariables.DevOAuthClientSecret, | ||
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.DevOAuthClientSecret, | ||
out string clientSecret)) | ||
{ | ||
return clientSecret; | ||
} | ||
|
||
return GitHubConstants.OAuthClientSecret; | ||
} | ||
} | ||
} |