oauth2_mac_client 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in oauth2_mac_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tom Brown
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,16 @@
1
+ = oauth2_mac_client
2
+
3
+ OAuth 2.0 MAC Client Library.
4
+
5
+ == Installation
6
+
7
+ gem install oauth2_mac_client
8
+
9
+ == Resources
10
+
11
+ * View Source on GitHub (https://github.com/herestomwiththeweather/oauth2_mac_client)
12
+ * Report Issues on GitHub (https://github.com/herestomwiththeweather/oauth2_mac_client/issues)
13
+
14
+ == Copyright
15
+
16
+ Copyright (c) 2012 Tom Brown. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ module Oauth2MacClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require "oauth2_mac_client/version"
2
+ require "active_support/core_ext"
3
+
4
+ module Oauth2MacClient
5
+ class Token
6
+ attr_accessor :access_token, :mac_key, :mac_algorithm, :issued_at
7
+ attr_accessor :method, :request_uri, :host, :port, :body_hash
8
+ attr_writer :nonce
9
+
10
+ def initialize(attributes={})
11
+ @access_token = attributes[:access_token]
12
+ @mac_key = attributes[:mac_key]
13
+ @mac_algorithm = attributes[:mac_algorithm]
14
+ @issued_at = attributes[:issued_at] || Time.now.utc
15
+ end
16
+
17
+ def age
18
+ age = Time.now.utc - @issued_at
19
+ age.to_i
20
+ end
21
+
22
+ def nonce
23
+ @nonce ||= [
24
+ age,
25
+ SecureRandom.hex
26
+ ].join(':')
27
+ end
28
+
29
+ def request_string
30
+ [nonce,
31
+ @method,
32
+ @request_uri,
33
+ @host,
34
+ @port,
35
+ @body_hash,
36
+ '', # ext
37
+ nil].join("\n")
38
+ end
39
+
40
+ def base64_encode(text)
41
+ Base64.encode64(text).gsub(/\n/,'')
42
+ end
43
+
44
+ def openssl_digest
45
+ @openssl_digest ||= case @mac_algorithm
46
+ when 'hmac-sha-256' then OpenSSL::Digest::SHA256.new
47
+ when 'hmac-sha-1' then OpenSSL::Digest::SHA1.new
48
+ end
49
+ end
50
+
51
+ def calculate_hmac
52
+ result = OpenSSL::HMAC.digest(openssl_digest,@mac_key,request_string)
53
+ base64_encode result
54
+ end
55
+
56
+ def construct_authorization_header(url,method,body="")
57
+ @body_hash = body.empty? ? "" : body_hash(body)
58
+ @method=method.upcase
59
+ uri = URI.parse(url)
60
+ @host=uri.host
61
+ @port=uri.port
62
+ @request_uri=uri.request_uri
63
+ @hmac=calculate_hmac
64
+ authorization_header
65
+ end
66
+
67
+ def authorization_header
68
+ header = "MAC"
69
+ header << " id=\"#{@access_token}\","
70
+ header << " nonce=\"#{nonce}\","
71
+ header << " bodyhash=\"#{@body_hash}\","
72
+ header << " mac=\"#{@hmac}\","
73
+ end
74
+
75
+ def body_hash(body)
76
+ result = openssl_digest.digest(body)
77
+ base64_encode result
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "oauth2_mac_client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "oauth2_mac_client"
7
+ s.version = Oauth2MacClient::VERSION
8
+ s.authors = ["Tom Brown"]
9
+ s.email = ["herestomwiththeweather@gmail.com"]
10
+ s.homepage = "https://github.com/herestomwiththeweather/oauth2_mac_client"
11
+ s.summary = %q{Send requests to OAuth 2 provider with MAC authentication}
12
+ s.description = %q{Send requests to OAuth 2 provider with MAC authentication}
13
+
14
+ s.rubyforge_project = "oauth2_mac_client"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "activesupport", ">= 2.3"
22
+ s.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module Oauth2MacClient
4
+ describe Token do
5
+ let :token do
6
+ Token.new(
7
+ access_token: 'access_token',
8
+ mac_key: 'secret',
9
+ mac_algorithm: 'hmac-sha-256',
10
+ issued_at: issued_at
11
+ )
12
+ end
13
+
14
+ let(:issued_at) { 1305820455 }
15
+ subject { token }
16
+
17
+ its(:mac_key) { should == 'secret' }
18
+ its(:mac_algorithm) { should == 'hmac-sha-256' }
19
+
20
+ describe '.calculate_hmac' do
21
+ it "produces the hmac expected from the spec" do
22
+ @token = Token.new(access_token:'abc',mac_key:'8yfrufh348h',mac_algorithm:'hmac-sha-1')
23
+ @token.nonce = '273156:di3hvdf8'
24
+ @token.method = 'POST'
25
+ @token.request_uri = '/request'
26
+ @token.host = 'example.com'
27
+ @token.port = 80
28
+ @token.body_hash = 'k9kbtCIy0CkI3/FEfpS/oIDjk6k='
29
+ @token.calculate_hmac.should == 'W7bdMZbv9UWOTadASIQHagZyirA='
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require 'oauth2_mac_client'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth2_mac_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Brown
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &78598890 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *78598890
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &78598680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *78598680
36
+ description: Send requests to OAuth 2 provider with MAC authentication
37
+ email:
38
+ - herestomwiththeweather@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - lib/oauth2_mac_client.rb
50
+ - lib/oauth2_mac_client/version.rb
51
+ - oauth2_mac_client.gemspec
52
+ - spec/oauth2_mac_client/token_spec.rb
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/herestomwiththeweather/oauth2_mac_client
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project: oauth2_mac_client
74
+ rubygems_version: 1.8.16
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Send requests to OAuth 2 provider with MAC authentication
78
+ test_files: []