|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
1 | 3 | using System;
|
2 | 4 | using System.Net.Http;
|
| 5 | +using Microsoft.Git.CredentialManager; |
3 | 6 | using Microsoft.Git.CredentialManager.Authentication.OAuth;
|
4 | 7 |
|
5 | 8 | namespace GitHub
|
6 | 9 | {
|
7 | 10 | public class GitHubOAuth2Client : OAuth2Client
|
8 | 11 | {
|
9 |
| - private static readonly string ClientId = "0120e057bd645470c1ed"; |
10 |
| - private static readonly string ClientSecret = "18867509d956965542b521a529a79bb883344c90"; |
11 |
| - private static readonly Uri RedirectUri = new Uri("http://localhost/"); |
12 |
| - |
13 |
| - public GitHubOAuth2Client(HttpClient httpClient, Uri baseUri) |
14 |
| - : base(httpClient, CreateEndpoints(baseUri), ClientId, RedirectUri, ClientSecret) { } |
| 12 | + public GitHubOAuth2Client(HttpClient httpClient, ISettings settings, Uri baseUri) |
| 13 | + : base(httpClient, CreateEndpoints(baseUri), |
| 14 | + GetClientId(settings), GetRedirectUri(settings), GetClientSecret(settings)) { } |
15 | 15 |
|
16 | 16 | private static OAuth2ServerEndpoints CreateEndpoints(Uri baseUri)
|
17 | 17 | {
|
18 |
| - Uri authEndpoint = new Uri(baseUri, "/login/oauth/authorize"); |
19 |
| - Uri tokenEndpoint = new Uri(baseUri, "/login/oauth/access_token"); |
| 18 | + Uri authEndpoint = new Uri(baseUri, GitHubConstants.OAuthAuthorizationEndpointRelativeUri); |
| 19 | + Uri tokenEndpoint = new Uri(baseUri, GitHubConstants.OAuthTokenEndpointRelativeUri); |
20 | 20 |
|
21 | 21 | Uri deviceAuthEndpoint = null;
|
22 | 22 | if (GitHubConstants.IsOAuthDeviceAuthSupported)
|
23 | 23 | {
|
24 |
| - deviceAuthEndpoint = new Uri(baseUri, "/login/oauth/authorize/device"); |
| 24 | + deviceAuthEndpoint = new Uri(baseUri, GitHubConstants.OAuthDeviceEndpointRelativeUri); |
25 | 25 | }
|
26 | 26 |
|
27 | 27 | return new OAuth2ServerEndpoints(authEndpoint, tokenEndpoint)
|
28 | 28 | {
|
29 | 29 | DeviceAuthorizationEndpoint = deviceAuthEndpoint
|
30 | 30 | };
|
31 | 31 | }
|
| 32 | + |
| 33 | + private static string GetClientId(ISettings settings) |
| 34 | + { |
| 35 | + // Check for developer override value |
| 36 | + if (settings.TryGetSetting( |
| 37 | + GitHubConstants.EnvironmentVariables.DevOAuthClientId, |
| 38 | + Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.DevOAuthClientId, |
| 39 | + out string clientId)) |
| 40 | + { |
| 41 | + return clientId; |
| 42 | + } |
| 43 | + |
| 44 | + return GitHubConstants.OAuthClientId; |
| 45 | + } |
| 46 | + |
| 47 | + private static Uri GetRedirectUri(ISettings settings) |
| 48 | + { |
| 49 | + // Check for developer override value |
| 50 | + if (settings.TryGetSetting( |
| 51 | + GitHubConstants.EnvironmentVariables.DevOAuthRedirectUri, |
| 52 | + Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.DevOAuthRedirectUri, |
| 53 | + out string redirectUriStr) && Uri.TryCreate(redirectUriStr, UriKind.Absolute, out Uri redirectUri)) |
| 54 | + { |
| 55 | + return redirectUri; |
| 56 | + } |
| 57 | + |
| 58 | + return GitHubConstants.OAuthRedirectUri; |
| 59 | + } |
| 60 | + |
| 61 | + private static string GetClientSecret(ISettings settings) |
| 62 | + { |
| 63 | + // Check for developer override value |
| 64 | + if (settings.TryGetSetting( |
| 65 | + GitHubConstants.EnvironmentVariables.DevOAuthClientSecret, |
| 66 | + Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.DevOAuthClientSecret, |
| 67 | + out string clientSecret)) |
| 68 | + { |
| 69 | + return clientSecret; |
| 70 | + } |
| 71 | + |
| 72 | + return GitHubConstants.OAuthClientSecret; |
| 73 | + } |
32 | 74 | }
|
33 | 75 | }
|
0 commit comments