clavem 1.4.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis-gemfile +2 -1
- data/.travis.yml +1 -2
- data/Gemfile +3 -2
- data/README.md +13 -6
- data/Rakefile +1 -1
- data/bin/clavem +29 -18
- data/clavem.gemspec +5 -4
- data/doc/Clavem.html +6 -6
- data/doc/Clavem/Authorizer.html +1141 -2216
- data/doc/Clavem/Exceptions.html +4 -4
- data/doc/Clavem/Exceptions/Failure.html +5 -5
- data/doc/Clavem/Server.html +370 -0
- data/doc/Clavem/Version.html +5 -5
- data/doc/_index.html +8 -15
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +21 -14
- data/doc/frames.html +1 -1
- data/doc/index.html +21 -14
- data/doc/method_list.html +32 -14
- data/doc/top-level-namespace.html +3 -3
- data/lib/clavem.rb +4 -3
- data/lib/clavem/authorizer.rb +75 -121
- data/lib/clavem/server.rb +52 -0
- data/lib/clavem/version.rb +3 -3
- data/locales/en.yml +15 -6
- data/locales/it.yml +15 -6
- data/spec/clavem/authorizer_spec.rb +86 -244
- data/spec/clavem/server_spec.rb +40 -0
- data/spec/coverage_helper.rb +2 -2
- data/spec/spec_helper.rb +2 -1
- data/test_server.rb +28 -10
- metadata +32 -27
- data/doc/Clavem/Exceptions/AuthorizationDenied.html +0 -133
- data/doc/Clavem/Exceptions/Timeout.html +0 -133
- data/doc/index.html.save +0 -133
- data/lib/clavem/template.html.erb +0 -25
data/doc/_index.html
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
<head>
|
5
5
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
6
6
|
<title>
|
7
|
-
Documentation by YARD 0.8.
|
7
|
+
Documentation by YARD 0.8.7
|
8
8
|
|
9
9
|
</title>
|
10
10
|
|
@@ -56,7 +56,7 @@
|
|
56
56
|
|
57
57
|
<iframe id="search_frame"></iframe>
|
58
58
|
|
59
|
-
<div id="content"><h1 class="noborder title">Documentation by YARD 0.8.
|
59
|
+
<div id="content"><h1 class="noborder title">Documentation by YARD 0.8.7</h1>
|
60
60
|
<div id="listing">
|
61
61
|
<h1 class="alphaindex">Alphabetic Index</h1>
|
62
62
|
|
@@ -84,13 +84,6 @@
|
|
84
84
|
<li class="letter">A</li>
|
85
85
|
<ul>
|
86
86
|
|
87
|
-
<li>
|
88
|
-
<span class='object_link'><a href="Clavem/Exceptions/AuthorizationDenied.html" title="Clavem::Exceptions::AuthorizationDenied (class)">AuthorizationDenied</a></span>
|
89
|
-
|
90
|
-
<small>(Clavem::Exceptions)</small>
|
91
|
-
|
92
|
-
</li>
|
93
|
-
|
94
87
|
<li>
|
95
88
|
<span class='object_link'><a href="Clavem/Authorizer.html" title="Clavem::Authorizer (class)">Authorizer</a></span>
|
96
89
|
|
@@ -145,14 +138,14 @@
|
|
145
138
|
</ul>
|
146
139
|
|
147
140
|
|
148
|
-
<ul id="
|
149
|
-
<li class="letter">
|
141
|
+
<ul id="alpha_S" class="alpha">
|
142
|
+
<li class="letter">S</li>
|
150
143
|
<ul>
|
151
144
|
|
152
145
|
<li>
|
153
|
-
<span class='object_link'><a href="Clavem/
|
146
|
+
<span class='object_link'><a href="Clavem/Server.html" title="Clavem::Server (class)">Server</a></span>
|
154
147
|
|
155
|
-
<small>(Clavem
|
148
|
+
<small>(Clavem)</small>
|
156
149
|
|
157
150
|
</li>
|
158
151
|
|
@@ -183,9 +176,9 @@
|
|
183
176
|
</div>
|
184
177
|
|
185
178
|
<div id="footer">
|
186
|
-
Generated on
|
179
|
+
Generated on Wed Aug 21 16:17:53 2013 by
|
187
180
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
188
|
-
0.8.
|
181
|
+
0.8.7 (ruby-2.0.0).
|
189
182
|
</div>
|
190
183
|
|
191
184
|
</body>
|
data/doc/class_list.html
CHANGED
@@ -46,7 +46,7 @@
|
|
46
46
|
|
47
47
|
<ul id="full_list" class="class">
|
48
48
|
<li><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></li>
|
49
|
-
<li><a class='toggle'></a> <span class='object_link'><a href="Clavem.html" title="Clavem (module)">Clavem</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><span class='object_link'><a href="Clavem/Authorizer.html" title="Clavem::Authorizer (class)">Authorizer</a></span> < Object<small class='search_info'>Clavem</small></li><li><a class='toggle'></a> <span class='object_link'><a href="Clavem/Exceptions.html" title="Clavem::Exceptions (module)">Exceptions</a></span><small class='search_info'>Clavem</small></li><ul><li><span class='object_link'><a href="Clavem/Exceptions/
|
49
|
+
<li><a class='toggle'></a> <span class='object_link'><a href="Clavem.html" title="Clavem (module)">Clavem</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><span class='object_link'><a href="Clavem/Authorizer.html" title="Clavem::Authorizer (class)">Authorizer</a></span> < Object<small class='search_info'>Clavem</small></li><li><a class='toggle'></a> <span class='object_link'><a href="Clavem/Exceptions.html" title="Clavem::Exceptions (module)">Exceptions</a></span><small class='search_info'>Clavem</small></li><ul><li><span class='object_link'><a href="Clavem/Exceptions/Failure.html" title="Clavem::Exceptions::Failure (class)">Failure</a></span> < StandardError<small class='search_info'>Clavem::Exceptions</small></li></ul><li><span class='object_link'><a href="Clavem/Server.html" title="Clavem::Server (class)">Server</a></span> < Connection<small class='search_info'>Clavem</small></li><li><span class='object_link'><a href="Clavem/Version.html" title="Clavem::Version (module)">Version</a></span><small class='search_info'>Clavem</small></li></ul>
|
50
50
|
|
51
51
|
</ul>
|
52
52
|
</div>
|
data/doc/file.README.html
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
3
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
4
|
<head>
|
5
|
-
<meta http-equiv="Content-Type" content="text/html; charset=
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
6
6
|
<title>
|
7
7
|
File: README
|
8
8
|
|
9
|
-
— Documentation by YARD 0.8.
|
9
|
+
— Documentation by YARD 0.8.7
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
@@ -82,23 +82,24 @@
|
|
82
82
|
<p>Simply instantiate the authorizer and run the authorize method with the URL:</p>
|
83
83
|
|
84
84
|
<p>```ruby
|
85
|
-
require
|
85
|
+
require “clavem”</p>
|
86
86
|
|
87
87
|
<h1 id="initialize-your-oauth-access">Initialize your oAuth access.</h1>
|
88
88
|
|
89
89
|
<p>authorizer = Clavem::Authorizer.new</p>
|
90
90
|
|
91
|
-
<h1 id="get-
|
92
|
-
|
93
|
-
|
91
|
+
<h1 id="get-the-token">Get the token</h1>
|
92
|
+
<p># You can also handle callback parameter by yourself.
|
93
|
+
# url += “?oauth_callback=#authorizerauthorizer.callback_url”
|
94
|
+
# authorizer.authorize(url, false)
|
94
95
|
authorizer.authorize(url)</p>
|
95
96
|
|
96
|
-
<p>if authorizer.
|
97
|
+
<p>if authorizer.succeeded? then
|
97
98
|
access_token = authorizer.token</p>
|
98
99
|
|
99
100
|
<p># Go on!
|
100
101
|
else
|
101
|
-
# Authorization denied
|
102
|
+
# Authorization denied or failed
|
102
103
|
end
|
103
104
|
```</p>
|
104
105
|
|
@@ -106,29 +107,35 @@ end
|
|
106
107
|
|
107
108
|
<p>See the <a href="http://rdoc.info/gems/clavem">documentation</a> for more information.</p>
|
108
109
|
|
110
|
+
<h2 id="use-on-jruby">Use on jRuby</h2>
|
111
|
+
|
112
|
+
<p>To use on jRuby, you need to install a gem with C extensions which must be compiled.</p>
|
113
|
+
|
114
|
+
<p>See jRuby documentation to see how to enabled extensions compilation.</p>
|
115
|
+
|
109
116
|
<h2 id="contributing-to-clavem">Contributing to clavem</h2>
|
110
117
|
|
111
118
|
<ul>
|
112
|
-
<li>Check out the latest master to make sure the feature hasn
|
113
|
-
<li>Check out the issue tracker to make sure someone already hasn
|
119
|
+
<li>Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.</li>
|
120
|
+
<li>Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.</li>
|
114
121
|
<li>Fork the project.</li>
|
115
122
|
<li>Start a feature/bugfix branch.</li>
|
116
123
|
<li>Commit and push until you are happy with your contribution.</li>
|
117
|
-
<li>Make sure to add tests for it. This is important so I don
|
124
|
+
<li>Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.</li>
|
118
125
|
<li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li>
|
119
126
|
</ul>
|
120
127
|
|
121
128
|
<h2 id="copyright">Copyright</h2>
|
122
129
|
|
123
|
-
<p>Copyright (C) 2013 and above Shogun (
|
130
|
+
<p>Copyright (C) 2013 and above Shogun (shogun@cowtech.it).</p>
|
124
131
|
|
125
132
|
<p>Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.</p>
|
126
133
|
</div></div>
|
127
134
|
|
128
135
|
<div id="footer">
|
129
|
-
Generated on
|
136
|
+
Generated on Wed Aug 21 16:17:53 2013 by
|
130
137
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
131
|
-
0.8.
|
138
|
+
0.8.7 (ruby-2.0.0).
|
132
139
|
</div>
|
133
140
|
|
134
141
|
</body>
|
data/doc/frames.html
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
5
|
<head>
|
6
6
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
-
<title>Documentation by YARD 0.8.
|
7
|
+
<title>Documentation by YARD 0.8.7</title>
|
8
8
|
</head>
|
9
9
|
<script type="text/javascript" charset="utf-8">
|
10
10
|
window.onload = function() {
|
data/doc/index.html
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
3
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
4
|
<head>
|
5
|
-
<meta http-equiv="Content-Type" content="text/html; charset=
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
6
6
|
<title>
|
7
7
|
File: README
|
8
8
|
|
9
|
-
— Documentation by YARD 0.8.
|
9
|
+
— Documentation by YARD 0.8.7
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
@@ -82,23 +82,24 @@
|
|
82
82
|
<p>Simply instantiate the authorizer and run the authorize method with the URL:</p>
|
83
83
|
|
84
84
|
<p>```ruby
|
85
|
-
require
|
85
|
+
require “clavem”</p>
|
86
86
|
|
87
87
|
<h1 id="initialize-your-oauth-access">Initialize your oAuth access.</h1>
|
88
88
|
|
89
89
|
<p>authorizer = Clavem::Authorizer.new</p>
|
90
90
|
|
91
|
-
<h1 id="get-
|
92
|
-
|
93
|
-
|
91
|
+
<h1 id="get-the-token">Get the token</h1>
|
92
|
+
<p># You can also handle callback parameter by yourself.
|
93
|
+
# url += “?oauth_callback=#authorizerauthorizer.callback_url”
|
94
|
+
# authorizer.authorize(url, false)
|
94
95
|
authorizer.authorize(url)</p>
|
95
96
|
|
96
|
-
<p>if authorizer.
|
97
|
+
<p>if authorizer.succeeded? then
|
97
98
|
access_token = authorizer.token</p>
|
98
99
|
|
99
100
|
<p># Go on!
|
100
101
|
else
|
101
|
-
# Authorization denied
|
102
|
+
# Authorization denied or failed
|
102
103
|
end
|
103
104
|
```</p>
|
104
105
|
|
@@ -106,29 +107,35 @@ end
|
|
106
107
|
|
107
108
|
<p>See the <a href="http://rdoc.info/gems/clavem">documentation</a> for more information.</p>
|
108
109
|
|
110
|
+
<h2 id="use-on-jruby">Use on jRuby</h2>
|
111
|
+
|
112
|
+
<p>To use on jRuby, you need to install a gem with C extensions which must be compiled.</p>
|
113
|
+
|
114
|
+
<p>See jRuby documentation to see how to enabled extensions compilation.</p>
|
115
|
+
|
109
116
|
<h2 id="contributing-to-clavem">Contributing to clavem</h2>
|
110
117
|
|
111
118
|
<ul>
|
112
|
-
<li>Check out the latest master to make sure the feature hasn
|
113
|
-
<li>Check out the issue tracker to make sure someone already hasn
|
119
|
+
<li>Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.</li>
|
120
|
+
<li>Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.</li>
|
114
121
|
<li>Fork the project.</li>
|
115
122
|
<li>Start a feature/bugfix branch.</li>
|
116
123
|
<li>Commit and push until you are happy with your contribution.</li>
|
117
|
-
<li>Make sure to add tests for it. This is important so I don
|
124
|
+
<li>Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.</li>
|
118
125
|
<li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li>
|
119
126
|
</ul>
|
120
127
|
|
121
128
|
<h2 id="copyright">Copyright</h2>
|
122
129
|
|
123
|
-
<p>Copyright (C) 2013 and above Shogun (
|
130
|
+
<p>Copyright (C) 2013 and above Shogun (shogun@cowtech.it).</p>
|
124
131
|
|
125
132
|
<p>Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.</p>
|
126
133
|
</div></div>
|
127
134
|
|
128
135
|
<div id="footer">
|
129
|
-
Generated on
|
136
|
+
Generated on Wed Aug 21 16:17:53 2013 by
|
130
137
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
131
|
-
0.8.
|
138
|
+
0.8.7 (ruby-2.0.0).
|
132
139
|
</div>
|
133
140
|
|
134
141
|
</body>
|
data/doc/method_list.html
CHANGED
@@ -48,25 +48,37 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
<li class="r1 ">
|
51
|
-
<span class='object_link'><a href="Clavem/Authorizer.html
|
51
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#%3Ai18n-instance_method" title="Clavem::Authorizer#:i18n (method)">#:i18n</a></span>
|
52
52
|
<small>Clavem::Authorizer</small>
|
53
53
|
</li>
|
54
54
|
|
55
55
|
|
56
56
|
<li class="r2 ">
|
57
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
57
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#authorize-instance_method" title="Clavem::Authorizer#authorize (method)">#authorize</a></span>
|
58
58
|
<small>Clavem::Authorizer</small>
|
59
59
|
</li>
|
60
60
|
|
61
61
|
|
62
62
|
<li class="r1 ">
|
63
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#callback_url-instance_method" title="Clavem::Authorizer#callback_url (method)">#callback_url</a></span>
|
64
|
+
<small>Clavem::Authorizer</small>
|
65
|
+
</li>
|
66
|
+
|
67
|
+
|
68
|
+
<li class="r2 ">
|
63
69
|
<span class='object_link'><a href="Clavem/Authorizer.html#command-instance_method" title="Clavem::Authorizer#command (method)">#command</a></span>
|
64
70
|
<small>Clavem::Authorizer</small>
|
65
71
|
</li>
|
66
72
|
|
67
73
|
|
74
|
+
<li class="r1 ">
|
75
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#denied%3F-instance_method" title="Clavem::Authorizer#denied? (method)">#denied?</a></span>
|
76
|
+
<small>Clavem::Authorizer</small>
|
77
|
+
</li>
|
78
|
+
|
79
|
+
|
68
80
|
<li class="r2 ">
|
69
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
81
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#failed%3F-instance_method" title="Clavem::Authorizer#failed? (method)">#failed?</a></span>
|
70
82
|
<small>Clavem::Authorizer</small>
|
71
83
|
</li>
|
72
84
|
|
@@ -84,25 +96,25 @@
|
|
84
96
|
|
85
97
|
|
86
98
|
<li class="r1 ">
|
87
|
-
<span class='object_link'><a href="Clavem/
|
88
|
-
<small>Clavem::
|
99
|
+
<span class='object_link'><a href="Clavem/Server.html#initialize-instance_method" title="Clavem::Server#initialize (method)">#initialize</a></span>
|
100
|
+
<small>Clavem::Server</small>
|
89
101
|
</li>
|
90
102
|
|
91
103
|
|
92
104
|
<li class="r2 ">
|
93
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
105
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#initialize-instance_method" title="Clavem::Authorizer#initialize (method)">#initialize</a></span>
|
94
106
|
<small>Clavem::Authorizer</small>
|
95
107
|
</li>
|
96
108
|
|
97
109
|
|
98
110
|
<li class="r1 ">
|
99
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
111
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#instance-class_method" title="Clavem::Authorizer.instance (method)">instance</a></span>
|
100
112
|
<small>Clavem::Authorizer</small>
|
101
113
|
</li>
|
102
114
|
|
103
115
|
|
104
116
|
<li class="r2 ">
|
105
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
117
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#localize-instance_method" title="Clavem::Authorizer#localize (method)">#localize</a></span>
|
106
118
|
<small>Clavem::Authorizer</small>
|
107
119
|
</li>
|
108
120
|
|
@@ -114,31 +126,31 @@
|
|
114
126
|
|
115
127
|
|
116
128
|
<li class="r2 ">
|
117
|
-
<span class='object_link'><a href="Clavem/
|
118
|
-
<small>Clavem::
|
129
|
+
<span class='object_link'><a href="Clavem/Server.html#process_http_request-instance_method" title="Clavem::Server#process_http_request (method)">#process_http_request</a></span>
|
130
|
+
<small>Clavem::Server</small>
|
119
131
|
</li>
|
120
132
|
|
121
133
|
|
122
134
|
<li class="r1 ">
|
123
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
135
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#response_handler-instance_method" title="Clavem::Authorizer#response_handler (method)">#response_handler</a></span>
|
124
136
|
<small>Clavem::Authorizer</small>
|
125
137
|
</li>
|
126
138
|
|
127
139
|
|
128
140
|
<li class="r2 ">
|
129
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
141
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#status-instance_method" title="Clavem::Authorizer#status (method)">#status</a></span>
|
130
142
|
<small>Clavem::Authorizer</small>
|
131
143
|
</li>
|
132
144
|
|
133
145
|
|
134
146
|
<li class="r1 ">
|
135
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
147
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#succeeded%3F-instance_method" title="Clavem::Authorizer#succeeded? (method)">#succeeded?</a></span>
|
136
148
|
<small>Clavem::Authorizer</small>
|
137
149
|
</li>
|
138
150
|
|
139
151
|
|
140
152
|
<li class="r2 ">
|
141
|
-
<span class='object_link'><a href="Clavem/Authorizer.html#
|
153
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#timeout-instance_method" title="Clavem::Authorizer#timeout (method)">#timeout</a></span>
|
142
154
|
<small>Clavem::Authorizer</small>
|
143
155
|
</li>
|
144
156
|
|
@@ -155,6 +167,12 @@
|
|
155
167
|
</li>
|
156
168
|
|
157
169
|
|
170
|
+
<li class="r1 ">
|
171
|
+
<span class='object_link'><a href="Clavem/Authorizer.html#waiting%3F-instance_method" title="Clavem::Authorizer#waiting? (method)">#waiting?</a></span>
|
172
|
+
<small>Clavem::Authorizer</small>
|
173
|
+
</li>
|
174
|
+
|
175
|
+
|
158
176
|
</ul>
|
159
177
|
</div>
|
160
178
|
</body>
|
@@ -6,7 +6,7 @@
|
|
6
6
|
<title>
|
7
7
|
Top Level Namespace
|
8
8
|
|
9
|
-
— Documentation by YARD 0.8.
|
9
|
+
— Documentation by YARD 0.8.7
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
@@ -103,9 +103,9 @@
|
|
103
103
|
</div>
|
104
104
|
|
105
105
|
<div id="footer">
|
106
|
-
Generated on
|
106
|
+
Generated on Wed Aug 21 16:17:53 2013 by
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
|
-
0.8.
|
108
|
+
0.8.7 (ruby-2.0.0).
|
109
109
|
</div>
|
110
110
|
|
111
111
|
</body>
|
data/lib/clavem.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
#
|
3
|
-
# This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <
|
3
|
+
# This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
4
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
5
|
#
|
6
6
|
|
7
7
|
require "r18n-desktop"
|
8
8
|
require "lazier"
|
9
|
-
require "
|
10
|
-
require "
|
9
|
+
require "eventmachine"
|
10
|
+
require "evma_httpserver"
|
11
11
|
|
12
12
|
require "clavem/version" if !defined?(Clavem::Version)
|
13
|
+
require "clavem/server"
|
13
14
|
require "clavem/authorizer"
|
data/lib/clavem/authorizer.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
#
|
3
|
-
# This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <
|
3
|
+
# This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
4
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
5
|
#
|
6
6
|
|
@@ -11,15 +11,7 @@ module Clavem
|
|
11
11
|
# Exceptions used by {Authorizer Authorizer}.
|
12
12
|
module Exceptions
|
13
13
|
# This exception is raised whether an error occurs.
|
14
|
-
class Failure < ::
|
15
|
-
end
|
16
|
-
|
17
|
-
# This exception is raised if the timeout expired.
|
18
|
-
class Timeout < ::Exception
|
19
|
-
end
|
20
|
-
|
21
|
-
# This exception is raised if the authorization was denied.
|
22
|
-
class AuthorizationDenied < ::RuntimeError
|
14
|
+
class Failure < ::StandardError
|
23
15
|
end
|
24
16
|
end
|
25
17
|
|
@@ -30,22 +22,18 @@ module Clavem
|
|
30
22
|
# @attribute host
|
31
23
|
# @return [String] The host address on which listening for replies. Default is `localhost`.
|
32
24
|
# @attribute port
|
33
|
-
# @return [Fixnum] The port on which listening for replies. Default is `
|
25
|
+
# @return [Fixnum] The port on which listening for replies. Default is `7772`.
|
34
26
|
# @attribute command
|
35
27
|
# @return [String] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
|
36
|
-
# @attribute title
|
37
|
-
# @return [String] The title for response template. Default is `Clavem Authorization`.
|
38
|
-
# @attribute template
|
39
|
-
# @return [String] Alternative template to show progress in user's browser.
|
40
28
|
# @attribute timeout
|
41
|
-
# @return [Fixnum] The amount of
|
29
|
+
# @return [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
|
42
30
|
# @attribute response_handler
|
43
|
-
# @return [Proc] A Ruby block to handle response and check for success.
|
31
|
+
# @return [Proc] A Ruby block to handle response and check for success. The block must accept a querystring hash (which all values are arrays) and return a token or `nil` if the authentication was denied.
|
44
32
|
# @attribute token
|
45
33
|
# @return [String] The token obtained by the remote endpoint.
|
46
|
-
# @attribute
|
47
|
-
# @return [Symbol] The status of the request. Can be `:
|
48
|
-
# @attribute
|
34
|
+
# @attribute status
|
35
|
+
# @return [Symbol] The status of the request. Can be `:succeeded`, `:denied`, `:failed` and `:waiting`.
|
36
|
+
# @attribute :i18n
|
49
37
|
# @return [R18N::Translation] A localizer object.
|
50
38
|
class Authorizer
|
51
39
|
include R18n::Helpers
|
@@ -53,8 +41,6 @@ module Clavem
|
|
53
41
|
attr_accessor :host
|
54
42
|
attr_accessor :port
|
55
43
|
attr_accessor :command
|
56
|
-
attr_accessor :title
|
57
|
-
attr_accessor :template
|
58
44
|
attr_accessor :timeout
|
59
45
|
attr_accessor :response_handler
|
60
46
|
attr_accessor :token
|
@@ -64,83 +50,60 @@ module Clavem
|
|
64
50
|
# Returns a unique (singleton) instance of the authorizer.
|
65
51
|
#
|
66
52
|
# @param host [String] The host address on which listening for replies. Default is `localhost`.
|
67
|
-
# @param port [Fixnum] The port on which listening for replies. Default is `
|
53
|
+
# @param port [Fixnum] The port on which listening for replies. Default is `7772`.
|
68
54
|
# @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
|
69
|
-
# @param
|
70
|
-
# @param
|
71
|
-
# @param timeout [Fixnum] The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
|
72
|
-
# @param response_handler [Proc] A Ruby block to handle response and check for success. See {#default_response_handler}.
|
55
|
+
# @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
|
56
|
+
# @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
|
73
57
|
# @param force [Boolean] If to force recreation of the instance.
|
74
58
|
# @return [Authorizer] The unique (singleton) instance of the authorizer.
|
75
|
-
def self.instance(host = "localhost", port =
|
59
|
+
def self.instance(host = "localhost", port = 7772, command = nil, timeout = 0, force = false, &response_handler)
|
76
60
|
@instance = nil if force
|
77
|
-
@instance ||= Clavem::Authorizer.new(host, port, command,
|
61
|
+
@instance ||= Clavem::Authorizer.new(host, port, command, timeout, &response_handler)
|
78
62
|
@instance
|
79
63
|
end
|
80
64
|
|
81
65
|
# Creates a new authorizer.
|
82
66
|
#
|
83
67
|
# @param host [String] The host address on which listening for replies. Default is `localhost`.
|
84
|
-
# @param port [Fixnum] The port on which listening for replies. Default is `
|
68
|
+
# @param port [Fixnum] The port on which listening for replies. Default is `7772`.
|
85
69
|
# @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
|
86
|
-
# @param
|
87
|
-
# @param
|
88
|
-
# @param timeout [Fixnum] The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
|
89
|
-
# @param response_handler [Proc] A Ruby block to handle response and check for success. See {#default_response_handler}.
|
70
|
+
# @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure. Default is `0`, which means *disabled*.
|
71
|
+
# @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
|
90
72
|
# @return [Authorizer] The new authorizer.
|
91
|
-
def initialize(host = "localhost", port =
|
73
|
+
def initialize(host = "localhost", port = 7772, command = nil, timeout = 0, &response_handler)
|
92
74
|
@i18n = self.localize
|
93
|
-
|
94
75
|
@host = host.ensure_string
|
95
76
|
@port = port.to_integer
|
96
77
|
@command = command.ensure_string
|
97
|
-
@title = title.ensure_string
|
98
|
-
@template = template.ensure_string
|
99
78
|
@timeout = timeout.to_integer
|
100
79
|
@response_handler = response_handler
|
101
|
-
|
102
|
-
sanitize_arguments
|
103
|
-
|
104
80
|
@token = nil
|
105
81
|
@status = :waiting
|
106
|
-
@compiled_template ||= ::ERB.new(@template)
|
107
|
-
@timeout_expired = false
|
108
|
-
@timeout_thread = nil
|
109
82
|
|
83
|
+
sanitize_arguments
|
110
84
|
self
|
111
85
|
end
|
112
86
|
|
113
87
|
# Starts the authorization flow.
|
114
88
|
#
|
115
89
|
# @param url [String] The URL where to send the user to start authorization.
|
116
|
-
# @
|
117
|
-
|
90
|
+
# @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
|
91
|
+
# @return [Boolean] `true` if authorization succeeded, `false` otherwise.
|
92
|
+
def authorize(url, append_callback = true)
|
93
|
+
url += "#{url.index("?") ? "&" : "?"}oauth_callback=#{callback_url}" if append_callback
|
118
94
|
@url = url
|
119
95
|
@status = :waiting
|
96
|
+
@token = nil
|
120
97
|
|
121
98
|
begin
|
122
|
-
|
123
|
-
|
124
|
-
setup_interruptions_handling
|
125
|
-
setup_timeout_handling
|
126
|
-
|
127
|
-
# Open the endpoint then start the server
|
128
|
-
open_endpoint
|
129
|
-
@server.start
|
130
|
-
|
131
|
-
raise Clavem::Exceptions::Timeout.new if @timeout_expired
|
132
|
-
rescue Clavem::Exceptions::Timeout => t
|
133
|
-
@status = :failure
|
134
|
-
raise t
|
99
|
+
perform_request
|
100
|
+
process_response
|
135
101
|
rescue => e
|
136
|
-
@status = :
|
102
|
+
@status = :failed
|
137
103
|
raise Clavem::Exceptions::Failure.new(@i18n.errors.response_failure(e.to_s))
|
138
|
-
ensure
|
139
|
-
cleanup
|
140
104
|
end
|
141
105
|
|
142
|
-
|
143
|
-
self
|
106
|
+
succeeded?
|
144
107
|
end
|
145
108
|
|
146
109
|
# Returns the callback_url for this authorizer.
|
@@ -150,14 +113,10 @@ module Clavem
|
|
150
113
|
"http://#{host}:#{port}/"
|
151
114
|
end
|
152
115
|
|
153
|
-
#
|
116
|
+
# Returns the response handler for the authorizer.
|
154
117
|
#
|
155
|
-
|
156
|
-
|
157
|
-
# @param [WEBrick::HTTPResponse] response The request to send to the browser.
|
158
|
-
# @return [String|nil] The oAuth access token. Returning `nil` means *authorization denied*.
|
159
|
-
def default_response_handler(authorizer, request, response)
|
160
|
-
request.query['oauth_token'].ensure_string
|
118
|
+
def response_handler
|
119
|
+
@response_handler || Proc.new {|querystring| (querystring || {}).fetch("oauth_token", []).ensure_array.first }
|
161
120
|
end
|
162
121
|
|
163
122
|
# Set the current locale for messages.
|
@@ -169,19 +128,45 @@ module Clavem
|
|
169
128
|
R18n::I18n.new([locale || :en, ENV["LANG"], R18n::I18n.system_locale].compact, @i18n_locales_path).t.clavem
|
170
129
|
end
|
171
130
|
|
131
|
+
# Checks if authentication succeeded.
|
132
|
+
#
|
133
|
+
# @return [Boolean] `true` if authorization succeeded, `false otherwise`.
|
134
|
+
def succeeded?
|
135
|
+
@status == :succeeded
|
136
|
+
end
|
137
|
+
|
138
|
+
# Checks if authentication was denied.
|
139
|
+
#
|
140
|
+
# @return [Boolean] `true` if authorization was denied, `false otherwise`.
|
141
|
+
def denied?
|
142
|
+
@status == :denied
|
143
|
+
end
|
144
|
+
|
145
|
+
# Checks if authentication failed (which means that some error occurred).
|
146
|
+
#
|
147
|
+
# @return [Boolean] `true` if authorization failed, `false otherwise`.
|
148
|
+
def failed?
|
149
|
+
@status == :failed
|
150
|
+
end
|
151
|
+
|
152
|
+
# Checks if authentication is still pending.
|
153
|
+
#
|
154
|
+
# @return [Boolean] `true` if authorization is still pending, `false otherwise`.
|
155
|
+
def waiting?
|
156
|
+
@status == :waiting
|
157
|
+
end
|
158
|
+
|
172
159
|
private
|
173
160
|
# sanitize_arguments
|
174
161
|
def sanitize_arguments
|
175
162
|
@host = "localhost" if @host.blank?
|
176
|
-
@port =
|
163
|
+
@port = 7772 if @port.to_integer < 1
|
177
164
|
@command = "open \"{{URL}}\"" if @command.blank?
|
178
|
-
@title = @i18n.default_title if @title.blank?
|
179
|
-
@template = File.read(File.dirname(__FILE__) + "/template.html.erb") if @template.blank?
|
180
165
|
@timeout = 0 if @timeout < 0
|
181
166
|
end
|
182
167
|
|
183
|
-
#
|
184
|
-
def
|
168
|
+
# Performs the authentication request.
|
169
|
+
def perform_request
|
185
170
|
# Open the oAuth endpoint into the browser
|
186
171
|
begin
|
187
172
|
Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
|
@@ -190,54 +175,23 @@ module Clavem
|
|
190
175
|
end
|
191
176
|
end
|
192
177
|
|
193
|
-
#
|
194
|
-
def
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
Process.kill("USR2", 0)
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
# Prepare the webserver for handling the response.
|
210
|
-
def setup_webserver
|
211
|
-
@server = ::WEBrick::HTTPServer.new(BindAddress: @host, Port: @port, Logger: WEBrick::Log.new("/dev/null"), AccessLog: [nil, nil])
|
212
|
-
@server.mount_proc("/"){ |request, response| dispatch_request(request, response) }
|
213
|
-
end
|
214
|
-
|
215
|
-
# Handles a response from the remote endpoint.
|
216
|
-
#
|
217
|
-
# @param [WEBrick::HTTPRequest] request The request that the remote endpoint made to notify authorization status.
|
218
|
-
# @param [WEBrick::HTTPResponse] response The request to send to the browser.
|
219
|
-
def dispatch_request(request, response)
|
220
|
-
@token = @response_handler ? @response_handler.call(self, request, response) : default_response_handler(self, request, response)
|
221
|
-
|
222
|
-
if @status == :waiting then
|
223
|
-
if @token.present? then
|
224
|
-
@status = :success
|
225
|
-
response.status = 200
|
226
|
-
else
|
227
|
-
@status = :denied
|
228
|
-
response.status = 403
|
229
|
-
end
|
230
|
-
|
231
|
-
response.body = @compiled_template.result(binding)
|
232
|
-
@server.shutdown
|
178
|
+
# Processes the authentication response.
|
179
|
+
def process_response
|
180
|
+
# Signal handling
|
181
|
+
["INT", "TERM", "KILL"].each {|signal|
|
182
|
+
Kernel.trap(signal){ EM.stop }
|
183
|
+
}
|
184
|
+
|
185
|
+
# Start eventmachine
|
186
|
+
EM.run do
|
187
|
+
EM.add_timer(@timeout){ handle_timeout } if @timeout > 0
|
188
|
+
EM.start_server(@host, @port, Clavem::Server, self)
|
233
189
|
end
|
234
190
|
end
|
235
191
|
|
236
|
-
#
|
237
|
-
def
|
238
|
-
|
239
|
-
@server.shutdown if @server
|
240
|
-
["USR2", "INT", "TERM", "KILL"].each {|signal| Kernel.trap(signal, "DEFAULT") }
|
192
|
+
# Handle timeouts.
|
193
|
+
def handle_timeout
|
194
|
+
EM.stop
|
241
195
|
end
|
242
196
|
end
|
243
197
|
end
|