Skip to content

Commit

Permalink
Add query to fetch license keys for enterprise accounts
Browse files Browse the repository at this point in the history
This will allow the license renewal process to be more self-service
  • Loading branch information
michaeljguarino committed Jan 30, 2025
1 parent 33a4a62 commit d5f60e5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions apps/core/lib/core/services/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,20 @@ defmodule Core.Services.Accounts do
do: allow(service_account, user, :impersonate)


@doc """
Grabs the license key for a users account
"""
@spec license_key(User.t) :: {:ok, binary} | {:error, term}
def license_key(%User{} = user) do
with {:ent, true} <- {:ent, Payments.enterprise?(user)},
{:ok, _} <- Payments.allow_billing(user) do
license_key()
else
{:ent, _} -> {:error, "only enterprise accounts can download license keys"}
err -> err
end
end

def license_key() do
exp = Timex.now() |> Timex.shift(days: 365) |> Timex.to_unix()
with {:ok, claims} <- Jwt.generate_claims(%{"enterprise" => true, "exp" => exp}),
Expand Down
5 changes: 5 additions & 0 deletions apps/core/lib/core/services/payments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ defmodule Core.Services.Payments do
def allow(%Subscription{} = subscription, %User{} = user),
do: allow(subscription, user, :access)

def allow_billing(%User{} = user) do
%{account: account} = Repo.preload(user, [:account])
allow(account, user, :pay)
end

@doc """
List all invoices against a subscription
"""
Expand Down
27 changes: 27 additions & 0 deletions apps/core/test/services/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,31 @@ defmodule Core.Services.AccountsTest do
assert updated.usage_updated
end
end

describe "#license_key/1" do
test "enterprise billing users can download license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, roles: %{admin: true}, account: account)
enterprise_plan(account)

{:ok, key} = Accounts.license_key(user)

assert is_binary(key)
end

test "non billing users cannot downlod license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, account: account)
enterprise_plan(account)

{:error, _} = Accounts.license_key(user)
end

test "non enterprise accounts cannot downlod license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, roles: %{admin: true}, account: account)

{:error, _} = Accounts.license_key(user)
end
end
end
3 changes: 3 additions & 0 deletions apps/graphql/lib/graphql/resolvers/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ defmodule GraphQl.Resolvers.Account do
def resolve_invite(%{id: secure_id}, _),
do: {:ok, Accounts.get_invite(secure_id)}

def license_key(_, %{context: %{current_user: user}}),
do: Accounts.license_key(user)

def create_service_account(%{attributes: attrs}, %{context: %{current_user: user}}),
do: Accounts.create_service_account(attrs, user)

Expand Down
6 changes: 6 additions & 0 deletions apps/graphql/lib/graphql/schema/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ defmodule GraphQl.Schema.Account do

resolve &Account.list_oauth_integrations/2
end

field :license_key, :string do
middleware Authenticated

resolve &Account.license_key/2
end
end

object :account_mutations do
Expand Down
16 changes: 16 additions & 0 deletions apps/graphql/test/queries/account_queries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,20 @@ defmodule GraphQl.AccountQueriesTest do
|> ids_equal(invites)
end
end

describe "licenseKey" do
test "enterprise billing users can download license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, roles: %{admin: true}, account: account)
enterprise_plan(account)

{:ok, %{data: %{"licenseKey" => k}}} = run_query("""
query {
licenseKey
}
""", %{}, %{current_user: user})

assert is_binary(k)
end
end
end

0 comments on commit d5f60e5

Please sign in to comment.