Skip to content

Commit

Permalink
chore: Add autocorrect force all offences
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkl committed Feb 26, 2024
1 parent 04b8402 commit d2650c6
Show file tree
Hide file tree
Showing 32 changed files with 441 additions and 364 deletions.
2 changes: 2 additions & 0 deletions lib/clerk/authenticatable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "active_support/concern"

module Clerk
Expand Down
6 changes: 4 additions & 2 deletions lib/clerk/authenticate_context.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "ostruct"
require "forwardable"
require "base64"
Expand Down Expand Up @@ -116,15 +118,15 @@ def document_request?
end

def accepts_html?
@headers.accept && @headers.accept.start_with?("text/html")
@headers.accept.to_s.start_with?("text/html")
end

def eligible_for_multi_domain?
is_satellite? && document_request? && !clerk_synced?
end

def active_client?
@cookies.client_uat.to_i > 0
@cookies.client_uat.to_i.positive?
end

def cross_origin_request?
Expand Down
4 changes: 3 additions & 1 deletion lib/clerk/authenticate_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Clerk
##
# This class represents a service object used to determine the current request state
Expand Down Expand Up @@ -215,7 +217,7 @@ def verify_token(token, **opts)
sdk.verify_token(token, **opts)
rescue JWT::ExpiredSignature, JWT::InvalidIatError => e
raise e
rescue JWT::DecodeError, JWT::RequiredDependencyError => e
rescue JWT::DecodeError, JWT::RequiredDependencyError
false
end
end
Expand Down
62 changes: 32 additions & 30 deletions lib/clerk/constants.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
# frozen_string_literal: true

module Clerk
SESSION_COOKIE = "__session".freeze
CLIENT_UAT_COOKIE = "__client_uat".freeze
SESSION_COOKIE = "__session"
CLIENT_UAT_COOKIE = "__client_uat"

# Dev Browser
DEV_BROWSER_COOKIE = "__clerk_db_jwt".freeze
DEV_BROWSER_COOKIE = "__clerk_db_jwt"

# Handshake
HANDSHAKE_COOKIE = "__clerk_handshake".freeze
HANDSHAKE_HELP_QUERY_PARAM = "__clerk_help".freeze
HANDSHAKE_COOKIE_DIRECTIVES_KEY = "handshake".freeze
HANDSHAKE_COOKIE = "__clerk_handshake"
HANDSHAKE_HELP_QUERY_PARAM = "__clerk_help"
HANDSHAKE_COOKIE_DIRECTIVES_KEY = "handshake"

# auth debug response headers
AUTH_STATUS_HEADER = "X-Clerk-Auth-Status".freeze
AUTH_REASON_HEADER = "X-Clerk-Auth-Reason".freeze
AUTH_MESSAGE_HEADER = "X-Clerk-Auth-Message".freeze
AUTH_STATUS_HEADER = "X-Clerk-Auth-Status"
AUTH_REASON_HEADER = "X-Clerk-Auth-Reason"
AUTH_MESSAGE_HEADER = "X-Clerk-Auth-Message"

CONTENT_TYPE_HEADER = "Content-Type".freeze
SEC_FETCH_DEST_HEADER = "HTTP_SEC_FETCH_DEST".freeze
CONTENT_TYPE_HEADER = "Content-Type"
SEC_FETCH_DEST_HEADER = "HTTP_SEC_FETCH_DEST"

# headers used in response - should be lowered case and without http prefix
LOCATION_HEADER = "Location".freeze
COOKIE_HEADER = "Set-Cookie".freeze
LOCATION_HEADER = "Location"
COOKIE_HEADER = "Set-Cookie"

# clerk url related headers
AUTHORIZATION_HEADER = "HTTP_AUTHORIZATION".freeze
ACCEPT_HEADER = "HTTP_ACCEPT".freeze
USER_AGENT_HEADER = "HTTP_USER_AGENT".freeze
ORIGIN_HEADER = "HTTP_ORIGIN".freeze
AUTHORIZATION_HEADER = "HTTP_AUTHORIZATION"
ACCEPT_HEADER = "HTTP_ACCEPT"
USER_AGENT_HEADER = "HTTP_USER_AGENT"
ORIGIN_HEADER = "HTTP_ORIGIN"

module TokenVerificationErrorReason
TOKEN_INVALID = "token-invalid".freeze
TOKEN_EXPIRED = "token-expired".freeze
TOKEN_NOT_ACTIVE_YET = "token-not-active-yet".freeze
JWK_FAILED_TO_RESOLVE = "jwk-failed-to-resolve".freeze
TOKEN_INVALID = "token-invalid"
TOKEN_EXPIRED = "token-expired"
TOKEN_NOT_ACTIVE_YET = "token-not-active-yet"
JWK_FAILED_TO_RESOLVE = "jwk-failed-to-resolve"
end

module AuthErrorReason
CLIENT_UAT_WITHOUT_SESSION_TOKEN = "client-uat-but-no-session-token".freeze
DEV_BROWSER_SYNC = "dev-browser-sync".freeze
PRIMARY_RESPONDS_TO_SYNCING = "primary-responds-to-syncing".freeze
SATELLITE_COOKIE_NEEDS_SYNCING = "satellite-needs-syncing".freeze
SESSION_TOKEN_AND_UAT_MISSING = "session-token-and-uat-missing".freeze
SESSION_TOKEN_MISSING = "session-token-missing".freeze
SESSION_TOKEN_OUTDATED = "session-token-outdated".freeze
SESSION_TOKEN_WITHOUT_CLIENT_UAT = "session-token-but-no-client-uat".freeze
UNEXPECTED_ERROR = "unexpected-error".freeze
CLIENT_UAT_WITHOUT_SESSION_TOKEN = "client-uat-but-no-session-token"
DEV_BROWSER_SYNC = "dev-browser-sync"
PRIMARY_RESPONDS_TO_SYNCING = "primary-responds-to-syncing"
SATELLITE_COOKIE_NEEDS_SYNCING = "satellite-needs-syncing"
SESSION_TOKEN_AND_UAT_MISSING = "session-token-and-uat-missing"
SESSION_TOKEN_MISSING = "session-token-missing"
SESSION_TOKEN_OUTDATED = "session-token-outdated"
SESSION_TOKEN_WITHOUT_CLIENT_UAT = "session-token-but-no-client-uat"
UNEXPECTED_ERROR = "unexpected-error"
end
end
2 changes: 2 additions & 0 deletions lib/clerk/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Clerk
module Errors
class Base < StandardError
Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/jwks_cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class JWKSCache
def initialize(lifetime)
@lifetime = lifetime
Expand Down
8 changes: 5 additions & 3 deletions lib/clerk/rack_middleware.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "sdk"

module Clerk
Expand All @@ -7,7 +9,7 @@ def initialize(app)
end

def call(env)
req = Rack::Request.new(env)
Rack::Request.new(env)
env["clerk"] = Proxy.new(env)
@app.call(env)
end
Expand Down Expand Up @@ -38,7 +40,7 @@ def session
end

def user_id
@user_id ||= session.dig("user_id")
@user_id ||= session["user_id"]
end

def user
Expand Down Expand Up @@ -86,7 +88,7 @@ def fetch_user(user_id)
end

def cached_fetch(key, &block)
if store = Clerk.configuration.middleware_cache_store
if (store = Clerk.configuration.middleware_cache_store)
store.fetch(key, expires_in: cache_ttl, &block)
else
yield
Expand Down
4 changes: 3 additions & 1 deletion lib/clerk/rack_middleware_v2.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "clerk"
require_relative "authenticate_context"
require_relative "authenticate_request"
Expand Down Expand Up @@ -77,7 +79,7 @@ def fetch_org(org_id)
end

def cached_fetch(key, &block)
if store = Clerk.configuration.middleware_cache_store
if (store = Clerk.configuration.middleware_cache_store)
store.fetch(key, expires_in: CACHE_TTL, &block)
else
yield
Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "resources/allowlist_identifiers"
require_relative "resources/allowlist"
require_relative "resources/clients"
Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/allowlist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "singular_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/allowlist_identifiers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/clients.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/email_addresses.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/emails.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/jwks.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/organizations.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/phone_numbers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/plural_resource.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Clerk
module Resources
class PluralResource
Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/sessions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/singular_resource.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Clerk
module Resources
class SingularResource
Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/resources/users.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require_relative "plural_resource"

Expand Down
3 changes: 1 addition & 2 deletions lib/clerk/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
require_relative "resources/phone_numbers"
require_relative "resources/sessions"
require_relative "resources/users"
require_relative "resources/users"
require_relative "resources/jwks"
require_relative "errors"
require_relative "jwks_cache"
Expand All @@ -26,7 +25,7 @@ class SDK
DEFAULT_HEADERS = {
"User-Agent" => "Clerk/#{Clerk::VERSION}; Faraday/#{Faraday::VERSION}; Ruby/#{RUBY_VERSION}",
"X-Clerk-SDK" => "ruby/#{Clerk::VERSION}"
}
}.freeze

# How often (in seconds) should JWKs be refreshed
JWKS_CACHE_LIFETIME = 3600 # 1 hour
Expand Down
2 changes: 2 additions & 0 deletions lib/clerk/utils.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Clerk
module Utils
module_function
Expand Down
44 changes: 24 additions & 20 deletions test/resources/allowlist_identifiers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@

require "test_helper"

class Clerk::Resources::AllowlistIdentifierTest < Minitest::Test
def mock_sdk
faraday = Faraday.new do |faraday|
faraday.adapter :test do |stub|
stub.get("/allowlist_identifiers") { json_ok("all_allowlist_identifiers") }
stub.post("/allowlist_identifiers") { json_ok("allowlist_identifier_1") }
stub.delete("/allowlist_identifiers/alid_1") { json_ok("allowlist_identifier_1_deleted") }
end
end
module Clerk
module Resources
class AllowlistIdentifierTest < Minitest::Test
def mock_sdk
faraday = Faraday.new do |faraday|
faraday.adapter :test do |stub|
stub.get("/allowlist_identifiers") { json_ok("all_allowlist_identifiers") }
stub.post("/allowlist_identifiers") { json_ok("allowlist_identifier_1") }
stub.delete("/allowlist_identifiers/alid_1") { json_ok("allowlist_identifier_1_deleted") }
end
end

::Clerk::SDK.new(connection: faraday)
end
::Clerk::SDK.new(connection: faraday)
end

def test_all_allowlist_identifiers
allowlist_identifiers = mock_sdk.allowlist_identifiers.all
assert_equal(["allowlist_identifier"], allowlist_identifiers.map { |h| h.dig("object") })
end
def test_all_allowlist_identifiers
allowlist_identifiers = mock_sdk.allowlist_identifiers.all
assert_equal(["allowlist_identifier"], allowlist_identifiers.map { |h| h["object"] })
end

def test_delete_allowlist_identifier
user = mock_sdk.allowlist_identifiers.delete("alid_1")
assert_equal true, user["deleted"]
assert_equal "allowlist_identifier", user["object"]
assert_equal "alid_1", user["id"]
def test_delete_allowlist_identifier
user = mock_sdk.allowlist_identifiers.delete("alid_1")
assert_equal true, user["deleted"]
assert_equal "allowlist_identifier", user["object"]
assert_equal "alid_1", user["id"]
end
end
end
end
28 changes: 16 additions & 12 deletions test/resources/allowlist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

require "test_helper"

class Clerk::Resources::AllowlistTest < Minitest::Test
def mock_sdk
faraday = Faraday.new do |faraday|
faraday.adapter :test do |stub|
stub.patch("/beta_features/allowlist") { json_ok("allowlist_toggle") }
end
end
module Clerk
module Resources
class AllowlistTest < Minitest::Test
def mock_sdk
faraday = Faraday.new do |faraday|
faraday.adapter :test do |stub|
stub.patch("/beta_features/allowlist") { json_ok("allowlist_toggle") }
end
end

::Clerk::SDK.new(connection: faraday)
end
::Clerk::SDK.new(connection: faraday)
end

def test_toggle_allowlist
resp = mock_sdk.allowlist.update({ restricted_to_allowlist: true })
assert_equal true, resp["restricted_to_allowlist"]
def test_toggle_allowlist
resp = mock_sdk.allowlist.update({ restricted_to_allowlist: true })
assert_equal true, resp["restricted_to_allowlist"]
end
end
end
end
Loading

0 comments on commit d2650c6

Please sign in to comment.