Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/Functions/Functions.Tests/ClientContractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ public async Task Invoke_ShouldSendBearerToken_GivenToken()
this.HeaderOf("Authorization").Should().Be("Bearer the-token");
}

[TestMethod]
public async Task Invoke_ShouldSendTheSetAuthToken_GivenNoToken()
{
this.client.SetAuth("the-session");
this.RespondWith(200, "ok");
await this.client.Invoke(FunctionName);
this.HeaderOf("Authorization").Should().Be("Bearer the-session");
}

[TestMethod]
public async Task Invoke_ShouldPreferTheGivenToken_GivenSetAuth()
{
this.client.SetAuth("the-session");
this.RespondWith(200, "ok");
await this.client.Invoke(FunctionName, "the-token");
this.HeaderOf("Authorization").Should().Be("Bearer the-token");
}

[TestMethod]
public async Task Invoke_ShouldSendClientInfoHeader()
{
Expand Down
5 changes: 5 additions & 0 deletions packages/Functions/Functions/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public partial class Client : IFunctionsClient
private readonly HttpClient httpClient;
private readonly string baseUrl;
private readonly FunctionRegion region;
private string? token;

/// <summary>
/// Function that can be set to return dynamic headers.
Expand All @@ -63,6 +64,9 @@ public Client(string baseUrl, FunctionRegion? region = null, ClientOptions? opti
this.httpClient = this.Options.HttpClient ?? DefaultHttpClientFactory.Create(proxy: this.Options.Proxy);
}

/// <inheritdoc />
public void SetAuth(string token) => this.token = token;

/// <summary>
/// Returns an <see cref="HttpContent"/> response, allowing for coersion into Streams, Strings, and byte[]
/// </summary>
Expand Down Expand Up @@ -172,6 +176,7 @@ private async Task<HttpResponseMessage> HandleRequest(
options.Headers = this.GetHeaders().MergeLeft(options.Headers);
}

token ??= this.token;
if (!string.IsNullOrEmpty(token))
{
options.Headers["Authorization"] = $"Bearer {token}";
Expand Down
6 changes: 6 additions & 0 deletions packages/Functions/Functions/Interfaces/IFunctionsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ public interface IFunctionsClient : IGettableHeaders
/// <param name="cancellationToken">Cancels the request and error-body reads. Cancel successful body reads separately.</param>
/// <returns>The response, which the caller must dispose.</returns>
Task<HttpResponseMessage> InvokeStream(string url, string? token = null, Client.InvokeFunctionOptions? options = null, CancellationToken cancellationToken = default);

/// <summary>
/// Sets the bearer token for calls that pass no token. It replaces any other Authorization header,
/// including the session token that Supabase.Client sends.
/// </summary>
void SetAuth(string token);
}
2 changes: 1 addition & 1 deletion packages/Functions/Functions/InvokeFunctionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class Client
/// <summary>
/// Options that can be supplied to a function invocation.
///
/// Note: If Headers.Authorization is set, it can be later overriden if a token is supplied in the method call.
/// Note: If Headers.Authorization is set, it can be later overriden if a token is supplied in the method call or set with SetAuth.
/// </summary>
public class InvokeFunctionOptions
{
Expand Down
2 changes: 2 additions & 0 deletions packages/Functions/Functions/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Supabase.Functions.Client.SetAuth(string! token) -> void
Supabase.Functions.Interfaces.IFunctionsClient.SetAuth(string! token) -> void
Loading