sinatra_persona 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +1 -0
- data/lib/sinatra_persona/js.rb +33 -0
- data/lib/sinatra_persona/verifier.rb +29 -0
- data/lib/sinatra_persona/version.rb +3 -0
- data/lib/sinatra_persona.rb +64 -0
- data/sinatra_persona.gemspec +26 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c1ba1fe80f2488f91d18942203c55ebe077f9174
|
4
|
+
data.tar.gz: 31bc6216e5469b6606a0837e7c29881e6b715255
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7dc897d527acf83713bb2a5118a753aa1f287a3cb86d29ae347e05b8ee9a006974957b7c5aec4494fee55c404eb0697ebb736433c6896f9c86473fdfd42fb214
|
7
|
+
data.tar.gz: 7e3b522355335e81ff034bfcd01baeb379c706eeb88fc405b470be03a59e93d2367564e0a39ba3c0b37b4382efa1bc23e64582764c1b6d933fbf1dd596f4821b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Colin J. Fuller
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# SinatraPersona
|
2
|
+
|
3
|
+
A Sinatra extension for logging in with [persona](https://persona.org/about) and verifying persona assertions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sinatra_persona'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sinatra_persona
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Include the extension in your app.
|
22
|
+
|
23
|
+
For a classic-style app:
|
24
|
+
```ruby
|
25
|
+
require 'sinatra_persona'
|
26
|
+
enable :sessions
|
27
|
+
```
|
28
|
+
|
29
|
+
For a modular-style app:
|
30
|
+
```ruby
|
31
|
+
require 'sinatra/base'
|
32
|
+
require 'sinatra_persona'
|
33
|
+
|
34
|
+
class MyApp < Sinatra::Base
|
35
|
+
enable :sessions
|
36
|
+
register Sinatra::Persona
|
37
|
+
# Application code here
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
### URL handlers
|
42
|
+
|
43
|
+
The extension defines a POST handler at `/auth/perona_verifier` to which the
|
44
|
+
assertions to be verified are sent. You don't need to use this explicitly if
|
45
|
+
you use the supplied login button helper and scripts helper.
|
46
|
+
|
47
|
+
### Interface
|
48
|
+
|
49
|
+
Several helpers are available for interacting with persona:
|
50
|
+
|
51
|
+
- `persona` gets the e-mail by which a user has identified or nil if not
|
52
|
+
identified.
|
53
|
+
- `persona?` is an alias for `persona` (if you want to use it to check if
|
54
|
+
someone has identified and follow the boolean-with-question-mark convention)
|
55
|
+
- `clear_persona!` clears out the persona information from the session. This
|
56
|
+
will require users to log in again.
|
57
|
+
- `persona_button` returns a string containing an html button element that will
|
58
|
+
trigger login. This is not styled by default, but has id
|
59
|
+
`persona-login-button` so you can style it however you like.
|
60
|
+
- `persona_scripts` returns a string containing html script tags that will
|
61
|
+
include the persona scripts from persona.org, a script to set the click
|
62
|
+
action of the persona button on the page, and jQuery hosted by google. See
|
63
|
+
the settings section below if don't want to inlcude the default jQuery and
|
64
|
+
instead supply your own self-hosted one.
|
65
|
+
|
66
|
+
### Settings
|
67
|
+
|
68
|
+
Several optional settings are available via the normal Sinatra settings
|
69
|
+
mechanism (i.e. to set setting `foo` to `'value'`, do `set :foo, 'value'` in
|
70
|
+
your app.
|
71
|
+
|
72
|
+
- `persona_login_button_text`: the text to display on the login button returned
|
73
|
+
by the `persona_button` helper. Defaults to "Log in with Persona".
|
74
|
+
|
75
|
+
- `persona_verifier_uri`: a stdlib URI (not a string) that is a location that
|
76
|
+
verifies persona assertions. Defaults to
|
77
|
+
"https://verifier.login.persona.org/verify". Make sure this uses https.
|
78
|
+
|
79
|
+
- `after_persona_redirect`: a string that is the location to redirect to after
|
80
|
+
verifying the persona assertion. Defaults to '/'.
|
81
|
+
|
82
|
+
- `persona_no_jquery`: set to true if you don't want to use the google-hosted
|
83
|
+
jQuery version. At the moment, the scripts returned by the `persona_scripts`
|
84
|
+
helper require jQuery, so you'll need to supply your own if you turn this
|
85
|
+
off.
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
License: MIT. See LICENSE.txt for the full text.
|
90
|
+
|
91
|
+
Pull requests welcome!
|
92
|
+
|
93
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module PersonaJS
|
2
|
+
BUTTON_SCRIPT = <<SCRIPT
|
3
|
+
$(function() {
|
4
|
+
$('#persona-login-button').click(function() {
|
5
|
+
navigator.id.get(verifyAssertion);
|
6
|
+
});
|
7
|
+
|
8
|
+
function verifyAssertion(assertion) {
|
9
|
+
$.ajax({
|
10
|
+
type: 'POST',
|
11
|
+
url: '/auth/persona_verifier',
|
12
|
+
data: {assertion: assertion},
|
13
|
+
success: function(res, status, xhr) {window.location.reload();},
|
14
|
+
failure: function(res, status, xhr) {window.location.reload();}
|
15
|
+
});
|
16
|
+
}
|
17
|
+
});
|
18
|
+
SCRIPT
|
19
|
+
|
20
|
+
def self.scripts_no_jquery
|
21
|
+
<<SCRIPTS
|
22
|
+
<script type="text/javascript">
|
23
|
+
#{BUTTON_SCRIPT}
|
24
|
+
</script>
|
25
|
+
<script src="https://login.persona.org/include.js"></script>
|
26
|
+
SCRIPTS
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.scripts
|
30
|
+
jquery = '<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>'
|
31
|
+
jquery + scripts_no_jquery
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'json'
|
3
|
+
require 'net/https'
|
4
|
+
|
5
|
+
module Persona
|
6
|
+
module Verifier
|
7
|
+
|
8
|
+
DEFAULT_VERIFIER_URI = URI("https://verifier.login.persona.org/verify")
|
9
|
+
|
10
|
+
def self.verify_assertion(assertion, audience, verifier_uri: nil)
|
11
|
+
verifier_uri ||= DEFAULT_VERIFIER_URI
|
12
|
+
Net::HTTP.start(verifier_uri.host, verifier_uri.port, use_ssl: true) do |conn|
|
13
|
+
params = {assertion: assertion, audience: audience}
|
14
|
+
verifier_uri.query = URI.encode_www_form(params)
|
15
|
+
request = Net::HTTP::Post.new verifier_uri
|
16
|
+
response = conn.request request
|
17
|
+
if not response.code == '200' or response.body.nil? then
|
18
|
+
return nil
|
19
|
+
end
|
20
|
+
parsed = JSON.parse response.body
|
21
|
+
if parsed['status'] == 'okay' then
|
22
|
+
parsed['email']
|
23
|
+
else
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'sinatra_persona/version'
|
2
|
+
require 'sinatra_persona/verifier'
|
3
|
+
require 'sinatra_persona/js'
|
4
|
+
require 'sinatra/base'
|
5
|
+
|
6
|
+
module Sinatra
|
7
|
+
module Persona
|
8
|
+
def self.registered(app)
|
9
|
+
app.helpers Persona::Helpers
|
10
|
+
app.post '/auth/persona_verifier' do
|
11
|
+
assertion = params[:assertion]
|
12
|
+
audience = request.host_with_port
|
13
|
+
verifier_uri = settings.respond_to? :persona_verifier_uri ? settings.persona_verifier_uri : nil
|
14
|
+
email = ::Persona::Verifier.verify_assertion(assertion, audience)
|
15
|
+
if email.nil?
|
16
|
+
session.delete(:persona)
|
17
|
+
else
|
18
|
+
session[:persona] = email
|
19
|
+
end
|
20
|
+
if session.respond_to? :after_persona_redirect
|
21
|
+
redirect to settings.after_persona_redirect
|
22
|
+
else
|
23
|
+
redirect to '/'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module Helpers
|
29
|
+
|
30
|
+
DEFAULT_LOGIN_TEXT = "Log in with Persona"
|
31
|
+
|
32
|
+
def persona?
|
33
|
+
session[:persona]
|
34
|
+
end
|
35
|
+
|
36
|
+
def persona
|
37
|
+
session[:persona]
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear_persona!
|
41
|
+
session[:persona] = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def persona_button
|
45
|
+
login_text = DEFAULT_LOGIN_TEXT
|
46
|
+
if settings.respond_to? :persona_login_button_text
|
47
|
+
login_text = settings.persona_login_button_text
|
48
|
+
end
|
49
|
+
"<button id='persona-login-button'>Log in with Persona</button>"
|
50
|
+
end
|
51
|
+
|
52
|
+
def persona_scripts
|
53
|
+
if settings.respond_to? :persona_no_jquery and settings.persona_no_jquery
|
54
|
+
PersonaJS.scripts_no_jquery
|
55
|
+
else
|
56
|
+
PersonaJS.scripts
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
register Persona
|
62
|
+
end
|
63
|
+
|
64
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sinatra_persona/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sinatra_persona"
|
8
|
+
spec.version = SinatraPersona::VERSION
|
9
|
+
spec.authors = ["Colin J. Fuller"]
|
10
|
+
spec.email = ["cjfuller@gmail.com"]
|
11
|
+
spec.summary = %q{Persona verifier for Sinatra}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_dependency "haml"
|
23
|
+
spec.add_dependency "sinatra"
|
24
|
+
end
|
25
|
+
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra_persona
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin J. Fuller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-02 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: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: haml
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sinatra
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- cjfuller@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/sinatra_persona.rb
|
82
|
+
- lib/sinatra_persona/js.rb
|
83
|
+
- lib/sinatra_persona/verifier.rb
|
84
|
+
- lib/sinatra_persona/version.rb
|
85
|
+
- sinatra_persona.gemspec
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.1
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Persona verifier for Sinatra
|
110
|
+
test_files: []
|