Merge pull request #1789 from SkynetLabs/include-subscription-info

include subscription info in internal accounts endpoint
This commit is contained in:
Karol Wypchło 2022-03-08 11:39:46 +01:00 committed by GitHub
commit 04f9970ae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 21 deletions

View File

@ -13,6 +13,3 @@ set $skylink "";
# cached account limits (json string) - applies only if accounts are enabled # cached account limits (json string) - applies only if accounts are enabled
set $account_limits ""; set $account_limits "";
# set this internal flag to true if current request should not be limited in any way
set $internal_no_limits "false";

View File

@ -402,14 +402,20 @@ location /__internal/do/not/use/accounts {
content_by_lua_block { content_by_lua_block {
local json = require('cjson') local json = require('cjson')
local accounts_enabled = require("skynet.account").accounts_enabled() local skynet_account = require("skynet.account")
local is_auth_required = require("skynet.account").is_auth_required()
local is_authenticated = accounts_enabled and require("skynet.account").is_authenticated() local accounts_enabled = skynet_account.accounts_enabled()
local is_auth_required = skynet_account.is_auth_required()
local is_subscription_required = skynet_account.is_subscription_required()
local is_authenticated = skynet_account.is_authenticated()
local has_subscription = skynet_account.has_subscription()
ngx.say(json.encode{ ngx.say(json.encode{
enabled = accounts_enabled, enabled = accounts_enabled,
auth_required = is_auth_required, auth_required = is_auth_required,
subscription_required = is_subscription_required,
authenticated = is_authenticated, authenticated = is_authenticated,
subscription = has_subscription,
}) })
return ngx.exit(ngx.HTTP_OK) return ngx.exit(ngx.HTTP_OK)
} }

View File

@ -1,13 +1,18 @@
local _M = {} local _M = {}
-- constant tier ids
local tier_id_anonymous = 0
local tier_id_free = 1
-- fallback - remember to keep those updated -- fallback - remember to keep those updated
local anon_limits = { ["tierName"] = "anonymous", ["upload"] = 655360, ["download"] = 655360, ["maxUploadSize"] = 1073741824, ["registry"] = 250 } local anon_limits = {
["tierID"] = tier_id_anonymous,
-- no limits applied ["tierName"] = "anonymous",
local no_limits = { ["tierName"] = "internal", ["upload"] = 0, ["download"] = 0, ["maxUploadSize"] = 0, ["registry"] = 0 } ["upload"] = 655360,
["download"] = 655360,
-- free tier name ["maxUploadSize"] = 1073741824,
local free_tier = "free" ["registry"] = 250
}
-- handle request exit when access to portal should be restricted to authenticated users only -- handle request exit when access to portal should be restricted to authenticated users only
function _M.exit_access_unauthorized(message) function _M.exit_access_unauthorized(message)
@ -32,10 +37,6 @@ end
function _M.get_account_limits() function _M.get_account_limits()
local cjson = require('cjson') local cjson = require('cjson')
if ngx.var.internal_no_limits == "true" then
return no_limits
end
if ngx.var.skynet_jwt == "" then if ngx.var.skynet_jwt == "" then
return anon_limits return anon_limits
end end
@ -62,16 +63,18 @@ end
-- detect whether current user is authenticated -- detect whether current user is authenticated
function _M.is_authenticated() function _M.is_authenticated()
if not _M.accounts_enabled() then return false end
local limits = _M.get_account_limits() local limits = _M.get_account_limits()
return limits.tierName ~= anon_limits.tierName return limits.tierID > tier_id_anonymous
end end
-- detect whether current user has active subscription -- detect whether current user has active subscription
function _M.is_subscription_account() function _M.has_subscription()
local limits = _M.get_account_limits() local limits = _M.get_account_limits()
return limits.tierName ~= anon_limits.tierName and limits.tierName ~= free_tier return limits.tierID > tier_id_free
end end
function _M.is_auth_required() function _M.is_auth_required()
@ -101,7 +104,7 @@ function _M.is_access_forbidden()
if is_access_always_allowed() then return false end if is_access_always_allowed() then return false end
-- check if active subscription is required and request is from user without it -- check if active subscription is required and request is from user without it
return _M.is_subscription_required() and not _M.is_subscription_account() return _M.is_subscription_required() and not _M.has_subscription()
end end
return _M return _M