-
-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guard against OIDC Provider configuration being stale (#5296)
Fixes TOB-RGM-14
- Loading branch information
Showing
7 changed files
with
87 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module JwtValidation | ||
extend ActiveSupport::Concern | ||
|
||
class UnsupportedIssuer < StandardError; end | ||
class UnverifiedJWT < StandardError; end | ||
class InvalidJWT < StandardError; end | ||
|
||
included do | ||
rescue_from InvalidJWT, with: :render_bad_request | ||
|
||
rescue_from( | ||
UnsupportedIssuer, UnverifiedJWT, | ||
JSON::JWT::VerificationFailed, JSON::JWK::Set::KidNotFound, | ||
OIDC::AccessPolicy::AccessError, | ||
with: :render_not_found | ||
) | ||
end | ||
|
||
def jwt_key_or_secret | ||
raise NotImplementedError | ||
end | ||
|
||
def decode_jwt | ||
@jwt = JSON::JWT.decode_compact_serialized(params.permit(:jwt).require(:jwt), jwt_key_or_secret) | ||
rescue JSON::JWT::InvalidFormat, JSON::ParserError, ArgumentError => e | ||
# invalid base64 raises ArgumentError | ||
render_bad_request(e) | ||
end | ||
|
||
def validate_jwt_format | ||
%w[nbf iat exp].each do |claim| | ||
raise InvalidJWT, "Missing/invalid #{claim}" unless @jwt[claim].is_a?(Integer) | ||
end | ||
%w[iss jti].each do |claim| | ||
raise InvalidJWT, "Missing/invalid #{claim}" unless @jwt[claim].is_a?(String) | ||
end | ||
end | ||
|
||
def validate_provider | ||
raise UnsupportedIssuer, "Provider is missing jwks" if @provider.jwks.blank? | ||
# TODO: delete &. after all providers have updated their configuration | ||
return unless @provider.configuration_updated_at&.before?(1.day.ago) | ||
raise UnsupportedIssuer, "Configuration last updated too long ago: #{@provider.configuration_updated_at}" | ||
end | ||
|
||
def verify_jwt_time | ||
now = Time.now.to_i | ||
return if @jwt["nbf"] <= now && now < @jwt["exp"] | ||
raise UnverifiedJWT, "Invalid time" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20240910152643_add_configuration_updated_at_to_oidc_providers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddConfigurationUpdatedAtToOIDCProviders < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :oidc_providers, :configuration_updated_at, :timestamp | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters