Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ApiKeyAuthenticationProvider to Kiota Core libraries #1902

Closed
darrelmiller opened this issue Oct 17, 2022 · 0 comments · Fixed by #1906
Closed

Add an ApiKeyAuthenticationProvider to Kiota Core libraries #1902

darrelmiller opened this issue Oct 17, 2022 · 0 comments · Fixed by #1906
Assignees
Labels
enhancement New feature or request fixed generator Issues or improvements relater to generation capabilities.

Comments

@darrelmiller
Copy link
Member

darrelmiller commented Oct 17, 2022

This class should handle the common case of adding an API key to either a request header or a query parameter.

Here's an example in C#

public class ApiKeyAuthenticationProvider : IAuthenticationProvider
{
    private readonly KeyLocation location;
    private readonly string keyName;
    private readonly  string keyValue;

    public enum KeyLocation 
    {
        QueryParameter,
        Header
    }

    public ApiKeyAuthenticationProvider(string keyName, string keyValue, KeyLocation location)
    {
        this.keyName = keyName;
        this.keyValue = keyValue;
        this.location = location;
    }

    public async Task AuthenticateRequestAsync(RequestInformation request, Dictionary<string, object> additionalAuthenticationContext = null, CancellationToken cancellationToken = default)
    {
        switch (location)
        {
            case KeyLocation.QueryParameter:
                var uriString = request.URI.OriginalString + (request.URI.Query != string.Empty?"&":"?") + $"{keyName}={keyValue}";
                request.URI = new Uri(uriString);
                break;
            case KeyLocation.Header:
                request.Headers.Add(keyName, keyValue);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(location), location, null);
        }
    }
}
@darrelmiller darrelmiller added the enhancement New feature or request label Oct 17, 2022
@baywet baywet self-assigned this Oct 18, 2022
@baywet baywet added the generator Issues or improvements relater to generation capabilities. label Oct 18, 2022
@baywet baywet added this to Kiota Oct 18, 2022
@baywet baywet moved this to Todo in Kiota Oct 18, 2022
@baywet baywet moved this from Todo to In Progress in Kiota Oct 18, 2022
baywet added a commit that referenced this issue Oct 18, 2022
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
@baywet baywet added the fixed label Oct 18, 2022
Repository owner moved this from In Progress to Done in Kiota Oct 18, 2022
baywet added a commit that referenced this issue Oct 18, 2022
- fixes #1902 adds documentation for API Key authentication provider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed generator Issues or improvements relater to generation capabilities.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants