Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add helpers for re-verification #72

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/clerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "clerk/version"
require_relative "clerk/sdk"
require_relative "clerk/constants"

module Clerk
class << self
Expand Down
10 changes: 10 additions & 0 deletions lib/clerk/authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def clerk_user_signed_in?
!!clerk_verified_session_claims
end

def clerk_session_needs_reverification?(params=StepUp::PRESETS[:strict])
!request.env['clerk'].is_user_reverified?(params)
end

def clerk_render_reverification(missing_config=nil)
payload = request.env['clerk'].reverification_mismatch_payload(missing_config)

render status: 403, json: payload
end

def clerk_sign_in_url
ENV.fetch("CLERK_SIGN_IN_URL")
end
Expand Down
10 changes: 10 additions & 0 deletions lib/clerk/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Clerk
module StepUp
PRESETS = {
very_strict: { after_minutes: 10, level: :multi_factor },
strict: { after_minutes: 10, level: :second_factor },
moderate: { after_minutes: 60, level: :second_factor },
lax: { after_minutes: 1440, level: :second_factor }
}
end
end
45 changes: 45 additions & 0 deletions lib/clerk/rack_middleware_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ def org_permissions
@session_claims["org_permissions"]
end

# Returns true if the session needs to perform step up verification
def is_user_reverified?(params)
return false if session_claims.nil?

fva = session_claims["fva"]
level = params[:level]
after_minutes = Integer(params[:after_minutes])

# the feature is disabled
return true if fva.nil?

return false if after_minutes.nil? || level.nil?

factor1_age, factor2_age = fva
is_valid_factor1 = factor1_age != -1 && after_minutes > factor1_age
is_valid_factor2 = factor2_age != -1 && after_minutes > factor2_age

case level
when :first_factor
is_valid_factor1
when :second_factor
factor2_age == -1 ? is_valid_factor1 : is_valid_factor2
when :multi_factor
factor2_age == -1 ? is_valid_factor1 : is_valid_factor1 && is_valid_factor2
end
end

def reverification_mismatch_payload(missing_config)
{
clerk_error: {
type: "forbidden",
reason: "reverification-mismatch",
metadata: { reverification: missing_config, }
}
}
end

def reverification_response(missing_config=nil)
[
403,
{ "Content-Type" => "application/json" },
[reverification_mismatch_payload(missing_config).to_json],
]
end

private

def fetch_user(user_id)
Expand Down
Loading