cloudfront-private 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloudfront-private.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Krishna Srihari
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,77 @@
1
+ # Cloudfront-Private
2
+
3
+ Streaming all media private contents from amazon cloudfront
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cloudfront-private'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cloudfront-private
18
+
19
+ Finally, boostrap the cloudfront-private
20
+
21
+ $ rails generate cloudfront:install
22
+
23
+ ## Getting started
24
+
25
+ Generate initializer cloudfront-private
26
+
27
+ $ rails generate cloudfront:install
28
+
29
+ this should generated a file called cloudfront-private.rb in config/initializers
30
+
31
+ checkout this file to add amazon cloudfront credentials
32
+
33
+ Cloudfront::Private.configure do |config|
34
+ config.key_pair_id = 'your-key-pair-id'
35
+ config.pem_file = 'your-private.pem'
36
+ end
37
+
38
+ Edit above file with your amazon cloudfront private key pair id and pem file name
39
+
40
+ The private pem file should placed in 'certs' directory under Rails root
41
+
42
+ Access private content in your view
43
+
44
+ <% filename = "bucket-name/filename.mp4/flv %>
45
+ <% file = Cloudfront.get_url(request,filename) %>
46
+ <div id='media-file'></div>
47
+
48
+ Write javascript file to stream your private in jwplayer with jquery
49
+
50
+ function private_streaming(){
51
+ var flashvars = {
52
+ file: media,
53
+ provider:'rtmp',
54
+ streamer: 'rtmp://xxxxx.cloudfront.net/cfx/st'
55
+ };
56
+ var params = {
57
+ allowfullscreen: 'false',
58
+ allowscriptaccess:'always'
59
+ };
60
+ var attributes ={
61
+ id: 'jwplayer',
62
+ name: 'jwplayer'
63
+ }
64
+ swfobject.embedSWF('/jwplayer/player.swf','media_player','300','24','9.0.115','false',
65
+ flashvars, params, attributes,flashLoaded);
66
+ }
67
+
68
+ $(document).ready(private_streaming);
69
+
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/cloudfront-private/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Krishna Srihari"]
6
+ gem.description = %q{Cloudfront private streaming from Ruby on Rails }
7
+ gem.summary = %q{Cloudfront private streaming for mp3, mp4 and flv}
8
+
9
+ gem.add_development_dependency "rails", ">= 3.0.0"
10
+ gem.add_development_dependency "rspec"
11
+ gem.add_development_dependency "cucumber"
12
+ gem.add_development_dependency "aruba"
13
+ gem.add_development_dependency "thor"
14
+
15
+ gem.files = `git ls-files`.split($\)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.name = "cloudfront-private"
19
+ gem.require_paths = ["lib"]
20
+ gem.version = Cloudfront::Private::VERSION
21
+ end
@@ -0,0 +1,19 @@
1
+ Feature:
2
+ In order to save the world
3
+ As an open-source evangelist
4
+ I want everyone to use Cloudfront-private and its generators.
5
+
6
+ Scenario: cloudfront:install generator bootstraps the basic files
7
+ Given I run "rails new my_app"
8
+ And I cd to "my_app"
9
+ And a file named "Gemfile" with:
10
+ """
11
+ source "http://rubygems.org"
12
+ gem 'rails'
13
+ gem 'sqlite3'
14
+ gem 'cloudfront-private', :path => '../../../'
15
+ """
16
+ When I run "bundle install"
17
+ And I run "rails generate cloudfront:install"
18
+ Then the following files should exist:
19
+ | config/initializers/cloudfront-private.rb |
@@ -0,0 +1,3 @@
1
+ Then /^the file "([^"]*)" should contain:$/ do |file, content|
2
+ check_file_content(file, content, true)
3
+ end
@@ -0,0 +1,3 @@
1
+ Before do
2
+ @aruba_timeout_seconds = 50
3
+ end
@@ -0,0 +1 @@
1
+ require 'aruba/cucumber'
@@ -0,0 +1,10 @@
1
+ require 'cloudfront-private/streaming/base'
2
+
3
+ module Cloudfront
4
+ module Private
5
+ class Base
6
+ include Cloudfront::Private::Streaming::Base
7
+ include Cloudfront::Private::Configuration
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_support/concern'
2
+
3
+ module Cloudfront
4
+ module Private
5
+ module Configuration
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ add_config :key_pair_id
10
+ add_config :pem_file
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def add_config(name)
16
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
17
+ def self.#{name}(value=nil)
18
+ @#{name} = value if value
19
+ return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
20
+ name = superclass.#{name}
21
+ return nil if name.nil? && !instance_variable_defined?("@#{name}")
22
+ @#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
23
+ end
24
+
25
+ def self.#{name}=(value)
26
+ @#{name} = value
27
+ end
28
+
29
+ def #{name}
30
+ value = self.class.#{name}
31
+ value.instance_of?(Proc) ? value.call : value
32
+ end
33
+ RUBY
34
+ end
35
+
36
+ def configure
37
+ yield self
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ require 'openssl'
2
+ require 'digest/sha2'
3
+ require 'base64'
4
+ require "cloudfront-private/version"
5
+ require 'cloudfront-private/configuration'
6
+
7
+ require 'rails'
8
+
9
+
10
+ module Cloudfront
11
+ module Private
12
+ module Streaming
13
+ module Base
14
+ extend ActiveSupport::Concern
15
+ include Cloudfront::Private::Configuration
16
+
17
+ KEY_PAIR_ID = @key_pair_id
18
+
19
+ def self.get_url(_request, _resource)
20
+ url = get_signed_url(_resource, _request)
21
+ # Escape characters for proper embedding in Flash parameters
22
+ return url.gsub("?","%3F").gsub("=","%3D").gsub("&","%26")
23
+ end
24
+
25
+ protected
26
+
27
+ def self.get_signed_url(_resource, _request)
28
+ pk_file = Rails.root.join('certs',@pem_file)
29
+ key = OpenSSL::PKey::RSA.new(File.readlines(pk_file).join(""))
30
+ policy = create_policy(_resource, _request)
31
+ sigcrypt = key.sign(OpenSSL::Digest::SHA1.new, policy)
32
+ urlsig = escape_string(Base64.encode64(sigcrypt))
33
+ encoded_policy = escape_string(Base64.encode64(policy))
34
+ return "#{_resource}?Policy=#{encoded_policy}&Signature=#{urlsig}&Key-Pair-Id=#{KEY_PAIR_ID}"
35
+ end
36
+
37
+ def self.create_policy(_resource, _request)
38
+ expires = 1.hour.from_now.to_i
39
+ # Skip IP check for localhost (AWS:SourceIp does not work with localhost)
40
+ if Rails.env.development?
41
+ policy = %{ {"Statement":
42
+ [{
43
+ "Resource": "#{_resource}",
44
+ "Condition": {
45
+ "DateLessThan": { "AWS:EpochTime": #{expires} }
46
+ }
47
+ }]}
48
+ }
49
+ else
50
+ ip = "#{_request.remote_ip}/24"
51
+ policy = %{ {"Statement":
52
+ [{
53
+ "Resource": "#{_resource}",
54
+ "Condition": {
55
+ "DateLessThan": { "AWS:EpochTime": #{expires} },
56
+ "IpAddress": { "AWS:SourceIp": "#{ip}" }
57
+ }
58
+ }]}
59
+ }
60
+ end
61
+ return policy
62
+ end
63
+
64
+ def self.escape_string(_str)
65
+ _str.gsub('+','-').gsub('=','_').gsub('/','~').gsub(/\n/,'')
66
+ end
67
+
68
+
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ module Cloudfront
2
+ module Private
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ require "cloudfront-private/version"
2
+ require 'cloudfront-private/base'
3
+ require 'cloudfront-private/configuration'
4
+ require 'active_support/concern'
5
+
6
+ module Cloudfront
7
+ module Private
8
+ class << self
9
+ attr_accessor :root, :base_path
10
+
11
+ def configure(&block)
12
+ Cloudfront::Private::Base.configure(&block)
13
+ end
14
+ end
15
+
16
+ autoload :Base, 'cloudfront-private/base'
17
+ autoload :Configuration, 'cloudfront-private/configuration'
18
+
19
+ module Streaming
20
+ autoload :Base, 'cloudfront-private/streaming/base'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+ require 'rails/generators'
3
+
4
+ module Cloudfront
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "This generator creates an initializer file at config/initializers"
9
+ def add_initializer
10
+ template "cloudfront-private.rb", "config/initializers/cloudfront-private.rb"
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,4 @@
1
+ Cloudfront::Private.configure do |config|
2
+ config.key_pair_id = 'your-key-pair-id'
3
+ config.pem_file = 'your-private.pem'
4
+ end
@@ -0,0 +1,22 @@
1
+ require 'cloudfront-private'
2
+
3
+ describe Cloudfront::Private do
4
+
5
+ context "configuration" do
6
+ before do
7
+ Cloudfront::Private.configure do |config|
8
+ config.key_pair_id = '867234983'
9
+ config.pem_file = 'xyz.pem'
10
+ end
11
+ end
12
+
13
+ it "key_pair_id" do
14
+ Cloudfront::Private::Base.key_pair_id.should eq('867234983')
15
+ end
16
+
17
+ it "pem file" do
18
+ Cloudfront::Private::Base.pem_file.should eq('xyz.pem')
19
+ end
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudfront-private
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Krishna Srihari
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &72008130 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *72008130
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &72007920 !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: *72007920
36
+ - !ruby/object:Gem::Dependency
37
+ name: cucumber
38
+ requirement: &72007690 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *72007690
47
+ - !ruby/object:Gem::Dependency
48
+ name: aruba
49
+ requirement: &72007480 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *72007480
58
+ - !ruby/object:Gem::Dependency
59
+ name: thor
60
+ requirement: &72007270 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *72007270
69
+ description: ! 'Cloudfront private streaming from Ruby on Rails '
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - cloudfront-private.gemspec
81
+ - features/generator.feature
82
+ - features/step_definitions/aruba_steps.rb
83
+ - features/support/env.rb
84
+ - features/support/setup.rb
85
+ - lib/cloudfront-private.rb
86
+ - lib/cloudfront-private/base.rb
87
+ - lib/cloudfront-private/configuration.rb
88
+ - lib/cloudfront-private/streaming/base.rb
89
+ - lib/cloudfront-private/version.rb
90
+ - lib/generators/cloudfront/install/install_generator.rb
91
+ - lib/generators/cloudfront/install/templates/cloudfront-private.rb
92
+ - spec/cloudfront-private_spec.rb
93
+ homepage:
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.6
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Cloudfront private streaming for mp3, mp4 and flv
117
+ test_files:
118
+ - features/generator.feature
119
+ - features/step_definitions/aruba_steps.rb
120
+ - features/support/env.rb
121
+ - features/support/setup.rb
122
+ - spec/cloudfront-private_spec.rb