Skip to content

Commit

Permalink
auth works
Browse files Browse the repository at this point in the history
  • Loading branch information
nerocui committed Oct 18, 2024
1 parent da666a1 commit 6cad8fd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 33 deletions.
12 changes: 6 additions & 6 deletions JitHub.Services.Accounts/LocalAccountsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public void RemoveUser()
_settingsService.Save(userIdKey, string.Empty);
}

public void SaveUser(int userId)
public void SaveUser(long userId)
{
_settingsService.Save(userIdKey, userId);
_settingsService.Save(doNotWarnDeleteRepoKey, false);
}

public int GetUser()
public long GetUser()
{
return _settingsService.Get<int>(userIdKey);
return _settingsService.Get<long>(userIdKey);
}

public bool Authorize(string token, string clientId, int userId)
public bool Authorize(string token, string clientId, long userId)
{
try
{
Expand All @@ -60,7 +60,7 @@ public bool Authorize(string token, string clientId, int userId)
return Authenticated;
}

public bool CheckAuth(int userId)
public bool CheckAuth(long userId)
{
try
{
Expand All @@ -73,7 +73,7 @@ public bool CheckAuth(int userId)
}
}

public string GetToken(int userId)
public string GetToken(long userId)
{
try
{
Expand Down
10 changes: 5 additions & 5 deletions JitHub.Services.Interfaces/IAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public interface IAccountService
{
bool Authenticated { get; set; }
void RemoveUser();
void SaveUser(int userId);
int GetUser();
void SaveUser(long userId);
long GetUser();
Task Authenticate(IEnumerable<string> scopes);
public bool Authorize(string token, string clientId, int userId);
string GetToken(int userId);
bool CheckAuth(int userId);
public bool Authorize(string token, string clientId, long userId);
string GetToken(long userId);
bool CheckAuth(long userId);
void SignOut();
}
8 changes: 6 additions & 2 deletions JitHub.Web/JitHub.Web.Client/RedirectToJitHub.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
@code {
[Parameter]
public string? Token { get; set; }
[Parameter]
public string? ClientId { get; set; }
[Parameter]
public long? UserId { get; set; }
protected override void OnInitialized()
{
if (string.IsNullOrWhiteSpace(Token))
if (string.IsNullOrWhiteSpace(Token) || string.IsNullOrWhiteSpace(ClientId) || UserId is null)
{
return;
}
NavigationManager.NavigateTo($"githubservicetest://auth?token={Token}");
NavigationManager.NavigateTo($"jithub://auth?token={Token}&clientId={ClientId}&userId={UserId}");
}
}
15 changes: 10 additions & 5 deletions JitHub.Web/JitHub.Web/Components/Pages/Authorize.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

<p>Authorizing...</p>

@if (_token is not null)
@if (_token is not null && _userId is not null && _clientId is not null)
{
<RedirectToJitHub Token="@_token"/>
<RedirectToJitHub Token="@_token" UserId="@_userId" ClientId="@_clientId"/>
}

@code {
[SupplyParameterFromQuery]
public string? Code { get; set; }

private string? _token { get; set; }
private long? _userId { get; set; }
private string? _clientId { get; set; }

protected override async Task OnInitializedAsync()
{
_clientId = Configuration["JithubClientId"];
await GetTokenFromCode();
}

Expand All @@ -40,10 +43,9 @@

public async Task<OauthToken> Detokenize(string code)
{
string clientId = Configuration["JithubClientId"];
string appSecret = Configuration["JithubAppSecret"];

if (clientId == null)
if (_clientId == null)
{
throw new Exception("Missing client information");
}
Expand All @@ -56,9 +58,12 @@
OauthToken token;
try
{
var request = new OauthTokenRequest(clientId, appSecret, code);
var request = new OauthTokenRequest(_clientId, appSecret, code);
var gitHubClient = new GitHubClient(new ProductHeaderValue("JitHubV3"));
token = await gitHubClient.Oauth.CreateAccessToken(request);
gitHubClient.Credentials = new Credentials(token.AccessToken);
var user = await gitHubClient.User.Current();
_userId = user.Id;
}
catch (Exception ex)
{
Expand Down
44 changes: 30 additions & 14 deletions JitHub/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ public void FocusMainWindow()
}
}

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
public void FocusMainWindowWithArguments(AppActivationArguments args)
{
FocusMainWindow();
HandleActivation(args);
}

private void HandleActivation(AppActivationArguments activatedEventArgs)
{
if (m_window == null)
{
m_window = new MainWindow();
}
m_window.Activate();
var navigationService = ServiceProvider.GetService<INavigationService>();
((NavigationService)navigationService).Init(((MainWindow)m_window).GetRootFrame());

var authenticated = false;
var activatedEventArgs = AppInstance.GetCurrent().GetActivatedEventArgs();

if (activatedEventArgs.Kind == ExtendedActivationKind.Protocol && activatedEventArgs.Data is ProtocolActivatedEventArgs protocolArgs)
{
var query = protocolArgs.Uri.Query;
Expand All @@ -79,8 +80,8 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
string token = queryParameters["token"];
string clientId = queryParameters["clientId"];
string userId = queryParameters["userId"];
int userIdVal;
int.TryParse(userId, out userIdVal);
long userIdVal;
long.TryParse(userId, out userIdVal);

var accountService = ServiceProvider.GetService<IAccountService>();
authenticated = accountService.Authorize(token, clientId, userIdVal);
Expand All @@ -91,17 +92,32 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
authenticated = accountService.Authenticated;
}

var dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();


if (authenticated)
{

navigationService.NavigateTo("Home", typeof(ShellPage));
dispatcherQueue.TryEnqueue(() => {
navigationService.NavigateTo("Home", typeof(ShellPage));
});
}
else
{
navigationService.NavigateTo("Login", typeof(LoginPage));
Console.WriteLine("Not authenticated");
dispatcherQueue.TryEnqueue(() => {
navigationService.NavigateTo("Login", typeof(LoginPage));
});
}
navigationService.NavigateTo("Home", typeof(LoginPage));
}

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
if (m_window == null)
{
m_window = new MainWindow();
}
m_window.Activate();
var activatedEventArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
HandleActivation(activatedEventArgs);
}

private WindowEx m_window;
Expand Down
7 changes: 7 additions & 0 deletions JitHub/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="jithub">
<uap:DisplayName>JitHub</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
</Application>
</Applications>

Expand Down
2 changes: 1 addition & 1 deletion JitHub/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static void OnActivated(object sender, AppActivationArguments args)
ExtendedActivationKind kind = args.Kind;
if (_instance != null)
{
_instance.FocusMainWindow();
_instance.FocusMainWindowWithArguments(args);
}
}
}
Expand Down

0 comments on commit 6cad8fd

Please sign in to comment.