caren-api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,8 +1,15 @@
1
1
  = Caren API reference implementation
2
2
 
3
3
  This is the reference implementation for the Caren CareProvider API (http://caren-cares.com).
4
- To get started you will need a care provider API shared key. Set `Caren::Api.shared_key` to that key.
5
- Next you will need to set your care provider url `Caren::Api.url` to your specific URL like https://example.caren-cares.com
4
+
5
+ To get started you will need a care provider API shared key. Set `Caren::Api.shared_key` to the key file.
6
+
7
+ Generate a private key using: `openssl genrsa -out example 1024`
8
+ Then extract the public key from it: `openssl rsa -in example -pubout -out example.pub`
9
+ Send the public key to info@caren-cares.com with an access request.
10
+
11
+ Next you will need to set your care provider url `Caren::Api.url` to your specific URL like https://example.caren-cares.com.
12
+ This url will be provided.
6
13
 
7
14
  == Contributing to caren-api
8
15
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/caren-api.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{caren-api}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andre Foeken"]
@@ -25,6 +25,9 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "caren-api.gemspec",
28
+ "certs/caren-api.pub",
29
+ "certs/example",
30
+ "certs/example.pub",
28
31
  "init.rb",
29
32
  "lib/caren-api.rb",
30
33
  "lib/caren/base.rb",
@@ -0,0 +1,6 @@
1
+ -----BEGIN PUBLIC KEY-----
2
+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDll+UxqVoqgd6ZHkw8av/0XYbZ
3
+ RP7JIZ8zNst1kK4SnBV0YZBlxl5j+1tRgjvzSUOxJehp0ikFquZk2dm9i7JHIeXL
4
+ OnfSjN4Hbw/C/d8RXDcmbtF/7RmYs4mVrhQS5eQNR7bDo6NKA1UfDEB0OFIcvhks
5
+ 6ev6vuETYzu7Y+DroQIDAQAB
6
+ -----END PUBLIC KEY-----
data/certs/example ADDED
@@ -0,0 +1,15 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
2
+ MIICXQIBAAKBgQDCbPwFkQhkA21mF96N0LSK0YtlyyCnkRcXMRgj9cxGZ88c5qti
3
+ VTD5As6YdEn4GHaHfEr/dMlJpT8eTYHmy4eb35gw+jiGYm24LqJqsQFK7rAJ1I0b
4
+ iE356NF8YaEOKP327K0QzTnftHijTrzDVm/6cHkXQxzAEpPaofU4TW0ljwIDAQAB
5
+ AoGBALoQDkdNepgr53/yi9tvxbervRmXgRKbeVuKci3HxswCAGUKIy2hlZJYK1oe
6
+ v8MbtiMGwcci/pbp+zOGvTAfyw+lZRP2aYIedRnRRLlmNtSYqD9BiD+F+ZfHUsHY
7
+ kFGZdQIF8FPs6utjdbqqfi3YnAkyYvEC/1ecoGb8JkqIkwRxAkEA93/E+CaXxure
8
+ /AteMIbm7CxEzasCPCvK7JQjRGgJbdRg5geB2Tjs6GjsCB1QI93KE0ZnK4M4bevI
9
+ gxzJaffIpQJBAMkajAs5OI+cpOBGZg1KMJ1GgLL21HH/47rt40KWJbZxAsbOd2Vy
10
+ 4rBdu/X40JGYT/adVJh76Pw11P1gwMolKyMCQDCK+OELAL3dFzssJ58w9l3gGOaV
11
+ 4exXUoewTnJ+TUsjWpYDZu5qAd1bpt+4kB1YxOee6WFZbVnx4vQEU2TbWpkCQQCe
12
+ sK8f0Ch80swcGhE/nXKdONwn3LSpvJX0lPGHQrpBrA3X2WbIDA8rwtumbpAOw9Ud
13
+ surlZKnmE4id5LJdEVVnAkAsUiPowyInJKUNEN6PbvyVsjkgZq6ykDTyTJdWcR2o
14
+ cT4sb4UzePHAAeh8g6VZeipCmO4SaugphkrPYvifWd7l
15
+ -----END RSA PRIVATE KEY-----
data/certs/example.pub ADDED
@@ -0,0 +1,6 @@
1
+ -----BEGIN PUBLIC KEY-----
2
+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCbPwFkQhkA21mF96N0LSK0Ytl
3
+ yyCnkRcXMRgj9cxGZ88c5qtiVTD5As6YdEn4GHaHfEr/dMlJpT8eTYHmy4eb35gw
4
+ +jiGYm24LqJqsQFK7rAJ1I0biE356NF8YaEOKP327K0QzTnftHijTrzDVm/6cHkX
5
+ QxzAEpPaofU4TW0ljwIDAQAB
6
+ -----END PUBLIC KEY-----
data/init.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "lib/caren.rb"
2
2
 
3
3
  Caren::Api.url = "https://www.carenzorgt.nl"
4
- Caren::Api.shared_key = ""
4
+ Caren::Api.caren_public_key_path = "certs/caren-api.pub"
5
+ Caren::Api.private_key_path = ""
data/lib/caren/caren.rb CHANGED
@@ -19,11 +19,27 @@ module Caren
19
19
  end
20
20
 
21
21
  class Api
22
- class << self
23
- attr_accessor :shared_key
22
+ class << self
23
+ # The public key file path used to verify request coming from Caren
24
+ attr_accessor :caren_public_key_path
25
+
26
+ # The private key file path used to sign requests coming from you
27
+ attr_accessor :private_key_path
28
+
29
+ # The care provider url provided by Caren. i.e. https://example.caren-cares.com
24
30
  attr_accessor :url
25
31
  end
26
32
 
33
+ # The public key file used to verify request coming from Caren
34
+ def self.caren_public_key
35
+ @caren_public_key ||= OpenSSL::PKey::RSA.new(File.read(self.caren_public_key_path))
36
+ end
37
+
38
+ # The private key file used to sign requests coming from you
39
+ def self.private_key
40
+ @private_key ||= OpenSSL::PKey::RSA.new(File.read(self.private_key_path))
41
+ end
42
+
27
43
  def self.put url, xml
28
44
  begin
29
45
  response = RestClient.put url, xml, :content_type => :xml, :accept => :xml, :signature => Caren::Api.sign(xml)
@@ -59,19 +75,29 @@ module Caren
59
75
  handle_error(e.response)
60
76
  end
61
77
  end
62
-
78
+
79
+ # Sign your string using Caren::Api.private_key
63
80
  def self.sign string=""
64
- raise "No API key given: Caren::Api.shared_key" unless Caren::Api.shared_key
65
- Digest::SHA256.hexdigest(string+Caren::Api.shared_key)
81
+ encrypted_digest = Caren::Api.private_key.sign( "sha1", string )
82
+ signature = CGI.escape(Base64.encode64(encrypted_digest))
83
+ return signature
66
84
  end
67
-
85
+
68
86
  private
69
-
87
+
88
+ # Check the signature of the response from rest-client
70
89
  def self.check_signature response
71
- return response if response.headers[:signature] == Caren::Api.sign(response.to_str)
90
+ return response if self.verify_signature( response.headers[:signature], response )
72
91
  raise Caren::Exceptions::SignatureMismatch.new
73
92
  end
74
93
 
94
+ # Verify the signature using the caren public key file
95
+ def self.verify_signature signature, string=""
96
+ signature = Base64.decode64(CGI.unescape(signature))
97
+ Caren::Api.caren_public_key.verify( "sha1", signature, string )
98
+ end
99
+
100
+ # Raise a Caren exception on errors
75
101
  def self.handle_error response
76
102
  errors = []
77
103
  doc = REXML::Document.new(response)
data/lib/caren-api.rb CHANGED
@@ -1,7 +1,3 @@
1
- require "digest/sha2"
2
- require "uri"
3
- require "net/https"
4
-
5
1
  begin
6
2
  require "rubygems"
7
3
  rescue LoadError
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,9 @@ require 'rspec'
6
6
  require 'fakeweb'
7
7
  require 'capybara'
8
8
 
9
- Caren::Api.shared_key = "specs"
9
+ # For the tests we need to know both the public and private key, so we share them here.
10
+ Caren::Api.caren_public_key_path = "certs/example.pub"
11
+ Caren::Api.private_key_path = "certs/example"
10
12
  Caren::Api.url = "http://example.com"
11
13
 
12
14
  FakeWeb.allow_net_connect = false
metadata CHANGED
@@ -4,10 +4,10 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
9
  segments_generated: true
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andre Foeken
@@ -166,6 +166,9 @@ files:
166
166
  - Rakefile
167
167
  - VERSION
168
168
  - caren-api.gemspec
169
+ - certs/caren-api.pub
170
+ - certs/example
171
+ - certs/example.pub
169
172
  - init.rb
170
173
  - lib/caren-api.rb
171
174
  - lib/caren/base.rb