browserid-rails 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,8 +34,8 @@ Configuration settings are properties of `config.browserid`.
34
34
 
35
35
  * `user_model` - The name of the ActiveModel class for application users.
36
36
  The default is `"User"`.
37
- * `email_field` - The name of the attribute on the user model which contains
38
- the user's email. The default is `"email"`.
37
+ * `email_field` - The attribute of the user model which contains the user's
38
+ email. The default is `:email`.
39
39
  * `session_variable` - The location the authenticated email is stored in the
40
40
  client's session. The default is `:browserid_email`.
41
41
  * `verifier` - The type of verifier to use to authenticate client BrowserID
@@ -52,6 +52,13 @@ Configuration settings are properties of `config.browserid`.
52
52
  a minor security risk. In production, this should be configured to a fixed
53
53
  value.
54
54
 
55
+ Additionally, there are two sub-structures `login` and `logout` for configuring
56
+ the associated paths and default link text. They have the following properties:
57
+
58
+ * `text` - The default text to give login and logout links.
59
+ * `path` - The target to give links and the path to `POST` authentication
60
+ requests to. Defaults to `"/login"` and `"/logout"` respectively.
61
+
55
62
  ### Controller Integration
56
63
 
57
64
  The `BrowserID::Rails::Base` module makes several controller methods available
@@ -119,15 +126,13 @@ of ways to control its behavior:
119
126
 
120
127
  Once that's accomplished, the app is ready to use BrowserID for authentication.
121
128
  To add login and logout links to the site, use the `login_link` and
122
- `logout_link` helpers. These accept optional link text and targets as parameters:
129
+ `logout_link` helpers. These accept an optional link text as a parameter:
123
130
 
124
- <%= login_link "Login with Persona" %>
131
+ <%= logout_link %>
125
132
 
126
- <%= login_link "Login", auth_path %>
133
+ <%= login_link "Login with Persona" %>
127
134
 
128
- If the path is not provided, the link helpers will use `login_path` and
129
- `logout_path` if they are available, otherwise the link targets will be `#`.
130
- The coffeescript assets add on-click handlers to the links which trigger the
135
+ The coffeescript asset adds on-click handlers to the links which trigger the
131
136
  Persona code to request new assertions or destroy existing ones.
132
137
 
133
138
  TODO: include Persona branding assets
@@ -25,26 +25,26 @@
25
25
  # default, it reloads the current page.
26
26
  onLogin: (data, status, xhr) ->
27
27
  if @debug
28
- alert("Login result: #{status} #{data}")
28
+ alert("Login: #{status}\n#{data}")
29
29
  else
30
30
  window.location.reload()
31
31
 
32
32
  # Public: This method is called when a user fails to authenticate.
33
33
  onLoginError: (xhr, status, err) ->
34
- alert("Login #{err} - #{xhr.responseText}")
34
+ alert("Login: #{status} #{err}\n#{xhr.responseText}")
35
35
 
36
36
  # Public: This method is called when a user clears their authentication. By
37
37
  # default, it reloads the current page.
38
38
  onLogout: (data, status, xhr) ->
39
39
  if @debug
40
- alert("Logout result: #{status} #{data}")
40
+ alert("Logout: #{status}\n#{data}")
41
41
  else
42
42
  window.location.reload()
43
43
 
44
44
  # Public: This method is called when a user fails to clear their
45
45
  # authentication.
46
46
  onLogoutError: (xhr, status, err) ->
47
- alert("Logout #{err} - #{xhr.responseText}")
47
+ alert("Logout: #{status} #{err}\n#{xhr.responseText}")
48
48
 
49
49
 
50
50
  ### INITIALIZATION ###
@@ -8,22 +8,37 @@ module BrowserID
8
8
  # the library methods. The presence of this engine also causes assets to
9
9
  # be included when the gem is added as a dependency.
10
10
  class Engine < ::Rails::Engine
11
+ # Initialize the engine configuration.
11
12
  config.before_configuration do
12
- BrowserIDConfig = Struct.new :user_model, :email_field, :session_variable, :verifier, :audience
13
+ BrowserIDConfig = Struct.new :user_model, :email_field, :session_variable, :verifier, :audience, :login, :logout
14
+ BrowserIDLinkConfig = Struct.new :text, :path
13
15
 
14
- config.browserid = BrowserIDConfig.new
15
- config.browserid.user_model = 'User'
16
- config.browserid.email_field = 'email'
17
- config.browserid.session_variable = :browserid_email
18
- config.browserid.verifier = :persona
19
- # config.browserid.audience should only be set in production
16
+ config.browserid = BrowserIDConfig.new.tap do |cfg|
17
+ cfg.user_model = 'User'
18
+ cfg.email_field = :email
19
+ cfg.session_variable = :browserid_email
20
+ cfg.verifier = :persona
21
+ # audience should only be set in production
22
+
23
+ cfg.login = BrowserIDLinkConfig.new.tap do |link|
24
+ link.text = "Login"
25
+ link.path = '/login'
26
+ end
27
+
28
+ cfg.logout = BrowserIDLinkConfig.new.tap do |link|
29
+ link.text = "Logout"
30
+ link.path = '/logout'
31
+ end
32
+ end
20
33
  end
21
34
 
22
- initializer "browserid-rails.extend" do |app|
35
+ # Mix in the controller and view helper methods.
36
+ config.before_initialize do
23
37
  ActionController::Base.send :include, BrowserID::Rails::Base
24
38
  ActionView::Base.send :include, BrowserID::Rails::Helpers
25
39
  end
26
40
 
41
+ # Create the assertion verifier.
27
42
  config.after_initialize do
28
43
  cfg = config.browserid
29
44
 
@@ -13,7 +13,7 @@ module BrowserID
13
13
  #
14
14
  # base - The Class this module is being included in.
15
15
  def self.included(base)
16
- base.send :helper_method, :browserid_email, :current_user, :authenticated?
16
+ base.send :helper_method, :browserid_config, :browserid_email, :current_user, :authenticated?
17
17
  end
18
18
 
19
19
  # Internal: Gets the application configuration for this gem.
@@ -25,6 +25,44 @@ module BrowserID
25
25
 
26
26
 
27
27
 
28
+ ##### HELPER METHODS #####
29
+
30
+ # Public: Gets the email address of the currently-authenticated user.
31
+ #
32
+ # Returns the authenticated email address String.
33
+ def browserid_email
34
+ session[browserid_config.session_variable]
35
+ end
36
+
37
+ # Public: Retrieves the user for the authenticated email address. This
38
+ # method uses the `browserid.user_model` and `browserid.email_field`
39
+ # config settings, which default to `User` and `email`.
40
+ #
41
+ # Returns the current authenticated user, or nil if no user exists.
42
+ def current_user
43
+ if browserid_email.nil?
44
+ nil
45
+ elsif @current_user
46
+ @current_user
47
+ else
48
+ config = browserid_config
49
+ user_model = config.user_model.constantize
50
+ find_method = "find_by_#{config.email_field}".intern
51
+
52
+ @current_user = user_model.send find_method, browserid_email
53
+ end
54
+ end
55
+
56
+ # Public: Determines whether the current client is authenticated as a
57
+ # registered User.
58
+ #
59
+ # Returns true if the client is authenticated and registered.
60
+ def authenticated?
61
+ !current_user.nil?
62
+ end
63
+
64
+
65
+
28
66
  ##### AUTHENTICATION METHODS #####
29
67
 
30
68
  # Public: Sets the given email address as the currently-authenticated user.
@@ -84,47 +122,10 @@ module BrowserID
84
122
  head :ok
85
123
  end
86
124
  rescue StandardError => e
125
+ # TODO: distinguish between process failures and invalid assertions
87
126
  logger.warn "Failed to verify BrowserID assertion: #{e.message}"
88
127
  render status: :forbidden, text: e.message
89
128
  end
90
-
91
-
92
-
93
- ##### HELPER METHODS #####
94
-
95
- # Public: Gets the email address of the currently-authenticated user.
96
- #
97
- # Returns the authenticated email address String.
98
- def browserid_email
99
- session[browserid_config.session_variable]
100
- end
101
-
102
- # Public: Retrieves the user for the authenticated email address. This
103
- # method uses the `browserid.user_model` and `browserid.email_field`
104
- # config settings, which default to `User` and `email`.
105
- #
106
- # Returns the current authenticated user, or nil if no user exists.
107
- def current_user
108
- if browserid_email.nil?
109
- nil
110
- elsif @current_user
111
- @current_user
112
- else
113
- config = browserid_config
114
- user_model = config.user_model.constantize
115
- find_method = "find_by_#{config.email_field}".intern
116
-
117
- @current_user = user_model.send find_method, browserid_email
118
- end
119
- end
120
-
121
- # Public: Determines whether the current client is authenticated as a
122
- # registered User.
123
- #
124
- # Returns true if the client is authenticated and registered.
125
- def authenticated?
126
- !current_user.nil?
127
- end
128
129
  end
129
130
  end
130
131
  end
@@ -7,9 +7,11 @@ module BrowserID
7
7
  #
8
8
  # options - Hash used to adjust the browserid asset setup (default: {}).
9
9
  # :login_path - String giving the path to POST assertions to
10
- # for verification.
10
+ # for verification. Defaults to the configured
11
+ # `browserid.login.path`.
11
12
  # :logout_path - String giving the path to POST logout
12
- # notifications to.
13
+ # notifications to. Defaults to the configured
14
+ # `browserid.logout.path`.
13
15
  # :debug - Boolean determining whether the browserid
14
16
  # javascript will refresh the page or show an
15
17
  # alert dialog.
@@ -32,29 +34,32 @@ module BrowserID
32
34
  # <% end %>
33
35
  #
34
36
  def setup_browserid(options={}, &block)
37
+ defaults = { login_path: browserid_config.login.path, logout_path: browserid_config.logout.path }
35
38
  content_for :browserid_setup, capture(&block) if block_given?
36
- render 'layouts/browserid', options: options
39
+ render 'layouts/browserid', options: defaults.merge(options)
37
40
  end
38
41
 
39
42
  # Public: Renders a login link which will request a new authentication
40
- # assertion from the BrowserID javascript code.
43
+ # assertion from the BrowserID javascript code. The default link text is
44
+ # configurable with `config.browserid.login.text`. The link target is
45
+ # similarly configurable with `config.browserid.login.path`.
41
46
  #
42
- # text - String to use as link text (default: 'Login').
43
- # path - String path to link to. If not provided, the `login_path` helper
44
- # will be used if it exists. Otherwise, the link will be to '#'.
45
- def login_link(text="Login", path=nil)
46
- target = path || respond_to?(:login_path) && login_path || '#'
47
+ # text - Optional String to use as link text (default: configured value).
48
+ def login_link(text=nil)
49
+ text ||= browserid_config.login.text
50
+ target = browserid_config.login.path || '#'
47
51
  link_to text, target, class: :browserid_login
48
52
  end
49
53
 
50
54
  # Public: Renders a logout link which will clear the current BrowserID
51
- # authentication status.
55
+ # authentication status. The default link text is configurable with
56
+ # `config.browserid.logout.text`. The link target is similarly
57
+ # configurable with `config.browserid.logout.path`.
52
58
  #
53
- # text - String to use as link text (default: 'Logout').
54
- # path - String path to link to. If not provided, the `logout_path` helper
55
- # will be used if it exists. Otherwise, the link will be to '#'.
56
- def logout_link(text="Logout", path=nil)
57
- target = path || respond_to?(:logout_path) && logout_path || '#'
59
+ # text - Optional String to use as link text (default: configured value).
60
+ def logout_link(text=nil)
61
+ text ||= browserid_config.logout.text
62
+ target = browserid_config.logout.path || '#'
58
63
  link_to text, target, class: :browserid_logout
59
64
  end
60
65
  end
@@ -1,5 +1,5 @@
1
1
  module BrowserID
2
2
  module Rails
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browserid-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.1'
21
+ version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,71 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '3.1'
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.11'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.11'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: tzinfo
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: jquery-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
30
94
  description:
31
95
  email:
32
96
  - greg@mvxcvi.com
@@ -57,7 +121,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
121
  version: '0'
58
122
  segments:
59
123
  - 0
60
- hash: -488449078399574217
124
+ hash: 3271099203233204299
61
125
  required_rubygems_version: !ruby/object:Gem::Requirement
62
126
  none: false
63
127
  requirements:
@@ -66,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
130
  version: '0'
67
131
  segments:
68
132
  - 0
69
- hash: -488449078399574217
133
+ hash: 3271099203233204299
70
134
  requirements: []
71
135
  rubyforge_project:
72
136
  rubygems_version: 1.8.24