oauthorizer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ config/oauthorizer_config.yml
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@oauthorizer --create
data/Gemfile CHANGED
@@ -1,6 +1,13 @@
1
1
  source 'https://rubygems.org'
2
- gem 'rack', '~> 1.4'
3
- gem 'capybara'
4
2
 
5
3
  # Specify your gem's dependencies in oauthorizer.gemspec
6
4
  gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'capybara'
8
+ gem 'debugger'
9
+ gem 'rspec-rails'
10
+ gem 'httparty'
11
+
12
+ gem 'rails'
13
+ gem 'addressable', git: 'https://github.com/sporkmonger/addressable.git'
data/apps/rails_app.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'rails'
2
+ require 'action_controller/railtie'
3
+
4
+ module RailsApp
5
+ class Application < Rails::Application
6
+
7
+ routes.draw do
8
+ resources :callbacks do
9
+ collection do
10
+ get 'google'
11
+ end
12
+ end
13
+
14
+ get '/' => 'rails/profiles#show'
15
+ end
16
+ end
17
+
18
+ class ProfilesController < ActionController::Base
19
+ def show
20
+ render text: 'heyo!'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ google:
2
+ client_id:
3
+ client_secret:
4
+ scope: https://www.googleapis.com/auth/latitude.all.best+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email
5
+ redirect_uri: http://localhost:3030/callbacks/google.json
6
+ server_port: 3030
7
+ user_email: 'test@example.com'
8
+ user_password: 'password'
9
+ facebook:
10
+ client_id:
11
+ client_secret:
12
+ scope: publish_checkins
data/lib/oauthorizer.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'oauthorizer/rack/oauthorizer'
1
2
  require "oauthorizer/version"
2
3
  require 'capybara'
3
4
  require 'capybara/dsl'
@@ -5,49 +6,97 @@ require 'capybara/dsl'
5
6
  module Oauthorizer
6
7
  class Token
7
8
  include Capybara::DSL
8
- def get_google_code
9
- Capybara.default_driver = :selenium
10
- Capybara.server_port = 3030
11
- self.visit "https://accounts.google.com/o/oauth2/auth?" +
12
- "scope=https://www.googleapis.com/auth/latitude.all.best+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email" +
13
- "&redirect_uri=http://localhost:3030/callbacks/google.json" +
14
- "&response_type=code" +
15
- "&client_id=26827780047.apps.googleusercontent.com" +
16
- "&approval_prompt=force" +
17
- "&access_type=offline"
18
- fill_in 'Email', with: 'test@rebelhold.com'
19
- fill_in 'Passwd', with: 'rht3sting'
20
- click_button 'signIn'
21
- click_button 'submit_approve_access'
22
-
23
- token = JSON.parse(page.text)
24
- end
25
- def get_google_token_hash
26
- Capybara.default_driver = :selenium
27
- Capybara.server_port = 3030
28
- self.visit "https://accounts.google.com/o/oauth2/auth?" +
29
- "scope=https://www.googleapis.com/auth/latitude.all.best+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email" +
30
- "&redirect_uri=http://localhost:3030/callbacks/google.json" +
31
- "&response_type=code" +
32
- "&client_id=26827780047.apps.googleusercontent.com" +
33
- "&approval_prompt=force" +
34
- "&access_type=offline"
35
- fill_in 'Email', with: 'test@rebelhold.com'
36
- fill_in 'Passwd', with: 'rht3sting'
37
- click_button 'signIn'
38
- click_button 'submit_approve_access'
39
-
40
- token = JSON.parse(page.text)
41
- end
42
- def self.update_from_token_hash provider_type, token_hash
43
- config_file = File.join(Rails.root, 'config', 'oautherizer_config.yml')
44
- credentials ||= YAML.load_file(config_file)
45
-
46
- credentials[provider_type].merge!(token_hash)
47
-
48
- new_file = File.open(config_file, 'w+')
49
- new_file.write(credentials.to_yaml.gsub("---\n", ''))
50
- new_file.close
51
- end
52
- end
9
+ def get_google_token_hash
10
+ config_file = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
11
+ oauthorizer_keys = YAML.load_file(config_file)['google']
12
+ if oauthorizer_keys['expires_at'].nil? || oauthorizer_keys['expires_at'] < Time.now
13
+ Capybara.default_driver = :selenium
14
+ Capybara.server_port = oauthorizer_keys['server_port']
15
+ self.visit "https://accounts.google.com/o/oauth2/auth?" +
16
+ "scope=#{oauthorizer_keys['scope']}" +
17
+ "&redirect_uri=#{oauthorizer_keys['redirect_uri']}" +
18
+ "&client_id=#{oauthorizer_keys['client_id']}" +
19
+ "&response_type=code" +
20
+ "&approval_prompt=force" +
21
+ "&access_type=offline"
22
+ fill_in 'Email', with: oauthorizer_keys['user_email']
23
+ fill_in 'Passwd', with: oauthorizer_keys['user_password']
24
+ click_button 'signIn'
25
+ click_button 'submit_approve_access'
26
+
27
+ parsed_response = JSON.parse page.text
28
+ self.update_from_token_hash 'google', parsed_response, 'expires_in'
29
+
30
+ parsed_response
31
+ else
32
+ oauthorizer_keys
33
+ end
34
+ end
35
+
36
+ def get_facebook_token_hash
37
+ config_file = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
38
+ oauthorizer_keys = YAML.load_file(config_file)['facebook']
39
+ if oauthorizer_keys['expires_at'].nil? || oauthorizer_keys['expires_at'] < Time.now
40
+ Capybara.default_driver = :selenium
41
+ Capybara.server_port = oauthorizer_keys['server_port']
42
+ self.visit "https://www.facebook.com/dialog/oauth?" +
43
+ "client_id=#{oauthorizer_keys['client_id']}" +
44
+ "&redirect_uri=#{oauthorizer_keys['redirect_uri']}" +
45
+ "&scope=#{oauthorizer_keys['scope']}" +
46
+ "&state=#{oauthorizer_keys['state']}"
47
+ fill_in 'email', with: oauthorizer_keys['user_email']
48
+ fill_in 'pass', with: oauthorizer_keys['user_password']
49
+ find('#loginbutton').click
50
+ find('#grant_required_clicked').click if page.has_css?('#grant_required_clicked')
51
+ find('#grant_clicked').click if page.has_css?('#grant_clicked')
52
+
53
+ parsed_response = JSON.parse page.text
54
+ self.update_from_token_hash 'facebook', parsed_response, 'expires'
55
+
56
+ parsed_response
57
+ else
58
+ oauthorizer_keys
59
+ end
60
+ end
61
+
62
+ def get_foursquare_token_hash
63
+ config_file = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
64
+ oauthorizer_keys = YAML.load_file(config_file)['foursquare']
65
+ if oauthorizer_keys['access_token'].nil?
66
+ Capybara.default_driver = :selenium
67
+ Capybara.server_port = oauthorizer_keys['server_port']
68
+ self.visit "https://www.foursquare.com/oauth2/authenticate?" +
69
+ "client_id=#{oauthorizer_keys['client_id']}" +
70
+ "&redirect_uri=#{oauthorizer_keys['redirect_uri']}" +
71
+ "&response_type=code"
72
+ find('.newGreenButton').click
73
+ fill_in 'username', with: oauthorizer_keys['user_email']
74
+ fill_in 'password', with: oauthorizer_keys['user_password']
75
+ find('.greenButton').click
76
+ # find('.newGreenButton').click
77
+
78
+ parsed_response = JSON.parse page.text
79
+ self.update_from_token_hash 'foursquare', parsed_response
80
+
81
+ parsed_response
82
+ else
83
+ oauthorizer_keys
84
+ end
85
+ end
86
+
87
+ def update_from_token_hash provider_type, token_hash, time_string=nil
88
+ config_file = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
89
+ credentials ||= YAML.load_file(config_file)
90
+ credentials[provider_type].merge!(token_hash)
91
+ credentials[provider_type].merge!('expires_at' => (Time.now + token_hash[time_string].to_i.seconds)) unless time_string.nil?
92
+
93
+ new_file = File.open(config_file, 'w+')
94
+ new_file.write(credentials.to_yaml.gsub("---\n", ''))
95
+ new_file.close
96
+ end
97
+
98
+ def self.get_config_file
99
+ YAML.load_file(File.expand_path('config/oauthorizer_config.yml'))
100
+ end
101
+ end
53
102
  end
@@ -0,0 +1,51 @@
1
+ module Rack
2
+ class Oauthorizer
3
+ require 'httparty'
4
+ require 'oauthorizer'
5
+ def initialize(app, options={})
6
+ @app = app
7
+ @options = options
8
+ end
9
+
10
+ def call(env)
11
+ @request = Rack::Request.new(env)
12
+ if @request.path == '/callbacks/google.json'
13
+ # oauthorizer_keys = Oauthorizer::Token.get_config_file['google']
14
+ post_hash = {
15
+ code: @request.params['code'],
16
+ grant_type: 'authorization_code',
17
+ redirect_uri: 'http://localhost:3030/callbacks/google.json',
18
+ client_id: @options['google']['client_id'],
19
+ client_secret: @options['google']['client_secret']
20
+ }
21
+ token_response = HTTParty.post('https://accounts.google.com/o/oauth2/token', body: post_hash).parsed_response
22
+ [200, {"Content-Type"=> 'application/json'}, [token_response.to_json]]
23
+ elsif @request.path == '/callbacks/facebook.json'
24
+ # oauthorizer_keys = Oauthorizer::Token.get_config_file['google']
25
+ post_hash = {
26
+ code: @request.params['code'],
27
+ redirect_uri: 'http://localhost:3030/callbacks/facebook.json',
28
+ client_id: @options['facebook']['client_id'],
29
+ client_secret: @options['facebook']['client_secret']
30
+ }
31
+
32
+ token_response = HTTParty.post('https://graph.facebook.com/oauth/access_token', body: post_hash).parsed_response
33
+
34
+ parsed_response = Rack::Utils.parse_query token_response
35
+ [200, {"Content-Type"=> 'application/json'}, [parsed_response.to_json]]
36
+ elsif @request.path == '/callbacks/foursquare.json'
37
+ # oauthorizer_keys = Oauthorizer::Token.get_config_file['google']
38
+ post_hash = {
39
+ code: @request.params['code'],
40
+ grant_type: 'authorization_code',
41
+ redirect_uri: 'http://localhost:3030/callbacks/foursquare.json',
42
+ client_id: @options['foursquare']['client_id'],
43
+ client_secret: @options['foursquare']['client_secret']
44
+ }
45
+
46
+ token_response = HTTParty.post('https://foursquare.com/oauth2/access_token', body: post_hash).parsed_response
47
+ [200, {"Content-Type"=> 'application/json'}, [token_response.to_json]]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module Oauthorizer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Google Oauth' do
4
+ it 'gets a code' do
5
+ oauther = Oauthorizer::Token.new
6
+ @token_hash = oauther.get_google_token_hash
7
+ @token_hash['access_token'].should be
8
+ end
9
+
10
+ end
@@ -0,0 +1,21 @@
1
+ # load test applications
2
+ Dir[File.expand_path('../../apps/*.rb', __FILE__)].each do |f|
3
+ require f
4
+ end
5
+ require 'capybara/rspec'
6
+ require 'oauthorizer/rack/oauthorizer.rb'
7
+ require 'oauthorizer'
8
+ require 'rspec/rails'
9
+
10
+ require 'YAML'
11
+ oauthorizer_keys = YAML.load_file(File.expand_path('config/oauthorizer_config.yml'))
12
+
13
+
14
+
15
+
16
+ # configure rails application to use MyRackMiddleware
17
+ RailsApp::Application.configure do |app|
18
+
19
+ app.middleware.insert_before ActionDispatch::Static, Rack::Oauthorizer, oauthorizer_keys
20
+
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauthorizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-27 00:00:00.000000000 Z
12
+ date: 2012-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
@@ -35,14 +35,19 @@ extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
37
  - .gitignore
38
+ - .rvmrc
38
39
  - Gemfile
39
40
  - LICENSE
40
41
  - README.md
41
42
  - Rakefile
43
+ - apps/rails_app.rb
44
+ - config/oauthorizer_config.yml.example
42
45
  - lib/oauthorizer.rb
43
- - lib/oauthorizer/rack/google_oauthorizer.rb
46
+ - lib/oauthorizer/rack/oauthorizer.rb
44
47
  - lib/oauthorizer/version.rb
45
48
  - oauthorizer.gemspec
49
+ - spec/google_spec.rb
50
+ - spec/spec_helper.rb
46
51
  homepage: ''
47
52
  licenses: []
48
53
  post_install_message:
@@ -67,5 +72,6 @@ rubygems_version: 1.8.24
67
72
  signing_key:
68
73
  specification_version: 3
69
74
  summary: Uses rack middleware and capbara to get oauth and refresh tokens from providers
70
- test_files: []
71
- has_rdoc:
75
+ test_files:
76
+ - spec/google_spec.rb
77
+ - spec/spec_helper.rb
File without changes