Skip to content
Open
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
25 changes: 13 additions & 12 deletions Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,21 @@
var body = new Dictionary<string, object> { { "email", email }, { "password", password } };
var endpoint = $"{Url}/signup";

if (options != null)
{
if (!string.IsNullOrEmpty(options.RedirectTo))
{
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo! } }).ToString();
}

if (options.Data != null)
{
body.Add("data", options.Data);
}
}
if (options != null)
{
if (options.Data != null)
{
body.Add("data", options.Data);
}
if (!string.IsNullOrEmpty(options.CaptchaToken))
{
body.Add("captcha_token", options.CaptchaToken);
}
}


var response = await Helpers.MakeRequest(HttpMethod.Post, endpoint, body, Headers);
var response = await Helpers.MakeRequest(HttpMethod.Post, endpoint, body, Headers);

if (!string.IsNullOrEmpty(response.Content))
{
Expand Down Expand Up @@ -545,9 +546,9 @@
{
var body = new Dictionary<string, object>
{
{ "friendly_name", mfaEnrollParams.FriendlyName },

Check warning on line 549 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 549 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.
{ "factor_type", mfaEnrollParams.FactorType },
{ "issuer", mfaEnrollParams.Issuer }

Check warning on line 551 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.

Check warning on line 551 in Gotrue/Api.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'value' in 'void Dictionary<string, object>.Add(string key, object value)'.
};

return Helpers.MakeRequest<MfaEnrollResponse>(HttpMethod.Post, $"{Url}/factors", body, CreateAuthedRequestHeaders(jwt));
Expand Down
18 changes: 17 additions & 1 deletion Gotrue/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,23 @@ internal static ProviderAuthState GetUrlForProvider(string url, Constants.Provid
result.PKCEVerifier = codeVerifier;
}

if (attr == null)
// Handle state parameter for CSRF protection
string stateParameter;
if (!string.IsNullOrEmpty(options.State))
{
// Developer provided their own state - use it
stateParameter = options.State;
}
else
{
// Auto-generate state for convenience and security
stateParameter = Helpers.GenerateNonce();
}

query.Add("state", stateParameter);
result.State = stateParameter;

if (attr == null)
throw new Exception("Unknown provider");

query.Add("provider", attr.Mapping);
Expand Down
6 changes: 6 additions & 0 deletions Gotrue/ProviderAuthState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public ProviderAuthState(Uri uri)
{
Uri = uri;
}

/// <summary>
/// The state parameter for CSRF protection.
/// This should be stored by the developer and validated when the OAuth callback is received.
/// </summary>
public string? State { get; set; }
}
}
7 changes: 7 additions & 0 deletions Gotrue/SignInOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ public class SignInOptions
/// PKCE is recommended for mobile and server-side applications.
/// </summary>
public OAuthFlowType FlowType { get; set; } = OAuthFlowType.Implicit;


/// <summary>
/// The state parameter for CSRF protection.
/// This should be stored by the developer and validated when the OAuth callback is received.
/// </summary>
public string? State { get; set; }
}
}
6 changes: 6 additions & 0 deletions Gotrue/SignUpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ public class SignUpOptions : SignInOptions
/// Optional user metadata.
/// </summary>
public Dictionary<string, object>? Data { get; set; }


/// <summary>
/// Captcha token for verification when captcha is enabled
/// </summary>
public string? CaptchaToken { get; set; }
}
}
19 changes: 18 additions & 1 deletion GotrueTests/AnonKeyClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,24 @@ public async Task SignUpUserPhone()
AreEqual("Testing", session.User.UserMetadata["firstName"]);
}

[TestMethod("Client: Triggers Token Refreshed Event")]
[TestMethod("Client: Sign Up with Captcha Token")]
public async Task SignUpUserWithCaptchaToken()
{
IsTrue(AuthStateIsEmpty());

var email = $"{RandomString(12)}@supabase.io";
var options = new SignUpOptions
{
CaptchaToken = "test-captcha-token-12345"
};

var session = await _client.SignUp(email, PASSWORD, options);

VerifyGoodSession(session);
}


[TestMethod("Client: Triggers Token Refreshed Event")]
public async Task ClientTriggersTokenRefreshedEvent()
{
var tsc = new TaskCompletionSource<string>();
Expand Down
Loading