OpenAuth2 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/License +9 -0
- data/Rakefile +126 -0
- data/Readme.markdown +224 -0
- data/Spec.markdown +0 -0
- data/examples/fb.rb +9 -0
- data/examples/google.rb +53 -0
- data/lib/open_auth2.rb +33 -0
- data/lib/open_auth2/client.rb +182 -0
- data/lib/open_auth2/config.rb +102 -0
- data/lib/open_auth2/connection.rb +38 -0
- data/lib/open_auth2/delegate_to_config.rb +25 -0
- data/lib/open_auth2/provider.rb +21 -0
- data/lib/open_auth2/provider/base.rb +31 -0
- data/lib/open_auth2/provider/default.rb +18 -0
- data/lib/open_auth2/provider/facebook.rb +23 -0
- data/lib/open_auth2/provider/google.rb +28 -0
- data/lib/open_auth2/token.rb +150 -0
- data/lib/open_auth2/version.rb +3 -0
- data/open_auth2.gemspec +74 -0
- data/spec/client_spec.rb +110 -0
- data/spec/config_spec.rb +119 -0
- data/spec/facebook/client_spec.rb +82 -0
- data/spec/facebook/token_spec.rb +92 -0
- data/spec/fixtures/creds.rb +18 -0
- data/spec/fixtures/vcr/fb/access_token.yml +36 -0
- data/spec/fixtures/vcr/fb/cocacola.yml +68 -0
- data/spec/fixtures/vcr/fb/me.yml +36 -0
- data/spec/fixtures/vcr/fb/post.yml +34 -0
- data/spec/fixtures/vcr/fb/refresh_token.yml +36 -0
- data/spec/fixtures/vcr/goog/access_token.yml +38 -0
- data/spec/fixtures/vcr/goog/list.yml +50 -0
- data/spec/fixtures/vcr/goog/post.yml +44 -0
- data/spec/fixtures/vcr/goog/refresh_token.yml +37 -0
- data/spec/google/client_spec.rb +59 -0
- data/spec/google/token_spec.rb +87 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/token_spec.rb +23 -0
- metadata +179 -0
data/Gemfile
ADDED
data/License
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2012, Senthil A. <senthil196@gmail.com>
|
4
|
+
|
5
|
+
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 isfurnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
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.
|
data/Rakefile
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Author:
|
2
|
+
# Tom Preston-Werner part of his https://github.com/mojombo/rakegem
|
3
|
+
#
|
4
|
+
# Modified:
|
5
|
+
# Senthil A
|
6
|
+
|
7
|
+
require 'date'
|
8
|
+
|
9
|
+
# Helper functions
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}/version.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"OpenAuth2-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
# Standard tasks
|
41
|
+
|
42
|
+
task :default => :spec
|
43
|
+
require "rspec/core/rake_task"
|
44
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
45
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
46
|
+
spec.rspec_opts = ['--backtrace']
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "Generate SimpleCov test coverage and open in your browser"
|
50
|
+
task :coverage do
|
51
|
+
sh "rake spec COVERAGE=true"
|
52
|
+
sh "open coverage/index.html"
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Open an irb session preloaded with this library"
|
56
|
+
task :irb do
|
57
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
58
|
+
end
|
59
|
+
|
60
|
+
desc "Open a pry session preloaded with this library"
|
61
|
+
task :pry do
|
62
|
+
sh "pry -r ./lib/#{name}.rb"
|
63
|
+
end
|
64
|
+
|
65
|
+
# Custom tasks (add your own tasks here)
|
66
|
+
|
67
|
+
desc "Open a pry session with examples/fb loaded"
|
68
|
+
task :fb do
|
69
|
+
sh "pry -r ./examples/fb.rb"
|
70
|
+
end
|
71
|
+
|
72
|
+
desc "Open a pry session with examples/google loaded"
|
73
|
+
task :google do
|
74
|
+
sh "pry -r ./examples/google.rb"
|
75
|
+
end
|
76
|
+
|
77
|
+
# Packaging tasks
|
78
|
+
|
79
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
80
|
+
task :release => :build do
|
81
|
+
unless `git branch` =~ /^\* master$/
|
82
|
+
puts "You must be on the master branch to release!"
|
83
|
+
exit!
|
84
|
+
end
|
85
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
86
|
+
sh "git tag v#{version}"
|
87
|
+
sh "git push origin master"
|
88
|
+
sh "git push origin v#{version}"
|
89
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Build #{gem_file} into the pkg directory"
|
93
|
+
task :build => :gemspec do
|
94
|
+
sh "mkdir -p pkg"
|
95
|
+
sh "gem build #{gemspec_file}"
|
96
|
+
sh "mv #{gem_file} pkg"
|
97
|
+
end
|
98
|
+
|
99
|
+
desc "Generate #{gemspec_file}"
|
100
|
+
task :gemspec do
|
101
|
+
# read spec file and split out manifest section
|
102
|
+
spec = File.read(gemspec_file)
|
103
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
104
|
+
|
105
|
+
# replace name version and date
|
106
|
+
replace_header(head, :name)
|
107
|
+
replace_header(head, :version)
|
108
|
+
replace_header(head, :date)
|
109
|
+
#comment this out if your rubyforge_project has a different name
|
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 =~ /^(rdoc|pkg)/ }.
|
118
|
+
map { |file| " #{file}" }.
|
119
|
+
join("\n")
|
120
|
+
|
121
|
+
# piece file back together and write
|
122
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
123
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
124
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
125
|
+
puts "Updated #{gemspec_file}"
|
126
|
+
end
|
data/Readme.markdown
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
# OpenAuth2 [![Build Status](https://secure.travis-ci.org/senthilnambi/OpenAuth2.png?branch=master)][travis]
|
2
|
+
|
3
|
+
[travis]: http://travis-ci.org/senthilnambi/OpenAuth2
|
4
|
+
|
5
|
+
OpenAuth2 is a thin OAuth2 wrapper written on top of Faraday in Ruby. The goal is a simple, well documented, easy to use interface for all your OAuth2 needs.
|
6
|
+
|
7
|
+
* This software is alpha, you're either very brave or very foolish to use this in production of rockets or anything else.
|
8
|
+
* This Readme is best viewed in [DocumentUp](http://documentup.com/senthilnambi/OpenAuth2).
|
9
|
+
|
10
|
+
## Config
|
11
|
+
|
12
|
+
To begin, let's setup the configuration. Here we're assuming connection to Facebook api. OpenAuth2 supports Google and Facebook out of the box (more to come soon). Other sites can be configured manually.
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require 'open_auth2'
|
16
|
+
|
17
|
+
# get this info by signing your app at developers.facebook.com
|
18
|
+
client_id = 'enter in your own value'
|
19
|
+
client_secret = 'enter in your own value'
|
20
|
+
redirect_uri = 'enter in your own value'
|
21
|
+
|
22
|
+
config = OpenAuth2::Config.new do |c|
|
23
|
+
# indicate what kind of provider you want to use
|
24
|
+
# Accepts: :google, :facebook or :default
|
25
|
+
#
|
26
|
+
c.provider = :facebook
|
27
|
+
|
28
|
+
c.client_id = client_id
|
29
|
+
c.client_secret = client_secret
|
30
|
+
c.redirect_uri = redirect_uri
|
31
|
+
c.scope = ['publish_stream']
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Client
|
36
|
+
|
37
|
+
Next, initialize a `client` object, which we'll use to make requests and pass in the `config` object we created earlier.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
client = OpenAuth2::Client.new do |c|
|
41
|
+
c.config = config
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
`Client#configure_connection` takes a block, which can be used to setup middleware like any other Faraday client, i.e:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
client.configure_connection do |c|
|
49
|
+
c.response :logger
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
## Authenticate Url
|
54
|
+
|
55
|
+
To start the OAuth2 exotic dance, you'll need to obtain a 'code' from the server, which you can then use to request an 'access_token'. Redirect the user/yourself to this url to obtain that 'code'.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
client.build_code_url
|
59
|
+
```
|
60
|
+
|
61
|
+
`build_code_url` takes optional params hash, which will be bundled into the url.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
client.build_code_url(:scope => 'publish_stream')
|
65
|
+
```
|
66
|
+
|
67
|
+
## Access token
|
68
|
+
|
69
|
+
Access token is used to sign the request so the server can identify the client sending the request. If you already have an access token, add it to the client with a block.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
access_token = 'enter in your value'
|
73
|
+
refresh_token = 'enter in your value'
|
74
|
+
|
75
|
+
client.configure do |c|
|
76
|
+
c.access_token = access_token
|
77
|
+
|
78
|
+
# optional, for fb its same as above
|
79
|
+
c.refresh_token = refresh_token
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
## GET Access Token
|
84
|
+
|
85
|
+
If you don't have an access token, we'll need to ask the server for it.
|
86
|
+
|
87
|
+
`token#configure_connection` takes an block, just like `client#configure_connection`, which can be used to setup middleware like any other Faraday client.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
token = client.token
|
91
|
+
token.configure_connection do |c|
|
92
|
+
c.response :logger
|
93
|
+
end
|
94
|
+
|
95
|
+
# asks Facebook for access_token
|
96
|
+
token.get
|
97
|
+
|
98
|
+
# the following methods are now available
|
99
|
+
client.access_token
|
100
|
+
client.refresh_token
|
101
|
+
client.token_expires_at
|
102
|
+
client.token_expired?
|
103
|
+
client.token_arrived_at
|
104
|
+
```
|
105
|
+
|
106
|
+
## GET Refresh Token
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
# tells Facebook api to extend the expiration of the access_token
|
110
|
+
token.refresh
|
111
|
+
```
|
112
|
+
|
113
|
+
## GET request
|
114
|
+
|
115
|
+
To make an api call, simply call `get` with a hash containing `path`.
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
client.get(:path => '/cocacola')
|
119
|
+
```
|
120
|
+
|
121
|
+
`get` accepts a Hash as second argument, which can be used to pass in additional parameters.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
client.get(:path => '/cocacola', :limit => 1)
|
125
|
+
```
|
126
|
+
|
127
|
+
## Faraday convenience methods
|
128
|
+
|
129
|
+
`Client#get` is a convenience method that calls `Faraday#get`. You can drop down to Faraday connection object itself and make requests via that also.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
client.connection.get do |conn|
|
133
|
+
conn.url('/cocacola')
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
`Client#run_request` points to `Faraday#run_request`. It takes hash since I can never remember the order in which to pass the arguments.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
path = "https://graph.facebook.com/cocacola"
|
141
|
+
client.run_request(verb: :get, path: path, body: nil, header: nil)
|
142
|
+
|
143
|
+
# same as
|
144
|
+
client.connection.run_request(:get, path, nil, nil)
|
145
|
+
```
|
146
|
+
|
147
|
+
## Google example
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
require 'open_auth2'
|
151
|
+
require 'json'
|
152
|
+
|
153
|
+
access_token = 'enter in your own value'
|
154
|
+
refresh_token = 'enter in your own value'
|
155
|
+
|
156
|
+
config = OpenAuth2::Config.new do |c|
|
157
|
+
c.provider = :google
|
158
|
+
c.access_token = access_token
|
159
|
+
c.refresh_token = refresh_token
|
160
|
+
c.scope = ['https://www.googleapis.com/auth/calendar']
|
161
|
+
c.redirect_uri = 'http://localhost:9393/google/callback'
|
162
|
+
c.path_prefix = '/calendar/v3'
|
163
|
+
end
|
164
|
+
|
165
|
+
client = OpenAuth2::Client.new do |c|
|
166
|
+
c.config = config
|
167
|
+
end
|
168
|
+
|
169
|
+
# get request
|
170
|
+
client.get(:path => '/users/me/calendarList')
|
171
|
+
|
172
|
+
post_url = '/calendar/v3/calendars/openauth2@gmail.com/events'
|
173
|
+
body = {
|
174
|
+
"summary" => "From OpenAuth2",
|
175
|
+
"start" => {"dateTime"=>"2012-01-20T10:00:00.000-07:00"},
|
176
|
+
"end" => {"dateTime"=>"2012-01-20T10:25:00.000-07:00"}
|
177
|
+
}
|
178
|
+
body = JSON.dump(body)
|
179
|
+
|
180
|
+
# post request
|
181
|
+
client.post(:path => post_url,
|
182
|
+
:body => body,
|
183
|
+
:content_type => content_type)
|
184
|
+
|
185
|
+
header = {"Content-Type" => "application/json"}
|
186
|
+
full_url = "#{post_url}?access_token=#{access_token}"
|
187
|
+
|
188
|
+
# post request via #run_request
|
189
|
+
client.run_request(:verb => :post,
|
190
|
+
:path => full_url,
|
191
|
+
:body => body,
|
192
|
+
:header => header)
|
193
|
+
```
|
194
|
+
|
195
|
+
## Requirements
|
196
|
+
|
197
|
+
* ActiveSupport
|
198
|
+
* Faraday
|
199
|
+
* URI
|
200
|
+
* Json
|
201
|
+
|
202
|
+
## Install
|
203
|
+
|
204
|
+
$ gem install open_auth2
|
205
|
+
|
206
|
+
## Source
|
207
|
+
|
208
|
+
OpenAuth2's git repo is available on GitHub:
|
209
|
+
|
210
|
+
https://github.com/senthilnambi/OpenAuth2
|
211
|
+
|
212
|
+
## Development
|
213
|
+
|
214
|
+
You will need these gems to get tests to pass:
|
215
|
+
|
216
|
+
* rspec2
|
217
|
+
* rake
|
218
|
+
* SimpleCov (optional for coverage)
|
219
|
+
|
220
|
+
See [meta](https://github.com/senthilnambi/meta) for more info on contributing and technology used to create this gem.
|
221
|
+
|
222
|
+
## Copyright
|
223
|
+
|
224
|
+
Copyright (c) 2012 Senthil A. See License for details.
|
data/Spec.markdown
ADDED
File without changes
|
data/examples/fb.rb
ADDED
data/examples/google.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require_relative '../lib/open_auth2'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
ClientId = nil
|
5
|
+
ClientSecret = nil
|
6
|
+
Code = nil
|
7
|
+
AccessToken = nil
|
8
|
+
RefreshToken = nil
|
9
|
+
PostEmail = nil
|
10
|
+
|
11
|
+
@config = OpenAuth2::Config.new do |c|
|
12
|
+
c.provider = :google
|
13
|
+
c.code = Code
|
14
|
+
c.client_id = ClientId
|
15
|
+
c.client_secret = ClientSecret
|
16
|
+
c.scope = ['https://www.googleapis.com/auth/calendar']
|
17
|
+
c.redirect_uri = 'http://localhost:9393/google/callback'
|
18
|
+
c.path_prefix = '/calendar/v3'
|
19
|
+
end
|
20
|
+
|
21
|
+
@client = OpenAuth2::Client.new do |c|
|
22
|
+
c.config = @config
|
23
|
+
end
|
24
|
+
|
25
|
+
@token = @client.token
|
26
|
+
|
27
|
+
params = {:approval_prompt => 'force', :access_type => 'offline'}
|
28
|
+
@url = @token.build_code_url(params)
|
29
|
+
|
30
|
+
# get request
|
31
|
+
@list = @client.get(:path => '/users/me/calendarList')
|
32
|
+
|
33
|
+
post_url = "/calendar/v3/calendars/#{PostEmail}/events"
|
34
|
+
body = {
|
35
|
+
"summary" => "From OpenAuth2",
|
36
|
+
"start" => {"dateTime"=>"2012-03-03T10:00:00.000-07:00"},
|
37
|
+
"end" => {"dateTime"=>"2012-03-03T10:25:00.000-07:00"}
|
38
|
+
}
|
39
|
+
body = JSON.dump(body)
|
40
|
+
|
41
|
+
# post request
|
42
|
+
@client.post(:path => post_url,
|
43
|
+
:body => body,
|
44
|
+
:content_type => "application/json")
|
45
|
+
|
46
|
+
header = {"Content-Type" => "application/json"}
|
47
|
+
full_url = "#{post_url}?access_token=#{AccessToken}"
|
48
|
+
|
49
|
+
# post request via #run_request
|
50
|
+
@client.run_request(:verb => :post,
|
51
|
+
:path => full_url,
|
52
|
+
:body => body,
|
53
|
+
:header => header)
|
data/lib/open_auth2.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'faraday'
|
3
|
+
require 'uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require_relative 'open_auth2/provider'
|
7
|
+
require_relative 'open_auth2/provider/base'
|
8
|
+
require_relative 'open_auth2/provider/default'
|
9
|
+
require_relative 'open_auth2/provider/facebook'
|
10
|
+
require_relative 'open_auth2/provider/google'
|
11
|
+
|
12
|
+
require_relative 'open_auth2/delegate_to_config'
|
13
|
+
require_relative 'open_auth2/config'
|
14
|
+
require_relative 'open_auth2/config'
|
15
|
+
require_relative 'open_auth2/connection'
|
16
|
+
require_relative 'open_auth2/token'
|
17
|
+
require_relative 'open_auth2/client'
|
18
|
+
|
19
|
+
require_relative 'open_auth2/version'
|
20
|
+
|
21
|
+
module OpenAuth2
|
22
|
+
|
23
|
+
# Raised in Config#provider= when user sets to provider not in
|
24
|
+
# 'lib/open_auth2/provider/' or included by them manually.
|
25
|
+
#
|
26
|
+
class UnknownProvider < StandardError; end
|
27
|
+
|
28
|
+
# Raised in Client#new unless @config is set.
|
29
|
+
class NoConfigObject < StandardError; end
|
30
|
+
|
31
|
+
# Raised in Client#new unless @config is set to OpenAuth2::Config.
|
32
|
+
class UnknownConfigObject < StandardError; end
|
33
|
+
end
|