mumukit-login 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3ee9172809ea39874cd16d18c98a2b180d645fb
4
- data.tar.gz: d9abbc2b9dbc329c2e2e402330880c759fa12f07
3
+ metadata.gz: e9469b5574895337bee449aca6ffb6a5be57ee70
4
+ data.tar.gz: f2bc55e4e42e7c7d0b8925400b8ceb16387acd1b
5
5
  SHA512:
6
- metadata.gz: d1860be5880a0f25c6e408f8b8e60efaaae81eafd1d38e2a671368f4754a3bb574b79f31b196228aef83d8ba456370a6230aa717cafc7c1ef7bb9ab939cbb35c
7
- data.tar.gz: fcc2e6f167b9235d77b7120ccfcebf713285897e65007d7819a959402d855ca2ca5d14fe7f8373e22ab41dbd76d7e586c901f558639f964a9cdcb5186a93dfc7
6
+ metadata.gz: cc7eaf5d94e79e2bb5a1f76659507e9e43bcb44c9b35536e1c1ae340a6503d025b267b3023168b1273a05c92b3d83ff434052d1b6d364203e97236c207808c59
7
+ data.tar.gz: 8e7c0fdb09272ddff505c397fc37b65ebffa1e1d45d390970a88a88ca21f9f66c0f8ac3e5c752cf67c32cb87e4184e09631e4d364afa22fe49301f01b8169ddf
@@ -34,6 +34,7 @@ module Mumukit::Login
34
34
  end
35
35
 
36
36
  require_relative './login/controller'
37
+ require_relative './login/current_user_store'
37
38
  require_relative './login/form'
38
39
  require_relative './login/framework'
39
40
  require_relative './login/origin_redirector'
@@ -4,6 +4,14 @@ class Mumukit::Login::Controller
4
4
  @native = native
5
5
  end
6
6
 
7
+ def current_user_store
8
+ if env['HTTP_AUTHORIZATION']
9
+ Mumukit::Login::JWTCurrentUserStore.new self
10
+ else
11
+ Mumukit::Login::SessionCurrentUserStore.new self
12
+ end
13
+ end
14
+
7
15
  def env
8
16
  @framework.env @native
9
17
  end
@@ -16,14 +24,14 @@ class Mumukit::Login::Controller
16
24
  @framework.render_html!(html, @native)
17
25
  end
18
26
 
19
- def request
20
- Rack::Request.new(env)
21
- end
22
-
23
27
  def url_for(path)
24
28
  URI.join(request.base_url, path).to_s
25
29
  end
26
30
 
31
+ def request
32
+ Rack::Request.new(env)
33
+ end
34
+
27
35
  def session
28
36
  request.session
29
37
  end
@@ -0,0 +1,44 @@
1
+ class Mumukit::Login::SessionCurrentUserStore
2
+ def initialize(controller)
3
+ @controller = controller
4
+ end
5
+
6
+ def get_uid
7
+ @controller.session[:user_uid]
8
+ end
9
+
10
+ def clear!
11
+ @controller.session[:user_uid] = nil
12
+ end
13
+
14
+ def set!(uid, values)
15
+ @controller.session[:user_uid] = uid
16
+ values.each { |k, v| @controller.session[k] = v }
17
+ end
18
+ end
19
+
20
+ class Mumukit::Login::JWTCurrentUserStore
21
+ def initialize(controller)
22
+ @controller = controller
23
+ end
24
+
25
+ def get_uid
26
+ token.uid
27
+ end
28
+
29
+ def clear!
30
+ raise 'JWT tokens are read-only'
31
+ end
32
+
33
+ def set!(*)
34
+ raise 'JWT tokens are read-only'
35
+ end
36
+
37
+ def token
38
+ @token ||= Mumukit::Auth::Token.decode_header(authorization_header).tap(&:verify_client!)
39
+ end
40
+
41
+ def authorization_header
42
+ @controller.env['HTTP_AUTHORIZATION']
43
+ end
44
+ end
@@ -1,4 +1,5 @@
1
1
  module Mumukit::Login::Framework
2
2
  end
3
3
 
4
- require_relative './framework/rails'
4
+ require_relative './framework/rails'
5
+ require_relative './framework/sinatra'
@@ -23,16 +23,22 @@ module Mumukit::Login::Framework::Rails
23
23
  #
24
24
  def self.configure_login_routes!(rails_router)
25
25
  rails_router.controller :login do
26
- rails_router.match 'auth/:provider/callback' => :callback, via: [:get, :post], as: 'auth_callback'
27
- rails_router.get 'auth/failure' => :failure
28
- rails_router.get 'logout' => :destroy
29
- rails_router.get 'login' => :login
26
+ rails_router.match 'auth/:provider/callback' => :callback_current_user, via: [:get, :post]
27
+ rails_router.get 'auth/failure' => :login_failure
28
+ rails_router.get 'logout' => :logout_current_user
29
+ rails_router.get 'login' => :login_current_user
30
30
  end
31
31
  end
32
32
 
33
33
  def self.configure_login_controller!(controller_class)
34
34
  controller_class.class_eval do
35
35
  include Mumukit::Login::LoginControllerHelpers
36
+
37
+ %w(callback_current_user login_failure logout_current_user login_current_user).each do |method|
38
+ define_method method do
39
+ self.send "#{method}!"
40
+ end
41
+ end
36
42
  end
37
43
  end
38
44
 
@@ -44,6 +50,7 @@ module Mumukit::Login::Framework::Rails
44
50
  Mumukit::Login.config.provider.configure_rails_forgery_protection!(controller_class)
45
51
  controller_class.class_eval do
46
52
  include Mumukit::Login::AuthenticationHelpers
53
+ include Mumukit::Login::AuthorizationHelpers
47
54
 
48
55
  helper_method :current_user,
49
56
  :current_user?,
@@ -0,0 +1,39 @@
1
+ module Mumukit::Login::Framework::Sinatra
2
+
3
+ def self.env(sinatra_handler)
4
+ sinatra_handler.request.env
5
+ end
6
+
7
+ def self.redirect!(path, sinatra_handler)
8
+ sinatra_handler.redirect path
9
+ end
10
+
11
+ def self.render_html!(content, sinatra_handler)
12
+ content
13
+ end
14
+
15
+ def self.configure_login_routes!(sinatra_module)
16
+ sinatra_module.instance_eval do
17
+ auth_callback = proc { callback_current_user! }
18
+ get '/auth/:provider/callback', &auth_callback
19
+ post '/auth/:provider/callback', &auth_callback
20
+ get('/auth/failure') { login_failure! }
21
+ get('/logout') { logout_current_user! }
22
+ get('/login') { login_current_user! }
23
+ end
24
+ end
25
+
26
+
27
+ def self.configure_login_controller!(sinatra_helpers)
28
+ sinatra_helpers.instance_eval do
29
+ include Mumukit::Login::LoginControllerHelpers
30
+ end
31
+ end
32
+
33
+ def self.configure_controller!(sinatra_helpers)
34
+ sinatra_helpers.instance_eval do
35
+ include Mumukit::Login::AuthenticationHelpers
36
+ include Mumukit::Login::AuthorizationHelpers
37
+ end
38
+ end
39
+ end
@@ -1,2 +1,4 @@
1
1
  require_relative './helpers/authentication_helpers'
2
- require_relative './helpers/login_controller_helpers'
2
+ require_relative './helpers/authorization_helpers'
3
+ require_relative './helpers/login_controller_helpers'
4
+ require_relative './helpers/user_permissions_helpers'
@@ -16,7 +16,7 @@ module Mumukit::Login::AuthenticationHelpers
16
16
 
17
17
  # default
18
18
  def current_user_uid
19
- mumukit_controller.session[:user_uid]
19
+ mumukit_controller.current_user_store.get_uid
20
20
  end
21
21
 
22
22
  # default
@@ -0,0 +1,15 @@
1
+ module Mumukit::Login::AuthorizationHelpers
2
+ def authorize!(role)
3
+ if current_user?
4
+ current_user.protect! role, authorization_slug
5
+ else
6
+ authenticate!
7
+ end
8
+ end
9
+
10
+ def has_permission?(role)
11
+ current_user.has_permission? role, authorization_slug
12
+ end
13
+
14
+ required :authorization_slug
15
+ end
@@ -1,31 +1,37 @@
1
1
  module Mumukit::Login::LoginControllerHelpers
2
2
 
3
- def login
3
+ def login_current_user!
4
4
  origin_redirector.save_location!
5
- login_provider.request_authentication! mumukit_controller, login_settings
5
+ if current_user?
6
+ origin_redirector.redirect!
7
+ else
8
+ login_provider.request_authentication! mumukit_controller, login_settings
9
+ end
6
10
  end
7
11
 
8
- def callback
9
- profile = Mumukit::Login::Profile.from_omniauth(env['omniauth.auth'])
12
+ def callback_current_user!
13
+ profile = Mumukit::Login::Profile.from_omniauth(mumukit_controller.env['omniauth.auth'])
10
14
  user = Mumukit::Login.config.user_class.for_profile profile
11
- save_session_user_uid! user
15
+ save_current_user_session! user
12
16
  origin_redirector.redirect!
13
17
  end
14
18
 
15
- def destroy
16
- destroy_session_user_uid!
19
+ def logout_current_user!
20
+ destroy_current_user_session!
17
21
  mumukit_controller.redirect! login_provider.logout_redirection_path
18
22
  end
19
23
 
20
24
  private
21
25
 
22
26
  # default
23
- def destroy_session_user_uid!
24
- mumukit_controller.session[:user_uid] = nil
27
+ def destroy_current_user_session!
28
+ mumukit_controller.current_user_store.clear!
25
29
  end
26
30
 
27
31
  # default
28
- def save_session_user_uid!(user)
29
- mumukit_controller.session[:user_uid] = user.uid
32
+ def save_current_user_session!(user)
33
+ mumukit_controller.current_user_store.set! user.uid,
34
+ user_name: user.name,
35
+ user_image_url: user.image_url
30
36
  end
31
- end
37
+ end
@@ -0,0 +1,13 @@
1
+ module Mumukit::Login::UserPermissionsHelpers
2
+ delegate :has_role?,
3
+ :add_permission!,
4
+ :remove_permission!,
5
+ :has_permission?,
6
+ :has_permission_delegation?,
7
+ :protect!,
8
+ :protect_delegation!, to: :permissions
9
+
10
+ def merge_permissions!(new_permissions)
11
+ self.permissions = permissions.merge(new_permissions)
12
+ end
13
+ end
@@ -16,6 +16,6 @@ class Mumukit::Login::OriginRedirector
16
16
  private
17
17
 
18
18
  def origin
19
- @controller.request.params['origin']
19
+ @controller.request.params['origin'] || '/'
20
20
  end
21
21
  end
@@ -1,5 +1,5 @@
1
1
  module Mumukit
2
2
  module Login
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumukit-login
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franco Leonardo Bulgarelli
@@ -173,12 +173,16 @@ extra_rdoc_files: []
173
173
  files:
174
174
  - lib/mumukit/login.rb
175
175
  - lib/mumukit/login/controller.rb
176
+ - lib/mumukit/login/current_user_store.rb
176
177
  - lib/mumukit/login/form.rb
177
178
  - lib/mumukit/login/framework.rb
178
179
  - lib/mumukit/login/framework/rails.rb
180
+ - lib/mumukit/login/framework/sinatra.rb
179
181
  - lib/mumukit/login/helpers.rb
180
182
  - lib/mumukit/login/helpers/authentication_helpers.rb
183
+ - lib/mumukit/login/helpers/authorization_helpers.rb
181
184
  - lib/mumukit/login/helpers/login_controller_helpers.rb
185
+ - lib/mumukit/login/helpers/user_permissions_helpers.rb
182
186
  - lib/mumukit/login/origin_redirector.rb
183
187
  - lib/mumukit/login/profile.rb
184
188
  - lib/mumukit/login/provider.rb