zas-client 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -7,14 +7,15 @@ This is a ruby client libraty for the Zero Authentication Service.
7
7
  Generally:
8
8
 
9
9
  client = Zas::Client.new
10
+ auth_result = client.authenticate(Zas::Credentials.new(:username => 'john', :password => 'letmein'))
11
+ p "You are authenticated" if auth_result.authenticated?
12
+
13
+ For HTTP Basic Auth
14
+
15
+ # everything from general usage
10
16
  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
17
+ auth_result = client.authenticate(Zas::HttpBasicCredentials.new('Zm9vOmJhcg=='))
18
+ p "You've been authenticated" if auth_result.authenticated?
18
19
 
19
20
  In irb:
20
21
 
data/lib/zas/client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'zmq'
2
2
  require 'syslogger'
3
3
  require 'yajl'
4
+ require 'hashie'
4
5
 
5
6
  module Zas
6
7
  class Client
@@ -14,22 +15,27 @@ module Zas
14
15
  self.port = config.port
15
16
  self.context = ZMQ::Context.new
16
17
  self.logger = Syslogger.new(config.name, Syslog::LOG_PID, Syslog::LOG_LOCAL0)
18
+
19
+ at_exit { disconnect }
17
20
  end
18
21
 
19
22
  # Authenticate the given credentials.
20
23
  #
21
24
  # credentials - The credentials
25
+ #
26
+ # Returns the results of the authentication.
22
27
  def authenticate(credentials)
23
28
  begin
24
29
  socket.send credentials.to_wire
25
- socket.recv
30
+ Hashie::Mash.new(Yajl::Parser.parse(socket.recv))
26
31
  rescue IOError => e
27
32
  logger.error "Shutting down: #{e.message}"
28
33
  end
29
34
  end
30
35
 
36
+ # Disconnect the socket.
31
37
  def disconnect
32
- socket.close
38
+ socket.close if socket
33
39
  @socket = nil
34
40
  end
35
41
 
@@ -0,0 +1,14 @@
1
+ module Zas
2
+ class Credentials
3
+ def initialize(credentials)
4
+ self.credentials = credentials
5
+ end
6
+
7
+ def to_wire
8
+ Yajl::Encoder.encode({:strategy => :auth, :credentials => credentials})
9
+ end
10
+
11
+ private
12
+ attr_accessor :credentials
13
+ end
14
+ end
@@ -7,7 +7,9 @@ module Zas
7
7
  self.base64_encoded_credentials = base64_encoded_credentials
8
8
  end
9
9
 
10
- # Construct a wire representation of the credentials
10
+ # Construct a wire representation of the credentials.
11
+ #
12
+ # Returns the encoded JSON
11
13
  def to_wire
12
14
  Yajl::Encoder.encode({:strategy => :http_basic_auth, :credentials => base64_encoded_credentials})
13
15
  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.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/zas/client.rb
22
22
  - lib/zas/client_configuration.rb
23
+ - lib/zas/credentials.rb
23
24
  - lib/zas/http_basic_credentials.rb
24
25
  - lib/zas.rb
25
26
  - LICENSE