Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a8a1bb5

Browse files
committed
Return LoginResult from LoginManager.
So we can return the scopes as well as the user.
1 parent 322df9b commit a8a1bb5

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

src/GitHub.Api/ILoginManager.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public interface ILoginManager
2121
/// <param name="client">An octokit client configured to access the server.</param>
2222
/// <param name="userName">The username.</param>
2323
/// <param name="password">The password.</param>
24-
/// <returns>The logged in user.</returns>
24+
/// <returns>A <see cref="LoginResult"/> with the details of the successful login.</returns>
2525
/// <exception cref="AuthorizationException">
2626
/// The login authorization failed.
2727
/// </exception>
28-
Task<User> Login(HostAddress hostAddress, IGitHubClient client, string userName, string password);
28+
Task<LoginResult> Login(HostAddress hostAddress, IGitHubClient client, string userName, string password);
2929

3030
/// <summary>
3131
/// Attempts to log into a GitHub server via OAuth in the browser.
@@ -35,11 +35,11 @@ public interface ILoginManager
3535
/// <param name="oauthClient">An octokit OAuth client configured to access the server.</param>
3636
/// <param name="openBrowser">A callback that should open a browser at the requested URL.</param>
3737
/// <param name="cancel">A cancellation token used to cancel the operation.</param>
38-
/// <returns>The logged in user.</returns>
38+
/// <returns>A <see cref="LoginResult"/> with the details of the successful login.</returns>
3939
/// <exception cref="AuthorizationException">
4040
/// The login authorization failed.
4141
/// </exception>
42-
Task<User> LoginViaOAuth(
42+
Task<LoginResult> LoginViaOAuth(
4343
HostAddress hostAddress,
4444
IGitHubClient client,
4545
IOauthClient oauthClient,
@@ -52,7 +52,8 @@ Task<User> LoginViaOAuth(
5252
/// <param name="hostAddress">The address of the server.</param>
5353
/// <param name="client">An octokit client configured to access the server.</param>
5454
/// <param name="token">The token.</param>
55-
Task<User> LoginWithToken(
55+
/// <returns>A <see cref="LoginResult"/> with the details of the successful login.</returns>
56+
Task<LoginResult> LoginWithToken(
5657
HostAddress hostAddress,
5758
IGitHubClient client,
5859
string token);
@@ -62,11 +63,11 @@ Task<User> LoginWithToken(
6263
/// </summary>
6364
/// <param name="hostAddress">The address of the server.</param>
6465
/// <param name="client">An octokit client configured to access the server.</param>
65-
/// <returns>The logged in user.</returns>
66+
/// <returns>A <see cref="LoginResult"/> with the details of the successful login.</returns>
6667
/// <exception cref="AuthorizationException">
6768
/// The login authorization failed.
6869
/// </exception>
69-
Task<User> LoginFromCache(HostAddress hostAddress, IGitHubClient client);
70+
Task<LoginResult> LoginFromCache(HostAddress hostAddress, IGitHubClient client);
7071

7172
/// <summary>
7273
/// Logs out of GitHub server.

src/GitHub.Api/LoginManager.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public LoginManager(
7070
}
7171

7272
/// <inheritdoc/>
73-
public async Task<User> Login(
73+
public async Task<LoginResult> Login(
7474
HostAddress hostAddress,
7575
IGitHubClient client,
7676
string userName,
@@ -129,7 +129,7 @@ public async Task<User> Login(
129129
}
130130

131131
/// <inheritdoc/>
132-
public async Task<User> LoginViaOAuth(
132+
public async Task<LoginResult> LoginViaOAuth(
133133
HostAddress hostAddress,
134134
IGitHubClient client,
135135
IOauthClient oauthClient,
@@ -152,13 +152,13 @@ public async Task<User> LoginViaOAuth(
152152
var token = await oauthClient.CreateAccessToken(request).ConfigureAwait(false);
153153

154154
await keychain.Save("[oauth]", token.AccessToken, hostAddress).ConfigureAwait(false);
155-
var user = await ReadUserWithRetry(client).ConfigureAwait(false);
156-
await keychain.Save(user.Login, token.AccessToken, hostAddress).ConfigureAwait(false);
157-
return user;
155+
var result = await ReadUserWithRetry(client).ConfigureAwait(false);
156+
await keychain.Save(result.User.Login, token.AccessToken, hostAddress).ConfigureAwait(false);
157+
return result;
158158
}
159159

160160
/// <inheritdoc/>
161-
public async Task<User> LoginWithToken(
161+
public async Task<LoginResult> LoginWithToken(
162162
HostAddress hostAddress,
163163
IGitHubClient client,
164164
string token)
@@ -171,9 +171,9 @@ public async Task<User> LoginWithToken(
171171

172172
try
173173
{
174-
var user = await ReadUserWithRetry(client).ConfigureAwait(false);
175-
await keychain.Save(user.Login, token, hostAddress).ConfigureAwait(false);
176-
return user;
174+
var result = await ReadUserWithRetry(client).ConfigureAwait(false);
175+
await keychain.Save(result.User.Login, token, hostAddress).ConfigureAwait(false);
176+
return result;
177177
}
178178
catch
179179
{
@@ -183,7 +183,7 @@ public async Task<User> LoginWithToken(
183183
}
184184

185185
/// <inheritdoc/>
186-
public Task<User> LoginFromCache(HostAddress hostAddress, IGitHubClient client)
186+
public Task<LoginResult> LoginFromCache(HostAddress hostAddress, IGitHubClient client)
187187
{
188188
Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
189189
Guard.ArgumentNotNull(client, nameof(client));
@@ -349,7 +349,7 @@ e is ForbiddenException ||
349349
apiException?.StatusCode == (HttpStatusCode)422);
350350
}
351351

352-
async Task<User> ReadUserWithRetry(IGitHubClient client)
352+
async Task<LoginResult> ReadUserWithRetry(IGitHubClient client)
353353
{
354354
var retry = 0;
355355

@@ -370,7 +370,7 @@ async Task<User> ReadUserWithRetry(IGitHubClient client)
370370
}
371371
}
372372

373-
async Task<User> GetUserAndCheckScopes(IGitHubClient client)
373+
async Task<LoginResult> GetUserAndCheckScopes(IGitHubClient client)
374374
{
375375
var response = await client.Connection.Get<User>(
376376
UserEndpoint, null, null).ConfigureAwait(false);
@@ -384,7 +384,7 @@ async Task<User> GetUserAndCheckScopes(IGitHubClient client)
384384

385385
if (ScopesMatch(minimumScopes, returnedScopes))
386386
{
387-
return response.Body;
387+
return new LoginResult(response.Body, returnedScopes);
388388
}
389389
else
390390
{

src/GitHub.Api/LoginResult.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Octokit;
4+
5+
namespace GitHub.Api
6+
{
7+
/// <summary>
8+
/// Holds the result of a successful login by <see cref="ILoginManager"/>.
9+
/// </summary>
10+
public class LoginResult
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="LoginResult"/> class.
14+
/// </summary>
15+
/// <param name="user">The logged-in user.</param>
16+
/// <param name="scopes">The login scopes.</param>
17+
public LoginResult(User user, IReadOnlyList<string> scopes)
18+
{
19+
User = user;
20+
Scopes = scopes;
21+
}
22+
23+
/// <summary>
24+
/// Gets the login scopes.
25+
/// </summary>
26+
public IReadOnlyList<string> Scopes { get; }
27+
28+
/// <summary>
29+
/// Gets the logged-in user.
30+
/// </summary>
31+
public User User { get; }
32+
}
33+
}

src/GitHub.VisualStudio/Services/ConnectionManager.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public async Task<IConnection> LogIn(HostAddress address, string userName, strin
7979
}
8080

8181
var client = CreateClient(address);
82-
var user = await loginManager.Login(address, client, userName, password);
83-
var connection = new Connection(address, userName, user);
82+
var login = await loginManager.Login(address, client, userName, password);
83+
var connection = new Connection(address, userName, login.User);
8484

8585
conns.Add(connection);
8686
await SaveConnections();
@@ -100,8 +100,8 @@ public async Task<IConnection> LogInViaOAuth(HostAddress address, CancellationTo
100100

101101
var client = CreateClient(address);
102102
var oauthClient = new OauthClient(client.Connection);
103-
var user = await loginManager.LoginViaOAuth(address, client, oauthClient, OpenBrowser, cancel);
104-
var connection = new Connection(address, user.Login, user);
103+
var login = await loginManager.LoginViaOAuth(address, client, oauthClient, OpenBrowser, cancel);
104+
var connection = new Connection(address, login.User.Login, login.User);
105105

106106
conns.Add(connection);
107107
await SaveConnections();
@@ -121,8 +121,8 @@ public async Task<IConnection> LogInWithToken(HostAddress address, string token)
121121
}
122122

123123
var client = CreateClient(address);
124-
var user = await loginManager.LoginWithToken(address, client, token);
125-
var connection = new Connection(address, user.Login, user);
124+
var login = await loginManager.LoginWithToken(address, client, token);
125+
var connection = new Connection(address, login.User.Login, login.User);
126126

127127
conns.Add(connection);
128128
await SaveConnections();
@@ -156,8 +156,8 @@ public async Task Retry(IConnection connection)
156156
try
157157
{
158158
var client = CreateClient(c.HostAddress);
159-
var user = await loginManager.LoginFromCache(connection.HostAddress, client);
160-
c.SetSuccess(user);
159+
var login = await loginManager.LoginFromCache(connection.HostAddress, client);
160+
c.SetSuccess(login.User);
161161
await usageTracker.IncrementCounter(x => x.NumberOfLogins);
162162
}
163163
catch (Exception e)
@@ -201,12 +201,12 @@ async Task LoadConnections(ObservableCollection<IConnection> result)
201201
foreach (Connection connection in result)
202202
{
203203
var client = CreateClient(connection.HostAddress);
204-
User user = null;
204+
LoginResult login = null;
205205

206206
try
207207
{
208-
user = await loginManager.LoginFromCache(connection.HostAddress, client);
209-
connection.SetSuccess(user);
208+
login = await loginManager.LoginFromCache(connection.HostAddress, client);
209+
connection.SetSuccess(login.User);
210210
await usageTracker.IncrementCounter(x => x.NumberOfLogins);
211211
}
212212
catch (Exception e)

test/GitHub.Api.UnitTests/LoginManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task LoggedInUserIsReturned()
4848
var target = new LoginManager(keychain, tfa, oauthListener, "id", "secret", scopes, scopes);
4949
var result = await target.Login(host, client, "foo", "bar");
5050

51-
Assert.That(user, Is.SameAs(result));
51+
Assert.That(user, Is.SameAs(result.User));
5252
}
5353

5454
[Test]

test/GitHub.VisualStudio.UnitTests/Services/ConnectionManagerTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,14 @@ static IConnectionCache CreateConnectionCache(params string[] hosts)
261261
static ILoginManager CreateLoginManager()
262262
{
263263
var result = Substitute.For<ILoginManager>();
264+
result.Login(null, null, null, null)
265+
.ReturnsForAnyArgs(new LoginResult(new User(), new[] { "scope1" }));
264266
result.Login(HostAddress.Create("invalid.com"), Arg.Any<IGitHubClient>(), Arg.Any<string>(), Arg.Any<string>())
265-
.Returns<User>(_ => { throw new AuthorizationException(); });
267+
.Returns<LoginResult>(_ => { throw new AuthorizationException(); });
266268
result.LoginFromCache(null, null)
267-
.ReturnsForAnyArgs(new User());
269+
.ReturnsForAnyArgs(new LoginResult(new User(), new[] { "scope1" }));
268270
result.LoginFromCache(HostAddress.Create("invalid.com"), Arg.Any<IGitHubClient>())
269-
.Returns<User>(_ => { throw new AuthorizationException(); });
271+
.Returns<LoginResult>(_ => { throw new AuthorizationException(); });
270272
return result;
271273
}
272274
}

0 commit comments

Comments
 (0)