omniauth-twitch_oauth2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+
7
+ group :test do #okay better move this one time to gemfile
8
+ gem 'rspec', '~> 2.8.0'
9
+ gem 'webmock'
10
+ gem 'rack-test'
11
+ gem 'simplecov'
12
+ end
13
+
14
+ group :example do #should it also support rails and devise?
15
+ gem 'sinatra'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-twitch_oauth2 (0.0.1)
5
+ omniauth (~> 1.0)
6
+ omniauth-oauth2
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ addressable (2.3.3)
12
+ crack (0.3.2)
13
+ diff-lcs (1.1.3)
14
+ faraday (0.8.7)
15
+ multipart-post (~> 1.1)
16
+ hashie (2.0.3)
17
+ httpauth (0.2.0)
18
+ jwt (0.1.8)
19
+ multi_json (>= 1.5)
20
+ multi_json (1.7.2)
21
+ multipart-post (1.2.0)
22
+ oauth2 (0.8.1)
23
+ faraday (~> 0.8)
24
+ httpauth (~> 0.1)
25
+ jwt (~> 0.1.4)
26
+ multi_json (~> 1.0)
27
+ rack (~> 1.2)
28
+ omniauth (1.1.4)
29
+ hashie (>= 1.2, < 3)
30
+ rack
31
+ omniauth-oauth2 (1.1.1)
32
+ oauth2 (~> 0.8.0)
33
+ omniauth (~> 1.0)
34
+ rack (1.5.2)
35
+ rack-protection (1.5.0)
36
+ rack
37
+ rack-test (0.6.2)
38
+ rack (>= 1.0)
39
+ rake (10.0.4)
40
+ rspec (2.8.0)
41
+ rspec-core (~> 2.8.0)
42
+ rspec-expectations (~> 2.8.0)
43
+ rspec-mocks (~> 2.8.0)
44
+ rspec-core (2.8.0)
45
+ rspec-expectations (2.8.0)
46
+ diff-lcs (~> 1.1.2)
47
+ rspec-mocks (2.8.0)
48
+ simplecov (0.7.1)
49
+ multi_json (~> 1.0)
50
+ simplecov-html (~> 0.7.1)
51
+ simplecov-html (0.7.1)
52
+ sinatra (1.4.2)
53
+ rack (~> 1.5, >= 1.5.2)
54
+ rack-protection (~> 1.4)
55
+ tilt (~> 1.3, >= 1.3.4)
56
+ tilt (1.3.7)
57
+ webmock (1.11.0)
58
+ addressable (>= 2.2.7)
59
+ crack (>= 0.3.2)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ omniauth-twitch_oauth2!
66
+ rack-test
67
+ rake
68
+ rspec (~> 2.8.0)
69
+ simplecov
70
+ sinatra
71
+ webmock
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # OmniAuth Twitch.tv OAuth2 Strategy
2
+
3
+ ## License
4
+
5
+ Copyright (c) 2013 by Roman Budnikov
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+
13
+ ### Contributors
14
+
15
+ nirnanaaa https://github.com/nirnanaaa
data/Rakefile ADDED
@@ -0,0 +1,134 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'date'
5
+ require 'rspec/core/rake_task'
6
+
7
+
8
+ #############
9
+ # internals #
10
+ #############
11
+
12
+ def name
13
+ @name ||= Dir['*.gemspec'].first.split('.').first
14
+ end
15
+
16
+ def version
17
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
18
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
19
+ end
20
+
21
+ # assumes x.y.z all digit version
22
+ def next_version
23
+ # x.y.z
24
+ v = version.split '.'
25
+ # bump z
26
+ v[-1] = v[-1].to_i + 1
27
+ v.join '.'
28
+ end
29
+ def bump_version
30
+ old_file = File.read("lib/#{name}.rb")
31
+ old_version_line = old_file[/^\s*VERSION\s*=\s*.*/]
32
+ new_version = next_version
33
+ old_file.sub!(old_version_line, " VERSION = '#{new_version}'")
34
+ File.write("lib/#{name}.rb", old_file)
35
+ new_version
36
+ end
37
+
38
+ def date
39
+ Date.today.to_s
40
+ end
41
+
42
+ def gemspec_file
43
+ "#{name}.gemspec"
44
+ end
45
+
46
+ def gem_file
47
+ "#{name}-#{version}.gem"
48
+ end
49
+
50
+ def rubyforge_project
51
+ name
52
+ end
53
+
54
+ def replace_header(head, header_name)
55
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
56
+ end
57
+
58
+ #########
59
+ # tasks #
60
+ #########
61
+ task :default => :spec
62
+ RSpec::Core::RakeTask.new(:spec) do |t|
63
+ t.verbose = false
64
+ end
65
+
66
+ desc "Open an irb session preloaded with this library"
67
+ task :console do
68
+ sh "irb -rubygems -r ./lib/#{name}.rb"
69
+ end
70
+
71
+ desc "Update version number and gemspec"
72
+ task :bump do
73
+ puts "Updated version to #{bump_version}"
74
+ # Execute does not invoke dependencies.
75
+ # Manually invoke gemspec then validate.
76
+ Rake::Task[:gemspec].execute
77
+ Rake::Task[:validate].execute
78
+ end
79
+
80
+ desc 'Create a release build'
81
+ task :release => :build do
82
+ unless `git branch` =~ /^\* master$/
83
+ puts "You must be on the master branch to release!"
84
+ exit!
85
+ end
86
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
87
+ sh "git pull"
88
+ sh "git tag v#{version}"
89
+ sh "git push origin master"
90
+ sh "git push origin v#{version}"
91
+ sh "gem push pkg/#{name}-#{version}.gem"
92
+ end
93
+
94
+ desc 'Build gem'
95
+ task :build => :gemspec do
96
+ sh "mkdir -p pkg"
97
+ sh "gem build #{gemspec_file}"
98
+ sh "mv #{gem_file} pkg"
99
+ end
100
+
101
+ desc 'Update gemspec'
102
+ task :gemspec => :validate do
103
+ # read spec file and split out manifest section
104
+ spec = File.read(gemspec_file)
105
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
106
+ # replace name version and date
107
+ replace_header(head, :name)
108
+ replace_header(head, :version)
109
+ replace_header(head, :date)
110
+ replace_header(head, :rubyforge_project)
111
+
112
+ # determine file list from git ls-files
113
+ files = `git ls-files`.
114
+ split("\n").
115
+ sort.
116
+ reject { |file| file =~ /^\./ }.
117
+ reject { |file| file =~ /^(doc|pkg|script|config|Home\.md|\.gitattributes)/ }.
118
+ map { |file| " #{file}" }.
119
+ join("\n")
120
+ # piece file back together and write
121
+ manifest = " s.files = %w[\n#{files}\n ]\n"
122
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
123
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
124
+ puts "Updated #{gemspec_file}"
125
+ end
126
+
127
+ desc 'Validate lib files and version file'
128
+ task :validate do
129
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}", "lib/generators"]
130
+ unless Dir['VERSION*'].empty?
131
+ puts "A `VERSION` file at root level violates Gem best practices."
132
+ exit!
133
+ end
134
+ end
@@ -0,0 +1,43 @@
1
+ # Sample app for Google OAuth2 Strategy
2
+ # Make sure to setup the ENV variables TWITCH_KEY and TWITCH_SECRET
3
+ # Run with "bundle exec rackup"
4
+
5
+ require 'rubygems'
6
+ require 'bundler'
7
+ require 'sinatra'
8
+ require 'omniauth'
9
+ require 'omniauth-twitch-oauth2'
10
+
11
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
12
+
13
+ class App < Sinatra::Base
14
+ get '/' do
15
+ <<-HTML
16
+ <ul>
17
+ <li><a href='/auth/twitch_oauth2'>Sign in with TWITCH</a></li>
18
+ </ul>
19
+ HTML
20
+ end
21
+
22
+ get '/auth/:provider/callback' do
23
+ content_type 'text/plain'
24
+ request.env['omniauth.auth'].to_hash.inspect rescue "No Data"
25
+ end
26
+
27
+ get '/auth/failure' do
28
+ content_type 'text/plain'
29
+ request.env['omniauth.auth'].to_hash.inspect rescue "No Data"
30
+ end
31
+ end
32
+
33
+ use Rack::Session::Cookie, :secret => ENV['RACK_COOKIE_SECRET']
34
+
35
+ use OmniAuth::Builder do
36
+ # Regular usage
37
+ provider :google_oauth2, ENV['TWITCH_KEY'], ENV['TWITCH_SECRET'], {}
38
+
39
+ # Custom scope supporting youtube
40
+ # provider :google_twitch, ENV['TWITCH_KEY'], ENV['TWITCH_SECRET'], {:scope => 'user_read channel_read'}
41
+ end
42
+
43
+ run App.new
@@ -0,0 +1,3 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ provider :twitch, ENV['TWITCH_KEY'], ENV['TWITCH_SECRET']
3
+ end
@@ -0,0 +1,13 @@
1
+ # see official omniauth documentation!
2
+ module OmniAuth
3
+
4
+ # Twitch strategy using oauth2
5
+ module TwitchOauth2
6
+
7
+
8
+ # version string
9
+ VERSION = '0.0.1'
10
+ end
11
+ end
12
+
13
+ require "omniauth/twitch_oauth2" #isn't it better to use Autoload :twitch_oauth2, "omniauth/twitch_oauth2" ?
@@ -0,0 +1,58 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class TwitchOauth2 < OAuth2
6
+
7
+ # Possible scopes:
8
+ #user_read: Read access to non-public user information, such as email address.
9
+ #user_blocks_edit: Ability to ignore or unignore on behalf of a user.
10
+ #user_blocks_read: Read access to a user's list of ignored users.
11
+ #user_follows_edit: Access to manage a user's followed channels.
12
+ #channel_read: Read access to non-public channel information, including email address and stream key.
13
+ #channel_editor: Write access to channel metadata (game, status, etc).
14
+ #channel_commercial: Access to trigger commercials on channel.
15
+ #channel_stream: Ability to reset a channel's stream key.
16
+ #channel_subscriptions: Read access to all subscribers to your channel.
17
+ #channel_check_subscription: Read access to check if a user is subscribed to your channel.
18
+ #chat_login: Ability to log into chat and send messages.
19
+ DEFAULT_SCOPE = "user_read"
20
+
21
+ option :name, 'twitch_oauth2'
22
+
23
+ option :client_options, {
24
+ :site => 'https://api.twitch.tv',
25
+ :authorize_url => '/kraken/oauth2/authorize',
26
+ :token_url => '/kraken/oauth2/token',
27
+ # user should be able to use a proxy i think
28
+ :proxy => ENV['http_proxy'] ? URI(ENV['http_proxy']) : nil
29
+ }
30
+
31
+
32
+ def authorize_params
33
+ super.tap do |params|
34
+ params[:scope] = params[:scope] ||= DEFAULT_SCOPE
35
+ session['omniauth.state'] = params[:state] if request.params['state']
36
+ end
37
+ end
38
+
39
+ uid{ raw_info['_id'] }
40
+
41
+ info do
42
+ {
43
+ nickname: raw_info['name'],
44
+ email: raw_info['email']
45
+ }
46
+ end
47
+
48
+ extra do
49
+ hash = {}
50
+ hash[:raw_info] = raw_info unless skip_info?
51
+ end
52
+
53
+ def raw_info
54
+ @raw_info ||= access_token.get('https://api.twitch.tv/kraken/user', params: { oauth_token: access_token.token }).parsed
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1 @@
1
+ require 'omniauth/strategies/twitch_oauth2'
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+
5
+ s.rubygems_version = '>= 1.3.5'
6
+
7
+ s.name = "omniauth-twitch_oauth2"
8
+ s.version = '0.0.1'
9
+ s.rubyforge_project = s.name
10
+
11
+
12
+ s.authors = ["Roman Budnikov"]
13
+ s.email = ["budnikov1990@gmail.com"]
14
+ s.description = "A Twitch oauth2 strategy for OmniAuth 1.0."
15
+ s.summary = "A Twitch oauth2 strategy for OmniAuth 1.0"
16
+ s.homepage = ""
17
+
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }# do you have any executables ?
19
+ s.test_files = `git ls-files -- spec/*`.split("\n")
20
+ s.require_paths = ["lib"]
21
+
22
+
23
+ # = MANIFEST =
24
+ s.files = %w[
25
+ Gemfile
26
+ Gemfile.lock
27
+ README.md
28
+ Rakefile
29
+ examples/config.ru
30
+ examples/omni_auth.rb
31
+ lib/omniauth-twitch_oauth2.rb
32
+ lib/omniauth/strategies/twitch_oauth2.rb
33
+ lib/omniauth/twitch_oauth2.rb
34
+ omniauth-twitch_oauth2.gemspec
35
+ spec/omniauth/strategies/twitch_oauth2_spec.rb
36
+ spec/omniauth/strategies/twitch_spec.rb
37
+ spec/spec_helper.rb
38
+ ]
39
+ # = MANIFEST =
40
+ s.add_runtime_dependency 'omniauth-oauth2'
41
+
42
+ s.add_dependency 'omniauth', '~> 1.0'
43
+
44
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::TwitchOauth2 do
4
+ subject do
5
+ OmniAuth::Strategies::TwitchOauth2.new({})
6
+ end
7
+
8
+ it 'should have a correct name' do
9
+ subject.options.name.should == 'twitch_oauth2'
10
+ end
11
+
12
+
13
+ describe "api options" do
14
+ let(:client_options){{:site=> 'https://api.twitch.tv',:authorize_url => '/kraken/oauth2/authorize',:token_url => '/kraken/oauth2/token'}}
15
+
16
+ it 'should use the right count of options' do
17
+ subject.options.client_options.should have(4).items #site, authorize_url, token_url
18
+ end
19
+
20
+ it 'should have the correct site' do
21
+ subject.options.client_options.site.should == client_options[:site]
22
+ end
23
+
24
+ it 'should have the correct authorize_url' do
25
+ subject.options.client_options.authorize_url.should == client_options[:authorize_url]
26
+ end
27
+
28
+ it 'should have the correct token_url' do
29
+ subject.options.client_options.token_url.should == client_options[:token_url]
30
+ end
31
+ end
32
+
33
+ end
File without changes
@@ -0,0 +1,20 @@
1
+ #require 'simplecov'
2
+ require 'rspec'
3
+ require 'rack/test'
4
+ require 'webmock/rspec'
5
+ require 'omniauth'
6
+ require 'omniauth-twitch_oauth2'
7
+
8
+ RSpec.configure do |config|
9
+ config.include WebMock::API
10
+ config.include Rack::Test::Methods
11
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
12
+ config.expect_with :rspec do |s|
13
+ s.syntax = :expec
14
+ end
15
+
16
+ config.alias_example_to :fit, :focused => true
17
+ config.filter_run :focused => true
18
+ config.run_all_when_everything_filtered = true
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-twitch_oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roman Budnikov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth-oauth2
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: omniauth
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ description: A Twitch oauth2 strategy for OmniAuth 1.0.
47
+ email:
48
+ - budnikov1990@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - README.md
56
+ - Rakefile
57
+ - examples/config.ru
58
+ - examples/omni_auth.rb
59
+ - lib/omniauth-twitch_oauth2.rb
60
+ - lib/omniauth/strategies/twitch_oauth2.rb
61
+ - lib/omniauth/twitch_oauth2.rb
62
+ - omniauth-twitch_oauth2.gemspec
63
+ - spec/omniauth/strategies/twitch_oauth2_spec.rb
64
+ - spec/omniauth/strategies/twitch_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
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: omniauth-twitch_oauth2
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A Twitch oauth2 strategy for OmniAuth 1.0
90
+ test_files:
91
+ - spec/omniauth/strategies/twitch_oauth2_spec.rb
92
+ - spec/omniauth/strategies/twitch_spec.rb
93
+ - spec/spec_helper.rb