[ TonTon Auth ]
This commit is contained in:
parent
239b90227b
commit
7085e69bb6
3 changed files with 163 additions and 1 deletions
1
Gemfile
1
Gemfile
|
|
@ -3,3 +3,4 @@ source "https://rubygems.org"
|
||||||
gem 'sinatra'
|
gem 'sinatra'
|
||||||
gem 'sinatra-flash'
|
gem 'sinatra-flash'
|
||||||
gem 'puma'
|
gem 'puma'
|
||||||
|
gem 'sod'
|
||||||
88
bin/tauth.rb
Executable file
88
bin/tauth.rb
Executable file
|
|
@ -0,0 +1,88 @@
|
||||||
|
#! /usr/bin/ruby
|
||||||
|
|
||||||
|
require 'sod'
|
||||||
|
|
||||||
|
require_relative '../lib/tonton_web/auth'
|
||||||
|
require 'io/console'
|
||||||
|
|
||||||
|
class CreateTable < Sod::Action
|
||||||
|
on "--create-table"
|
||||||
|
|
||||||
|
def call(*)
|
||||||
|
context.tauth.create_tables
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class CreateUser < Sod::Command
|
||||||
|
handle "create-user"
|
||||||
|
|
||||||
|
class Username < Sod::Action
|
||||||
|
on "--username", argument: "USERNAME"
|
||||||
|
|
||||||
|
def call(username) = context.input[:username] = username
|
||||||
|
end
|
||||||
|
|
||||||
|
class Name < Sod::Action
|
||||||
|
on "--name", argument: "NAME"
|
||||||
|
|
||||||
|
def call(name) = context.input[:name] = name
|
||||||
|
end
|
||||||
|
|
||||||
|
class Email < Sod::Action
|
||||||
|
on "--email", argument: "EMAIL"
|
||||||
|
|
||||||
|
def call(email) = context.input[:email] = email
|
||||||
|
end
|
||||||
|
|
||||||
|
on Username
|
||||||
|
on Name
|
||||||
|
on Email
|
||||||
|
|
||||||
|
def call
|
||||||
|
context.input[:password] = IO::console.getpass("User password: ")
|
||||||
|
|
||||||
|
context.tauth.create_user(**context.input)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class DeleteUser < Sod::Action
|
||||||
|
on "--delete-user", argument: "USER_ID"
|
||||||
|
|
||||||
|
def call(user_id)
|
||||||
|
context.tauth.delete_user user_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Info < Sod::Command
|
||||||
|
handle "info"
|
||||||
|
|
||||||
|
description "Information about user."
|
||||||
|
|
||||||
|
class Username < Sod::Action
|
||||||
|
on "--username", argument: "TEXT"
|
||||||
|
|
||||||
|
def call text
|
||||||
|
puts context.tauth.find_user(username: text)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
on Username
|
||||||
|
end
|
||||||
|
|
||||||
|
tauth = TonTonWeb::Auth.new Dir.getwd
|
||||||
|
|
||||||
|
context = Sod::Context[tauth: tauth, input: {}]
|
||||||
|
|
||||||
|
cli = Sod.new(banner: "Tauth 0.0.0: TonTon Auth") do
|
||||||
|
on(CreateTable, context:)
|
||||||
|
|
||||||
|
on(CreateUser, context:)
|
||||||
|
|
||||||
|
on(DeleteUser, context:)
|
||||||
|
|
||||||
|
on(Info, context:)
|
||||||
|
|
||||||
|
on Sod::Prefabs::Actions::Help, self
|
||||||
|
end
|
||||||
|
|
||||||
|
cli.call
|
||||||
73
lib/tonton_web/auth.rb
Normal file
73
lib/tonton_web/auth.rb
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
require 'pathname'
|
||||||
|
require 'sqlite3'
|
||||||
|
require 'bcrypt'
|
||||||
|
|
||||||
|
module TonTonWeb
|
||||||
|
User = Struct.new(:id, :name, :username, :email, :password_digest)
|
||||||
|
|
||||||
|
class Auth
|
||||||
|
attr_reader :root
|
||||||
|
|
||||||
|
def initialize root_path
|
||||||
|
@root = Pathname.new root_path
|
||||||
|
|
||||||
|
@db = SQLite3::Database.new(@root+ 'database.sqlite')
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_tables
|
||||||
|
@db.execute "create table users (id integer primary key, name text, username text, email text, password_digest text);"
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_user **options
|
||||||
|
name = options.fetch :name
|
||||||
|
username = options.fetch :username
|
||||||
|
email = options.fetch :email
|
||||||
|
password = options.fetch :password
|
||||||
|
|
||||||
|
# Assert user doesn't exists
|
||||||
|
|
||||||
|
hashed_password = BCrypt::Password.create(password)
|
||||||
|
|
||||||
|
@db.execute(
|
||||||
|
"insert into users (name, username, email, password_digest) values (?,?,?,?);",
|
||||||
|
[name, username, email, password]
|
||||||
|
)
|
||||||
|
|
||||||
|
return # Return user
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_user id
|
||||||
|
@db.execute "delete from users where id = ?;", id
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_user **options
|
||||||
|
name = options[:name]
|
||||||
|
username = options[:username]
|
||||||
|
email = options[:email]
|
||||||
|
|
||||||
|
if not (name or username or email)
|
||||||
|
raise "No options to find user."
|
||||||
|
end
|
||||||
|
|
||||||
|
user = nil
|
||||||
|
|
||||||
|
user_query_template = "select id, name, username, email, password_digest from users where name = ? or username = ? or email = ?"
|
||||||
|
|
||||||
|
@db.execute(user_query_template, [name, username, email]) do |row|
|
||||||
|
user = TonTonWeb::User.new(*row)
|
||||||
|
end
|
||||||
|
|
||||||
|
return user
|
||||||
|
end
|
||||||
|
|
||||||
|
def authenticate username, password
|
||||||
|
user = find_user(username: username)
|
||||||
|
|
||||||
|
if BCrypt::Password.new(user.password_digest) == password
|
||||||
|
return user
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue