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 / golang-singleton-new-4.go
Last active July 20, 2022 23:50
golang-singleton-new-4.go
var atomicinz uint64
// type global
type singleton map[string]string
var (
instance singleton
)
func NewClass() singleton {
@truongnmt
truongnmt / sessions_controller.rb
Created January 9, 2022 23:16
callback_sessions_controller.rb
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"],
@truongnmt
truongnmt / AuthenticatorAssertionResponse.json
Created January 9, 2022 22:53
AuthenticatorAssertionResponse.json
{
"clientExtensionResults": {},
"id": "Abn91boMsByBbqE9lxpBfGEJWMq0GM6_LZ6ToIIehjzxWO-F1li2KvPmKRy-J_CkG7MJWadYzmcSahO0CcNyN2SjWnTaMalxZlP1xlFKEOiG5TVO_u5ANQBGxhOuISTpBJfJF5vsutAknQhDew",
"rawId": "Abn91boMsByBbqE9lxpBfGEJWMq0GM6_LZ6ToIIehjzxWO-F1li2KvPmKRy-J_CkG7MJWadYzmcSahO0CcNyN2SjWnTaMalxZlP1xlFKEOiG5TVO_u5ANQBGxhOuISTpBJfJF5vsutAknQhDew",
"response": {
"authenticatorData": "SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2MFYPEyFg",
"clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiYkpwMzBIMUZYNEVTY3h4YndleUhUUTJ4aG9zZ2xYSTZsQlJpNWoyWGZuVSIsIm9yaWdpbiI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDQ0NyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
"signature": "MEUCIQCtTUYcAAz5KFmBodTMpt8bMAUWkzOCzxIf-NghNCaLRwIgJ-2o41-AhWl1ZERB2EDEFQ2vEgEx8WrZR5Ad-5mvld8",
"userHandle": "OXtCbb6VcBaLFC5GBOjIfpKYCMxJom04VPw9c-WRvhJ5uAHWhBkQZk_-0NGOmNnK4Yx2G5Pw9nyLLRKZkkCndA"
},
@truongnmt
truongnmt / credential.js
Created January 9, 2022 22:41
credential.js
function get(credentialOptions) {
WebAuthnJSON.get({ "publicKey": credentialOptions }).then(function(credential) {
callback("/session/callback", credential);
}).catch(function(error) {
showMessage(error);
});
console.log("Getting public key credential...");
}
@truongnmt
truongnmt / new_session_controller.js
Created January 9, 2022 22:40
new_session_controller.js
create(event) {
var [data, status, xhr] = event.detail;
console.log(data);
var credentialOptions = data;
Credential.get(credentialOptions);
}
@truongnmt
truongnmt / get_options.json
Created January 9, 2022 22:27
get_options.json
{
"challenge": "pW3wrtt7b65n5ehxWbVpb8vGml9EGn9yXXTcuBzXI7E",
"timeout": 120000,
"allowCredentials": [
{
"type": "public-key",
"id": "Abn91boMsByBbqE9lxpBfGEJWMq0GM6/LZ6ToIIehjzxWO+F1li2KvPmKRy+J/CkG7MJWadYzmcSahO0CcNyN2SjWnTaMalxZlP1xlFKEOiG5TVO/u5ANQBGxhOuISTpBJfJF5vsutAknQhDew=="
}
]
}
@truongnmt
truongnmt / sessions_controller.rb
Created January 9, 2022 22:25
sessions_controller.rb
class SessionsController < ApplicationController
def create
user = User.find_by(username: session_params[:username])
if user
get_options = WebAuthn::Credential.options_for_get(allow: user.credentials.pluck(:external_id))
session[:current_authentication] = { challenge: get_options.challenge, username: session_params[:username] }
respond_to do |format|
@truongnmt
truongnmt / credentials_controller.rb
Created January 9, 2022 22:14
credentials_controller.rb
class CredentialsController < ApplicationController
def create
create_options = WebAuthn::Credential.options_for_create(
user: {
id: current_user.webauthn_id,
name: current_user.username,
},
exclude: current_user.credentials.pluck(:external_id)
)
@truongnmt
truongnmt / registrations_controller.rb
Created January 9, 2022 21:57
registrations_controller.rb
def callback
webauthn_credential = WebAuthn::Credential.from_create(params)
user = User.create!(session["current_registration"]["user_attributes"])
begin
webauthn_credential.verify(session["current_registration"]["challenge"])
credential = user.credentials.build(
external_id: Base64.strict_encode64(webauthn_credential.raw_id),
@truongnmt
truongnmt / AuthenticatorAttestationResponse.json
Last active January 9, 2022 22:56
AuthenticatorAttestationResponse.json
{
"type": "public-key",
"id": "Abn91boMsByBbqE9lxpBfGEJWMq0GM6_LZ6ToIIehjzxWO-F1li2KvPmKRy-J_CkG7MJWadYzmcSahO0CcNyN2SjWnTaMalxZlP1xlFKEOiG5TVO_u5ANQBGxhOuISTpBJfJF5vsutAknQhDew",
"rawId": "Abn91boMsByBbqE9lxpBfGEJWMq0GM6_LZ6ToIIehjzxWO-F1li2KvPmKRy-J_CkG7MJWadYzmcSahO0CcNyN2SjWnTaMalxZlP1xlFKEOiG5TVO_u5ANQBGxhOuISTpBJfJF5vsutAknQhDew",
"response": {
"clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoia0liNDc2Y2liQ2NWWDg3NU9PaXhjQ0g1N2JFRkRNSkY1WFhpekVaTnFyOCIsIm9yaWdpbiI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDQ0NyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
"attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YVjrSZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2NFYO_muK3OAAI1vMYKZIsLJfHwVQMAZwFdfvtoAi1yxff-Wxlg8GZ-hT_zqgFnymO-JnJGrVjIDeuaOTlvKtpPV1W2JfJAKj7aRKAZTNKKShI8A6mEQQyGGwJdOfDvGUFkvq9DNC_vYpHLt7SrjMXxZ0WfYFOrZwx-kKs2MFKlAQIDJiABIVggWjsfxY309TGcZh9BHpQEQnmVBeBRn817wQm5QOUldaIiWCAwED0ApsiRecCzp5TrwmdtGLZ9WS1EHNxSy0ofb5OsZg"
},
"clientExtensionResults": {},
"credential_nickname": "mbp"