cas-client 0.1.3 → 0.2.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
  SHA256:
3
- metadata.gz: 3dec8e7b15d53fc005cf9b9d4d3f5bf39e1e0cf7c5df64387dd2e2615bb89bcc
4
- data.tar.gz: 305516439c3dadc49a90cd02c27a8d65868ea8ae1b020e23c7306e10cbf165c8
3
+ metadata.gz: cd0f12781655ef893c92fb9b5275ccc32e42b1304d82eb8f13cab07b4a458fb8
4
+ data.tar.gz: 92277075f6dc6fc4a1507a36baf5ea0a8e1d3380ce940fdaad3abc14dc138a85
5
5
  SHA512:
6
- metadata.gz: 46b9c8224cc102f852bb2f7aded2810b4701ed662b6477812095612acd28921c3d25d2a28e452af210702b6d80e035674f4fabbc5f626572f4bd522d8ea9c9ce
7
- data.tar.gz: ad0f99174aa6ceeea27ee2b60f756539cef7db143f5f3cd133b72d602ae9090645c7fc9ae3ebc28352466f3d8ce64b9067fd5c5cb4bb692ac98869cee46e3c9c
6
+ metadata.gz: 2e003ef0e3ccdcf50de97fbf30abdc0aba1a2d267fac0ab393191b0faa4a680313920eb304a5dd6d3fb7700ae6f787e3e906368d6e0345113952228480d5bb97
7
+ data.tar.gz: d6d0a79b004c54ad886660efa7618f89e576802b0a79ccc201cc2ed77f19f58a7a05016c81bf702c4e320e6e01bf84f652b9e4bc6930c34a5d2206a57a8c5911
data/README.md CHANGED
@@ -9,7 +9,7 @@ TODO: Delete this and the text above, and describe your gem
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'cas_client'
12
+ gem 'cas-client'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -18,17 +18,25 @@ And then execute:
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install cas_client
21
+ $ gem install cas-client
22
22
 
23
23
  ## Usage
24
24
 
25
25
  Configure middleware from the specific environment file
26
26
 
27
27
  ```ruby
28
- config.middleware.insert_after ActionDispatch::Session::CookieStore, Cas::Client::Middleware, {
29
- server_url: "https://idpqa.americanfinancing.net",
30
- extra_attributes: [:first_name, :last_name, :email, :external_id]
31
- }
28
+ config.middleware.insert_after ActionDispatch::Session::CookieStore, Cas::Client::Middleware do |config|
29
+ config.server_url = "https://staging.cas-server"
30
+ config.extra_attributes = [:first_name, :last_name, :email]
31
+ end
32
+ ```
33
+
34
+ Configure global/non-environment specific options
35
+
36
+ ```ruby
37
+ Cas::Client.configure do |config|
38
+ config.extra_attributes = [:first_name, :last_name, :email]
39
+ end
32
40
  ```
33
41
 
34
42
  ## Development
@@ -4,8 +4,17 @@ require 'cas/client/response'
4
4
  require 'cas/client/server'
5
5
  require 'cas/client/url'
6
6
  require 'cas/client/version'
7
+ require 'cas/client/configuration'
7
8
 
8
9
  module Cas
9
10
  module Client
11
+ class << self
12
+ attr_accessor :configuration
13
+
14
+ def configure
15
+ self.configuration ||= Configuration.new
16
+ yield configuration
17
+ end
18
+ end
10
19
  end
11
20
  end
@@ -0,0 +1,12 @@
1
+ module Cas
2
+ module Client
3
+ class Configuration
4
+ attr_accessor :cas_namespace, :extra_attributes, :server_url
5
+
6
+ def initialize
7
+ @extra_attributes = []
8
+ @cas_namespace = 'cas'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -3,25 +3,25 @@ require 'net/http'
3
3
  module Cas
4
4
  module Client
5
5
  class Middleware
6
- def initialize(app, config={})
6
+ def initialize(app, &block)
7
7
  @app = app
8
- @config = config
9
- @config[:extra_attributes] = [] if config[:extra_attributes].nil?
10
- @request = nil
8
+
9
+ Cas::Client.configure(&block) if block_given?
11
10
  end
12
11
 
13
12
  def call(env)
14
13
  @request = Rack::Request.new(env)
15
- server = Cas::Client::Server.new(@config[:server_url])
16
14
  status, headers, rack_body = @app.call(env)
17
- log(env, "Middleware called. Status: #{status}, Headers: #{headers}")
18
15
 
19
16
  if ticket_validation?
20
- attributes = server.validate_service(self_url(@request), ticket_param, {extra_attributes: @config[:extra_attributes]})
21
- set_session(@request, attributes)
22
- return redirect_to(self_url(@request))
17
+ attributes = server.validate_service(self_url, ticket_param)
18
+ set_session(attributes)
19
+
20
+ return redirect_to(self_url)
23
21
  elsif status == 401
24
- return redirect_to(server.login_url({service_url: self_url(@request)}))
22
+ log(env, "Cas::Client::Middleware detected 401, Status: #{status}, Headers: #{headers}\n")
23
+
24
+ return redirect_to(server.login_url({ service_url: self_url }))
25
25
  else
26
26
  return [status, headers, rack_body]
27
27
  end
@@ -29,28 +29,32 @@ module Cas
29
29
 
30
30
  private
31
31
 
32
- def set_session(req, attributes)
33
- req.session['cas'] = attributes
32
+ def server
33
+ @_server ||= Cas::Client::Server.new
34
+ end
35
+
36
+ def set_session(attributes)
37
+ @request.session['cas'] = attributes
34
38
  end
35
39
 
36
40
  def redirect_to(url, status=302)
37
41
  [ status, { 'Location' => url, 'Content-Type' => 'text/plain' }, ["Redirecting you to #{url}"] ]
38
42
  end
39
43
 
40
- def self_url(req)
41
- req.url.split('?')[0]
44
+ def self_url
45
+ @request.url.split('?')[0]
42
46
  end
43
47
 
44
48
  def ticket_validation?
45
- !!(@request.get? && ticket_param && ticket_param.to_s =~ /\AST\-[^\s]{1,253}\Z/)
49
+ @request.get? && param_service_ticket?
46
50
  end
47
51
 
48
52
  def ticket_param
49
53
  @request.params['ticket']
50
54
  end
51
55
 
52
- def xml_namespace
53
- @config[:cas_namespace] || 'cas'
56
+ def param_service_ticket?
57
+ ticket_param.to_s =~ /\AST\-[^\s]{1,253}\Z/
54
58
  end
55
59
 
56
60
  def log(env, message, level = :info)
@@ -11,21 +11,21 @@ module Cas
11
11
  end
12
12
 
13
13
  def success?
14
- @response.to_s.match(/<cas:authenticationSuccess>/).captures.length > 0
14
+ @response.to_s.match(/<#{xml_namespace}:authenticationSuccess>/)
15
15
  end
16
16
 
17
- def all_attributes(extra_attributes)
18
- {}.merge!(get_user(@response)).merge!(get_extra_attributes(@response, extra_attributes))
17
+ def all_attributes
18
+ get_user.merge!(get_extra_attributes)
19
19
  end
20
20
 
21
21
  protected
22
22
 
23
23
  def xml_namespace
24
- "cas"
24
+ Cas::Client.configuration.cas_namespace
25
25
  end
26
26
 
27
- def get_user(res)
28
- match = res.match(/<#{xml_namespace}:user>(.*)<\/#{xml_namespace}:user>/)
27
+ def get_user
28
+ match = @response.match(/<#{xml_namespace}:user>(.*)<\/#{xml_namespace}:user>/)
29
29
  if match
30
30
  { user: match.captures.first }
31
31
  else # Failed login
@@ -33,15 +33,18 @@ module Cas
33
33
  end
34
34
  end
35
35
 
36
- def get_extra_attributes(res, extra_attributes)
37
- attributes = {}
38
- extra_attributes.each do |ea|
39
- match = res.match(/<#{xml_namespace}:#{ea}>(.*)<\/#{xml_namespace}:#{ea}>/)
40
- if match
41
- attributes[ea] = match.captures.first
36
+ def get_extra_attributes
37
+ {}.tap do |attributes|
38
+ Cas::Client.configuration.extra_attributes.each do |ea|
39
+ match = @response.match(/<#{xml_namespace}:#{ea}>(.*)<\/#{xml_namespace}:#{ea}>/)
40
+
41
+ if match
42
+ attributes[ea] = match.captures.first
43
+ else
44
+ attributes
45
+ end
42
46
  end
43
47
  end
44
- attributes
45
48
  end
46
49
  end
47
50
  end
@@ -1,7 +1,7 @@
1
1
  module Cas
2
2
  module Client
3
3
  class Server
4
- def initialize(server_url)
4
+ def initialize
5
5
  @url = Cas::Client::URL.new(server_url)
6
6
  end
7
7
 
@@ -21,15 +21,23 @@ module Cas
21
21
  end
22
22
  end
23
23
 
24
- def validate_service(service_url, ticket, option={})
24
+ def self.logout_url(options={})
25
+ new.logout_url(options).to_s
26
+ end
27
+
28
+ def validate_service(service_url, ticket)
25
29
  uri = Cas::Client::URL.new(validate_service_url(service_url, ticket)).to_uri
26
30
  res = Cas::Client::Response.new(uri)
27
31
  res.validate_service_response
28
- res.all_attributes(option[:extra_attributes]||[])
32
+ res.all_attributes
29
33
  end
30
34
 
31
35
  protected
32
36
 
37
+ def server_url
38
+ Cas::Client.configuration.server_url
39
+ end
40
+
33
41
  def validate_service_url(service_url, ticket)
34
42
  protocol_path = "p3"
35
43
  server_url = @url
@@ -1,5 +1,5 @@
1
1
  module Cas
2
2
  module Client
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cas-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donavan White
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-29 00:00:00.000000000 Z
11
+ date: 2018-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -86,6 +86,7 @@ files:
86
86
  - bin/setup
87
87
  - cas-client.gemspec
88
88
  - lib/cas-client.rb
89
+ - lib/cas/client/configuration.rb
89
90
  - lib/cas/client/middleware.rb
90
91
  - lib/cas/client/response.rb
91
92
  - lib/cas/client/server.rb