moguera-authentication 0.1.1 → 0.2.0
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 +4 -4
- data/README.md +11 -3
- data/lib/moguera/authentication/exception.rb +6 -0
- data/lib/moguera/authentication/version.rb +1 -1
- data/lib/moguera/authentication.rb +1 -1
- data/lib/rails/moguera_authentication/authable.rb +97 -0
- data/lib/rails/moguera_authentication/install.rb +22 -0
- data/lib/rails/moguera_authentication/railtie.rb +23 -0
- data/lib/rails/moguera_authentication.rb +6 -0
- data/sample/config.ru +5 -2
- data/sample/server.rb +4 -1
- data/spec/authentication/authentication_spec.rb +2 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d6a12e8bf765eea7438dc1cb9553a02dff5ccc0
|
4
|
+
data.tar.gz: 4273174cdfebf58dc6b90ba5e420686cf6da2035
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 648d0938793be93bf7ae8de811b5108d4019e39c72444c20f692678b73a03c6cfcaa1ad55135b4268ed7a46ecb57ed392a048b42c570be8a9a4ce4a539e2ccf1
|
7
|
+
data.tar.gz: 55ccb61e61572a27bb4da245c0f9e76e3064faa1447d6c2e169d82010bdf2992ca6599e2f09946c8b5509b4fdf5da64f65e8950b1c794013d938387bfb0848c9
|
data/README.md
CHANGED
@@ -72,8 +72,13 @@ map '/login' do
|
|
72
72
|
# # example credential.json
|
73
73
|
# #=> {"user01":"secret"}
|
74
74
|
file = File.join(File.expand_path(File.dirname(__FILE__)),'credential.json')
|
75
|
-
|
76
|
-
|
75
|
+
secret_key = JSON.parse(File.open(file, &:read))[request_access_key]
|
76
|
+
|
77
|
+
unless secret_key
|
78
|
+
raise Moguera::Authentication::UserNotFound, "access_key: " + request_access_key
|
79
|
+
end
|
80
|
+
|
81
|
+
secret_key
|
77
82
|
end
|
78
83
|
|
79
84
|
run Private
|
@@ -103,10 +108,13 @@ class Private < Sinatra::Base
|
|
103
108
|
if e = env['moguera.error']
|
104
109
|
$stderr.puts e.message
|
105
110
|
case e
|
106
|
-
when Moguera::Authentication::ParameterInvalid
|
111
|
+
when Moguera::Authentication::ParameterInvalid,
|
112
|
+
Moguera::Authentication::RequestTokenRequired
|
107
113
|
halt 400, "400 Bad Request: #{e.message}\n"
|
108
114
|
when Moguera::Authentication::AuthenticationError
|
109
115
|
halt 401, "401 Unauthorized: #{e.message}\n"
|
116
|
+
when Moguera::Authentication::UserNotFound
|
117
|
+
halt 404, "404 Not Found: #{e.message}\n"
|
110
118
|
else
|
111
119
|
halt 500, "500 Internal Server Error\n"
|
112
120
|
end
|
@@ -7,7 +7,7 @@ module Moguera
|
|
7
7
|
attr_accessor :allow_time_interval
|
8
8
|
|
9
9
|
def initialize(request_token = nil)
|
10
|
-
raise
|
10
|
+
raise RequestTokenRequired, 'Missing request token.' unless request_token
|
11
11
|
|
12
12
|
@request_token = request_token
|
13
13
|
@allow_time_interval = allow_time_interval || 600
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module MogueraAuthentication
|
2
|
+
module Authable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :current_user, :user_signed_in?
|
7
|
+
end
|
8
|
+
|
9
|
+
def current_user=(user)
|
10
|
+
@current_user = user
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_user
|
14
|
+
@current_user
|
15
|
+
end
|
16
|
+
|
17
|
+
def user_signed_in?
|
18
|
+
!!current_user
|
19
|
+
end
|
20
|
+
|
21
|
+
def require_sign_in!
|
22
|
+
validate_user!
|
23
|
+
sign_in!(user_class.find_by(find_key => env['moguera.auth'].try(:access_key)))
|
24
|
+
end
|
25
|
+
|
26
|
+
def sign_in!(user)
|
27
|
+
self.current_user = user
|
28
|
+
end
|
29
|
+
|
30
|
+
def sign_out!
|
31
|
+
self.current_user = nil
|
32
|
+
end
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
def require_sign_in!(options={})
|
36
|
+
before_filter :require_sign_in!, options
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def user_class
|
43
|
+
Rails.application.config.moguera_authentication.user_class
|
44
|
+
end
|
45
|
+
|
46
|
+
def find_key
|
47
|
+
Rails.application.config.moguera_authentication.find_key
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate_user!
|
51
|
+
if e = env['moguera.error']
|
52
|
+
logger.error "ERROR: #{e.inspect}"
|
53
|
+
case e
|
54
|
+
when Moguera::Authentication::ParameterInvalid,
|
55
|
+
Moguera::Authentication::RequestTokenRequired
|
56
|
+
status = 400
|
57
|
+
response = {
|
58
|
+
code: e.class.to_s,
|
59
|
+
message: "#{status} Bad Request: #{e.message}",
|
60
|
+
status: status
|
61
|
+
}
|
62
|
+
when Moguera::Authentication::AuthenticationError
|
63
|
+
status = 401
|
64
|
+
response = {
|
65
|
+
code: e.class.to_s,
|
66
|
+
message: "#{status} Unauthorized: #{e.message}",
|
67
|
+
status: status
|
68
|
+
}
|
69
|
+
require 'pp'
|
70
|
+
PP.pp ({
|
71
|
+
request_token: e.request_token,
|
72
|
+
server_request_token: e.server_request.token,
|
73
|
+
request_path: e.server_request.request_path,
|
74
|
+
request_method: e.server_request.request_method,
|
75
|
+
http_date: e.server_request.http_date,
|
76
|
+
content_type: e.server_request.content_type
|
77
|
+
}), STDERR
|
78
|
+
when Moguera::Authentication::UserNotFound
|
79
|
+
status = 404
|
80
|
+
response = {
|
81
|
+
code: e.class.to_s,
|
82
|
+
message: "#{status} Not Found: #{e.message}",
|
83
|
+
status: status
|
84
|
+
}
|
85
|
+
else
|
86
|
+
status = 500
|
87
|
+
response = {
|
88
|
+
code: e.class.to_s,
|
89
|
+
message: "#{status} Internal Server Error: #{e.message}",
|
90
|
+
status: status
|
91
|
+
}
|
92
|
+
end
|
93
|
+
render(json: response, status: status)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MogueraAuthentication
|
2
|
+
class Install < Rails::Generators::Base
|
3
|
+
desc 'Initialize MogueraAuthentication'
|
4
|
+
def create_initializer_file
|
5
|
+
initializer 'moguera_authentication.rb' do
|
6
|
+
<<-FILE.strip_heredoc
|
7
|
+
Rails.application.config.middleware.use Rack::MogueraAuthentication do |key|
|
8
|
+
secret_key = Rails.application.config.moguera_authentication.user_class
|
9
|
+
.find_by(Rails.application.config.moguera_authentication.find_key => key)
|
10
|
+
.try(Rails.application.config.moguera_authentication.secret_access_key)
|
11
|
+
|
12
|
+
unless secret_key
|
13
|
+
raise Moguera::Authentication::UserNotFound, "access_key: " + key
|
14
|
+
end
|
15
|
+
|
16
|
+
secret_key
|
17
|
+
end
|
18
|
+
FILE
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MogueraAuthentication
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
config.moguera_authentication = ActiveSupport::OrderedOptions.new
|
4
|
+
config.eager_load_namespaces << MogueraAuthentication::Railtie
|
5
|
+
|
6
|
+
initializer 'moguera_authentication.controller_ext' do
|
7
|
+
require 'rails/moguera_authentication/authable'
|
8
|
+
ActiveSupport.on_load(:action_controller) do
|
9
|
+
ActionController::Base.send :include, MogueraAuthentication::Authable
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer 'moguera_authentication.set_config' do
|
14
|
+
config.moguera_authentication.user_class ||= User rescue nil
|
15
|
+
config.moguera_authentication.find_key ||= :access_key
|
16
|
+
config.moguera_authentication.secret_access_key ||= :secret_access_key
|
17
|
+
end
|
18
|
+
|
19
|
+
generators do
|
20
|
+
require 'rails/moguera_authentication/install'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/sample/config.ru
CHANGED
@@ -12,8 +12,11 @@ end
|
|
12
12
|
map '/login' do
|
13
13
|
use Rack::MogueraAuthentication do |key|
|
14
14
|
file = File.join(File.expand_path(File.dirname(__FILE__)), 'credential.json')
|
15
|
-
|
16
|
-
|
15
|
+
secret_key = JSON.parse(File.open(file, &:read))[key]
|
16
|
+
|
17
|
+
raise Moguera::Authentication::UserNotFound unless secret_key
|
18
|
+
|
19
|
+
secret_key
|
17
20
|
end
|
18
21
|
|
19
22
|
run Private
|
data/sample/server.rb
CHANGED
@@ -18,10 +18,13 @@ class Private < Sinatra::Base
|
|
18
18
|
if e = env['moguera.error']
|
19
19
|
$stderr.puts e.message
|
20
20
|
case e
|
21
|
-
when Moguera::Authentication::ParameterInvalid
|
21
|
+
when Moguera::Authentication::ParameterInvalid,
|
22
|
+
Moguera::Authentication::RequestTokenRequired
|
22
23
|
halt 400, "400 Bad Request: #{e.message}\n"
|
23
24
|
when Moguera::Authentication::AuthenticationError
|
24
25
|
halt 401, "401 Unauthorized: #{e.message}\n"
|
26
|
+
when Moguera::Authentication::UserNotFound
|
27
|
+
halt 404, "404 Not Found: #{e.message}\n"
|
25
28
|
else
|
26
29
|
halt 500, "500 Internal Server Error\n"
|
27
30
|
end
|
@@ -31,10 +31,10 @@ describe Moguera::Authentication do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
describe 'Invalid token' do
|
34
|
-
it 'should be raise
|
34
|
+
it 'should be raise RequestTokenRequired with missing request token message' do
|
35
35
|
expect {
|
36
36
|
Moguera::Authentication.new
|
37
|
-
}.to raise_error(Moguera::Authentication::
|
37
|
+
}.to raise_error(Moguera::Authentication::RequestTokenRequired, 'Missing request token.')
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should be raise AuthenticationError with invalid token message' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moguera-authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hiro-su
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,6 +86,10 @@ files:
|
|
86
86
|
- lib/moguera/authentication/request.rb
|
87
87
|
- lib/moguera/authentication/version.rb
|
88
88
|
- lib/rack/moguera_authentication.rb
|
89
|
+
- lib/rails/moguera_authentication.rb
|
90
|
+
- lib/rails/moguera_authentication/authable.rb
|
91
|
+
- lib/rails/moguera_authentication/install.rb
|
92
|
+
- lib/rails/moguera_authentication/railtie.rb
|
89
93
|
- moguera-authentication.gemspec
|
90
94
|
- sample/client.rb
|
91
95
|
- sample/config.ru
|