Skip to content

Commit

Permalink
- adds pat sign in command
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Nov 28, 2022
1 parent a115c94 commit f7a39e5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
41 changes: 35 additions & 6 deletions src/kiota/Handlers/BaseKiotaCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ namespace kiota.Handlers;

internal abstract class BaseKiotaCommandHandler : ICommandHandler
{
protected TempFolderCachingAccessTokenProvider GitHubDeviceAuthenticationProvider(ILogger logger) => new(){
protected TempFolderCachingAccessTokenProvider GetGitHubDeviceStorageService(ILogger logger) => new(){
Logger = logger,
ApiBaseUrl = Configuration.Search.GitHub.ApiBaseUrl,
Concrete = null,
AppId = Configuration.Search.GitHub.AppId,
};
protected static TempFolderTokenStorageService GetGitHubPatStorageService(ILogger logger) => new() {
Logger = logger,
FileName = "pat-api.github.com"
};
internal static readonly HttpClient httpClient = new();
public required Option<LogLevel> LogLevelOption { get;init; }
protected KiotaConfiguration Configuration { get => ConfigurationFactory.Value; }
Expand All @@ -38,17 +42,41 @@ internal abstract class BaseKiotaCommandHandler : ICommandHandler
return configObject;
});
private const string GitHubScope = "repo";
protected Func<CancellationToken, Task<bool>> GetIsGitHubSignedInCallback(ILogger logger) => (cancellationToken) => {
var provider = GitHubDeviceAuthenticationProvider(logger);
private Func<CancellationToken, Task<bool>> GetIsGitHubDeviceSignedInCallback(ILogger logger) => (cancellationToken) => {
var provider = GetGitHubDeviceStorageService(logger);
return provider.TokenStorageService.Value.IsTokenPresentAsync(cancellationToken);
};
protected IAuthenticationProvider GetAuthenticationProvider(ILogger logger) =>
private Func<CancellationToken, Task<bool>> GetIsGitHubPatSignedInCallback(ILogger logger) => (cancellationToken) => {
var provider = GetGitHubPatStorageService(logger);
return provider.IsTokenPresentAsync(cancellationToken);
};
private IAuthenticationProvider GetGitHubAuthenticationProvider(ILogger logger) =>
new DeviceCodeAuthenticationProvider(Configuration.Search.GitHub.AppId,
GitHubScope,
new List<string> { Configuration.Search.GitHub.ApiBaseUrl.Host },
httpClient,
DisplayGitHubDeviceCodeLoginMessage,
logger);
private IAuthenticationProvider GetGitHubPatAuthenticationProvider(ILogger logger) =>
new PatAuthenticationProvider(Configuration.Search.GitHub.AppId,
GitHubScope,
new List<string> { Configuration.Search.GitHub.ApiBaseUrl.Host },
logger,
GetGitHubPatStorageService(logger));
protected async Task<KiotaSearcher> GetKiotaSearcher(ILoggerFactory loggerFactory, CancellationToken cancellationToken) {
var logger = loggerFactory.CreateLogger<KiotaSearcher>();
var deviceCodeSignInCallback = GetIsGitHubDeviceSignedInCallback(logger);
var patSignInCallBack = GetIsGitHubPatSignedInCallback(logger);
var isDeviceCodeSignedIn = await deviceCodeSignInCallback(cancellationToken).ConfigureAwait(false);
var isPatSignedIn = await patSignInCallBack(cancellationToken).ConfigureAwait(false);
var (provider, callback) = (isDeviceCodeSignedIn, isPatSignedIn) switch {
(true, _) => (GetGitHubAuthenticationProvider(logger), deviceCodeSignInCallback),
(_, true) => (GetGitHubPatAuthenticationProvider(logger), patSignInCallBack),
(_, _) => (null, null)
};

return new KiotaSearcher(logger, Configuration.Search, httpClient, provider, callback);
}
public int Invoke(InvocationContext context)
{
return InvokeAsync(context).GetAwaiter().GetResult();
Expand Down Expand Up @@ -187,8 +215,9 @@ protected void DisplaySearchBasicHint() {
"Example: kiota search <search term>");
}
protected async Task DisplayLoginHint(ILogger logger, CancellationToken token) {
var authProvider = GitHubDeviceAuthenticationProvider(logger);
if(!await authProvider.TokenStorageService.Value.IsTokenPresentAsync(token)) {
var deviceCodeAuthProvider = GetGitHubDeviceStorageService(logger);
var patStorage = GetGitHubPatStorageService(logger);
if(!await deviceCodeAuthProvider.TokenStorageService.Value.IsTokenPresentAsync(token) && !await patStorage.IsTokenPresentAsync(token)) {
DisplayHint("Hint: use the login command to sign in to GitHub and access private OpenAPI descriptions.",
"Example: kiota login github");
}
Expand Down
4 changes: 2 additions & 2 deletions src/kiota/Handlers/KiotaDownloadCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
logger.LogTrace("configuration: {configuration}", JsonSerializer.Serialize(Configuration));

try {
var results = await new KiotaSearcher(logger, Configuration.Search, httpClient, GetAuthenticationProvider(logger), GetIsGitHubSignedInCallback(logger))
.SearchAsync(searchTerm, version, cancellationToken);
var searcher = await GetKiotaSearcher(loggerFactory, cancellationToken).ConfigureAwait(false);
var results = await searcher.SearchAsync(searchTerm, version, cancellationToken).ConfigureAwait(false);
return await SaveResultsAsync(searchTerm, version, results, logger, cancellationToken);
} catch (Exception ex) {
#if DEBUG
Expand Down
7 changes: 4 additions & 3 deletions src/kiota/Handlers/KiotaGitHubLogoutCommandhandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var (loggerFactory, logger) = GetLoggerAndFactory<TempFolderCachingAccessTokenProvider>(context);
using (loggerFactory) {
try {
var authProvider = GitHubDeviceAuthenticationProvider(logger);
var result = await authProvider.TokenStorageService.Value.DeleteTokenAsync(cancellationToken);
if(result)
var deviceCodeAuthProvider = GetGitHubDeviceStorageService(logger);
var deviceCodeResult = await deviceCodeAuthProvider.TokenStorageService.Value.DeleteTokenAsync(cancellationToken).ConfigureAwait(false);
var patResult = await GetGitHubPatStorageService(logger).DeleteTokenAsync(cancellationToken).ConfigureAwait(false);
if(deviceCodeResult || patResult)
DisplaySuccess("Logged out successfully.");
else
DisplaySuccess("Already logged out.");
Expand Down
6 changes: 1 addition & 5 deletions src/kiota/Handlers/KiotaGitHubPatLoginCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ private async Task<int> LoginAsync(ILogger logger, string patValue, Cancellation
logger.LogCritical("no personal access token provided");
return 1;
}
var tokenStorageService = new TempFolderTokenStorageService {
Logger = logger,
FileName = "pat-api.github.com"
};
await tokenStorageService.SetTokenAsync(patValue, cancellationToken).ConfigureAwait(false);
await GetGitHubPatStorageService(logger).SetTokenAsync(patValue, cancellationToken).ConfigureAwait(false);
DisplaySuccess("Authentication successful.");
DisplaySearchBasicHint();
DisplayGitHubLogoutHint();
Expand Down
4 changes: 2 additions & 2 deletions src/kiota/Handlers/KiotaSeachBasedCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal abstract class KiotaSearchBasedCommandHandler : BaseKiotaCommandHandler
if (string.IsNullOrEmpty(openapi) && !string.IsNullOrEmpty(searchTerm))
{
logger.LogInformation("Searching for {searchTerm} in the OpenAPI description repository", searchTerm);
var searcher = new KiotaSearcher(loggerFactory.CreateLogger<KiotaSearcher>(), Configuration.Search, httpClient, GetAuthenticationProvider(logger), GetIsGitHubSignedInCallback(logger));
var results = await searcher.SearchAsync(searchTerm, version, cancellationToken);
var searcher = await GetKiotaSearcher(loggerFactory, cancellationToken).ConfigureAwait(false);
var results = await searcher.SearchAsync(searchTerm, version, cancellationToken).ConfigureAwait(false);
if (results.Count == 1)
return (results.First().Value.DescriptionUrl.ToString(), null);
else if(!results.Any()) {
Expand Down
4 changes: 2 additions & 2 deletions src/kiota/Handlers/KiotaSearchCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
logger.LogTrace("configuration: {configuration}", JsonSerializer.Serialize(Configuration));

try {
var results = await new KiotaSearcher(logger, Configuration.Search, httpClient, GetAuthenticationProvider(logger), GetIsGitHubSignedInCallback(logger))
.SearchAsync(searchTerm, version, cancellationToken);
var searcher = await GetKiotaSearcher(loggerFactory, cancellationToken).ConfigureAwait(false);
var results = await searcher.SearchAsync(searchTerm, version, cancellationToken);
await DisplayResults(searchTerm, version, results, logger, cancellationToken);
return 0;
} catch (Exception ex) {
Expand Down

0 comments on commit f7a39e5

Please sign in to comment.