Created
January 9, 2022 23:16
-
-
Save truongnmt/14f444f67329eef80607f2d19b2ab0ad to your computer and use it in GitHub Desktop.
callback_sessions_controller.rb
This file contains hidden or 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
def callback | |
webauthn_credential = WebAuthn::Credential.from_get(params) | |
user = User.find_by(username: session["current_authentication"]["username"]) | |
raise "user #{session["current_authentication"]["username"]} never initiated sign up" unless user | |
credential = user.credentials.find_by(external_id: Base64.strict_encode64(webauthn_credential.raw_id)) | |
begin | |
webauthn_credential.verify( | |
session["current_authentication"]["challenge"], | |
public_key: credential.public_key, | |
sign_count: credential.sign_count | |
) | |
credential.update!(sign_count: webauthn_credential.sign_count) | |
sign_in(user) | |
render json: { status: "ok" }, status: :ok | |
rescue WebAuthn::Error => e | |
render json: "Verification failed: #{e.message}", status: :unprocessable_entity | |
ensure | |
session.delete("current_authentication") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment