gds-sso 13.4.0 → 13.5.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: 4c393c1d9c9bba89a0d99faa3c8bc93d3288865c
4
- data.tar.gz: 43137c891d2a30c618b7c73294378b422b5abd37
3
+ metadata.gz: 72fc2354ba2f54ba192293b50861f8aff690edca
4
+ data.tar.gz: eeaa7c1c30baa2bec9190cf962dd7c83ac042d63
5
5
  SHA512:
6
- metadata.gz: 17c5755ed0581e06fb11ca975372b962d3b62d1521c724753c16e7502c3236c7f52056bd8e871aef13eb5df7c1a2f49e8d23a777f16bcc144660b188372141aa
7
- data.tar.gz: 5e60cbaa4fe46ab34429aa1213afa9c9ba6c6caa9f1940d99abce10dec9d9aa344b6640bddfbeede3a1209f1902321d9d3a6b932b4cd302583cb4158943dae85
6
+ metadata.gz: 149b0ebb611792c93e615537564092f9f4d5fb212f552a9b041134be095a1769643ca4e02a9ec49117dc74f2965b4ad2c53adf9c4a16574425040ed4a63edd48
7
+ data.tar.gz: f9f0a816379772d203bc9171ff523dd9fe2bb0918c60920879820823dad4204823048de7b9d3f78fe87a9ae5bab7b0371c1bcabc9278037628efa9a494590b46
data/README.md CHANGED
@@ -12,7 +12,7 @@ Some of the applications that use this gem:
12
12
 
13
13
  ## Usage
14
14
 
15
- ### Integration with a Rails 3+ app
15
+ ### Integration with a Rails 4+ app
16
16
 
17
17
  To use gds-sso you will need an oAuth client ID and secret for Signon or a compatible system.
18
18
  These can be provided by one of the team with admin access to Signon.
@@ -130,6 +130,18 @@ GDS::SSO.config do |config|
130
130
  end
131
131
  ```
132
132
 
133
+ If you are using a Rails 5 app in
134
+ [api_only](http://guides.rubyonrails.org/api_app.html) mode this gem will
135
+ automatically disable the oauth layers which use session persistence. You can
136
+ configure this gem to be in api_only mode (or not) with:
137
+
138
+ ```ruby
139
+ GDS::SSO.config do |config|
140
+ # ...
141
+ # Only support bearer token authentication and send responses in JSON
142
+ config.api_only = true
143
+ end
144
+ ```
133
145
 
134
146
  ### Use in development mode
135
147
 
data/config/routes.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  Rails.application.routes.draw do
2
+ next if GDS::SSO::Config.api_only?
2
3
  get '/auth/gds/callback', to: 'authentications#callback', as: :gds_sign_in
3
4
  get '/auth/gds/sign_out', to: 'authentications#sign_out', as: :gds_sign_out
4
5
  get '/auth/failure', to: 'authentications#failure', as: :auth_failure
@@ -23,6 +23,8 @@ module GDS
23
23
  mattr_accessor :cache
24
24
  @@cache = ActiveSupport::Cache::NullStore.new
25
25
 
26
+ mattr_writer :api_only
27
+
26
28
  def self.user_klass
27
29
  user_model.to_s.constantize
28
30
  end
@@ -36,6 +38,12 @@ module GDS
36
38
 
37
39
  ENV.fetch("GDS_SSO_STRATEGY", default_strategy) == "mock"
38
40
  end
41
+
42
+ def self.api_only?
43
+ config = Rails.configuration
44
+ default = config.respond_to?(:api_only) ? config.api_only : false
45
+ @@api_only.nil? ? default : @@api_only
46
+ end
39
47
  end
40
48
  end
41
49
  end
@@ -6,10 +6,17 @@ module GDS
6
6
 
7
7
  def self.included(base)
8
8
  base.rescue_from PermissionDeniedException do |e|
9
- render "authorisations/unauthorised", layout: "unauthorised", status: :forbidden, locals: { message: e.message }
9
+ if GDS::SSO::Config.api_only?
10
+ render json: { message: e.message }, status: :forbidden
11
+ else
12
+ render "authorisations/unauthorised", layout: "unauthorised", status: :forbidden, locals: { message: e.message }
13
+ end
14
+ end
15
+
16
+ unless GDS::SSO::Config.api_only?
17
+ base.helper_method :user_signed_in?
18
+ base.helper_method :current_user
10
19
  end
11
- base.helper_method :user_signed_in?
12
- base.helper_method :current_user
13
20
  end
14
21
 
15
22
 
@@ -8,11 +8,18 @@ module GDS
8
8
  class FailureApp < ActionController::Metal
9
9
  include ActionController::UrlFor
10
10
  include ActionController::Redirecting
11
+ include AbstractController::Rendering
12
+ include ActionController::Rendering
13
+ include ActionController::Renderers
14
+ use_renderers :json
15
+
11
16
  include Rails.application.routes.url_helpers
12
17
 
13
18
  def self.call(env)
14
- if ::GDS::SSO::ApiAccess.api_call?(env)
15
- [ 401, {'WWW-Authenticate' => %(Bearer error="invalid_token") }, [] ]
19
+ if GDS::SSO::ApiAccess.api_call?(env)
20
+ action(:api_invalid_token).call(env)
21
+ elsif GDS::SSO::Config.api_only?
22
+ action(:api_missing_token).call(env)
16
23
  else
17
24
  action(:redirect).call(env)
18
25
  end
@@ -23,6 +30,14 @@ module GDS
23
30
  redirect_to '/auth/gds'
24
31
  end
25
32
 
33
+ def api_invalid_token
34
+ api_unauthorized('Bearer token does not appear to be valid', 'invalid_token')
35
+ end
36
+
37
+ def api_missing_token
38
+ api_unauthorized('No bearer token was provided', 'invalid_request')
39
+ end
40
+
26
41
  # Stores requested uri to redirect the user after signing in. We cannot use
27
42
  # scoped session provided by warden here, since the user is not authenticated
28
43
  # yet, but we still need to store the uri based on scope, so different scopes
@@ -33,6 +48,12 @@ module GDS
33
48
  session["return_to"] = request.env['warden.options'][:attempted_path] if request.get?
34
49
  end
35
50
 
51
+ private
52
+
53
+ def api_unauthorized(message, bearer_error)
54
+ headers['WWW-Authenticate'] = %(Bearer error="#{bearer_error}")
55
+ render json: { message: message }, status: :unauthorized
56
+ end
36
57
  end
37
58
  end
38
59
  end
@@ -1,5 +1,5 @@
1
1
  module GDS
2
2
  module SSO
3
- VERSION = "13.4.0"
3
+ VERSION = "13.5.0"
4
4
  end
5
5
  end
data/lib/gds-sso.rb CHANGED
@@ -25,6 +25,7 @@ module GDS
25
25
  config.before_eager_load { |app| app.reload_routes! }
26
26
 
27
27
  config.app_middleware.use ::OmniAuth::Builder do
28
+ next if GDS::SSO::Config.api_only?
28
29
  provider :gds, GDS::SSO::Config.oauth_id, GDS::SSO::Config.oauth_secret,
29
30
  client_options: {
30
31
  site: GDS::SSO::Config.oauth_root_url,