Skip to content

Instantly share code, notes, and snippets.

View truongnmt's full-sized avatar
🎯
Rails, Auth, Blockchain, Deep Learning

Truong Nguyen truongnmt

🎯
Rails, Auth, Blockchain, Deep Learning
View GitHub Profile
@truongnmt
truongnmt / credential.js
Created January 9, 2022 10:01
create_credential.js
function create(callbackUrl, credentialOptions) {
WebAuthnJSON.create({ "publicKey": credentialOptions }).then(function(credential) {
callback(callbackUrl, credential);
}).catch(function(error) {
showMessage(error);
});
console.log("Creating new public key credential...");
}
@truongnmt
truongnmt / new_registration_controller.js
Last active January 9, 2022 10:00
new_registration_controller.js
create(event) {
var [data, status, xhr] = event.detail;
console.log(data);
var credentialOptions = data;
// Registration
if (credentialOptions["user"]) {
var credential_nickname = event.target.querySelector("input[name='registration[nickname]']").value;
var callback_url = `/registration/callback?credential_nickname=${credential_nickname}`
@truongnmt
truongnmt / create_options.json
Created January 9, 2022 08:58
create_options.json
{
"challenge": "Wj4vWoL1BeG0PB8iJTKMfvLH8rBt3CA6PfM4QBIyKmU",
"timeout": 120000,
"rp": { "name": "WebAuthn Rails Demo App" },
"user": {
"name": "truongnmt",
"id": "OXtCbb6VcBaLFC5GBOjIfpKYCMxJom04VPw9c-WRvhJ5uAHWhBkQZk_-0NGOmNnK4Yx2G5Pw9nyLLRKZkkCndA",
"displayName": "truong"
},
"pubKeyCredParams": [
@truongnmt
truongnmt / registrations_controller.rb
Created January 9, 2022 08:08
registrations_controller.rb
class RegistrationsController < ApplicationController
def create
user = User.new(username: params[:registration][:username])
create_options = WebAuthn::Credential.options_for_create(
user: {
name: params[:registration][:username],
id: user.webauthn_id
}
)
@truongnmt
truongnmt / user.rb
Created January 4, 2022 23:29
app/models/user.rb
class User < ApplicationRecord
devise :database_authenticatable, :recoverable, :rememberable
def authenticatable_salt
return super unless session_token
"#{super}#{session_token}"
end
@truongnmt
truongnmt / serialize_from_session.js
Created January 4, 2022 11:30
serialize_from_session
def serialize_from_session(key, salt)
record = to_adapter.get(key)
record if record && record.authenticatable_salt == salt
end
@truongnmt
truongnmt / authenticatable_salt.rb
Created January 3, 2022 00:12
authenticatable_salt
def authenticatable_salt
encrypted_password[0,29] if encrypted_password
end
@truongnmt
truongnmt / serialize_into_session.rb
Created January 3, 2022 00:09
serialize_into_session
def serialize_into_session(record)
[record.to_key, record.authenticatable_salt]
end
# @param {Character[][]} board
# @param {Integer[]} click
# @return {Character[][]}
# def update_board(board, click)
# y = click[0]
# x = click[1]
# total_x = board[0].length
# total_y = board.length
# board[y][x] = 'X' if board[y][x] == 'M'
# @param {String} s
# @param {String} t
# @return {Boolean}
# def is_subsequence(s, t)
# i = 0
# t.each_char do |c|
# i += 1 if c == s[i]
# end
# i == s.length
# end