Skip to content

Commit 29c3913

Browse files
Merge pull request #23 from Ruby-Network/db
Add multi user auth (private mode only)
2 parents 9a42dc2 + 2e783c7 commit 29c3913

18 files changed

Lines changed: 433 additions & 61 deletions

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ gem 'rack_csrf'
1414
gem 'dry-schema'
1515
gem 'dry-validation'
1616
gem 'yaml'
17+
gem 'sequel'
18+
gem 'pg'
19+
gem 'bcrypt'
20+
gem 'thor'
21+
gem 'readline'
22+
gem 'readline-ext'
1723
gem "rack-reverse-proxy", require: "rack/reverse_proxy"
1824
group :development, :test do
1925
gem "rerun"

cli/cli.rb

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
require 'thor'
2+
require 'colorize'
3+
require 'sequel'
4+
require 'bcrypt'
5+
require 'readline'
6+
require 'yaml'
7+
require_relative '../ruby/db.rb'
8+
9+
settings = YAML.load_file(File.join(File.dirname(__FILE__), '../config/settings.yml'))
10+
$host = ENV['DB_HOST'] || settings['database']['host'].to_s
11+
$user = ENV['DB_USERNAME'] || settings['database']['username'].to_s
12+
$password = ENV['DB_PASSWORD'] || settings['database']['password'].to_s
13+
$database = ENV['DB_DATABASE'] || settings['database']['dbname'].to_s
14+
15+
class RubyCLI < Thor
16+
desc "create", "Create a new user"
17+
def create
18+
puts "Creating a new user...".red
19+
username = Readline.readline("Username: ", true)
20+
username = username.gsub(/[^0-9A-Za-z]/, '')
21+
username = username.downcase
22+
while true
23+
password = Readline.readline("Password: ", true)
24+
passwordConfirm = Readline.readline("Retype password: ", true)
25+
if password != passwordConfirm
26+
puts "\e[H\e[2J"
27+
puts "Passwords do not match! Try again.".red
28+
else
29+
break
30+
end
31+
end
32+
33+
db = connectDB($host, $user, $password, $database)
34+
hashedPassword = BCrypt::Password.create(password)
35+
if db[:users].where(username: username).count > 0
36+
puts "User already exists!".red
37+
db.disconnect
38+
exit
39+
else
40+
db[:users].insert(username: username, password: hashedPassword, admin: false)
41+
puts "User created!".blue
42+
db.disconnect
43+
end
44+
45+
end
46+
desc "delete", "Delete a user"
47+
def delete
48+
puts "Deleting a user...".red
49+
username = Readline.readline("Username: ", true)
50+
username = username.gsub(/[^0-9A-Za-z]/, '')
51+
username = username.downcase
52+
db = connectDB($host, $user, $password, $database)
53+
while true
54+
usernameConfirm = Readline.readline("Are you sure you want to delete #{username}? (y/n): ", true).downcase
55+
if usernameConfirm == "y" || usernameConfirm == "yes"
56+
if db[:users].where(username: username).count > 0
57+
db[:users].where(username: username).delete
58+
puts "User deleted!".blue
59+
db.disconnect
60+
else
61+
puts "User does not exist! (use the list command to see all users)".red
62+
db.disconnect
63+
end
64+
exit
65+
elsif usernameConfirm == "n" || usernameConfirm == "no"
66+
puts "Ok, exiting...".blue
67+
db.disconnect
68+
exit
69+
end
70+
end
71+
end
72+
desc "list", "List all users"
73+
def list
74+
db = connectDB($host, $user, $password, $database)
75+
puts "Listing all users...".red
76+
users = db[:users]
77+
users.each{|user| puts user[:username]}
78+
end
79+
desc "reset", "Reset a user's password"
80+
def reset
81+
puts "Resetting a user's password...".red
82+
username = Readline.readline("Username: ", true)
83+
username = username.gsub(/[^0-9A-Za-z]/, '')
84+
username = username.downcase
85+
db = connectDB($host, $user, $password, $database)
86+
if db[:users].where(username: username).count > 0
87+
while true
88+
password = Readline.readline("New Password: ", true)
89+
passwordConfirm = Readline.readline("Retype new password: ", true)
90+
if password != passwordConfirm
91+
puts "\e[H\e[2J"
92+
puts "Passwords do not match! Try again.".red
93+
else
94+
break
95+
end
96+
end
97+
hashedPassword = BCrypt::Password.create(password)
98+
db[:users].where(username: username).update(password: hashedPassword)
99+
puts "Password reset!".blue
100+
db.disconnect
101+
else
102+
puts "User does not exist! (use the list command to see all users)".red
103+
db.disconnect
104+
end
105+
end
106+
end
107+
108+
RubyCLI.start(ARGV)

config/settings.example.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
port: 9293
2-
verboseLogging: "false"
3-
private: "false"
4-
username: "ruby"
5-
password: "ruby"
6-
mainURL: "http://localhost:3000/browser/"
1+
port: 9293 #currently does nothing, but will be used in the future
2+
verboseLogging: "false" #change this to "true" to enable verbose logging
3+
private: "false" #change this to "true" to enable private mode
4+
username: "ruby" #change this to your username (when using private mode)
5+
password: "ruby" #change this to your password (when using private mode)
6+
7+
# Everything below this line is optional is some form or another
8+
mainURL: "https://localhost:9293/" # set to a URL to redirect to when the user visits the root of the server (e.g. http://example.com/) WILL be ignored when private mode is enabled
9+
multiuser: "true" # set to true to enable multiuser mode when using private mode (if not using private mode, this will be ignored)
10+
11+
# Database Settings Only Needed When Using Multiuser Mode is Enabled
12+
# These are ignored when multiuser mode is not enabled
13+
# NOTE: when using docker these values should not be changed
14+
database:
15+
username: "ruby" # change this to your database username
16+
password: "ruby" # change this to your database password
17+
host: "db" # change this to your database host
18+
dbname: "ruby" # change this to your database name

docker/docker-compose.build.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ services:
1010
- your port here:9293
1111
volumes:
1212
- ./config.yml:/usr/src/app/config/settings.yml
13-
#networks:
14-
# default:
15-
# external:
16-
# name: default_net
13+
#
14+
# Uncomment the following lines if you want to use a database (mutliuser mode)
15+
#db:
16+
# image: postgres
17+
# restart: unless-stopped
18+
# environment:
19+
# POSTGRES_PASSWORD: ruby
20+
# POSTGRES_USER: ruby
21+
# POSTGRES_DB: ruby
22+
# volumes:
23+
# - ./db:/var/lib/postgresql/data
24+
25+
# Uncomment the following lines if you want to use adminer (database management)
26+
#adminer:
27+
# image: adminer
28+
# restart: unless-stopped
29+
# ports:
30+
# - 8099:8080

docker/docker-compose.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
version: '2'
1+
version: '3'
22
services:
33
ruby:
44
restart: unless-stopped
5-
image: 'ghcr.io/ruby-network/ruby:latest'
5+
image: 'motortruck1221/ruby:latest'
66
ports:
77
#DO NOT CHANGE 9293!
8-
- 9293:9293
8+
- your port here:9293
99
volumes:
1010
- ./config.yml:/usr/src/app/config/settings.yml
11-
#networks:
12-
# default:
13-
# external:
14-
# name: default_net
11+
12+
#
13+
# Uncomment the following lines if you want to use a database (mutliuser mode)
14+
#db:
15+
# image: postgres
16+
# restart: unless-stopped
17+
# environment:
18+
# POSTGRES_PASSWORD: ruby
19+
# POSTGRES_USER: ruby
20+
# POSTGRES_DB: ruby
21+
# volumes:
22+
# - ./db:/var/lib/postgresql/data
23+
24+
# Uncomment the following lines if you want to use adminer (database management)
25+
#adminer:
26+
# image: adminer
27+
# restart: unless-stopped
28+
# ports:
29+
# - 8099:8080

docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
## Getting Started
1212
- Local setup (no docker) [here](./terminal.md)
1313
- Local setup (with docker)(*recommended*) [here](./docker.md)
14-
- Private instance setup [here](./private.md) (both docker and non-docker)
14+
- Private instance setup [here](./private.md) (both docker and non-docker, including multiuser)
15+
- CLI commands for multiuser mode [here](./multiuser.md)
1516
- Ruby and Bundler installation [here](./install-ruby.md)
1617
- Docker Installation [here](./docker-install.md)
1718

docs/advanced-config.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33
##### This provides a list of all the configuration options available to you.
44
##### The config file can be found [here](../config/settings.example.yml)
55

6-
`port` - The port the Ruby server will run on. Default is `9293`
6+
- `port` - The port the Ruby server will run on. Default is `9293` (does not currently work)
77

8-
`verboseLogging` - Whether or not to log all requests to the console. Default is `false`
8+
- `verboseLogging` - Whether or not to log all requests to the console. Default is `false`
99

10-
`private` - Whether or not to enable private mode. Default is `false`
10+
- `private` - Whether or not to enable private mode. Default is `false`
1111

12-
`username` - The username for use in either, private instances. If it is a normal instance, username will always be `ruby`
12+
- `username` - The username for use in either, private instances. If it is a normal instance, username will always be `ruby`
1313

14-
`password` - The password for use in either, private instances. If it is a normal instance, password will always be `ruby`
14+
- `password` - The password for use in either, private instances. If it is a normal instance, password will always be `ruby`
1515

16-
`mainUrl` - The main URL for use in a normal instance. If you are trying to make a private instance set this value to `NA`
16+
- `mainUrl` - The main URL for use in a normal instance. If you are trying to make a private instance set this value to anything or delete it.
17+
18+
- `multiuser` - Whether or not to enable multiuser mode. Default is `false` **ONLY WORKS IN PRIVATE MODE**
19+
20+
- `database` - A set of options for the database connection. **ONLY WORKS IN PRIVATE MODE AND MULTIUSER IS ENABLED, CURRENTLY ONLY SUPPORTS POSTGRESQL**
21+
- `host` - The host of the database. Default is `localhost`
22+
- `dbName` - The name of the database. Default is `ruby`
23+
- `username` - The username for the database. Default is `ruby`
24+
- `password` - The password for the database. Default is `ruby`
1725

1826
---
1927
#### Options coming soon:
20-
`port` - Will be switched to `rubyPort` and will be the port the Ruby server will run on. Default is `9292`
28+
- `port` - Will be switched to `rubyPort` and will be the port the Ruby server will run on. Default is `9292`
2129

22-
`nodePort` - Will be the port the Node server will run on. Default is `9293`
30+
- `nodePort` - Will be the port the Node server will run on. Default is `9293`

docs/docker.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,35 @@
3131

3232
Or just copy this config:
3333
```yml
34-
version: '2'
34+
version: "3"
3535
services:
36-
ruby:
37-
restart: unless-stopped
38-
image: 'ghcr.io/ruby-network/ruby'
39-
ports:
40-
#DO NOT CHANGE 9293!
41-
- your port here:9293
42-
volumes:
43-
- ./config.yml:/usr/src/app/config/settings.yml
44-
#networks:
45-
# default:
46-
# external:
47-
# name: default_net
36+
ruby:
37+
image: 'motortruck1221/ruby:latest'
38+
container_name: ruby
39+
restart: unless-stopped
40+
ports:
41+
# DO NOT CHANGE 9293
42+
- "your port here:9293"
43+
volumes:
44+
- ./config.yml:/usr/src/app/config/settings.yml
45+
46+
# Uncomment the following lines if you want to use a database (multiuser mode)
47+
#db:
48+
# image: postgres
49+
# restart: unless-stopped
50+
# environment:
51+
# POSTGRES_PASSWORD: ruby
52+
# POSTGRES_USER: ruby
53+
# POSTGRES_DB: ruby
54+
# volumes:
55+
# - ./db:/var/lib/postgresql/data
56+
57+
# Uncomment the following lines if you want to use adminer (database management)
58+
#adminer:
59+
# image: adminer
60+
# restart: unless-stopped
61+
# ports:
62+
# - 8099:8080
4863
```
4964
2. Download our settings.example.yml file [here](https://github.com/ruby-network/ruby/tree/main/config/settings.example.yml)
5065

docs/multiuser.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Multiuser mode
2+
3+
## Prerequisites
4+
- A setup private instance of Ruby (using Docker Compose, or standalone) with multiuser mode enabled (see [here](./private.md#docker-multiuser) for more info)
5+
6+
---
7+
8+
## How to execute commands
9+
10+
There are two ways to execute the CLI, either using `yarn cli` or `bundler exec ruby ./cli/cli.rb`
11+
12+
This tutorial will use `yarn cli` as it is easier to type
13+
14+
## Commands
15+
16+
- `yarn cli` is the command to execute the CLI
17+
- `yarn cli help [command]` to get help with a command
18+
- `yarn cli create` - Create a new user
19+
- `yarn cli delete` - Delete a user
20+
- `yarn cli list` - List all users
21+
- `yarn cli reset` - Reset a users password
22+
23+
## How to use in Docker Compose
24+
25+
- `docker-compose exec ruby yarn cli [command]` - where `[command]` is one of the commands listed above

0 commit comments

Comments
 (0)