-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1991 from microsoft/feature/private-apis
feature/private apis
- Loading branch information
Showing
119 changed files
with
2,855 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
namespace Kiota.Builder.Configuration; | ||
|
||
public abstract class SearchConfigurationBase { | ||
public string SearchTerm { get; set; } | ||
public bool ClearCache { get; set; } | ||
public string Version { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace Kiota.Builder.Extensions; | ||
|
||
public static class UriExtensions { | ||
public static string GetFileName(this Uri uri) { | ||
if(uri is null) return string.Empty; | ||
return Path.GetFileName($"{uri.Scheme}://{uri.Host}{uri.AbsolutePath}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Kiota.Builder/SearchProviders/GitHub/Authentication/AccessCodeResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Kiota.Builder.SearchProviders.GitHub.Authentication; | ||
|
||
public class AccessCodeResponse { | ||
[JsonPropertyName("access_token")] | ||
public string AccessToken { get; set; } | ||
[JsonPropertyName("token_type")] | ||
public string TokenType { get; set; } | ||
[JsonPropertyName("scope")] | ||
public string Scope { get; set; } | ||
[JsonPropertyName("error")] | ||
public string Error { get; set; } | ||
[JsonPropertyName("error_description")] | ||
public string ErrorDescription { get; set; } | ||
[JsonPropertyName("error_uri")] | ||
public Uri ErrorUri { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Kiota.Builder/SearchProviders/GitHub/Authentication/BaseAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Kiota.Abstractions; | ||
using Microsoft.Kiota.Abstractions.Authentication; | ||
|
||
namespace Kiota.Builder.SearchProviders.GitHub.Authentication; | ||
|
||
public class BaseAuthenticationProvider<T> : AnonymousAuthenticationProvider where T: class, IAccessTokenProvider | ||
{ | ||
public BaseAuthenticationProvider(string clientId, | ||
string scope, | ||
IEnumerable<string> validHosts, | ||
ILogger logger, | ||
Func<string, string, IEnumerable<string>, T> accessTokenProviderFactory, | ||
bool enableCache = true) | ||
{ | ||
ArgumentNullException.ThrowIfNull(validHosts); | ||
ArgumentNullException.ThrowIfNull(logger); | ||
ArgumentNullException.ThrowIfNull(accessTokenProviderFactory); | ||
if (string.IsNullOrEmpty(clientId)) | ||
throw new ArgumentNullException(nameof(clientId)); | ||
if (string.IsNullOrEmpty(scope)) | ||
throw new ArgumentNullException(nameof(scope)); | ||
|
||
AccessTokenProvider = accessTokenProviderFactory(clientId, scope, validHosts); | ||
if(enableCache) | ||
AccessTokenProvider = new TempFolderCachingAccessTokenProvider { | ||
Concrete = AccessTokenProvider, | ||
Logger = logger, | ||
ApiBaseUrl = new Uri($"https://{validHosts.FirstOrDefault() ?? "api.github.com"}"), | ||
AppId = clientId, | ||
}; | ||
} | ||
public IAccessTokenProvider AccessTokenProvider {get; private set;} | ||
private const string AuthorizationHeaderKey = "Authorization"; | ||
private const string ClaimsKey = "claims"; | ||
public override Task AuthenticateRequestAsync(RequestInformation request, Dictionary<string, object> additionalAuthenticationContext = null, CancellationToken cancellationToken = default) { | ||
ArgumentNullException.ThrowIfNull(request); | ||
return AuthenticateRequestInternalAsync(request, additionalAuthenticationContext, cancellationToken); | ||
} | ||
private async Task AuthenticateRequestInternalAsync(RequestInformation request, Dictionary<string, object> additionalAuthenticationContext = null, CancellationToken cancellationToken = default) { | ||
await base.AuthenticateRequestAsync(request, additionalAuthenticationContext, cancellationToken).ConfigureAwait(false); | ||
if(additionalAuthenticationContext != null && | ||
additionalAuthenticationContext.ContainsKey(ClaimsKey) && | ||
request.Headers.ContainsKey(AuthorizationHeaderKey)) | ||
request.Headers.Remove(AuthorizationHeaderKey); | ||
|
||
if(!request.Headers.ContainsKey(AuthorizationHeaderKey)) | ||
{ | ||
var token = await AccessTokenProvider.GetAuthorizationTokenAsync(request.URI, additionalAuthenticationContext, cancellationToken); | ||
if(!string.IsNullOrEmpty(token)) | ||
request.Headers.Add(AuthorizationHeaderKey, $"Bearer {token}"); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Kiota.Builder/SearchProviders/GitHub/Authentication/ITokenStorageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Kiota.Builder.SearchProviders.GitHub.Authentication; | ||
|
||
public interface ITokenStorageService { | ||
Task<string> GetTokenAsync(CancellationToken cancellationToken); | ||
Task SetTokenAsync(string value, CancellationToken cancellationToken); | ||
Task<bool> IsTokenPresentAsync(CancellationToken cancellationToken); | ||
Task<bool> DeleteTokenAsync(CancellationToken cancellationToken); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Kiota.Builder/SearchProviders/GitHub/Authentication/PatAccessTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Kiota.Abstractions.Authentication; | ||
|
||
namespace Kiota.Builder.SearchProviders.GitHub.Authentication; | ||
|
||
public class PatAccessTokenProvider : IAccessTokenProvider | ||
{ | ||
public AllowedHostsValidator AllowedHostsValidator { get; set; } = new(); | ||
public required ITokenStorageService StorageService { get; init; } | ||
public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = null, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(uri); | ||
if("https".Equals(uri.Scheme, StringComparison.OrdinalIgnoreCase) && AllowedHostsValidator.IsUrlHostValid(uri)) | ||
return StorageService.GetTokenAsync(cancellationToken); | ||
return Task.FromResult(string.Empty); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Kiota.Builder/SearchProviders/GitHub/Authentication/PatAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Kiota.Builder.SearchProviders.GitHub.Authentication; | ||
public class PatAuthenticationProvider : BaseAuthenticationProvider<PatAccessTokenProvider> | ||
{ | ||
public PatAuthenticationProvider(string clientId, string scope, IEnumerable<string> validHosts, ILogger logger, ITokenStorageService StorageService) : | ||
base(clientId, scope, validHosts, logger, (_, _, validHosts) => new PatAccessTokenProvider { | ||
StorageService = StorageService, | ||
AllowedHostsValidator = new (validHosts), | ||
}, false) | ||
{ | ||
ArgumentNullException.ThrowIfNull(StorageService); | ||
} | ||
} |
Oops, something went wrong.