mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 11:12:44 +00:00
identity: use subscription v2 types & api
This commit is contained in:
committed by
Abdullah Atta
parent
55a2223198
commit
500a64de18
@@ -109,7 +109,7 @@ namespace Streetwriters.Identity.Controllers
|
||||
await UserManager.AddToRoleAsync(user, client.Id);
|
||||
if (Constants.IS_SELF_HOSTED)
|
||||
{
|
||||
await UserManager.AddClaimAsync(user, UserService.SubscriptionTypeToClaim(client.Id, Common.Enums.SubscriptionType.PREMIUM));
|
||||
await UserManager.AddClaimAsync(user, UserService.SubscriptionPlanToClaim(client.Id, SubscriptionPlan.BELIEVER));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,49 +1,50 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Streetwriters.Common.Messages;
|
||||
using Streetwriters.Common.Models;
|
||||
using Streetwriters.Common;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.Security.Claims;
|
||||
using System.Linq;
|
||||
using Streetwriters.Identity.Services;
|
||||
|
||||
namespace Streetwriters.Identity.MessageHandlers
|
||||
{
|
||||
public class CreateSubscription
|
||||
{
|
||||
public static async Task Process(CreateSubscriptionMessage message, UserManager<User> userManager)
|
||||
{
|
||||
var user = await userManager.FindByIdAsync(message.UserId);
|
||||
var client = Clients.FindClientByAppId(message.AppId);
|
||||
if (client == null || user == null) return;
|
||||
|
||||
IdentityUserClaim<string> statusClaim = user.Claims.FirstOrDefault((c) => c.ClaimType == UserService.GetClaimKey(client.Id));
|
||||
Claim subscriptionClaim = UserService.SubscriptionTypeToClaim(client.Id, message.Type);
|
||||
if (statusClaim?.ClaimValue == subscriptionClaim.Value) return;
|
||||
if (statusClaim != null)
|
||||
await userManager.ReplaceClaimAsync(user, statusClaim.ToClaim(), subscriptionClaim);
|
||||
else
|
||||
await userManager.AddClaimAsync(user, subscriptionClaim);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Streetwriters.Common.Messages;
|
||||
using Streetwriters.Common.Models;
|
||||
using Streetwriters.Common;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.Security.Claims;
|
||||
using System.Linq;
|
||||
using Streetwriters.Identity.Services;
|
||||
|
||||
namespace Streetwriters.Identity.MessageHandlers
|
||||
{
|
||||
public class CreateSubscription
|
||||
{
|
||||
public static async Task Process(CreateSubscriptionMessage message, UserManager<User> userManager)
|
||||
{
|
||||
var user = await userManager.FindByIdAsync(message.UserId);
|
||||
var client = Clients.FindClientByAppId(message.AppId);
|
||||
if (client == null || user == null) return;
|
||||
|
||||
IdentityUserClaim<string> statusClaim = user.Claims.FirstOrDefault((c) => c.ClaimType == UserService.GetClaimKey(client.Id));
|
||||
Claim subscriptionClaim = UserService.SubscriptionTypeToClaim(client.Id, message.Type);
|
||||
if (statusClaim?.ClaimValue == subscriptionClaim.Value) return;
|
||||
if (statusClaim != null)
|
||||
await userManager.ReplaceClaimAsync(user, statusClaim.ToClaim(), subscriptionClaim);
|
||||
// we no longer accept legacy subscriptions.
|
||||
// else
|
||||
// await userManager.AddClaimAsync(user, subscriptionClaim);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Streetwriters.Common.Messages;
|
||||
using Streetwriters.Common.Models;
|
||||
using Streetwriters.Common;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.Security.Claims;
|
||||
using System.Linq;
|
||||
using Streetwriters.Identity.Services;
|
||||
|
||||
namespace Streetwriters.Identity.MessageHandlers
|
||||
{
|
||||
public class CreateSubscriptionV2
|
||||
{
|
||||
public static async Task Process(CreateSubscriptionMessageV2 message, UserManager<User> userManager)
|
||||
{
|
||||
var user = await userManager.FindByIdAsync(message.UserId);
|
||||
var client = Clients.FindClientByAppId(message.AppId);
|
||||
if (client == null || user == null) return;
|
||||
|
||||
IdentityUserClaim<string> statusClaim = user.Claims.FirstOrDefault((c) => c.ClaimType == UserService.GetClaimKey(client.Id));
|
||||
Claim subscriptionClaim = UserService.SubscriptionPlanToClaim(client.Id, message.Plan);
|
||||
if (statusClaim?.ClaimValue == subscriptionClaim.Value) return;
|
||||
if (statusClaim != null)
|
||||
await userManager.ReplaceClaimAsync(user, statusClaim.ToClaim(), subscriptionClaim);
|
||||
else
|
||||
await userManager.AddClaimAsync(user, subscriptionClaim);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -169,10 +169,12 @@ namespace Streetwriters.Identity.Services
|
||||
if ((method != MFAMethods.Email && method != MFAMethods.SMS) || !IsValidMFAMethod(method))
|
||||
throw new Exception("Invalid method.");
|
||||
|
||||
var userPlan = UserService.GetUserSubscriptionPlan(client.Id, user);
|
||||
if (isSetup &&
|
||||
method == MFAMethods.SMS &&
|
||||
!UserService.IsUserPremium(client.Id, user))
|
||||
throw new Exception("Due to the high costs of SMS, currently 2FA via SMS is only available for Pro users.");
|
||||
!UserService.IsUserPremium(client.Id, user) &&
|
||||
userPlan != SubscriptionPlan.BELIEVER && userPlan != SubscriptionPlan.PRO)
|
||||
throw new Exception("Due to the high costs of SMS, 2FA via SMS is only available on Pro & Believer plans.");
|
||||
|
||||
// if (!user.EmailConfirmed) throw new Exception("Please confirm your email before activating 2FA by email.");
|
||||
await GetAuthenticatorDetailsAsync(user, client);
|
||||
|
||||
@@ -1,89 +1,127 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Streetwriters.Common.Enums;
|
||||
using Streetwriters.Common.Models;
|
||||
|
||||
namespace Streetwriters.Identity.Services
|
||||
{
|
||||
public class UserService
|
||||
{
|
||||
public static SubscriptionType GetUserSubscriptionStatus(string clientId, User user)
|
||||
{
|
||||
var claimKey = GetClaimKey(clientId);
|
||||
var status = user.Claims.FirstOrDefault((c) => c.ClaimType == claimKey).ClaimValue;
|
||||
switch (status)
|
||||
{
|
||||
case "basic":
|
||||
return SubscriptionType.BASIC;
|
||||
case "trial":
|
||||
return SubscriptionType.TRIAL;
|
||||
case "premium":
|
||||
return SubscriptionType.PREMIUM;
|
||||
case "premium_canceled":
|
||||
return SubscriptionType.PREMIUM_CANCELED;
|
||||
case "premium_expired":
|
||||
return SubscriptionType.PREMIUM_EXPIRED;
|
||||
default:
|
||||
return SubscriptionType.BASIC;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static bool IsUserPremium(string clientId, User user)
|
||||
{
|
||||
var status = GetUserSubscriptionStatus(clientId, user);
|
||||
return status == SubscriptionType.PREMIUM || status == SubscriptionType.PREMIUM_CANCELED;
|
||||
}
|
||||
|
||||
public static Claim SubscriptionTypeToClaim(string clientId, SubscriptionType type)
|
||||
{
|
||||
var claimKey = GetClaimKey(clientId);
|
||||
switch (type)
|
||||
{
|
||||
case SubscriptionType.BASIC:
|
||||
return new Claim(claimKey, "basic");
|
||||
case SubscriptionType.TRIAL:
|
||||
return new Claim(claimKey, "trial");
|
||||
case SubscriptionType.PREMIUM:
|
||||
return new Claim(claimKey, "premium");
|
||||
case SubscriptionType.PREMIUM_CANCELED:
|
||||
return new Claim(claimKey, "premium_canceled");
|
||||
case SubscriptionType.PREMIUM_EXPIRED:
|
||||
return new Claim(claimKey, "premium_expired");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetClaimKey(string clientId)
|
||||
{
|
||||
return $"{clientId}:status";
|
||||
}
|
||||
|
||||
public static async Task<bool> IsUserValidAsync(UserManager<User> userManager, User user, string clientId)
|
||||
{
|
||||
return user != null && await userManager.IsInRoleAsync(user, clientId);
|
||||
}
|
||||
}
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Streetwriters.Common.Enums;
|
||||
using Streetwriters.Common.Models;
|
||||
|
||||
namespace Streetwriters.Identity.Services
|
||||
{
|
||||
public class UserService
|
||||
{
|
||||
public static SubscriptionType GetUserSubscriptionStatus(string clientId, User user)
|
||||
{
|
||||
var claimKey = GetClaimKey(clientId);
|
||||
var status = user.Claims.FirstOrDefault((c) => c.ClaimType == claimKey).ClaimValue;
|
||||
switch (status)
|
||||
{
|
||||
case "basic":
|
||||
return SubscriptionType.BASIC;
|
||||
case "trial":
|
||||
return SubscriptionType.TRIAL;
|
||||
case "premium":
|
||||
return SubscriptionType.PREMIUM;
|
||||
case "premium_canceled":
|
||||
return SubscriptionType.PREMIUM_CANCELED;
|
||||
case "premium_expired":
|
||||
return SubscriptionType.PREMIUM_EXPIRED;
|
||||
default:
|
||||
return SubscriptionType.BASIC;
|
||||
}
|
||||
}
|
||||
|
||||
public static SubscriptionPlan GetUserSubscriptionPlan(string clientId, User user)
|
||||
{
|
||||
var claimKey = GetClaimKey(clientId);
|
||||
var status = user.Claims.FirstOrDefault((c) => c.ClaimType == claimKey).ClaimValue;
|
||||
switch (status)
|
||||
{
|
||||
case "free":
|
||||
return SubscriptionPlan.FREE;
|
||||
case "believer":
|
||||
return SubscriptionPlan.BELIEVER;
|
||||
case "education":
|
||||
return SubscriptionPlan.EDUCATION;
|
||||
case "essential":
|
||||
return SubscriptionPlan.ESSENTIAL;
|
||||
case "pro":
|
||||
return SubscriptionPlan.PRO;
|
||||
default:
|
||||
return SubscriptionPlan.FREE;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsUserPremium(string clientId, User user)
|
||||
{
|
||||
var status = GetUserSubscriptionStatus(clientId, user);
|
||||
return status == SubscriptionType.PREMIUM || status == SubscriptionType.PREMIUM_CANCELED;
|
||||
}
|
||||
|
||||
public static Claim SubscriptionTypeToClaim(string clientId, SubscriptionType type)
|
||||
{
|
||||
var claimKey = GetClaimKey(clientId);
|
||||
switch (type)
|
||||
{
|
||||
case SubscriptionType.BASIC:
|
||||
return new Claim(claimKey, "basic");
|
||||
case SubscriptionType.TRIAL:
|
||||
return new Claim(claimKey, "trial");
|
||||
case SubscriptionType.PREMIUM:
|
||||
return new Claim(claimKey, "premium");
|
||||
case SubscriptionType.PREMIUM_CANCELED:
|
||||
return new Claim(claimKey, "premium_canceled");
|
||||
case SubscriptionType.PREMIUM_EXPIRED:
|
||||
return new Claim(claimKey, "premium_expired");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Claim SubscriptionPlanToClaim(string clientId, SubscriptionPlan plan)
|
||||
{
|
||||
var claimKey = GetClaimKey(clientId);
|
||||
switch (plan)
|
||||
{
|
||||
case SubscriptionPlan.FREE:
|
||||
return new Claim(claimKey, "free");
|
||||
case SubscriptionPlan.BELIEVER:
|
||||
return new Claim(claimKey, "believer");
|
||||
case SubscriptionPlan.EDUCATION:
|
||||
return new Claim(claimKey, "education");
|
||||
case SubscriptionPlan.ESSENTIAL:
|
||||
return new Claim(claimKey, "essential");
|
||||
case SubscriptionPlan.PRO:
|
||||
return new Claim(claimKey, "pro");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetClaimKey(string clientId)
|
||||
{
|
||||
return $"{clientId}:status";
|
||||
}
|
||||
|
||||
public static async Task<bool> IsUserValidAsync(UserManager<User> userManager, User user, string clientId)
|
||||
{
|
||||
return user != null && await userManager.IsInRoleAsync(user, clientId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -244,6 +244,17 @@ namespace Streetwriters.Identity
|
||||
await MessageHandlers.CreateSubscription.Process(message, userManager);
|
||||
}
|
||||
});
|
||||
|
||||
realm.Subscribe(SubscriptionServerTopics.CreateSubscriptionV2Topic, async (CreateSubscriptionMessageV2 message) =>
|
||||
{
|
||||
using (var serviceScope = app.ApplicationServices.CreateScope())
|
||||
{
|
||||
var services = serviceScope.ServiceProvider;
|
||||
var userManager = services.GetRequiredService<UserManager<User>>();
|
||||
await MessageHandlers.CreateSubscriptionV2.Process(message, userManager);
|
||||
}
|
||||
});
|
||||
|
||||
realm.Subscribe(SubscriptionServerTopics.DeleteSubscriptionTopic, async (DeleteSubscriptionMessage message) =>
|
||||
{
|
||||
using (var serviceScope = app.ApplicationServices.CreateScope())
|
||||
|
||||
Reference in New Issue
Block a user