devise-remote-user 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +24 -5
- data/Rakefile +6 -0
- data/devise_remote_user.gemspec +1 -0
- data/lib/devise_remote_user.rb +30 -13
- data/lib/devise_remote_user/manager.rb +59 -0
- data/lib/devise_remote_user/model.rb +3 -29
- data/lib/devise_remote_user/strategy.rb +1 -1
- data/lib/devise_remote_user/version.rb +1 -1
- data/spec/spec_helper.rb +17 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ba593036cec8c5f0fc38ba79eb8e2040897dd7e
|
4
|
+
data.tar.gz: cf65b35d2b96bd7ea3bf3bc38d1a22bce3b0684c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cf34573776063dc5c328d5122e8d5f9585d4a03fa73fa8655b4e90be8d39832478c26758f9682ccd894bf163713443fc18202f6f50c2e07cd34faa3b047c01d
|
7
|
+
data.tar.gz: ad3d09d20daebf8d1de5e252b91522011e3f160cad5b98df85e4954a5eafccd41afa47e3af84703e607d3cfb04b7c037be0a8a6f98d8cc3b3d34bbb6a53c3539
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -20,12 +20,31 @@ bundle install
|
|
20
20
|
Sorry, there are no generators yet, so ...
|
21
21
|
|
22
22
|
- Add `:remote_user_authenticatable` symbol to `devise` statement in User model.
|
23
|
-
- Add `require 'devise-remote-user'` to devise initializer at `config/initializers/devise.rb`
|
24
23
|
- Add `before_filter :authenticate_user!` to ApplicationController, if not already present. This ensures that remote user is logged in locally (via database)
|
25
24
|
|
26
|
-
Configuration options
|
25
|
+
Configuration options:
|
27
26
|
|
28
|
-
`
|
29
|
-
`
|
30
|
-
`
|
27
|
+
- `env_var` - String (default: `'REMOTE_USER'`). Request environment key for the remote user id.
|
28
|
+
- `attribute_map` - Hash (default: `{}`). Map of User model attributes to request environment keys for updating the local user when auto-creation is enabled.
|
29
|
+
- `auto_create` - Boolean (default: `false`). Whether to auto-create a local user from the remote user attributes. Note: Also requires adding the Warden callbacks as shown below.
|
30
|
+
- `auto_update` - Boolean (default: `false`). Whether to auto-update authenticated user attributes from remote user attributes.
|
31
31
|
|
32
|
+
Set options in `config/intializers/devise.rb`:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'devise-remote-user'
|
36
|
+
|
37
|
+
DeviseRemoteUser.configure do |config|
|
38
|
+
config.env_key = 'REMOTE_USER'
|
39
|
+
config.auto_create = true
|
40
|
+
config.auto_update = true
|
41
|
+
config.attribute_map = {email: 'mail'}
|
42
|
+
end
|
43
|
+
|
44
|
+
# Uses Warden after_authentication callback to implement auto_update feature
|
45
|
+
DeviseRemoteUser.add_warden_callbacks
|
46
|
+
```
|
47
|
+
|
48
|
+
## Tests
|
49
|
+
|
50
|
+
Sorry, they haven't been added yet because this code was refactored from en existing Rails app.
|
data/Rakefile
CHANGED
data/devise_remote_user.gemspec
CHANGED
data/lib/devise_remote_user.rb
CHANGED
@@ -1,31 +1,48 @@
|
|
1
1
|
require 'devise'
|
2
2
|
|
3
3
|
module DeviseRemoteUser
|
4
|
-
|
5
|
-
end
|
6
|
-
end
|
4
|
+
|
5
|
+
class Engine < Rails::Engine; end
|
7
6
|
|
8
|
-
module Devise
|
9
7
|
# request.env key for remote user name
|
10
8
|
# Set to 'HTTP_REMOTE_USER' in config/initializers/devise.rb if behind reverse proxy
|
11
|
-
mattr_accessor :
|
12
|
-
@@
|
9
|
+
mattr_accessor :env_key
|
10
|
+
@@env_key = 'REMOTE_USER'
|
13
11
|
|
14
|
-
# Enable user auto-creation
|
15
|
-
mattr_accessor :
|
16
|
-
@@
|
12
|
+
# Enable user auto-creation of user from remote user attributes
|
13
|
+
mattr_accessor :auto_create
|
14
|
+
@@auto_create = false
|
15
|
+
|
16
|
+
# Enable user auto-update of user attributes from remote user attributes
|
17
|
+
mattr_accessor :auto_update
|
18
|
+
@@auto_update = false
|
17
19
|
|
18
20
|
# User attribute used for lookup of remote user
|
19
21
|
# Defaults to Devise.authentication_keys.first
|
20
|
-
mattr_accessor :
|
21
|
-
@@
|
22
|
+
mattr_accessor :auth_key
|
23
|
+
@@auth_key = nil
|
22
24
|
|
23
25
|
# Map of User model attributes to request.env keys for updating a local user when auto-creation is enabled.
|
24
|
-
mattr_accessor :
|
25
|
-
@@
|
26
|
+
mattr_accessor :attribute_map
|
27
|
+
@@attribute_map = {}
|
28
|
+
|
29
|
+
def self.configure
|
30
|
+
yield self
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.add_warden_callbacks
|
34
|
+
if auto_update
|
35
|
+
Warden::Manager.after_authentication do |user, auth, opts|
|
36
|
+
manager = DeviseRemoteUser::Manager.new(auth.env)
|
37
|
+
manager.update_user(user)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
26
42
|
end
|
27
43
|
|
28
44
|
Devise.add_module(:remote_user_authenticatable,
|
29
45
|
:strategy => true,
|
30
46
|
:controller => :sessions,
|
31
47
|
:model => 'devise_remote_user/model')
|
48
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module DeviseRemoteUser
|
2
|
+
|
3
|
+
#
|
4
|
+
# The Manager class is responsible for connecting the appliation's User
|
5
|
+
# class with remote user information in the request environment.
|
6
|
+
#
|
7
|
+
class Manager
|
8
|
+
|
9
|
+
attr_reader :env
|
10
|
+
|
11
|
+
def initialize(env)
|
12
|
+
@env = env
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_or_create_user
|
16
|
+
user = find_user
|
17
|
+
if !user && DeviseRemoteUser.auto_create
|
18
|
+
user = create_user
|
19
|
+
end
|
20
|
+
user
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_user
|
24
|
+
User.where(user_criterion).first
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_user
|
28
|
+
random_password = SecureRandom.hex(16)
|
29
|
+
attrs = user_criterion.merge({password: random_password, password_confirmation: random_password})
|
30
|
+
user = User.create(attrs)
|
31
|
+
update_user(user)
|
32
|
+
user
|
33
|
+
end
|
34
|
+
|
35
|
+
def update_user(user)
|
36
|
+
user.update_attributes(remote_user_attributes)
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def remote_user_attributes
|
42
|
+
DeviseRemoteUser.attribute_map.inject({}) { |h, (k, v)| h[k] = env[v] if env.has_key?(v); h }
|
43
|
+
end
|
44
|
+
|
45
|
+
def user_criterion
|
46
|
+
{auth_key => remote_user_id}
|
47
|
+
end
|
48
|
+
|
49
|
+
def remote_user_id
|
50
|
+
env[DeviseRemoteUser.env_key]
|
51
|
+
end
|
52
|
+
|
53
|
+
def auth_key
|
54
|
+
DeviseRemoteUser.auth_key || Devise.authentication_keys.first
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'devise_remote_user/strategy'
|
2
|
+
require 'devise_remote_user/manager'
|
2
3
|
|
3
4
|
module Devise::Models
|
4
5
|
module RemoteUserAuthenticatable
|
@@ -7,35 +8,8 @@ module Devise::Models
|
|
7
8
|
module ClassMethods
|
8
9
|
|
9
10
|
def find_for_remote_user_authentication(env)
|
10
|
-
|
11
|
-
|
12
|
-
user = create_user!(env)
|
13
|
-
end
|
14
|
-
user
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def auth_key
|
20
|
-
Devise.remote_user_auth_key || self.authentication_keys.first
|
21
|
-
end
|
22
|
-
|
23
|
-
def create_user!(env)
|
24
|
-
random_password = SecureRandom.hex(16)
|
25
|
-
attrs = {
|
26
|
-
auth_key => remote_user_id(env),
|
27
|
-
:password => random_password,
|
28
|
-
:password_confirmation => random_password
|
29
|
-
}.merge(remote_user_attributes(env))
|
30
|
-
User.create! attrs
|
31
|
-
end
|
32
|
-
|
33
|
-
def remote_user_id(env)
|
34
|
-
env[Devise.remote_user_env_key]
|
35
|
-
end
|
36
|
-
|
37
|
-
def remote_user_attributes(env)
|
38
|
-
Devise.remote_user_attribute_map.inject({}) { |h, (k, v)| h[k] = env[v] if env.has_key?(v); h }
|
11
|
+
manager = DeviseRemoteUser::Manager.new(env)
|
12
|
+
manager.find_or_create_user
|
39
13
|
end
|
40
14
|
|
41
15
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise-remote-user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Chandek-Stark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: A devise extension for remote user authentication.
|
42
56
|
email:
|
43
57
|
- lib-drs@duke.edu
|
@@ -48,6 +62,7 @@ extra_rdoc_files:
|
|
48
62
|
- README.md
|
49
63
|
files:
|
50
64
|
- .gitignore
|
65
|
+
- .rspec
|
51
66
|
- CONTRIBUTING.md
|
52
67
|
- Gemfile
|
53
68
|
- LICENSE
|
@@ -55,9 +70,11 @@ files:
|
|
55
70
|
- Rakefile
|
56
71
|
- devise_remote_user.gemspec
|
57
72
|
- lib/devise_remote_user.rb
|
73
|
+
- lib/devise_remote_user/manager.rb
|
58
74
|
- lib/devise_remote_user/model.rb
|
59
75
|
- lib/devise_remote_user/strategy.rb
|
60
76
|
- lib/devise_remote_user/version.rb
|
77
|
+
- spec/spec_helper.rb
|
61
78
|
homepage: http://github.com/duke-libraries/devise-remote-user
|
62
79
|
licenses:
|
63
80
|
- BSD
|