warden_cookie_session 0.1.0.18557

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 46a015924a170feeea0d09f25da23bc658db4ba2588b8bb1f2c7f6d19c7c2471
4
+ data.tar.gz: b2b6c4ac6f14348f28dc277498016c7c435eee4f599c6d6a2ce71b9df9591abb
5
+ SHA512:
6
+ metadata.gz: 05305ede70cd30e474c833c53abb1ed332145606139f829be05366641cd199f4dd15fd91e6594c060184b401019b7cc4088decef1a57e4807705e0d370143d32
7
+ data.tar.gz: c92c8c6e667722973dc3b5ae1dc288a9f790941966c8f1f234e0180e1fed354a47634db4d21361f91b93894a91b3c1eea4f09f064baa21c4dd321b7f4c26733f
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014-2019 Рнд Софт
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Warden Cookie Session
2
+
3
+ Warden Cookie Session is a warden strategy to store auth in custom encrypted cookie(instead of rack:session).
4
+ The main puprpose to allow store authorization between multiple rails applications, without sharing `secret_key_base`.
5
+
6
+
7
+ # Usage
8
+
9
+ Setup `Warden::CookieSession` in initializer and provide wrapper.
10
+
11
+ ```ruby
12
+
13
+ Warden::CookieSession.configure do |config|
14
+ config.cookie = Rails.application.secrets['shared_cookie']
15
+ config.secret = Rails.application.secrets['shared_secret']
16
+
17
+ config.wrapper = Warden::CookieSession::DefaultWrapper.new(User)
18
+ end
19
+ ```
20
+
21
+ Default wrapper just fetch user from model:
22
+ ```ruby
23
+ module Warden
24
+ module CookieSession
25
+ class DefaultWrapper
26
+
27
+ def initialize(klass = nil)
28
+ @klass = klass
29
+ end
30
+
31
+ def serialize_record(record)
32
+ # like in https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb
33
+ [record.to_key, record.authenticatable_salt]
34
+ end
35
+
36
+ def fetch_record(key)
37
+ @klass.find(key.first)
38
+ end
39
+
40
+ def validate_record(record, salt)
41
+ # like in https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb
42
+ record if record && record.authenticatable_salt == salt
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ ```
49
+
50
+ # Advansed Usage
51
+
52
+ With `Warden::CookieSession` we can fetch user data remotly ex. from API:
53
+
54
+ ```ruby
55
+
56
+ Warden::CookieSession.configure do |config|
57
+ config.cookie = Rails.application.secrets['shared_cookie']
58
+ config.secret = Rails.application.secrets['shared_secret']
59
+
60
+ class RemoteWrapper
61
+ def serialize_record(record)
62
+ [record.to_key, record.authenticatable_salt]
63
+ end
64
+
65
+ def fetch_record(key)
66
+ FetchRemoteUserAndSalt.run!(key)
67
+ end
68
+
69
+ def validate_record(record, salt)
70
+ record if record && record.authenticatable_salt == salt
71
+ end
72
+
73
+ end
74
+
75
+ config.wrapper = Warden::CookieSession::DefaultWrapper.new(User)
76
+ end
77
+
78
+
79
+ # Installation
80
+
81
+ It's a gem:
82
+ ```bash
83
+ gem install warden_cookie_session
84
+ ```
85
+ There's also the wonders of [the Gemfile](http://bundler.io):
86
+ ```ruby
87
+ gem 'warden_cookie_session'
88
+ ```
@@ -0,0 +1,34 @@
1
+ require 'logger'
2
+
3
+ module Warden
4
+ module CookieSession
5
+ class Configuration
6
+
7
+ attr_accessor :cookie, :secret, :wrapper, :logger
8
+
9
+ # Override defaults for configuration
10
+ # @param cookie [String] cookie name to store encrypted data
11
+ # @param secret [String] secret key(shared between applications) to use in ActiveSupport::MessageEncryptor
12
+ def initialize(cookie = 'cookie_session', secret = nil)
13
+ @cookie = cookie
14
+ @secret = secret
15
+ @logger = Logger.new(STDOUT, level: Logger::INFO, progname: 'CookieSession')
16
+ end
17
+
18
+ def serialize_record(record)
19
+ @wrapper&.serialize_record(record)
20
+ end
21
+
22
+ def fetch_record(key)
23
+ @wrapper&.fetch_record(key)
24
+ end
25
+
26
+ def validate_record(record, salt)
27
+ @wrapper&.validate_record(record, salt)
28
+ end
29
+
30
+
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,29 @@
1
+ class Warden::CookieSession::EncryptedCookie
2
+
3
+ attr_reader :store, :cookie, :secret, :encryptor
4
+
5
+ def initialize(store:, cookie:, secret:)
6
+ @store = store
7
+ @cookie = cookie
8
+ @secret = secret
9
+
10
+ @encryptor ||= ActiveSupport::MessageEncryptor.new(secret)
11
+ end
12
+
13
+ def get
14
+ value = store[cookie]
15
+ return nil unless value
16
+
17
+ JSON(encryptor.decrypt_and_verify(value))
18
+ end
19
+
20
+ def put(data)
21
+ store[cookie] = encryptor.encrypt_and_sign(data.to_json)
22
+ end
23
+
24
+ def clear
25
+ store.delete(cookie)
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,42 @@
1
+ require 'warden/cookie_session/encrypted_cookie'
2
+
3
+ class Warden::CookieSession::Strategy < ::Warden::Strategies::Base
4
+
5
+ def valid?
6
+ cookies[Warden::CookieSession.config.cookie]
7
+ end
8
+
9
+ def store?
10
+ false
11
+ end
12
+
13
+ def authenticate!
14
+ key, salt = encrypted_cookie.get
15
+ record = Warden::CookieSession.config.fetch_record(key)
16
+ success!(record) if record && Warden::CookieSession.config.validate_record(record, salt)
17
+ rescue StandardError => e
18
+ logger.warn "Warden::CookieSession::Strategy failed: #{e}"
19
+ fail!(e)
20
+ logger.debug { e.backtrace }
21
+ end
22
+
23
+ def self.encrypted_cookie(cookies)
24
+ Warden::CookieSession::EncryptedCookie.new(
25
+ store: cookies,
26
+ cookie: Warden::CookieSession.config.cookie,
27
+ secret: Warden::CookieSession.config.secret
28
+ )
29
+ end
30
+
31
+ def encrypted_cookie
32
+ @encrypted_cookie ||= Warden::CookieSession::Strategy.encrypted_cookie(cookies)
33
+ end
34
+
35
+ private
36
+
37
+ def logger
38
+ Warden::CookieSession.config.logger || Logger.new(nil)
39
+ end
40
+
41
+ end
42
+
@@ -0,0 +1,8 @@
1
+ module Warden
2
+ module CookieSession
3
+
4
+ VERSION = '0.1.0'.freeze
5
+
6
+ end
7
+ end
8
+
@@ -0,0 +1,44 @@
1
+ require 'warden'
2
+ require 'warden/cookie_session/version'
3
+ require 'warden/cookie_session/default_wrapper'
4
+ require 'warden/cookie_session/configuration'
5
+ require 'warden/cookie_session/strategy'
6
+
7
+ module Warden
8
+ module CookieSession
9
+
10
+ class << self
11
+
12
+ attr_accessor :config
13
+
14
+ end
15
+
16
+ self.config ||= Warden::CookieSession::Configuration.new
17
+
18
+ class << self
19
+
20
+ def configure
21
+ self.config ||= Warden::CookieSession::Configuration.new
22
+ yield(config)
23
+ setup_warden(config)
24
+ end
25
+
26
+ def setup_warden(config)
27
+ Warden::Strategies.add(:cookie_session, Warden::CookieSession::Strategy)
28
+
29
+ Warden::Manager.after_set_user do |user, auth, _opts|
30
+ encrypted_cookie = Warden::CookieSession::Strategy.encrypted_cookie(auth.cookies)
31
+ encrypted_cookie.put(config.serialize_record(user))
32
+ end
33
+
34
+ Warden::Manager.before_logout do |_user, auth, _opts|
35
+ encrypted_cookie = Warden::CookieSession::Strategy.encrypted_cookie(auth.cookies)
36
+ encrypted_cookie.clear
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+
@@ -0,0 +1,2 @@
1
+ require 'warden/cookie_session'
2
+
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: warden_cookie_session
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.18557
5
+ platform: ruby
6
+ authors:
7
+ - Samoilenko Yuri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
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
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec_junit_formatter
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov-console
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: warden
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: "!!!!"
118
+ email:
119
+ - kinnalru@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - LICENSE
125
+ - README.md
126
+ - lib/warden/cookie_session.rb
127
+ - lib/warden/cookie_session/configuration.rb
128
+ - lib/warden/cookie_session/encrypted_cookie.rb
129
+ - lib/warden/cookie_session/strategy.rb
130
+ - lib/warden/cookie_session/version.rb
131
+ - lib/warden_cookie_session.rb
132
+ homepage: https://github.com/RnD-Soft/timeouter
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubygems_version: 3.0.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: "!!!!"
155
+ test_files: []