mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 02:43:42 +08:00
0af6c5efdc
This is part 1 of 3, split up of PR #23529. This PR refactors the webauthn code to support passkey authentication/registration. Passkeys aren't used yet, that is coming in PRs 2 and 3. Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
96 lines
2.5 KiB
Ruby
96 lines
2.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseWebauthn
|
|
ACCEPTABLE_REGISTRATION_TYPE = "webauthn.create"
|
|
ACCEPTABLE_AUTHENTICATION_TYPE = "webauthn.get"
|
|
|
|
# -7 - ES256
|
|
# -257 - RS256 (Windows Hello supported alg.)
|
|
SUPPORTED_ALGORITHMS = COSE::Algorithm.registered_algorithm_ids.freeze
|
|
VALID_ATTESTATION_FORMATS = %w[none packed fido-u2f].freeze
|
|
|
|
class SecurityKeyError < StandardError
|
|
end
|
|
|
|
class InvalidOriginError < SecurityKeyError
|
|
end
|
|
class InvalidRelyingPartyIdError < SecurityKeyError
|
|
end
|
|
class UserVerificationError < SecurityKeyError
|
|
end
|
|
class UserPresenceError < SecurityKeyError
|
|
end
|
|
class ChallengeMismatchError < SecurityKeyError
|
|
end
|
|
class InvalidTypeError < SecurityKeyError
|
|
end
|
|
class UnsupportedPublicKeyAlgorithmError < SecurityKeyError
|
|
end
|
|
class UnsupportedAttestationFormatError < SecurityKeyError
|
|
end
|
|
class CredentialIdInUseError < SecurityKeyError
|
|
end
|
|
class MalformedAttestationError < SecurityKeyError
|
|
end
|
|
class KeyNotFoundError < SecurityKeyError
|
|
end
|
|
class MalformedPublicKeyCredentialError < SecurityKeyError
|
|
end
|
|
class OwnershipError < SecurityKeyError
|
|
end
|
|
class PublicKeyError < SecurityKeyError
|
|
end
|
|
class UnknownCOSEAlgorithmError < SecurityKeyError
|
|
end
|
|
|
|
##
|
|
# Usage:
|
|
#
|
|
# These methods should be used in controllers where we
|
|
# are challenging the user that has a security key, and
|
|
# they must respond with a valid webauthn response and
|
|
# credentials.
|
|
def self.stage_challenge(user, secure_session)
|
|
::DiscourseWebauthn::ChallengeGenerator.generate.commit_to_session(secure_session, user)
|
|
end
|
|
|
|
def self.allowed_credentials(user, secure_session)
|
|
return {} if !user.security_keys_enabled?
|
|
credential_ids = user.second_factor_security_key_credential_ids
|
|
{
|
|
allowed_credential_ids: credential_ids,
|
|
challenge: secure_session[self.session_challenge_key(user)],
|
|
}
|
|
end
|
|
|
|
def self.challenge(user, secure_session)
|
|
secure_session[self.session_challenge_key(user)]
|
|
end
|
|
|
|
def self.rp_id
|
|
Rails.env.production? ? Discourse.current_hostname : "localhost"
|
|
end
|
|
|
|
def self.origin
|
|
case Rails.env
|
|
when "development"
|
|
# defaults to the Ember CLI local port
|
|
# you might need to change this and the rp_id above
|
|
# if you are using a non-default port/hostname locally
|
|
"http://localhost:4200"
|
|
when "test"
|
|
"http://localhost:3000"
|
|
else
|
|
Discourse.base_url
|
|
end
|
|
end
|
|
|
|
def self.rp_name
|
|
SiteSetting.title
|
|
end
|
|
|
|
def self.session_challenge_key(user)
|
|
"staged-webauthn-challenge-#{user&.id}"
|
|
end
|
|
end
|