ssomg 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7a915f1e8e7d224b1fbd808064f43a75fe8f296
4
- data.tar.gz: 56e665119602b870bd1cb5c78d731f181a340f79
3
+ metadata.gz: 1ee8b1cd9c36768547fd3d9e1511bb1df13fcbce
4
+ data.tar.gz: 3fc17a322be2b7bb821a6c63852d8c3b1048e6b1
5
5
  SHA512:
6
- metadata.gz: 9fd23c9ba6eb3f36f34acea4a82aabbae6481c960c7512f64b33b42a3508633215b6281aeccf2984f9047395380d506c66e42f0dfe98f8ce49d0ae0927f00dbd
7
- data.tar.gz: 399397abab8dc293116ef5965a47193b13c5d333b8afa71a4411a5a664cfd179e930921365f0a8762520cc1f8ea8a443023be39ed2f47aef83451c55c86265cd
6
+ metadata.gz: a27bfae2520e1b5cc6c6c47c67386e1aea8eebd73da5ac801d14b667d4286199063c06fcd505e92bdc3573fe613f02fd104afe6a990d901086aa8c366347ac90
7
+ data.tar.gz: 41d6e2cd1cb628d370f3250aac0587fefa97db46aaa97b62a010ed4439a5a733cd87dd2a5e6eae75c0f3be4a9a805ed9d7dcb63b077442ab265c7bb268d89452
@@ -0,0 +1,16 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Ssomg
5
+ class ApiController < RootController
6
+
7
+ before_action :register_user, unless: -> { request.query_parameters["token"] }
8
+
9
+ private
10
+
11
+ def get_token
12
+ bearer_token
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,143 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Ssomg
5
+ class RootController < ::ActionController::Base
6
+
7
+ before_action :register_user, unless: -> { request.query_parameters["token"] }
8
+
9
+ private
10
+
11
+ def register_user
12
+ token = get_token
13
+ if( token )
14
+ begin
15
+ decoded_token = ::JWT.decode token, Ssomg.PUB_KEY, true, { algorithm: 'RS256' }
16
+ @user = decoded_token[ 0 ]
17
+ rescue ::JWT::ExpiredSignature
18
+ if ( cookies["ssomg"] )
19
+ begin
20
+ decoded_token = ::JWT.decode token, Ssomg.PUB_KEY, true, { exp_leeway: 432000, algorithm: 'RS256' } #5 day leeway to ensure token is read
21
+ accessTokens = JSON.parse refresh( decoded_token[ 0 ]["refresh_token"] )
22
+ userToken = accessTokens[ENV["APP_ID"]]
23
+ cookies["ssomg" ] = { :value => accessTokens[ENV["APP_ID"]], :secure => Rails.env.production?, :httponly => true }
24
+ withoutMain = accessTokens.except!( ENV["APP_ID"] )
25
+ cookies["ssomg_all" ] = { :value => withoutMain.keys.join(","), :secure => Rails.env.production?, :httponly => true }
26
+ withoutMain.each { |key, value|
27
+ cookies["ssomg_" + key ] = { :value => value, :secure => Rails.env.production?, :httponly => true }
28
+ }
29
+ decoded_user = ::JWT.decode userToken, Ssomg.PUB_KEY, true, { algorithm: 'RS256' }
30
+ @user = decoded_user[ 0 ]
31
+ rescue StandardError => e
32
+ # raise e
33
+ end
34
+ else
35
+ cookies["ssomg_meta" ] = { :value => request.original_url, :secure => Rails.env.production?, :httponly => true }
36
+ go_to_provider
37
+ end
38
+ rescue StandardError => e
39
+ # raise e
40
+ end
41
+ end
42
+ end
43
+
44
+ def verify_token
45
+ if request.query_parameters["token"]
46
+ accessTokens = JSON.parse refresh( request.query_parameters["token"] )
47
+ cookies["ssomg" ] = { :value => accessTokens[ENV["APP_ID"]], :secure => Rails.env.production?, :httponly => true }
48
+ withoutMain = accessTokens.except!( ENV["APP_ID"] )
49
+ cookies["ssomg_all" ] = { :value => withoutMain.keys.join(","), :secure => Rails.env.production?, :httponly => true }
50
+ withoutMain.each { |key, value|
51
+ cookies["ssomg_" + key ] = { :value => value, :secure => Rails.env.production?, :httponly => true }
52
+ }
53
+ if ( cookies["ssomg_meta"] )
54
+ path = cookies["ssomg_meta"]
55
+ cookies.delete "ssomg_meta"
56
+ redirect_to path and return
57
+ end
58
+ end
59
+ end
60
+
61
+ def protect( roles )
62
+ if ( @user )
63
+ if !roles.kind_of?(Array)
64
+ roles = [ roles ]
65
+ end
66
+ authorised = false;
67
+ for role in roles
68
+ if ( @user["roles"].include? role )
69
+ authorised = true
70
+ break
71
+ end
72
+ end
73
+ if ( !authorised )
74
+ head(403) and return
75
+ end
76
+ else
77
+ cookies["ssomg_meta" ] = { :value => request.original_url, :secure => Rails.env.production?, :httponly => true }
78
+ go_to_provider
79
+ end
80
+
81
+ end
82
+
83
+ def refresh( token )
84
+ begin
85
+ uri = URI(ENV["SSO_HOST"] + "/auth/sso")
86
+ http = Net::HTTP.new(uri.host, uri.port )
87
+ req = Net::HTTP::Post.new(uri.path, {'Content-Type' =>'application/json'})
88
+ if uri.scheme == "https"
89
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
90
+ http.use_ssl = true
91
+ end
92
+ req.body = { :token => token }.to_json
93
+ res = http.request(req)
94
+ jwt = res.body
95
+ return jwt
96
+ rescue StandardError => e
97
+ # puts "failed #{e}"
98
+ end
99
+ end
100
+
101
+ def refresh_silent token
102
+ begin
103
+ uri = URI(ENV["SSO_HOST"] + "/auth/sso")
104
+ http = Net::HTTP.new(uri.host, uri.port )
105
+ req = Net::HTTP::Post.new(uri.path, {'Content-Type' =>'application/json'})
106
+ if uri.scheme == "https"
107
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
108
+ http.use_ssl = true
109
+ end
110
+ req.body = { :token => token }.to_json
111
+ res = http.request(req)
112
+ jwt = res.body
113
+ return jwt
114
+ rescue StandardError => e
115
+ end
116
+ end
117
+
118
+ def bearer_token
119
+ pattern = /^Bearer /
120
+ header = request.headers['Authorization']
121
+ header.gsub(pattern, '') if header && header.match(pattern)
122
+ end
123
+
124
+ def clear_linked_cookies
125
+ if cookies["ssomg_all"]
126
+ all_cookies = cookies["ssomg_all"].split(",")
127
+ all_cookies.each { |key| cookies.delete "ssomg_" + key }
128
+ cookies.delete "ssomg_all"
129
+ end
130
+ end
131
+
132
+ def clear_cookies
133
+ cookies.delete "ssomg_meta"
134
+ cookies.delete "ssomg"
135
+ end
136
+
137
+ def go_to_provider
138
+ clear_linked_cookies
139
+ redirect_to ENV["SSO_HOST"] + "/auth/login?app_id=" + ENV["APP_ID"] and return
140
+ end
141
+
142
+ end
143
+ end
data/lib/ssomg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ssomg
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssomg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry McIntosh
@@ -83,8 +83,10 @@ files:
83
83
  - bin/setup
84
84
  - lib/ssomg.rb
85
85
  - lib/ssomg/config/routes.rb
86
+ - lib/ssomg/controllers/api_controller.rb
86
87
  - lib/ssomg/controllers/auth_controller.rb
87
88
  - lib/ssomg/controllers/base_controller.rb
89
+ - lib/ssomg/controllers/root_controller.rb
88
90
  - lib/ssomg/engine.rb
89
91
  - lib/ssomg/version.rb
90
92
  - ssomg.gemspec