Skip to content

Commit

Permalink
Refactor exceptions, code cleanup, and move to delegate auth state …
Browse files Browse the repository at this point in the history
…changes (#57)

* Add GoTrue specific words to dictionary

* Minor cleanup.

Remove unused imports. Add missing docs. Minor tweaks.

* Minor cleanup.

Remove unused imports. Add missing docs. Minor tweaks.

* Remove unneeded declarations

* Remove unused imports.

* Simplify imports

* Remove unused imports

* Remove unused imports

* Remove unused imports

* Remove unused imports

* Remove unused imports

* Remove unused imports

* Remove unused import

* Remove unused import

* Removed unused imports.

A few of these imports can cause problems (e.g. Linq, compiler services)

* Simplify imports

* Simplify imports

* Simplify imports

* Fix doc

* Simplify imports, fix doc

* Simplify imports, doc fixes

* Add bit more coverage for nonce tests

* Import code formatting rules from existing code base

* Remove debug. Fix floating point warning.

* Remove debug.

* Replace debug. Also ran code format on existing rules.

* Simplify exceptions, persistence, state changes

* Updates/fixes for persistence and notifications

* Simplify exceptions

* Add failure test cases. Split anon and service tests

* Remove unneeded imports

* Add abbreviations to spelling check

* Make consistent with rest of project code style

* Changed to use Options headers as sole source of truth

* Create ConfigurationFailureTests.cs

* Remove readonly

* Remove client launch

* Add bearer token failure

* Comment out debug (used to match server errors)

* Remove client launch

* Remove unused empty default headers

* Remove imports

* Add load user from persistence test

* simplify with static import

* format

* Add info on changes

* Remove imports

* Doc fix

* Add doc

* Add doc

* Reformatted, added a few more words to spell checker

* Add settings endpoint

* Minor method changes. Added some docs.

* Simplified to holder class

* Added User hint to clarify user needs to fix

* Minor method renames, changed magic link back to sign in

* Session wrapper object

* reformat

* Simplified/tweaked to use persistence object

* Settings object JSON wrapper

* Add settings API request

* Update Reason name, moved test to failure test

* Changes to tests per other changes

* Add settings api test case

* Update README to reflect changes

* Add note about settings api

* Add internal to set

* Cleanup methods that are essentially aliasing `Api` calls.

* Add code highlights

* Update gotrue-csharp.sln.DotSettings

* Simplify session persistence with interface

* Move setting persistence to method instead of options

* Extract interface for `PersistenceListener`

* Update `IGotrueClient` interface for Persistence

---------

Co-authored-by: Joseph Schultz <9093699+acupofjose@users.noreply.github.com>
  • Loading branch information
wiverson and acupofjose authored May 7, 2023
1 parent aceac60 commit f0674df
Show file tree
Hide file tree
Showing 34 changed files with 2,013 additions and 1,741 deletions.
46 changes: 22 additions & 24 deletions Gotrue/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ namespace Supabase.Gotrue
{
public class Api : IGotrueApi<User, Session>
{
protected string Url { get; private set; }
private string Url { get; }

/// <summary>
/// Function that can be set to return dynamic headers.
///
/// Headers specified in the constructor will ALWAYS take precedence over headers returned by this function.
/// </summary>
public Func<Dictionary<string, string>>? GetHeaders { get; set; }
Expand All @@ -39,14 +38,13 @@ protected Dictionary<string, string> Headers
}

/// <summary>
/// Creates a new user using their email address.
/// Creates a new API client
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
public Api(string url, Dictionary<string, string>? headers = null)
{
Url = url;

headers ??= new Dictionary<string, string>();
_headers = headers;
}
Expand All @@ -61,8 +59,7 @@ public Api(string url, Dictionary<string, string>? headers = null)
public async Task<Session?> SignUpWithEmail(string email, string password, SignUpOptions? options = null)
{
var body = new Dictionary<string, object> { { "email", email }, { "password", password } };

string endpoint = $"{Url}/signup";
var endpoint = $"{Url}/signup";

if (options != null)
{
Expand Down Expand Up @@ -94,10 +91,7 @@ public Api(string url, Dictionary<string, string>? headers = null)

return session;
}
else
{
return null;
}
return null;
}

/// <summary>
Expand Down Expand Up @@ -138,12 +132,12 @@ public async Task<PasswordlessSignInState> SignInWithOtp(SignInWithPasswordlessE
{
{ "email", options.Email },
{ "data", options.Data },
{ "create_user", options.ShouldCreateUser },
{ "create_user", options.ShouldCreateUser }
};

if (options.FlowType == OAuthFlowType.PKCE)
{
string challenge = Helpers.GenerateNonce();
var challenge = Helpers.GenerateNonce();
verifier = Helpers.GeneratePKCENonceVerifier(challenge);

body.Add("code_challenge", challenge);
Expand Down Expand Up @@ -205,16 +199,18 @@ public async Task<PasswordlessSignInState> SignInWithOtp(SignInWithPasswordlessP
/// <param name="nonce"></param>
/// <param name="captchaToken"></param>
/// <returns></returns>
/// <exception cref="InvalidProviderException"></exception>
/// <exception>
/// <cref>InvalidProviderException</cref>
/// </exception>
public Task<Session?> SignInWithIdToken(Provider provider, string idToken, string? nonce = null, string? captchaToken = null)
{
if (provider != Provider.Google && provider != Provider.Apple)
throw new InvalidProviderException($"Provider must either be: `Provider.Google` or `Provider.Apple`.");
throw new GotrueException($"Provider must be `Provider.Google` or `Provider.Apple` not {provider}");

var body = new Dictionary<string, object?>
{
{"provider", Core.Helpers.GetMappedToAttr(provider).Mapping },
{"id_token", idToken },
{"id_token", idToken }
};

if (!string.IsNullOrEmpty(nonce))
Expand All @@ -237,7 +233,7 @@ public Task<BaseResponse> SendMagicLinkEmail(string email, SignInOptions? option
{
var data = new Dictionary<string, string> { { "email", email } };

string endpoint = $"{Url}/magiclink";
var endpoint = $"{Url}/magiclink";

if (options != null)
{
Expand Down Expand Up @@ -304,7 +300,7 @@ public Task<BaseResponse> InviteUserByEmail(string email, string jwt)
{
var data = new Dictionary<string, object> {
{ "phone", phone },
{ "password", password },
{ "password", password }
};
return Helpers.MakeRequest<Session>(HttpMethod.Post, $"{Url}/token?grant_type=password", data, Headers);
}
Expand Down Expand Up @@ -406,9 +402,9 @@ public ProviderAuthState GetUriForProvider(Provider provider, SignInOptions? opt
result.PKCEVerifier = codeVerifier;
}

if (attr is MapToAttribute mappedAttr)
if (attr is MapToAttribute)
{
query.Add("provider", mappedAttr.Mapping);
query.Add("provider", attr.Mapping);

if (!string.IsNullOrEmpty(options.Scopes))
query.Add("scopes", options.Scopes);
Expand Down Expand Up @@ -500,7 +496,7 @@ public Task<BaseResponse> SignOut(string jwt)
/// </summary>
/// <param name="jwt">A valid JWT. Must be a full-access API key (e.g. service_role key).</param>
/// <param name="filter">A string for example part of the email</param>
/// <param name="sortBy">Snake case string of the given key, currently only created_at is suppported</param>
/// <param name="sortBy">Snake case string of the given key, currently only created_at is supported</param>
/// <param name="sortOrder">asc or desc, if null desc is used</param>
/// <param name="page">page to show for pagination</param>
/// <param name="perPage">items per page for pagination</param>
Expand Down Expand Up @@ -548,10 +544,7 @@ private Dictionary<string, string> TransformListUsersParams(string? filter = nul
/// <returns></returns>
public Task<User?> CreateUser(string jwt, AdminUserAttributes? attributes = null)
{
if (attributes == null)
{
attributes = new AdminUserAttributes();
}
attributes ??= new AdminUserAttributes();

return Helpers.MakeRequest<User>(HttpMethod.Post, $"{Url}/admin/users", attributes, CreateAuthedRequestHeaders(jwt));
}
Expand Down Expand Up @@ -580,6 +573,11 @@ public Task<BaseResponse> DeleteUser(string uid, string jwt)
return Helpers.MakeRequest(HttpMethod.Delete, $"{Url}/admin/users/{uid}", data, CreateAuthedRequestHeaders(jwt));
}

public Task<Settings?> Settings()
{
return Helpers.MakeRequest<Settings>(HttpMethod.Get, $"{Url}/settings");
}

/// <summary>
/// Generates a new JWT
/// </summary>
Expand Down
Loading

0 comments on commit f0674df

Please sign in to comment.