Skip to content

Commit

Permalink
feat: Updates relative to [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilewski committed Jan 26, 2025
1 parent 218ab1d commit dc92cb0
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
clerk-sdk-ruby (4.0.0.beta4)
clerk-http-client (~> 0.0.1)
clerk-http-client (= 2.0.0.beta5)
concurrent-ruby (~> 1.1)
faraday (>= 1.4.1, < 3.0)
jwt (~> 2.5)
Expand Down Expand Up @@ -45,7 +45,7 @@ GEM
bigdecimal (3.1.9)
builder (3.3.0)
byebug (11.1.3)
clerk-http-client (0.0.1)
clerk-http-client (2.0.0.beta5)
faraday (>= 1.0.1, < 3.0)
faraday-multipart
marcel
Expand Down
2 changes: 1 addition & 1 deletion clerk-sdk-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "faraday", ">= 1.4.1", "< 3.0"
spec.add_dependency "jwt", '~> 2.5'
spec.add_dependency "clerk-http-client", "~> 0.0.1"
spec.add_dependency "clerk-http-client", "2.0.0.beta5"
spec.add_dependency "concurrent-ruby", "~> 1.1"

spec.add_development_dependency "byebug", "~> 11.1"
Expand Down
2 changes: 1 addition & 1 deletion lib/clerk/jwks_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fetch(sdk, force_refresh: false, kid_not_found: false)
@lock.with_write_lock do
@last_update = Time.now.to_i
@jwks = begin
sdk.jwks.get.keys.map(&:to_hash)
sdk.jwks.get_jwks.keys.map(&:to_hash)
rescue Clerk::Error, ClerkHttpClient::ApiError
nil
end
Expand Down
4 changes: 2 additions & 2 deletions lib/clerk/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ def user_reverification_rack_response(config = nil)

def fetch_user(user_id)
cached_fetch("clerk:user:#{user_id}") do
sdk.users.find(user_id)
sdk.users.get_user(user_id)
end
end

def fetch_org(org_id)
cached_fetch("clerk:org:#{org_id}") do
sdk.organizations.find(org_id)
sdk.organizations.get_organization(org_id)
end
end

Expand Down
29 changes: 0 additions & 29 deletions lib/clerk/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,5 @@ def verify_token(token, force_refresh_jwks: false, algorithms: ["RS256"], timeou

JWT.decode(token, nil, true, algorithms: algorithms, exp_leeway: timeout, jwks: jwk_loader).first
end

private

# TODO: Temporary solution until generators are improved
HTTP_CLIENT_CUSTOM_MAPPING = {
allowlist: "AllowListBlockList",
blocklist: "AllowListBlockList",
email_sms_templates: "EmailSMSTemplates",
oauth_applications: "OAuthApplications",
jwt_templates: "JWTTemplates",
redirect_urls: "RedirectURLs",
saml_connections: "SAMLConnections"
}

def generate_const_name(method_name)
"#{HTTP_CLIENT_CUSTOM_MAPPING[method_name] || Utils.camel_case(method_name)}Api"
end

def respond_to_missing?(method_name, include_private = false)
ClerkHttpClient.const_get(generate_const_name(method_name)).respond_to?(:new)
rescue NameError
false
end

def method_missing(method_name, *arguments)
ClerkHttpClient.const_get(generate_const_name(method_name)).new
rescue NameError
raise NoMethodError, "undefined method `#{method_name}` for #{self.class.name}"
end
end
end
6 changes: 0 additions & 6 deletions lib/clerk/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
module Clerk
module Utils
class << self
def camel_case(str)
str = str.to_s
return str if str !~ /_/ && str =~ /[A-Z]+.*/
str.split("_").map { |s| s.capitalize }.join
end

def decode_publishable_key(publishable_key)
Base64.decode64(publishable_key.split("_")[2].to_s)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/clerk/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Clerk
VERSION = "4.0.0.beta4"
VERSION = "4.0.0.beta5"
end
20 changes: 10 additions & 10 deletions spec/clerk/jwks_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

before do
allow(sdk).to receive(:jwks).and_return(jwks_api)
allow(jwks_api).to receive(:get).and_return(double(keys: mock_keys))
allow(jwks_api).to receive(:get_jwks).and_return(double(keys: mock_keys))
end

describe "#fetch" do
Expand All @@ -21,43 +21,43 @@

it "returns cached result on subsequent calls within lifetime" do
first_result = cache.fetch(sdk)
expect(jwks_api).to have_received(:get).once
expect(jwks_api).to have_received(:get_jwks).once

second_result = cache.fetch(sdk)
expect(second_result).to eq(first_result)
expect(jwks_api).to have_received(:get).once
expect(jwks_api).to have_received(:get_jwks).once
end

it "refreshes cache when force_refresh is true" do
cache.fetch(sdk)
expect(jwks_api).to have_received(:get).once
expect(jwks_api).to have_received(:get_jwks).once

cache.fetch(sdk, force_refresh: true)
expect(jwks_api).to have_received(:get).twice
expect(jwks_api).to have_received(:get_jwks).twice
end

it "refreshes cache when lifetime has expired" do
cache.fetch(sdk)
expect(jwks_api).to have_received(:get).once
expect(jwks_api).to have_received(:get_jwks).once

allow(Time).to receive(:now).and_return(Time.now + lifetime + 1)

cache.fetch(sdk)
expect(jwks_api).to have_received(:get).twice
expect(jwks_api).to have_received(:get_jwks).twice
end

it "refreshes cache when kid_not_found is true and last update was over 5 minutes ago" do
cache.fetch(sdk)
expect(jwks_api).to have_received(:get).once
expect(jwks_api).to have_received(:get_jwks).once

allow(Time).to receive(:now).and_return(Time.now + 301) # 5 minutes + 1 second

cache.fetch(sdk, kid_not_found: true)
expect(jwks_api).to have_received(:get).twice
expect(jwks_api).to have_received(:get_jwks).twice
end

it "returns nil when API call fails" do
allow(jwks_api).to receive(:get).and_raise(ClerkHttpClient::ApiError)
allow(jwks_api).to receive(:get_jwks).and_raise(ClerkHttpClient::ApiError)

result = cache.fetch(sdk)
expect(result).to be_nil
Expand Down
4 changes: 2 additions & 2 deletions spec/clerk/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
let(:user_object) { double("User") }

before do
allow(sdk_double).to receive_message_chain(:users, :find).with(user_id).and_return(user_object)
allow(sdk_double).to receive_message_chain(:users, :get_user).with(user_id).and_return(user_object)
end

it "returns nil when not authenticated" do
Expand Down Expand Up @@ -80,7 +80,7 @@
let(:org_object) { double("Organization") }

before do
allow(sdk_double).to receive_message_chain(:organizations, :find).with(org_id).and_return(org_object)
allow(sdk_double).to receive_message_chain(:organizations, :get_organization).with(org_id).and_return(org_object)
end

it "returns nil when no organization" do
Expand Down
18 changes: 6 additions & 12 deletions spec/clerk/sdk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@
end
end

describe "SDK helpers" do
describe "SDK helper availability" do
# Litmus test
{
actor_tokens: ClerkHttpClient::ActorTokensApi,
allowlist: ClerkHttpClient::AllowListBlockListApi,
allow_list_block_list: ClerkHttpClient::AllowListBlockListApi,
beta_features: ClerkHttpClient::BetaFeaturesApi,
blocklist: ClerkHttpClient::AllowListBlockListApi,
clients: ClerkHttpClient::ClientsApi,
domains: ClerkHttpClient::DomainsApi,
email_addresses: ClerkHttpClient::EmailAddressesApi,
Expand All @@ -123,7 +123,6 @@
jwt_templates: ClerkHttpClient::JWTTemplatesApi,
miscellaneous: ClerkHttpClient::MiscellaneousApi,
oauth_applications: ClerkHttpClient::OAuthApplicationsApi,
organization_domain: ClerkHttpClient::OrganizationDomainApi,
organization_domains: ClerkHttpClient::OrganizationDomainsApi,
organization_invitations: ClerkHttpClient::OrganizationInvitationsApi,
organization_memberships: ClerkHttpClient::OrganizationMembershipsApi,
Expand All @@ -137,19 +136,14 @@
sign_ups: ClerkHttpClient::SignUpsApi,
testing_tokens: ClerkHttpClient::TestingTokensApi,
users: ClerkHttpClient::UsersApi,
waitlist_entries: ClerkHttpClient::WaitlistEntriesApi,
webhooks: ClerkHttpClient::WebhooksApi
}.each do |method, instance_class|
it "##{method} returns an instance of #{instance_class}" do
expect(clerk_client.send(:respond_to?, method)).to be true
expect(clerk_client.send(method)).to be_an_instance_of(instance_class)
end
end

it "should appropriately report on methods that are not implemented" do
expect(clerk_client.respond_to?(:foo)).to be false
expect {
clerk_client.foo
}.to raise_error(NoMethodError, "undefined method `foo` for #{clerk_client.class.name}")
end
end
end


0 comments on commit dc92cb0

Please sign in to comment.