zas-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2012 Anthony Eden
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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 NONINFRINGEMENT. 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.
8
+
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Zero Authentication Service - Ruby Client
2
+
3
+ This is a ruby client libraty for the Zero Authentication Service.
4
+
5
+ ## Usage
6
+
7
+ Generally:
8
+
9
+ client = Zas::Client.new
10
+ require 'zas/http_basic_credentials'
11
+ credentials = Zas::HttpBasicCredentials.new('Zm9vOmJhcg==')
12
+ auth_result = client.authenticate(credentials)
13
+ if auth_result.authenticated?
14
+ p "You've been authenticated"
15
+ else
16
+ p "I don't know you"
17
+ end
18
+
19
+ In irb:
20
+
21
+ bundle exec irb -Ilib -rzas
22
+ => # everything from general usage
23
+
24
+
25
+ ## Configuration
26
+
27
+ config = Zas::ClientConfiguration.new(:name => 'my-client')
28
+ client = Zas::Client.new(config)
29
+
30
+ ## Notes
31
+
32
+ * Zas::Client is not threadsafe
33
+ * When using in irb call client.disconnect or CTRL-C to exit
34
+
data/lib/zas/client.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'zmq'
2
+ require 'syslogger'
3
+ require 'yajl'
4
+
5
+ module Zas
6
+ class Client
7
+ require 'zas/client_configuration'
8
+
9
+ # Initialize the client with the given configuration.
10
+ #
11
+ # config - Configuration spec for the client.
12
+ def initialize(config=ClientConfiguration.new)
13
+ self.host = config.host
14
+ self.port = config.port
15
+ self.context = ZMQ::Context.new
16
+ self.logger = Syslogger.new(config.name, Syslog::LOG_PID, Syslog::LOG_LOCAL0)
17
+ end
18
+
19
+ # Authenticate the given credentials.
20
+ #
21
+ # credentials - The credentials
22
+ def authenticate(credentials)
23
+ begin
24
+ socket.send credentials.to_wire
25
+ socket.recv
26
+ rescue IOError => e
27
+ logger.error "Shutting down: #{e.message}"
28
+ end
29
+ end
30
+
31
+ def disconnect
32
+ socket.close
33
+ @socket = nil
34
+ end
35
+
36
+ private
37
+ attr_accessor :context
38
+ attr_accessor :host
39
+ attr_accessor :port
40
+ attr_accessor :logger
41
+
42
+ def socket
43
+ @socket ||= build_socket
44
+ end
45
+
46
+ def build_socket
47
+ socket = context.socket ZMQ::REQ
48
+ socket.connect "tcp://#{host}:#{port}"
49
+ Signal.trap(:INT) { disconnect }
50
+ socket
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ module Zas
2
+ # Struct used to configure the Zas::Client.
3
+ #
4
+ # host - The IP address of the service. Defaults to 127.0.0.1
5
+ # port - The port number for the service.
6
+ # name - The name of the client for logging.
7
+ class ClientConfiguration < Struct.new(:host, :port, :name)
8
+ def initialize(attributes={})
9
+ attributes.each do |k, v|
10
+ self[k] = v
11
+ end
12
+ end
13
+ def host
14
+ self[:host] || '127.0.0.1'
15
+ end
16
+ def port
17
+ self[:port] || '5555'
18
+ end
19
+ def name
20
+ self[:name] || 'zas-client'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ module Zas
2
+ # Credentials as taken from HTTP Basic Authentication.
3
+ class HttpBasicCredentials
4
+ # Construct the credentials with the base64 encoded value
5
+ # passed directly from an HTTP request.
6
+ def initialize(base64_encoded_credentials)
7
+ self.base64_encoded_credentials = base64_encoded_credentials
8
+ end
9
+
10
+ # Construct a wire representation of the credentials
11
+ def to_wire
12
+ Yajl::Encoder.encode({:strategy => :http_basic_auth, :credentials => base64_encoded_credentials})
13
+ end
14
+
15
+ private
16
+ attr_accessor :base64_encoded_credentials
17
+ end
18
+ end
data/lib/zas.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Zas
2
+ require 'zas/client'
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zas-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anthony Eden
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Client for communicating with ZeroAS
15
+ email:
16
+ - anthonyeden+zero-as@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/zas/client.rb
22
+ - lib/zas/client_configuration.rb
23
+ - lib/zas/http_basic_credentials.rb
24
+ - lib/zas.rb
25
+ - LICENSE
26
+ - README.md
27
+ homepage: http://github.com/zero-as/zas-client-ruby
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 1.3.6
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.21
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Client for ZeroAS
51
+ test_files: []