-
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.
github: allow OAuth params to be overridden at runtime
Allow the OAuth client ID, secret, and redirect URI to be overridden at runtime using environment variables or config.
- Loading branch information
1 parent
7efe852
commit b6063c2
Showing
3 changed files
with
62 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,72 @@ | ||
// 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) | ||
{ | ||
if (settings.TryGetSetting( | ||
GitHubConstants.EnvironmentVariables.OAuthClientId, | ||
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.OAuthClientId, | ||
out string clientId)) | ||
{ | ||
return clientId; | ||
} | ||
|
||
return GitHubConstants.OAuthClientId; | ||
} | ||
|
||
private static Uri GetRedirectUri(ISettings settings) | ||
{ | ||
if (settings.TryGetSetting( | ||
GitHubConstants.EnvironmentVariables.OAuthRedirectUri, | ||
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.OAuthRedirectUri, | ||
out string redirectUriStr) && Uri.TryCreate(redirectUriStr, UriKind.Absolute, out Uri redirectUri)) | ||
{ | ||
return redirectUri; | ||
} | ||
|
||
return GitHubConstants.OAuthRedirectUri; | ||
} | ||
|
||
private static string GetClientSecret(ISettings settings) | ||
{ | ||
if (settings.TryGetSetting( | ||
GitHubConstants.EnvironmentVariables.OAuthClientSecret, | ||
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.OAuthClientSecret, | ||
out string clientSecret)) | ||
{ | ||
return clientSecret; | ||
} | ||
|
||
return GitHubConstants.OAuthClientSecret; | ||
} | ||
} | ||
} |