sinatra-session_helpers 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sinatra/session_helpers/version.rb +5 -0
- data/lib/sinatra/session_helpers.rb +45 -0
- data/sinatra-session_helpers.gemspec +24 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1055db28b1aa44b5fa3791f60ba17fdb3f407db7
|
4
|
+
data.tar.gz: 327271558c5b3496b2ee52de0c3fcd93d710d4b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f2aeec69bc7a26d48596b4f7c9abc05b66fc595d08019ef0d2ee7e5b817627cda9b93b18becdf1014087abecf9f6d37d6eedc9ead81f19b27fd3d10f0cc227d
|
7
|
+
data.tar.gz: e8695777493c024fd6cb1f39df6785aeff3e0bdbb1781cf2ab932b32f6bcf010bdde6343577e4255f20e0a0aac3b87564013b00479451cc3be035f5825d6b365
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Julien Ma
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# Sinatra::SessionHelpers
|
2
|
+
|
3
|
+
Helper methods for Sinatra's :sessions management
|
4
|
+
|
5
|
+
This is the same code than https://github.com/mjackson/sinatra-session, but adapted to use Sinatra's built-in `sessions`, instead of `Rack::Session::Cookie`.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sinatra-session_helpers'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sinatra-session_helpers
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Helper Methods
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
`session_start!` -> Starts the session
|
29
|
+
`session_end!(destroy=true)` -> Ends the session. If `destroy` is false then
|
30
|
+
session data will be preserved, even though future calls to `session?` will return false
|
31
|
+
`session?` -> Returns true if the session is valid
|
32
|
+
`session!` -> Redirects the client to the URL specified in
|
33
|
+
the `session_fail` option unless the session is valid
|
34
|
+
```
|
35
|
+
|
36
|
+
### Settings
|
37
|
+
|
38
|
+
```
|
39
|
+
`session_fail` -> A URL in your app that the client will be redirected to
|
40
|
+
when `session!` is called and the session is not valid.
|
41
|
+
Defaults to '/login'
|
42
|
+
```
|
43
|
+
|
44
|
+
You can force a specific secret key on the session:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
set :session_secret, '$ecR3t'
|
48
|
+
```
|
49
|
+
|
50
|
+
Or other optional parameters like this:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
set :sessions, :key => 'sinatra.session',
|
54
|
+
:path => '/',
|
55
|
+
:domain => 'foo.com',
|
56
|
+
:expire_after => 2592000, # 30 days
|
57
|
+
:secret => 'change_me'
|
58
|
+
```
|
59
|
+
|
60
|
+
## Examples
|
61
|
+
|
62
|
+
As with all Sinatra extensions, Sinatra::SessionHelpers may be used in both classic and modular-style apps. First, classic.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'sinatra'
|
66
|
+
require 'sinatra/session_helpers'
|
67
|
+
|
68
|
+
set :session_fail, '/login'
|
69
|
+
set :session_secret, 'So0perSeKr3t!'
|
70
|
+
set :sessions, :key => 'my_app'
|
71
|
+
|
72
|
+
get '/' do
|
73
|
+
session!
|
74
|
+
'Hello, ' + session[:name] + '! Click <a href="/logout">here</a> to logout.'
|
75
|
+
end
|
76
|
+
|
77
|
+
get '/login' do
|
78
|
+
if session?
|
79
|
+
redirect '/'
|
80
|
+
else
|
81
|
+
'<form method="POST" action="/login">' +
|
82
|
+
'Please enter your name: <input type="text" name="name">' +
|
83
|
+
'<input type="submit" value="Submit">' +
|
84
|
+
'</form>'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
post '/login' do
|
89
|
+
if params[:name]
|
90
|
+
session_start!
|
91
|
+
session[:name] = params[:name]
|
92
|
+
redirect '/'
|
93
|
+
else
|
94
|
+
redirect '/login'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
get '/logout' do
|
99
|
+
session_end!
|
100
|
+
redirect '/'
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
Modular apps use very similar code but must also mixin Sinatra::Session via Sinatra#register, like so:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
require 'sinatra/base'
|
108
|
+
require 'sinatra/session_helpers'
|
109
|
+
|
110
|
+
class MyApp < Sinatra::Base
|
111
|
+
register Sinatra::Session
|
112
|
+
|
113
|
+
# insert settings and routes here, same as in
|
114
|
+
# classic example above
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
## Development
|
119
|
+
|
120
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
121
|
+
|
122
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/julienma/sinatra-session_helpers.
|
127
|
+
|
128
|
+
|
129
|
+
## License
|
130
|
+
|
131
|
+
Copyright 2016 Julien Ma
|
132
|
+
|
133
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
134
|
+
|
135
|
+
Based on software code Copyright 2012 Michael Jackson
|
136
|
+
|
137
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
138
|
+
|
139
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
140
|
+
|
141
|
+
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sinatra/session_helpers"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require "sinatra/session_helpers/version"
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
module SessionHelpers
|
6
|
+
|
7
|
+
module Helpers
|
8
|
+
# After this method is called, calls to #session? will return +true+.
|
9
|
+
def session_start!
|
10
|
+
session[:authorized] = true
|
11
|
+
end
|
12
|
+
|
13
|
+
# After this method is called, calls to #session? will return +false+. If you want to keep the old session data around in the cookie for some reason, set +destroy+ to +false+.
|
14
|
+
def session_end!(destroy=true)
|
15
|
+
if destroy
|
16
|
+
session.clear
|
17
|
+
else
|
18
|
+
session[:authorized] = false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns +true+ if the current session is valid, +false+ otherwise.
|
23
|
+
def session?
|
24
|
+
!! session[:authorized]
|
25
|
+
end
|
26
|
+
|
27
|
+
# Redirects the client to the URL given in the +session_fail+ setting if #session? returns +false+.
|
28
|
+
def session!
|
29
|
+
redirect(settings.session_fail) unless session? || settings.session_fail == request.path_info
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.registered(app)
|
34
|
+
app.helpers SessionHelpers::Helpers
|
35
|
+
|
36
|
+
# This should be set to the redirect URL the client will be sent to if the session is not valid.
|
37
|
+
app.set :session_fail, '/login'
|
38
|
+
|
39
|
+
# Enable Sinatra built-in sessions
|
40
|
+
app.enable :sessions
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
register SessionHelpers
|
45
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sinatra/session_helpers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sinatra-session_helpers"
|
8
|
+
spec.version = Sinatra::SessionHelpers::VERSION
|
9
|
+
spec.authors = ["Julien Ma"]
|
10
|
+
spec.email = ["julienma@users.noreply.github.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Helper methods for Sinatra's :sessions management.}
|
13
|
+
spec.homepage = "https://github.com/julienma/sinatra-session_helpers"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "sinatra", "~> 1.4.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-session_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Ma
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- julienma@users.noreply.github.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/sinatra/session_helpers.rb
|
70
|
+
- lib/sinatra/session_helpers/version.rb
|
71
|
+
- sinatra-session_helpers.gemspec
|
72
|
+
homepage: https://github.com/julienma/sinatra-session_helpers
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Helper methods for Sinatra's :sessions management.
|
96
|
+
test_files: []
|