Skip to content

Commit 148d7ca

Browse files
Implement Corlink
1 parent 2d9f872 commit 148d7ca

6 files changed

Lines changed: 53 additions & 15 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ gem 'bcrypt'
2020
gem 'thor'
2121
gem 'readline'
2222
gem 'readline-ext'
23+
gem 'httparty'
2324
gem "rack-reverse-proxy", require: "rack/reverse_proxy"
2425
group :development do
2526
gem "rerun"

config/settings.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ database:
1616
password: "ruby" # change this to your database password
1717
host: "db" # change this to your database host
1818
dbname: "ruby" # change this to your database name
19+
20+
corlink:
21+
enabled: "false"
22+
url: "https://corlink.com"
23+
apiKey: "yourapikey"

main.rb

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,39 @@
9494

9595
#Auth to login to the site
9696
post '/auth' do
97-
if Settings.private == "false" || Settings.multiuser == "false"
98-
if params[:password] == Settings.password && params[:username].downcase == Settings.username.downcase
99-
session[:auth] = true
100-
session[:uid] = SecureRandom.alphanumeric(2048)
101-
redirect '/'
97+
if Settings.corlink.enabled == "true" && Settings.private == "false"
98+
password = params[:password]
99+
username = params[:username]
100+
req = HTTParty.post("#{Settings.corlink.url}", headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{Settings.corlink.apiKey}", 'Key' => "#{password}" })
101+
if req.code == 200
102+
if username == "ruby"
103+
session[:auth] = true
104+
session[:uid] = SecureRandom.alphanumeric(2048)
105+
redirect '/'
106+
else
107+
redirect '/'
108+
end
102109
else
103110
redirect '/'
104111
end
105112
else
106-
loggedIn = login(params[:username], params[:password])
107-
if loggedIn == true
108-
session[:auth] = true
109-
session[:uid] = SecureRandom.alphanumeric(2048)
110-
redirect '/'
113+
if Settings.private == "false" || Settings.multiuser == "false"
114+
if params[:password] == Settings.password && params[:username].downcase == Settings.username.downcase
115+
session[:auth] = true
116+
session[:uid] = SecureRandom.alphanumeric(2048)
117+
redirect '/'
118+
else
119+
redirect '/'
120+
end
111121
else
112-
redirect '/'
122+
loggedIn = login(params[:username], params[:password])
123+
if loggedIn == true
124+
session[:auth] = true
125+
session[:uid] = SecureRandom.alphanumeric(2048)
126+
redirect '/'
127+
else
128+
redirect '/'
129+
end
113130
end
114131
end
115132
end

require.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'yaml'
1313
require 'sequel'
1414
require 'bcrypt'
15+
require 'httparty'
1516
require './ruby/utils.rb'
1617
require './ruby/proxyPaths.rb'
1718
require './ruby/auth.rb'

ruby/auth.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ def call(env)
1313
# session[:auth] = true
1414
# session[:uid] = SecureRandom.alphanumeric(2048)
1515
# return [302, {'Location' => '/'}, []]
16-
if Settings.private == "false" && params['unlock'] == '' || params['unlock'] == 'unlock' || params['unlock'] == 'true' || params['unlock'] == ' '
17-
session[:auth] = true
18-
session[:uid] = SecureRandom.alphanumeric(2048)
19-
return [302, {'Location' => '/'}, []]
16+
if Settings.corlink.enabled == "true" && Settings.private == "false"
17+
auth = params['unlock']
18+
req = HTTParty.post("#{Settings.corlink.url}", headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{Settings.corlink.apiKey}", 'Key' => "#{auth}" })
19+
if req.code == 200
20+
session[:auth] = true
21+
session[:uid] = SecureRandom.alphanumeric(2048)
22+
return [302, {'Location' => '/'}, []]
23+
end
24+
else
25+
if Settings.private == "false" && params['unlock'] == '' || params['unlock'] == 'unlock' || params['unlock'] == 'true' || params['unlock'] == ' '
26+
session[:auth] = true
27+
session[:uid] = SecureRandom.alphanumeric(2048)
28+
return [302, {'Location' => '/'}, []]
29+
end
2030
end
2131
end
2232
@app.call(env)

ruby/validator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class YamlValidator < Dry::Validation::Contract
1313
required(:password).filled(:string)
1414
required(:dbname).filled(:string)
1515
end
16+
optional(:corlink).filled(:hash).schema do
17+
required(:url).filled(:string)
18+
required(:apiKey).filled(:string)
19+
end
1620
end
1721
rule(:port) do
1822
key.failure('must be greater than 0') if value <= 0

0 commit comments

Comments
 (0)