You've already forked CC-Framework.BriskGameServer
Add package sync workflow
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public sealed class BriskAuthModule
|
||||
: BriskModuleBase
|
||||
{
|
||||
public async Task<BriskLoginResult> LoginWithUserIdAsync(string loginProvider, string loginUserId, BriskProfile profile = null)
|
||||
{
|
||||
RequireNotEmpty(loginProvider, nameof(loginProvider));
|
||||
RequireNotEmpty(loginUserId, nameof(loginUserId));
|
||||
|
||||
return await LoginInternalAsync(CreateLoginBody(loginProvider, profile, loginUserId, null), loginProvider, loginUserId);
|
||||
}
|
||||
|
||||
public async Task<BriskLoginResult> LoginWithCodeAsync(string loginProvider, string code, BriskProfile profile = null)
|
||||
{
|
||||
RequireNotEmpty(loginProvider, nameof(loginProvider));
|
||||
RequireNotEmpty(code, nameof(code));
|
||||
|
||||
return await LoginInternalAsync(CreateLoginBody(loginProvider, profile, null, code), loginProvider, null);
|
||||
}
|
||||
|
||||
public async Task LogoutAsync()
|
||||
{
|
||||
var context = GetContext();
|
||||
|
||||
try
|
||||
{
|
||||
if (context.Session.HasAccessToken)
|
||||
{
|
||||
await context.HttpClient.PostJsonAsync("/auth/logout", new Dictionary<string, object>(), true);
|
||||
}
|
||||
}
|
||||
catch (BriskAuthExpiredException)
|
||||
{
|
||||
// Local logout should still succeed even if the remote token has already expired.
|
||||
}
|
||||
catch (BriskBlockingException exception)
|
||||
{
|
||||
Brisk.NotifyBlockingError(exception);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
context.Session.Clear();
|
||||
await context.TokenStore.ClearAsync();
|
||||
Brisk.NotifyLoggedOut();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<BriskLoginResult> LoginInternalAsync(Dictionary<string, object> body, string requestedLoginProvider, string requestedLoginUserId)
|
||||
{
|
||||
return await ExecutePublicAsync(async context =>
|
||||
{
|
||||
var data = await context.HttpClient.PostJsonAsync("/auth/login/exchange", body, false);
|
||||
var result = BriskModelMapper.ToLoginResult(data);
|
||||
if (string.IsNullOrWhiteSpace(result.LoginProvider))
|
||||
{
|
||||
result.LoginProvider = requestedLoginProvider;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(result.LoginUserId))
|
||||
{
|
||||
result.LoginUserId = requestedLoginUserId;
|
||||
}
|
||||
|
||||
await UpdateSessionAsync(context, result);
|
||||
Brisk.NotifyLoggedIn();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private Dictionary<string, object> CreateLoginBody(string loginProvider, BriskProfile profile, string loginUserId, string code)
|
||||
{
|
||||
var context = GetContext();
|
||||
var body = new Dictionary<string, object>
|
||||
{
|
||||
{ "game_key", context.Options.GameKey },
|
||||
{ "login_provider", loginProvider }
|
||||
};
|
||||
|
||||
AddIfNotEmpty(body, "login_user_id", loginUserId);
|
||||
AddIfNotEmpty(body, "code", code);
|
||||
AddIfNotEmpty(body, "device_id", context.Options.DeviceId);
|
||||
AddIfNotEmpty(body, "client_version", context.Options.ClientVersion);
|
||||
|
||||
if (profile != null)
|
||||
{
|
||||
AddIfNotEmpty(body, "nickname", profile.Nickname);
|
||||
AddIfNotEmpty(body, "avatar_url", profile.AvatarUrl);
|
||||
if (profile.ProfileJson != null)
|
||||
{
|
||||
body["profile_json"] = profile.ProfileJson;
|
||||
}
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
private static void AddIfNotEmpty(Dictionary<string, object> body, string key, string value)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
body[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateSessionAsync(BriskContext context, BriskLoginResult result)
|
||||
{
|
||||
var expiresAt = result.ExpiresIn > 0
|
||||
? DateTimeOffset.UtcNow.AddSeconds(result.ExpiresIn)
|
||||
: (DateTimeOffset?)null;
|
||||
|
||||
context.Session.Update(
|
||||
result.AccessToken,
|
||||
expiresAt,
|
||||
result.PlayerId,
|
||||
result.ProjectAccountId,
|
||||
result.LoginProvider,
|
||||
result.LoginUserId);
|
||||
|
||||
var storedSession = new BriskStoredSession
|
||||
{
|
||||
AccessToken = result.AccessToken,
|
||||
ExpiresAt = expiresAt,
|
||||
PlayerId = result.PlayerId,
|
||||
ProjectAccountId = result.ProjectAccountId,
|
||||
LoginProvider = result.LoginProvider,
|
||||
LoginUserId = result.LoginUserId
|
||||
};
|
||||
|
||||
await context.TokenStore.SaveAsync(storedSession);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user