Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 1789e9d

Browse files
Ability to login with a token
1 parent b30fc38 commit 1789e9d

File tree

8 files changed

+81
-11
lines changed

8 files changed

+81
-11
lines changed

octorun/src/octokit.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ var createOctokit = function (appName, host) {
99
}
1010
};
1111

12-
if(host) {
13-
octokitConfiguration.baseUrl = "https://" + host;
12+
if (host) {
13+
octokitConfiguration.host = host;
14+
octokitConfiguration.pathPrefix = 'api/v3';
1415
}
1516

1617
return Octokit(octokitConfiguration);

octorun/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
902910f45
1+
902910f46

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,27 @@ public void GetCurrentUser(Action<GitHubUser> onSuccess, Action<Exception> onErr
248248
.Start();
249249
}
250250

251+
public void LoginWithToken(string token, Action<bool> result)
252+
{
253+
Guard.ArgumentNotNull(token, "token");
254+
Guard.ArgumentNotNull(result, "result");
255+
256+
new FuncTask<bool>(taskManager.Token,
257+
() => loginManager.LoginWithToken(OriginalUrl, token))
258+
.FinallyInUI((success, ex, res) =>
259+
{
260+
if (!success)
261+
{
262+
logger.Warning(ex);
263+
result(false);
264+
return;
265+
}
266+
267+
result(res);
268+
})
269+
.Start();
270+
}
271+
251272
public void Login(string username, string password, Action<LoginResult> need2faCode, Action<bool, string> result)
252273
{
253274
Guard.ArgumentNotNull(need2faCode, "need2faCode");

src/GitHub.Api/Application/IApiClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void CreateRepository(string name, string description, bool isPrivate,
1111
void GetOrganizations(Action<Organization[]> onSuccess, Action<Exception> onError = null);
1212
void Login(string username, string password, Action<LoginResult> need2faCode, Action<bool, string> result);
1313
void ContinueLogin(LoginResult loginResult, string code);
14+
void LoginWithToken(string token, Action<bool> result);
1415
ITask Logout(UriString host);
1516
void GetCurrentUser(Action<GitHubUser> onSuccess, Action<Exception> onError = null);
1617
void GetServerMeta(Action<GitHubHostMeta> onSuccess, Action<Exception> onError = null);

src/GitHub.Api/Authentication/ILoginManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ interface ILoginManager
2727
/// <param name="hostAddress">The address of the server.</param>
2828
/// <inheritdoc/>
2929
ITask Logout(UriString hostAddress);
30+
31+
bool LoginWithToken(
32+
UriString host,
33+
string token);
3034
}
3135
}

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ public LoginManager(
4444
this.environment = environment;
4545
}
4646

47+
public bool LoginWithToken(
48+
UriString host,
49+
string token)
50+
{
51+
Guard.ArgumentNotNull(host, nameof(host));
52+
Guard.ArgumentNotNullOrWhiteSpace(token, nameof(token));
53+
54+
var keychainAdapter = keychain.Connect(host);
55+
keychainAdapter.Set(new Credential(host, "[token]", token));
56+
57+
try
58+
{
59+
var username = RetrieveUsername(token, host);
60+
keychainAdapter.Update(token, username);
61+
keychain.SaveToSystem(host);
62+
63+
return true;
64+
}
65+
catch (Exception e)
66+
{
67+
logger.Warning(e, "Login Exception");
68+
69+
keychain.Clear(host, false);
70+
return false;
71+
}
72+
}
73+
4774
/// <inheritdoc/>
4875
public LoginResultData Login(
4976
UriString host,
@@ -73,7 +100,7 @@ public LoginResultData Login(
73100

74101
if (loginResultData.Code == LoginResultCodes.Success)
75102
{
76-
username = RetrieveUsername(loginResultData.Token, username, host);
103+
username = RetrieveUsername(loginResultData.Token, host, username);
77104
keychainAdapter.Update(loginResultData.Token, username);
78105
keychain.SaveToSystem(host);
79106
}
@@ -113,7 +140,7 @@ public LoginResultData ContinueLogin(LoginResultData loginResultData, string two
113140
}
114141

115142
keychainAdapter.Update(loginResultData.Token, username);
116-
username = RetrieveUsername(loginResultData.Token, username, host);
143+
username = RetrieveUsername(loginResultData.Token, host, username);
117144
keychainAdapter.Update(loginResultData.Token, username);
118145
keychain.SaveToSystem(host);
119146

@@ -180,9 +207,9 @@ private LoginResultData TryLogin(
180207
return new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host);
181208
}
182209

183-
private string RetrieveUsername(string token, string username, UriString host)
210+
private string RetrieveUsername(string token, UriString host, string username = null)
184211
{
185-
if (!username.Contains("@"))
212+
if (username != null && !username.Contains("@"))
186213
{
187214
return username;
188215
}

src/UnityExtension/Assets/Editor/GitHub.Unity/Services/AuthenticationService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ public void Login(string username, string password, Action<string> twofaRequired
2828
}, authResult);
2929
}
3030

31-
public void LoginWithToken(string token, Action<string> twofaRequired, Action<bool, string> authResult)
31+
public void LoginWithToken(string token, Action<bool> authResult)
3232
{
33-
Login("[token]", token, twofaRequired, authResult);
33+
client.LoginWithToken(token, authResult);
3434
}
3535

36-
3736
public void LoginWith2fa(string code)
3837
{
3938
if (loginResultData == null)

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/GitHubEnterpriseAuthenticationView.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private void OnGUITokenLogin()
249249
GUI.FocusControl(null);
250250
isBusy = true;
251251
GetAuthenticationService(serverAddress)
252-
.LoginWithToken(token, DoRequire2fa, DoResult);
252+
.LoginWithToken(token, DoTokenResult);
253253
}
254254
}
255255
GUILayout.EndHorizontal();
@@ -346,6 +346,23 @@ private void DoResult(bool success, string msg)
346346
}
347347
}
348348

349+
private void DoTokenResult(bool success)
350+
{
351+
isBusy = false;
352+
if (success)
353+
{
354+
UsageTracker.IncrementAuthenticationViewButtonAuthentication();
355+
356+
Clear();
357+
Finish(true);
358+
}
359+
else
360+
{
361+
errorMessage = "Error validating token.";
362+
Redraw();
363+
}
364+
}
365+
349366
private void ShowMessage()
350367
{
351368
if (message != null)

0 commit comments

Comments
 (0)