google_business_api_url_signer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/google_business_api_url_signer.gemspec +20 -0
- data/lib/google_business_api_url_signer.rb +27 -0
- data/lib/google_business_api_url_signer/errors.rb +7 -0
- data/lib/google_business_api_url_signer/signer.rb +103 -0
- data/lib/google_business_api_url_signer/version.rb +3 -0
- data/spec/google_business_api_url_signer/signer_spec.rb +58 -0
- data/spec/google_business_api_url_signer_spec.rb +20 -0
- data/spec/spec_helper.rb +6 -0
- metadata +99 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Thorbjørn Hermansen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Google Business Api Url Signer
|
2
|
+
|
3
|
+
Signs URLs used to call Google's business APIs.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'google_business_api_url_signer', git: 'git://github.com/Skalar/google_business_api_url_signer.git'
|
10
|
+
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
private_key = "my-private-key-here"
|
19
|
+
url = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"
|
20
|
+
GoogleBusinessApiUrlSigner.add_signature(url, private_key)
|
21
|
+
=> "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA="
|
22
|
+
|
23
|
+
|
24
|
+
The private key may also be set as a default value on the Signer class,
|
25
|
+
in which case you don't have to give it when calling `add_signature`.
|
26
|
+
|
27
|
+
GoogleBusinessApiUrlSigner::Signer.default_private_key = 'my-default-private-key-here'
|
28
|
+
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/google_business_api_url_signer/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Thorbjørn Hermansen"]
|
6
|
+
gem.email = ["thhermansen@gmail.com"]
|
7
|
+
gem.description = %q{Signs URLs used to call Google's business APIs}
|
8
|
+
gem.summary = %q{Signs URLs used to call Google's business APIs}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "google_business_api_url_signer"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = GoogleBusinessApiUrlSigner::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "activesupport", "~> 3.2.0"
|
19
|
+
gem.add_development_dependency "rspec", "2.11.0"
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require "google_business_api_url_signer/version"
|
3
|
+
require "google_business_api_url_signer/errors"
|
4
|
+
require "google_business_api_url_signer/signer"
|
5
|
+
|
6
|
+
module GoogleBusinessApiUrlSigner
|
7
|
+
# Public: Adds a signature to given URL
|
8
|
+
#
|
9
|
+
# url - The Google API URL you want to sign.
|
10
|
+
# The URL should contain your Google client ID set as get parameter 'client'
|
11
|
+
# private_key - Your private key which you got from Google when signing up for the business APIs
|
12
|
+
# This can be left blank if you set GoogleBusinessApiUrlSigner::Signer.default_private_key
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# Example
|
16
|
+
#
|
17
|
+
# private_key = "my-private-key-here"
|
18
|
+
# url = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"
|
19
|
+
# GoogleBusinessApiUrlSigner.add_signature(url, private_key)
|
20
|
+
# # => "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA="
|
21
|
+
#
|
22
|
+
def add_signature(url, private_key = nil)
|
23
|
+
Signer.new(url: url, private_key: private_key).signed_url
|
24
|
+
end
|
25
|
+
|
26
|
+
extend self
|
27
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'openssl'
|
3
|
+
|
4
|
+
module GoogleBusinessApiUrlSigner
|
5
|
+
# Public: Takes care of signing URLs
|
6
|
+
#
|
7
|
+
# Google's documentation for this can be found here:
|
8
|
+
# https://developers.google.com/maps/documentation/business/webservices#generating_valid_signatures
|
9
|
+
#
|
10
|
+
class Signer
|
11
|
+
BASE_64_DECODE_ENCODE_REPLACEMENTS = ['-_', '+/']
|
12
|
+
|
13
|
+
cattr_accessor :default_private_key
|
14
|
+
self.default_private_key = ''
|
15
|
+
|
16
|
+
attr_reader :url
|
17
|
+
|
18
|
+
# Public: Initializes the signer
|
19
|
+
#
|
20
|
+
# options - url must be given within the options,
|
21
|
+
# private_key can be given, or you can set default value with:
|
22
|
+
# GoogleBusinessApiUrlSigner::Signer.default_private_key = 'key'
|
23
|
+
#
|
24
|
+
def initialize(options = {})
|
25
|
+
@url = options.fetch :url
|
26
|
+
@private_key = options.fetch :private_key, default_private_key
|
27
|
+
@private_key = default_private_key if @private_key.blank?
|
28
|
+
end
|
29
|
+
|
30
|
+
def private_key
|
31
|
+
return @private_key if @private_key.present?
|
32
|
+
fail MissingPrivateKeyError
|
33
|
+
end
|
34
|
+
|
35
|
+
# Public: Calculates the signature from the given URL and private key
|
36
|
+
def signature
|
37
|
+
Base64.encode64(signature_digest).tr(*BASE_64_DECODE_ENCODE_REPLACEMENTS.reverse).chomp
|
38
|
+
end
|
39
|
+
|
40
|
+
# Public: Calculates the signature and returns a signed version of the URL
|
41
|
+
def signed_url
|
42
|
+
[
|
43
|
+
parsed_url.scheme,
|
44
|
+
'://',
|
45
|
+
parsed_url.host,
|
46
|
+
parsed_url.path,
|
47
|
+
'?',
|
48
|
+
query_params_as_string_with_signature
|
49
|
+
].join.html_safe
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def signature_digest
|
57
|
+
OpenSSL::HMAC.digest(
|
58
|
+
OpenSSL::Digest.new('sha1'),
|
59
|
+
private_key_decoded,
|
60
|
+
path_and_query
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
def parsed_url
|
67
|
+
@parsed_url ||= URI(url)
|
68
|
+
end
|
69
|
+
|
70
|
+
def path_and_query
|
71
|
+
[parsed_url.path, query_params_as_string].join '?'
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
def query_params
|
77
|
+
return @query_params if @query_params
|
78
|
+
|
79
|
+
@query_params = Hash[(parsed_url.query || '').split('&').collect { |key_value| key_value.split('=')}]
|
80
|
+
|
81
|
+
fail MissingClientIdError if @query_params['client'].blank?
|
82
|
+
fail UrlAlreadySignedError if @query_params['signature'].present?
|
83
|
+
|
84
|
+
@query_params
|
85
|
+
end
|
86
|
+
|
87
|
+
def query_params_as_string(params = nil)
|
88
|
+
(params || query_params).to_a.collect { |pair| pair.join('=') }.join('&')
|
89
|
+
end
|
90
|
+
|
91
|
+
def query_params_as_string_with_signature
|
92
|
+
query_params_as_string(
|
93
|
+
query_params.update(signature: signature)
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def private_key_decoded
|
100
|
+
Base64.decode64 private_key.tr(*BASE_64_DECODE_ENCODE_REPLACEMENTS)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GoogleBusinessApiUrlSigner::Signer do
|
4
|
+
let(:url) { "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID" }
|
5
|
+
let(:private_key) { "vNIXE0xscrmjlyV-12Nj_BvUPaw=" }
|
6
|
+
let(:signature) { "KrU1TzVQM7Ur0i8i7K3huiw3MsA=" }
|
7
|
+
let(:signed_url) { "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=#{signature}" }
|
8
|
+
|
9
|
+
subject do
|
10
|
+
described_class.new(
|
11
|
+
url: url,
|
12
|
+
private_key: private_key
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
its(:url) { should eq url }
|
17
|
+
its(:private_key) { should eq private_key }
|
18
|
+
its(:signature) { should eq signature }
|
19
|
+
its(:signed_url) { should eq signed_url }
|
20
|
+
its(:signed_url) { should be_html_safe }
|
21
|
+
|
22
|
+
it "ensures that the URL contains a client id" do
|
23
|
+
expect {
|
24
|
+
described_class.new(url: '', private_key: private_key).signature
|
25
|
+
}.to raise_error GoogleBusinessApiUrlSigner::MissingClientIdError
|
26
|
+
end
|
27
|
+
|
28
|
+
it "ensures that no signature exists within the URL" do
|
29
|
+
expect {
|
30
|
+
described_class.new(url: signed_url, private_key: private_key).signature
|
31
|
+
}.to raise_error GoogleBusinessApiUrlSigner::UrlAlreadySignedError
|
32
|
+
end
|
33
|
+
|
34
|
+
it "ensures that private key is set" do
|
35
|
+
expect {
|
36
|
+
described_class.new(url: signed_url, private_key: '').signature
|
37
|
+
}.to raise_error GoogleBusinessApiUrlSigner::MissingPrivateKeyError
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
describe "default private key" do
|
42
|
+
after { described_class.default_private_key = nil }
|
43
|
+
|
44
|
+
it "uses default private key when set" do
|
45
|
+
described_class.default_private_key = 'default'
|
46
|
+
|
47
|
+
Base64.should_receive(:decode64).with('default').and_return 'decoded'
|
48
|
+
described_class.new(url: url).signature
|
49
|
+
end
|
50
|
+
|
51
|
+
it "uses the private key when options are filled with a blank private key" do
|
52
|
+
described_class.default_private_key = 'default'
|
53
|
+
|
54
|
+
Base64.should_receive(:decode64).with('default').and_return 'decoded'
|
55
|
+
described_class.new(url: url, private_key: nil).signature
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GoogleBusinessApiUrlSigner do
|
4
|
+
describe ".add_signature" do
|
5
|
+
let(:signer) { mock }
|
6
|
+
let(:private_key) { "vNIXE0xscrmjlyV-12Nj_BvUPaw=" }
|
7
|
+
let(:url) { "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID" }
|
8
|
+
let(:signed_url) { "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=" }
|
9
|
+
|
10
|
+
it "delegates to signer" do
|
11
|
+
GoogleBusinessApiUrlSigner::Signer.should_receive(:new).with(hash_including(
|
12
|
+
url: url,
|
13
|
+
private_key: private_key
|
14
|
+
)).and_return signer
|
15
|
+
signer.should_receive(:signed_url).and_return signed_url
|
16
|
+
|
17
|
+
described_class.add_signature(url, private_key).should eq signed_url
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_business_api_url_signer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thorbjørn Hermansen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
prerelease: false
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.2.0
|
23
|
+
none: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.2.0
|
29
|
+
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
prerelease: false
|
32
|
+
name: rspec
|
33
|
+
type: :development
|
34
|
+
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 2.11.0
|
39
|
+
none: false
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 2.11.0
|
45
|
+
none: false
|
46
|
+
description: Signs URLs used to call Google's business APIs
|
47
|
+
email:
|
48
|
+
- thhermansen@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- google_business_api_url_signer.gemspec
|
59
|
+
- lib/google_business_api_url_signer.rb
|
60
|
+
- lib/google_business_api_url_signer/errors.rb
|
61
|
+
- lib/google_business_api_url_signer/signer.rb
|
62
|
+
- lib/google_business_api_url_signer/version.rb
|
63
|
+
- spec/google_business_api_url_signer/signer_spec.rb
|
64
|
+
- spec/google_business_api_url_signer_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: -119753422113709265
|
79
|
+
version: '0'
|
80
|
+
none: false
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -119753422113709265
|
88
|
+
version: '0'
|
89
|
+
none: false
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.23
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Signs URLs used to call Google's business APIs
|
96
|
+
test_files:
|
97
|
+
- spec/google_business_api_url_signer/signer_spec.rb
|
98
|
+
- spec/google_business_api_url_signer_spec.rb
|
99
|
+
- spec/spec_helper.rb
|