Skip to content

Commit 0525476

Browse files
authored
Merge pull request #147 from mjcheetham/github-oauthdevice
Enable OAuth device authorisation grant support for GitHub
2 parents 2b944be + ecf36a3 commit 0525476

File tree

3 files changed

+8
-37
lines changed

3 files changed

+8
-37
lines changed

src/shared/GitHub/GitHubAuthentication.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ public async Task<AuthenticationPromptResult> GetAuthenticationAsync(Uri targetU
6161
{
6262
ThrowIfUserInteractionDisabled();
6363

64-
// If the GitHub auth stack doesn't support flows such as RFC 8628 and we do not have
65-
// an interactive desktop session, we cannot offer OAuth authentication.
66-
if ((modes & AuthenticationModes.OAuth) != 0
67-
&& !Context.SessionManager.IsDesktopSession
68-
&& !GitHubConstants.IsOAuthDeviceAuthSupported)
69-
{
70-
Context.Trace.WriteLine("Ignoring OAuth authentication mode because we are not in an interactive desktop session. GitHub does not support RFC 8628.");
71-
72-
modes &= ~AuthenticationModes.OAuth;
73-
}
74-
7564
if (modes == AuthenticationModes.None)
7665
{
7766
throw new ArgumentException($"Must specify at least one {nameof(AuthenticationModes)}", nameof(modes));
@@ -211,21 +200,14 @@ public async Task<OAuth2TokenResult> GetOAuthTokenAsync(Uri targetUri, IEnumerab
211200
{
212201
ThrowIfTerminalPromptsDisabled();
213202

214-
if (GitHubConstants.IsOAuthDeviceAuthSupported)
215-
{
216-
OAuth2DeviceCodeResult deviceCodeResult = await oauthClient.GetDeviceCodeAsync(scopes, CancellationToken.None);
217-
218-
string deviceMessage = $"To complete authentication please visit {deviceCodeResult.VerificationUri} and enter the following code:" +
219-
Environment.NewLine +
220-
deviceCodeResult.UserCode;
221-
Context.Terminal.WriteLine(deviceMessage);
203+
OAuth2DeviceCodeResult deviceCodeResult = await oauthClient.GetDeviceCodeAsync(scopes, CancellationToken.None);
222204

223-
return await oauthClient.GetTokenByDeviceCodeAsync(deviceCodeResult, CancellationToken.None);
224-
}
205+
string deviceMessage = $"To complete authentication please visit {deviceCodeResult.VerificationUri} and enter the following code:" +
206+
Environment.NewLine +
207+
deviceCodeResult.UserCode;
208+
Context.Terminal.WriteLine(deviceMessage);
225209

226-
// We'd like to try using an OAuth2 flow that does not require a web browser on this device
227-
// such as the device code flow (RFC 8628) but GitHub's auth stack does not support this.
228-
throw new NotSupportedException("GitHub OAuth authentication is not supported without an interactive desktop session.");
210+
return await oauthClient.GetTokenByDeviceCodeAsync(deviceCodeResult, CancellationToken.None);
229211
}
230212
}
231213

src/shared/GitHub/GitHubConstants.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class GitHubConstants
1616
public static readonly Uri OAuthRedirectUri = new Uri("http://localhost/");
1717
public static readonly Uri OAuthAuthorizationEndpointRelativeUri = new Uri("/login/oauth/authorize", UriKind.Relative);
1818
public static readonly Uri OAuthTokenEndpointRelativeUri = new Uri("/login/oauth/access_token", UriKind.Relative);
19-
public static readonly Uri OAuthDeviceEndpointRelativeUri = new Uri("/login/oauth/authorize/device", UriKind.Relative);
19+
public static readonly Uri OAuthDeviceEndpointRelativeUri = new Uri("/login/device/code", UriKind.Relative);
2020

2121
/// <summary>
2222
/// The GitHub required HTTP accepts header value
@@ -36,12 +36,6 @@ public static class GitHubConstants
3636
// TODO: remove Basic once the GCM OAuth app is whitelisted and does not require installation in every organization
3737
public const AuthenticationModes DotDomAuthenticationModes = AuthenticationModes.Basic | AuthenticationModes.OAuth;
3838

39-
/// <summary>
40-
/// Check if RFC 8628 is supported by GitHub.com and GHE.
41-
/// </summary>
42-
// TODO: remove this once device auth is supported
43-
public const bool IsOAuthDeviceAuthSupported = false;
44-
4539
public static class TokenScopes
4640
{
4741
public const string Gist = "gist";

src/shared/GitHub/GitHubOAuth2Client.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ private static OAuth2ServerEndpoints CreateEndpoints(Uri baseUri)
1717
{
1818
Uri authEndpoint = new Uri(baseUri, GitHubConstants.OAuthAuthorizationEndpointRelativeUri);
1919
Uri tokenEndpoint = new Uri(baseUri, GitHubConstants.OAuthTokenEndpointRelativeUri);
20-
21-
Uri deviceAuthEndpoint = null;
22-
if (GitHubConstants.IsOAuthDeviceAuthSupported)
23-
{
24-
deviceAuthEndpoint = new Uri(baseUri, GitHubConstants.OAuthDeviceEndpointRelativeUri);
25-
}
20+
Uri deviceAuthEndpoint = new Uri(baseUri, GitHubConstants.OAuthDeviceEndpointRelativeUri);
2621

2722
return new OAuth2ServerEndpoints(authEndpoint, tokenEndpoint)
2823
{

0 commit comments

Comments
 (0)