zas-client 0.1.3 → 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.
- data/lib/zas/middleware/zas_authenticator.rb +47 -0
- metadata +2 -1
@@ -0,0 +1,47 @@
|
|
1
|
+
module Zas
|
2
|
+
module Middleware
|
3
|
+
# A Rack Middleware implementation that can pass HTTP basic auth credentials to a ZAS service
|
4
|
+
# for authentication. If the requestor is authenticated then the user's identifier will
|
5
|
+
# be added to the request environment as `env['zas.user.identifier']`.
|
6
|
+
class ZasAuthenticator
|
7
|
+
# Initialize the middleware with given app.
|
8
|
+
def initialize(app)
|
9
|
+
@app = app
|
10
|
+
|
11
|
+
require 'zas/client'
|
12
|
+
client_config = Zas::ClientConfiguration.new
|
13
|
+
client_config.logger.level = Logger::INFO
|
14
|
+
@zas_client = Zas::Client.new(client_config)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Call the middleware
|
18
|
+
def call(env)
|
19
|
+
type, credentials = authorization_header_value(env)
|
20
|
+
if type && type.downcase == 'basic'
|
21
|
+
auth_result = @zas_client.authenticate(Zas::HttpBasicCredentials.new(credentials))
|
22
|
+
if auth_result.authenticated?
|
23
|
+
env['zas.user.identifier'] = auth_result.identifier
|
24
|
+
@app.call(env)
|
25
|
+
else
|
26
|
+
authentication_failed
|
27
|
+
end
|
28
|
+
else
|
29
|
+
authentication_failed
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def authorization_header_value(env)
|
35
|
+
if v = env['HTTP_AUTHORIZATION']
|
36
|
+
v.split
|
37
|
+
else
|
38
|
+
[nil, nil]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def authentication_failed
|
43
|
+
[401, {'Content-Type' => 'application/json'}, [Yajl::Encoder.encode({"errors" => "Authentication failed"})]]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zas-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- lib/zas/client_configuration.rb
|
87
87
|
- lib/zas/credentials.rb
|
88
88
|
- lib/zas/http_basic_credentials.rb
|
89
|
+
- lib/zas/middleware/zas_authenticator.rb
|
89
90
|
- lib/zas.rb
|
90
91
|
- LICENSE
|
91
92
|
- README.md
|