-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved session cookie handling, abstracting more away to specific h…
…andlers
- Loading branch information
Showing
6 changed files
with
67 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
using System; | ||
|
||
namespace PinguApps.Appwrite.Client.Clients; | ||
namespace PinguApps.Appwrite.Client.Clients; | ||
|
||
internal interface ISessionAware | ||
{ | ||
public string? Session { get; protected set; } | ||
|
||
public void UpdateSession(string? session) => Session = session; | ||
|
||
event EventHandler<string?>? SessionChanged; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/PinguApps.Appwrite.Client/Handlers/ClientCookieSessionHandler.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,56 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using PinguApps.Appwrite.Client.Internals; | ||
|
||
namespace PinguApps.Appwrite.Client.Handlers; | ||
internal class ClientCookieSessionHandler : DelegatingHandler | ||
{ | ||
private readonly Lazy<IAppwriteClient> _appwriteClient; | ||
|
||
public ClientCookieSessionHandler(Lazy<IAppwriteClient> appwriteClient) | ||
{ | ||
_appwriteClient = appwriteClient; | ||
} | ||
|
||
private IAppwriteClient AppwriteClient => _appwriteClient.Value; | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var result = await base.SendAsync(request, cancellationToken); | ||
|
||
SaveSession(result); | ||
|
||
return result; | ||
} | ||
|
||
private void SaveSession(HttpResponseMessage response) | ||
{ | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
if (response.Headers.TryGetValues("Set-Cookie", out var values)) | ||
{ | ||
var sessionCookie = values.FirstOrDefault(x => x.StartsWith("a_session", StringComparison.OrdinalIgnoreCase) && !x.Contains("legacy", StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (sessionCookie is null) | ||
return; | ||
|
||
var base64 = sessionCookie.Split('=')[1].Split(';')[0]; | ||
|
||
var decodedBytes = Convert.FromBase64String(base64); | ||
var decoded = Encoding.UTF8.GetString(decodedBytes); | ||
|
||
var sessionData = JsonSerializer.Deserialize<CookieSessionData>(decoded); | ||
|
||
if (sessionData is null) | ||
return; | ||
|
||
AppwriteClient.SetSession(sessionData.Secret); | ||
} | ||
} | ||
} | ||
} |
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