ditty 0.6.0 → 0.7.0.pre.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -4
- data/config.ru +4 -18
- data/ditty.gemspec +2 -0
- data/lib/ditty/components/app.rb +4 -3
- data/lib/ditty/controllers/application.rb +28 -5
- data/lib/ditty/controllers/auth.rb +179 -0
- data/lib/ditty/controllers/component.rb +1 -3
- data/lib/ditty/controllers/main.rb +6 -155
- data/lib/ditty/controllers/users.rb +1 -0
- data/lib/ditty/helpers/component.rb +50 -22
- data/lib/ditty/helpers/response.rb +1 -0
- data/lib/ditty/helpers/views.rb +10 -0
- data/lib/ditty/listener.rb +1 -1
- data/lib/ditty/middleware/accept_extension.rb +31 -0
- data/lib/ditty/models/user.rb +1 -5
- data/lib/ditty/policies/identity_policy.rb +10 -2
- data/lib/ditty/policies/user_policy.rb +8 -1
- data/lib/ditty/services/authentication.rb +16 -7
- data/lib/ditty/services/logger.rb +4 -3
- data/lib/ditty/services/settings.rb +8 -0
- data/lib/ditty/version.rb +1 -1
- data/views/400.haml +2 -0
- data/views/{identity/forgot.haml → auth/forgot_password.haml} +1 -1
- data/views/auth/identity.haml +15 -0
- data/views/auth/login.haml +18 -0
- data/views/auth/register.haml +19 -0
- data/views/auth/register_identity.haml +14 -0
- data/views/{identity/reset.haml → auth/reset_password.haml} +2 -3
- data/views/layout.haml +2 -2
- data/views/partials/actions.haml +6 -4
- data/views/partials/form_tag.haml +2 -1
- data/views/partials/navbar.haml +2 -3
- data/views/partials/search.haml +1 -1
- data/views/partials/sidebar.haml +3 -3
- data/views/roles/display.haml +1 -2
- data/views/roles/index.haml +0 -4
- data/views/users/display.haml +2 -4
- data/views/users/index.haml +11 -10
- data/views/users/profile.haml +2 -4
- metadata +41 -8
- data/views/identity/login.haml +0 -29
- data/views/identity/register.haml +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c189c1afda14a46a7d4011c7d2ea9a675c3efaf8800287379e66b2d0a8f7938
|
4
|
+
data.tar.gz: 788dfa56d302d52afca0ff0d42bd1769abb78b5e991c586edf4a6b6a38430384
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fddf3ffc9c33965ecc3e093c0fc21f4ce689eb110ab351f1a331cd7a9477bea416e3d6bbdc6141e9858d55db4d5cf48ad15c1e22bb930462fd245b0853cde042
|
7
|
+
data.tar.gz: 2bb572a73c374592ee7c526a7fb0df708bf43ed533ca4898dddd347f2b06afba687662dc234b0e1566856161b8d44de656eca567cdd0da2d0b17925afc01346b
|
data/.rubocop.yml
CHANGED
data/config.ru
CHANGED
@@ -5,29 +5,15 @@ use Rack::Session::Cookie,
|
|
5
5
|
key: '_Ditty_session',
|
6
6
|
# :secure=>!TEST_MODE, # Uncomment if only allowing https:// access
|
7
7
|
secret: File.read('.session_secret')
|
8
|
-
use Rack::Protection::RemoteToken
|
9
|
-
use Rack::Protection::SessionHijacking
|
10
8
|
|
11
9
|
require 'ditty/components/app'
|
12
10
|
Ditty.component :app
|
13
11
|
|
14
|
-
require '
|
15
|
-
require 'omniauth/identity'
|
16
|
-
OmniAuth.config.logger = Ditty::Services::Logger.instance
|
17
|
-
OmniAuth.config.on_failure = proc { |env|
|
18
|
-
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
|
19
|
-
}
|
20
|
-
require 'ditty/controllers/main'
|
21
|
-
require 'ditty/models/identity'
|
12
|
+
require 'ditty/services/authentication'
|
22
13
|
use OmniAuth::Builder do
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
callback_path: '/auth/identity/callback',
|
27
|
-
model: Ditty::Identity,
|
28
|
-
on_login: Ditty::Main,
|
29
|
-
on_registration: Ditty::Main,
|
30
|
-
locate_conditions: ->(req) { { username: req['username'] } }
|
14
|
+
Ditty::Services::Authentication.config.each do |prov, config|
|
15
|
+
provider prov, *config[:arguments]
|
16
|
+
end
|
31
17
|
end
|
32
18
|
|
33
19
|
run Rack::URLMap.new Ditty::Components.routes
|
data/ditty.gemspec
CHANGED
@@ -38,11 +38,13 @@ Gem::Specification.new do |spec|
|
|
38
38
|
spec.add_dependency 'omniauth-identity', '~> 1.0'
|
39
39
|
spec.add_dependency 'pundit', '~> 1.0'
|
40
40
|
spec.add_dependency 'rack-contrib', '~> 1.0'
|
41
|
+
spec.add_dependency 'rack_csrf', '~> 1.0'
|
41
42
|
spec.add_dependency 'rake', '~> 12.0'
|
42
43
|
spec.add_dependency 'sequel', '>= 4.0'
|
43
44
|
spec.add_dependency 'sinatra', '>= 2.0'
|
44
45
|
spec.add_dependency 'sinatra-contrib', '~> 2.0'
|
45
46
|
spec.add_dependency 'sinatra-flash', '~> 0.3'
|
47
|
+
spec.add_dependency 'sinatra-param', '~> 1.5'
|
46
48
|
spec.add_dependency 'tilt', '>= 2'
|
47
49
|
spec.add_dependency 'thor', '>= 0.20'
|
48
50
|
spec.add_dependency 'will_paginate', '>= 3.1'
|
data/lib/ditty/components/app.rb
CHANGED
@@ -5,7 +5,7 @@ require 'ditty'
|
|
5
5
|
module Ditty
|
6
6
|
class App
|
7
7
|
def self.load
|
8
|
-
controllers = File.expand_path('
|
8
|
+
controllers = File.expand_path('../controllers', __dir__)
|
9
9
|
Dir.glob("#{controllers}/*.rb").each { |f| require f }
|
10
10
|
|
11
11
|
require 'ditty/models/user'
|
@@ -20,17 +20,18 @@ module Ditty
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.migrations
|
23
|
-
File.expand_path('
|
23
|
+
File.expand_path('../../../migrate', __dir__)
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.view_folder
|
27
|
-
File.expand_path('
|
27
|
+
File.expand_path('../../../views', __dir__)
|
28
28
|
end
|
29
29
|
|
30
30
|
def self.routes
|
31
31
|
load
|
32
32
|
{
|
33
33
|
'/' => ::Ditty::Main,
|
34
|
+
'/auth' => ::Ditty::Auth,
|
34
35
|
'/users' => ::Ditty::Users,
|
35
36
|
'/roles' => ::Ditty::Roles,
|
36
37
|
'/audit-logs' => ::Ditty::AuditLogs
|
@@ -4,6 +4,7 @@ require 'wisper'
|
|
4
4
|
require 'oga'
|
5
5
|
require 'sinatra/base'
|
6
6
|
require 'sinatra/flash'
|
7
|
+
require 'sinatra/param'
|
7
8
|
require 'sinatra/respond_with'
|
8
9
|
require 'ditty/helpers/views'
|
9
10
|
require 'ditty/helpers/pundit'
|
@@ -12,6 +13,7 @@ require 'ditty/services/logger'
|
|
12
13
|
require 'active_support'
|
13
14
|
require 'active_support/inflector'
|
14
15
|
require 'rack/contrib'
|
16
|
+
require 'rack/csrf'
|
15
17
|
|
16
18
|
module Ditty
|
17
19
|
class Application < Sinatra::Base
|
@@ -21,14 +23,20 @@ module Ditty
|
|
21
23
|
set :map_path, nil
|
22
24
|
set :view_location, nil
|
23
25
|
set :model_class, nil
|
26
|
+
set :raise_sinatra_param_exceptions, true
|
27
|
+
set track_actions: false
|
28
|
+
|
24
29
|
# The order here is important, since Wisper has a deprecated method respond_with method
|
25
30
|
helpers Wisper::Publisher
|
26
31
|
helpers Helpers::Pundit, Helpers::Views, Helpers::Authentication
|
32
|
+
helpers Sinatra::Param
|
27
33
|
|
28
34
|
register Sinatra::Flash, Sinatra::RespondWith
|
29
35
|
|
36
|
+
use Rack::Csrf, raise: ENV['APP_ENV'] == 'development' unless ENV['APP_ENV'] == 'test'
|
30
37
|
use Rack::PostBodyContentTypeParser
|
31
38
|
use Rack::MethodOverride
|
39
|
+
use Rack::NestedParams
|
32
40
|
|
33
41
|
helpers do
|
34
42
|
def base_path
|
@@ -73,7 +81,7 @@ module Ditty
|
|
73
81
|
status 401
|
74
82
|
format.html do
|
75
83
|
flash[:warning] = 'Please log in first.'
|
76
|
-
redirect with_layout("#{settings.map_path}/auth/
|
84
|
+
redirect with_layout("#{settings.map_path}/auth/login")
|
77
85
|
end
|
78
86
|
format.json do
|
79
87
|
json code: 401, errors: ['Not Authenticated']
|
@@ -96,6 +104,19 @@ module Ditty
|
|
96
104
|
end
|
97
105
|
end
|
98
106
|
|
107
|
+
error Sinatra::Param::InvalidParameterError do
|
108
|
+
respond_to do |format|
|
109
|
+
status 400
|
110
|
+
format.html do
|
111
|
+
flash.now[:danger] = env['sinatra.error'].message
|
112
|
+
haml :'400', locals: { title: '4 oh oh' }, layout: layout
|
113
|
+
end
|
114
|
+
format.json do
|
115
|
+
json code: 400, errors: { env['sinatra.error'].param => env['sinatra.error'].message }, full_errors: [env['sinatra.error'].message]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
99
120
|
error ::Sequel::ForeignKeyConstraintViolation do
|
100
121
|
error = env['sinatra.error']
|
101
122
|
broadcast(:application_error, error)
|
@@ -127,17 +148,19 @@ module Ditty
|
|
127
148
|
end
|
128
149
|
|
129
150
|
before(/.*/) do
|
130
|
-
::Ditty::Services::Logger.instance.debug "Running with #{self.class}"
|
151
|
+
::Ditty::Services::Logger.instance.debug "Running with #{self.class} - #{request.path_info}"
|
131
152
|
if request.path =~ /.*\.json\Z/
|
132
153
|
content_type :json
|
133
154
|
request.path_info = request.path_info.gsub(/.json$/, '')
|
155
|
+
elsif request.env['ACCEPT']
|
156
|
+
content_type request.env['ACCEPT']
|
157
|
+
else
|
158
|
+
content_type(:json) if request.accept.count.eql?(1) && request.accept.first.to_s.eql?('*/*')
|
134
159
|
end
|
135
|
-
# Ensure the accept header is set. People forget to include it in API requests
|
136
|
-
content_type(:json) if request.accept.count.eql?(1) && request.accept.first.to_s.eql?('*/*')
|
137
160
|
end
|
138
161
|
|
139
162
|
after do
|
140
|
-
return if params[
|
163
|
+
return if params[:layout].nil?
|
141
164
|
response.body = response.body.map do |resp|
|
142
165
|
document = Oga.parse_html(resp)
|
143
166
|
document.css('a').each do |elm|
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'ditty/controllers/application'
|
2
|
+
require 'ditty/services/email'
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
module Ditty
|
6
|
+
class Auth < Application
|
7
|
+
set track_actions: true
|
8
|
+
|
9
|
+
def find_template(views, name, engine, &block)
|
10
|
+
super(views, name, engine, &block) # Root
|
11
|
+
super(::Ditty::App.view_folder, name, engine, &block) # Basic Plugin
|
12
|
+
end
|
13
|
+
|
14
|
+
def redirect_path
|
15
|
+
return "#{settings.map_path}/" unless env['omniauth.origin']
|
16
|
+
return "#{settings.map_path}/" if env['omniauth.origin'] =~ %r{/#{settings.map_path}/auth/?}
|
17
|
+
env['omniauth.origin']
|
18
|
+
end
|
19
|
+
|
20
|
+
def omniauth_callback(provider)
|
21
|
+
return failed_login unless env['omniauth.auth']
|
22
|
+
user = User.first(email: env['omniauth.auth']['info']['email'])
|
23
|
+
user = register_user if user.nil? && ['ldap', 'google_oauth2'].include?(provider)
|
24
|
+
return failed_login if user.nil?
|
25
|
+
successful_login(user)
|
26
|
+
end
|
27
|
+
|
28
|
+
def failed_login
|
29
|
+
broadcast(:user_failed_login, target: self, details: "IP: #{request.ip}")
|
30
|
+
flash[:warning] = 'Invalid credentials. Please try again.'
|
31
|
+
redirect "#{settings.map_path}/auth/login"
|
32
|
+
end
|
33
|
+
|
34
|
+
def successful_login(user)
|
35
|
+
halt 200 if request.xhr?
|
36
|
+
self.current_user = user
|
37
|
+
broadcast(:user_login, target: self, details: "IP: #{request.ip}")
|
38
|
+
flash[:success] = 'Logged In'
|
39
|
+
redirect redirect_path
|
40
|
+
end
|
41
|
+
|
42
|
+
def register_user
|
43
|
+
user = User.create(email: env['omniauth.auth']['info']['email'])
|
44
|
+
broadcast(:user_register, target: self, values: { user: user }, details: "IP: #{request.ip}")
|
45
|
+
flash[:info] = 'Successfully Registered.'
|
46
|
+
user
|
47
|
+
end
|
48
|
+
|
49
|
+
before '/login' do
|
50
|
+
return if User.where(roles: Role.find_or_create(name: 'super_admin')).count.positive?
|
51
|
+
flash[:info] = 'Please register the super admin user.'
|
52
|
+
redirect "#{settings.map_path}/auth/register"
|
53
|
+
end
|
54
|
+
|
55
|
+
# TODO: Make this work for both LDAP and Identity
|
56
|
+
get '/login' do
|
57
|
+
authorize ::Ditty::Identity, :login
|
58
|
+
|
59
|
+
haml :'auth/login', locals: { title: 'Log In' }
|
60
|
+
end
|
61
|
+
|
62
|
+
get '/forgot-password' do
|
63
|
+
authorize ::Ditty::Identity, :forgot_password
|
64
|
+
|
65
|
+
haml :'auth/forgot_password', locals: { title: 'Forgot your password?' }
|
66
|
+
end
|
67
|
+
|
68
|
+
post '/forgot-password' do
|
69
|
+
authorize ::Ditty::Identity, :forgot_password
|
70
|
+
|
71
|
+
param :email, String, required: true
|
72
|
+
email = params[:email]
|
73
|
+
identity = Identity[username: email]
|
74
|
+
if identity
|
75
|
+
# Update record
|
76
|
+
token = SecureRandom.hex(16)
|
77
|
+
identity.update(reset_token: token, reset_requested: Time.now)
|
78
|
+
# Send Email
|
79
|
+
reset_url = "#{request.base_url}#{settings.map_path}/reset-password?token=#{token}"
|
80
|
+
Ditty::Services::Email.deliver(
|
81
|
+
:forgot_password,
|
82
|
+
email,
|
83
|
+
locals: { identity: identity, reset_url: reset_url, request: request }
|
84
|
+
)
|
85
|
+
end
|
86
|
+
flash[:info] = 'An email was sent to the email provided with instructions on how to reset your password'
|
87
|
+
redirect '/login'
|
88
|
+
end
|
89
|
+
|
90
|
+
get '/reset-password' do
|
91
|
+
authorize ::Ditty::Identity.new, :reset_password
|
92
|
+
|
93
|
+
param :token, String, required: true
|
94
|
+
identity = Identity[reset_token: params[:token]]
|
95
|
+
halt 404 unless identity && identity.reset_requested && identity.reset_requested > (Time.now - (24 * 60 * 60))
|
96
|
+
|
97
|
+
haml :'auth/reset_password', locals: { title: 'Reset your password', identity: identity }
|
98
|
+
end
|
99
|
+
|
100
|
+
put '/reset-password' do
|
101
|
+
param :token, String, required: true
|
102
|
+
identity = Identity[reset_token: params[:token]]
|
103
|
+
|
104
|
+
halt 404 unless identity
|
105
|
+
authorize identity, :reset_password
|
106
|
+
|
107
|
+
identity_params = permitted_attributes(Identity, :update)
|
108
|
+
identity.set identity_params.merge(reset_token: nil, reset_requested: nil)
|
109
|
+
if identity.valid? && identity.save
|
110
|
+
broadcast(:identity_update_password, target: self, details: "IP: #{request.ip}")
|
111
|
+
flash[:success] = 'Password Updated'
|
112
|
+
redirect "#{settings.map_path}/auth/login"
|
113
|
+
else
|
114
|
+
broadcast(:identity_update_password_failed, target: self, details: "IP: #{request.ip}")
|
115
|
+
haml :'auth/reset_password', locals: { title: 'Reset your password', identity: identity }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Register Page
|
120
|
+
get '/register' do
|
121
|
+
authorize ::Ditty::User.new, :register
|
122
|
+
|
123
|
+
identity = Identity.new
|
124
|
+
haml :'auth/register', locals: { title: 'Register', identity: identity }
|
125
|
+
end
|
126
|
+
|
127
|
+
# Register Action
|
128
|
+
post '/register/identity' do
|
129
|
+
param :identity, Hash, required: true
|
130
|
+
identity = Identity.new(params[:identity])
|
131
|
+
user = User.new(email: identity.username)
|
132
|
+
authorize user, :register
|
133
|
+
|
134
|
+
begin
|
135
|
+
DB.transaction do
|
136
|
+
user.save
|
137
|
+
user.add_identity identity
|
138
|
+
broadcast(:user_register, target: self, values: { user: user }, details: "IP: #{request.ip}")
|
139
|
+
flash[:info] = 'Successfully Registered. Please log in'
|
140
|
+
redirect "#{settings.map_path}/auth/login"
|
141
|
+
end
|
142
|
+
rescue Sequel::ValidationFailed
|
143
|
+
flash.now[:warning] = 'Could not complete the registration. Please try again.'
|
144
|
+
haml :'auth/register', locals: { identity: identity }
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Logout Action
|
149
|
+
delete '/' do
|
150
|
+
broadcast(:user_logout, target: self, details: "IP: #{request.ip}")
|
151
|
+
logout
|
152
|
+
|
153
|
+
halt 200 if request.xhr?
|
154
|
+
flash[:info] = 'Logged Out'
|
155
|
+
redirect(Ditty::Services::Settings[:logout_redirect_path] || "#{settings.map_path}/")
|
156
|
+
end
|
157
|
+
|
158
|
+
# Unauthenticated
|
159
|
+
get '/unauthenticated' do
|
160
|
+
redirect back
|
161
|
+
end
|
162
|
+
|
163
|
+
# Auth Failure
|
164
|
+
get '/failure' do
|
165
|
+
failed_login
|
166
|
+
end
|
167
|
+
|
168
|
+
# Identity
|
169
|
+
# LDAP
|
170
|
+
post '/:provider/callback' do |provider|
|
171
|
+
omniauth_callback provider
|
172
|
+
end
|
173
|
+
|
174
|
+
# Google OAuth login
|
175
|
+
get '/:provider/callback' do |provider|
|
176
|
+
omniauth_callback provider
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -31,9 +31,7 @@ module Ditty
|
|
31
31
|
|
32
32
|
after '/' do
|
33
33
|
return if settings.environment == 'production' || request.request_method != 'GET'
|
34
|
-
if (response.successful? || response.redirection?) && @skip_verify == false
|
35
|
-
verify_policy_scoped
|
36
|
-
end
|
34
|
+
verify_policy_scoped if (response.successful? || response.redirection?) && @skip_verify == false
|
37
35
|
end
|
38
36
|
|
39
37
|
# List
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'ditty/controllers/application'
|
4
|
-
require 'ditty/
|
5
|
-
require '
|
4
|
+
require 'ditty/models/role'
|
5
|
+
require 'ditty/models/user'
|
6
6
|
|
7
7
|
module Ditty
|
8
8
|
class Main < Application
|
@@ -13,16 +13,10 @@ module Ditty
|
|
13
13
|
super(::Ditty::App.view_folder, name, engine, &block) # Basic Plugin
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
# Redirect to the registration page if there's no SA user
|
21
|
-
sa = Role.find_or_create(name: 'super_admin')
|
22
|
-
if User.where(roles: sa).count == 0
|
23
|
-
flash[:info] = 'Please register the super admin user.'
|
24
|
-
redirect "#{settings.map_path}/auth/identity/register"
|
25
|
-
end
|
16
|
+
before '/' do
|
17
|
+
return if User.where(roles: Role.find_or_create(name: 'super_admin')).count.positive?
|
18
|
+
flash[:info] = 'Please register the super admin user.'
|
19
|
+
redirect "#{settings.map_path}/auth/register"
|
26
20
|
end
|
27
21
|
|
28
22
|
# Home Page
|
@@ -30,148 +24,5 @@ module Ditty
|
|
30
24
|
authenticate!
|
31
25
|
haml :index, locals: { title: 'Home' }
|
32
26
|
end
|
33
|
-
|
34
|
-
# OmniAuth Identity Stuff
|
35
|
-
# Log in Page
|
36
|
-
get '/auth/identity' do
|
37
|
-
haml :'identity/login', locals: { title: 'Log In' }
|
38
|
-
end
|
39
|
-
|
40
|
-
get '/auth/identity/forgot' do
|
41
|
-
haml :'identity/forgot', locals: { title: 'Forgot your password?' }
|
42
|
-
end
|
43
|
-
|
44
|
-
post '/auth/identity/forgot' do
|
45
|
-
email = params['email']
|
46
|
-
identity = Identity[username: email]
|
47
|
-
if identity
|
48
|
-
# Update record
|
49
|
-
token = SecureRandom.hex(16)
|
50
|
-
identity.update(reset_token: token, reset_requested: Time.now)
|
51
|
-
# Send Email
|
52
|
-
reset_url = "#{request.base_url}#{settings.map_path}/auth/identity/reset?token=#{token}"
|
53
|
-
Ditty::Services::Email.deliver(
|
54
|
-
:forgot_password,
|
55
|
-
email,
|
56
|
-
locals: { identity: identity, reset_url: reset_url, request: request }
|
57
|
-
)
|
58
|
-
end
|
59
|
-
flash[:info] = 'An email was sent to the email provided with instructions on how to reset your password'
|
60
|
-
redirect '/auth/identity'
|
61
|
-
end
|
62
|
-
|
63
|
-
get '/auth/identity/reset' do
|
64
|
-
identity = Identity[reset_token: params['token']]
|
65
|
-
halt 404 unless identity && identity.reset_requested && identity.reset_requested > (Time.now - (24 * 60 * 60))
|
66
|
-
|
67
|
-
haml :'identity/reset', locals: { title: 'Reset your password', identity: identity }
|
68
|
-
end
|
69
|
-
|
70
|
-
put '/auth/identity/reset' do
|
71
|
-
identity = Identity[reset_token: params['token']]
|
72
|
-
halt 404 unless identity && identity.reset_requested && identity.reset_requested > (Time.now - (24 * 60 * 60))
|
73
|
-
|
74
|
-
identity_params = permitted_attributes(Identity, :update)
|
75
|
-
|
76
|
-
identity.set identity_params.merge(reset_token: nil, reset_requested: nil)
|
77
|
-
if identity.valid? && identity.save
|
78
|
-
broadcast(:identity_update_password, target: self, details: "IP: #{request.ip}")
|
79
|
-
flash[:success] = 'Password Updated'
|
80
|
-
redirect "#{settings.map_path}/auth/identity"
|
81
|
-
else
|
82
|
-
broadcast(:identity_update_password_failed, target: self, details: "IP: #{request.ip}")
|
83
|
-
haml :'identity/reset', locals: { title: 'Reset your password', identity: identity }
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
get '/auth/failure' do
|
88
|
-
broadcast(:user_failed_login, target: self, details: "IP: #{request.ip}")
|
89
|
-
flash[:warning] = 'Invalid credentials. Please try again.'
|
90
|
-
redirect "#{settings.map_path}/auth/identity"
|
91
|
-
end
|
92
|
-
|
93
|
-
# Register Page
|
94
|
-
get '/auth/identity/register' do
|
95
|
-
authorize ::Ditty::Identity, :register
|
96
|
-
|
97
|
-
identity = Identity.new
|
98
|
-
haml :'identity/register', locals: { title: 'Register', identity: identity }
|
99
|
-
end
|
100
|
-
|
101
|
-
# Register Action
|
102
|
-
post '/auth/identity/new' do
|
103
|
-
authorize ::Ditty::Identity, :register
|
104
|
-
|
105
|
-
identity = Identity.new(params['identity'])
|
106
|
-
begin
|
107
|
-
DB.transaction do
|
108
|
-
identity.save # Will trigger a Sequel::ValidationFailed exception if the model is incorrect
|
109
|
-
user = User.find(email: identity.username)
|
110
|
-
if user.nil?
|
111
|
-
user = User.create(email: identity.username)
|
112
|
-
|
113
|
-
broadcast(:user_register, target: self, values: { user: user }, details: "IP: #{request.ip}")
|
114
|
-
end
|
115
|
-
user.add_identity identity
|
116
|
-
flash[:info] = 'Successfully Registered. Please log in'
|
117
|
-
redirect "#{settings.map_path}/auth/identity"
|
118
|
-
end
|
119
|
-
rescue Sequel::ValidationFailed
|
120
|
-
flash.now[:warning] = 'Could not complete the registration. Please try again.'
|
121
|
-
haml :'identity/register', locals: { identity: identity }
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
# Logout Action
|
126
|
-
delete '/auth/identity' do
|
127
|
-
broadcast(:user_logout, target: self, details: "IP: #{request.ip}")
|
128
|
-
logout
|
129
|
-
flash[:info] = 'Logged Out'
|
130
|
-
|
131
|
-
redirect "#{settings.map_path}/"
|
132
|
-
end
|
133
|
-
|
134
|
-
post '/auth/identity/callback' do
|
135
|
-
if env['omniauth.auth']
|
136
|
-
# Successful Login
|
137
|
-
user = User.find(email: env['omniauth.auth']['info']['email'])
|
138
|
-
self.current_user = user
|
139
|
-
broadcast(:user_login, target: self, details: "IP: #{request.ip}")
|
140
|
-
flash[:success] = 'Logged In'
|
141
|
-
redirect env['omniauth.origin'] || "#{settings.map_path}/"
|
142
|
-
else
|
143
|
-
# Failed Login
|
144
|
-
broadcast(:identity_failed_login, target: self, details: "IP: #{request.ip}")
|
145
|
-
flash[:warning] = 'Invalid credentials. Please try again.'
|
146
|
-
redirect "#{settings.map_path}/auth/identity"
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
get '/auth/:provider/callback' do
|
151
|
-
if env['omniauth.auth']
|
152
|
-
# Successful Login
|
153
|
-
user = User.find(email: env['omniauth.auth']['info']['email'])
|
154
|
-
if user.nil?
|
155
|
-
DB.transaction do
|
156
|
-
user = User.create(email: env['omniauth.auth']['info']['email'])
|
157
|
-
broadcast(:user_register, target: self, values: { user: user }, details: "IP: #{request.ip}")
|
158
|
-
end
|
159
|
-
end
|
160
|
-
self.current_user = user
|
161
|
-
broadcast(:user_login, target: self, details: "IP: #{request.ip}")
|
162
|
-
flash[:success] = 'Logged In'
|
163
|
-
redirect env['omniauth.origin'] || "#{settings.map_path}/"
|
164
|
-
else
|
165
|
-
# Failed Login
|
166
|
-
broadcast(:user_failed_login, target: self, details: "IP: #{request.ip}")
|
167
|
-
flash[:warning] = 'Invalid credentials. Please try again.'
|
168
|
-
redirect "#{settings.map_path}/auth/identity"
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
# Unauthenticated
|
173
|
-
get '/unauthenticated' do
|
174
|
-
redirect "#{settings.map_path}/auth/identity"
|
175
|
-
end
|
176
27
|
end
|
177
28
|
end
|