omniauth_qiita 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a9fe22365dce488b67e41fcefef6074e058b1714
4
+ data.tar.gz: 008c6db49fcb24351600cec3f04aa98bf3989a6d
5
+ SHA512:
6
+ metadata.gz: 0b7e3accddaf1f6bdae743af8d3019354055ad9f2c138a30f9b0a192dfc876547cb1c9d5f2552d3d710a63d9c2a80b4f04ff41227616bcdf862a867c3290ebdf
7
+ data.tar.gz: 68a982385428c80e61bfb9aa9d20dd8fd84a735e07459da68db5ca1b370558ee7949e710fe875eeede57ce89bab728e992d004c72c6ef80e0679ed11542c5367
@@ -0,0 +1,23 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ /tags
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - jruby
6
+ - rbx-2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth_qiita.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Takuya Miyamoto
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.
@@ -0,0 +1,99 @@
1
+ # OmniAuth Qiita  [![Build Status](https://secure.travis-ci.org/tmiyamon/omniauth_qiita.png?branch=master)](https://travis-ci.org/tmiyamon/omniauth_qiita)
2
+
3
+ **These notes are based on master, please see tags for README pertaining to specific releases.**
4
+
5
+ Qiita OAuth2 Strategy for OmniAuth.
6
+
7
+ Supports the OAuth 2.0 server-side and client-side flows. Read the Qiita docs for more details: https://qiita.com/api/v2/docs
8
+
9
+ ## Installing
10
+
11
+ Add to your `Gemfile`:
12
+
13
+ ```ruby
14
+ gem 'omniauth_qiita'
15
+ ```
16
+
17
+ Then `bundle install`.
18
+
19
+ ## Usage
20
+
21
+ `OmniAuth::Strategies::Qiita` is simply a Rack middleware. Read the OmniAuth docs for detailed instructions: https://github.com/intridea/omniauth.
22
+
23
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
24
+
25
+ ```ruby
26
+ Rails.application.config.middleware.use OmniAuth::Builder do
27
+ provider :qiita, ENV['QIITA_CLIENT_ID'], ENV['QIITA_CLIENT_SECRET']
28
+ end
29
+ ```
30
+
31
+ ## Configuring
32
+
33
+ You can configure scope option, which you pass in to the `provider` method via a `Hash`:
34
+
35
+ ```ruby
36
+ Rails.application.config.middleware.use OmniAuth::Builder do
37
+ provider :qiita, ENV['QIITA_CLIENT_ID'], ENV['QIITA_CLIENT_SECRET'],
38
+ :scope => 'read_qiita read_qiita_team write_qiita write_qiita_team'
39
+ end
40
+ ```
41
+
42
+ ## Auth Hash
43
+
44
+ Here's an example *Auth Hash* available in `request.env['omniauth.auth']`:
45
+
46
+ ```ruby
47
+ {
48
+ :provider => "qiita",
49
+ :uid => "tmiyamon",
50
+ :info => {
51
+ :nickname => "tmiyamon",
52
+ :name => "",
53
+ :location => "",
54
+ :image => "https://....",
55
+ :description => "",
56
+ :urls => {
57
+ :Facebook => "https://www.facebook.com/...",
58
+ :Github => "https://github.com/tmiyamon",
59
+ :Twitter => "https://twitter.com/..."
60
+ }
61
+ },
62
+ :credentials => {
63
+ :token => "abc...",
64
+ :expires => false
65
+ },
66
+ :extra => {
67
+ :raw_info => {
68
+ :description => "",
69
+ :facebook_id => "...",
70
+ :followers_count => 5,
71
+ :followees_count => 0,
72
+ :github_login_name => "tmiyamon",
73
+ :id => "tmiyamon",
74
+ :items_count => 3,
75
+ :linkedin_id => "",
76
+ :location => "",
77
+ :name => "",
78
+ :organization => "",
79
+ :profile_image_url => "https://...",
80
+ :twitter_screen_name => "...",
81
+ :website_url => ""
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ The precise information available may depend on the permissions which you request.
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it ( https://github.com/tmiyamon/omniauth_qiita/fork )
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create a new Pull Request
96
+
97
+ ## License
98
+
99
+ This software is released under the MIT License, see LICENSE.txt.
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Default: run specs.'
8
+ task :default => :spec
@@ -0,0 +1,104 @@
1
+ require 'omniauth/strategies/oauth2'
2
+ require 'multi_json'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ class Qiita < OmniAuth::Strategies::OAuth2
7
+ option :name, "qiita"
8
+
9
+ DEFAULT_SCOPE = 'read_qiita'
10
+
11
+ option :client_options, {
12
+ :site => "https://qiita.com",
13
+ :authorize_url => "https://qiita.com/api/v2/oauth/authorize",
14
+ :token_url => '/api/v2/access_tokens'
15
+ }
16
+
17
+ option :token_params, {
18
+ parse: :json
19
+ }
20
+
21
+ option :authorize_options, [:scope]
22
+
23
+ uid { raw_info['id'] }
24
+
25
+ info do
26
+ {
27
+ 'nickname' => raw_info['id'],
28
+ 'name' => raw_info['name'],
29
+ 'location' => raw_info['location'],
30
+ 'image' => raw_info['profile_image_url'],
31
+ 'description' => raw_info['description'],
32
+ 'urls' => urls(raw_info)
33
+ }
34
+ end
35
+
36
+ extra do
37
+ { 'raw_info' => raw_info }
38
+ end
39
+
40
+ def raw_info
41
+ @raw_info ||= access_token.get('/api/v2/authenticated_user').parsed || {}
42
+ end
43
+
44
+ def urls(raw_info)
45
+ hash = {}
46
+
47
+ [
48
+ ['Facebook', 'facebook_id', ->(id){"https://www.facebook.com/#{id}"}],
49
+ ['Github', 'github_login_name', ->(id){"https://github.com/#{id}"}],
50
+ ['LinkedIn', 'linkedin_id', ->(id){"https://www.linkedin.com/in/#{id}"}],
51
+ ['Twitter', 'twitter_screen_name', ->(id){"https://twitter.com/#{id}"}],
52
+ ['Website', 'website_url', ->(url){url}]
53
+ ].each do |label, key, url_gen|
54
+ if raw_info.key? key and raw_info[key] and raw_info[key].length > 0
55
+ hash[label] = url_gen.call(raw_info[key])
56
+ end
57
+ end
58
+
59
+ hash
60
+ end
61
+
62
+ def authorize_params
63
+ super.tap do |params|
64
+ set_scope params
65
+ end
66
+ end
67
+
68
+ def set_scope params
69
+ params[:scope] ||= request.params['scope'] || DEFAULT_SCOPE
70
+ end
71
+
72
+ def build_params_for_access_token
73
+ {
74
+ code: request.params['code'],
75
+ client_id: options.client_id,
76
+ client_secret: options.client_secret
77
+ }
78
+ end
79
+
80
+ def build_request_option_for_access_token
81
+ {
82
+ headers: { 'Content-Type' => 'application/json' },
83
+ body: MultiJson.dump(build_params_for_access_token),
84
+ }.merge(options['token_params'])
85
+ end
86
+
87
+ def request_access_token
88
+ client.request(:post, client.token_url, build_request_option_for_access_token).tap do |res|
89
+ res.parsed['access_token'] = res.parsed.delete('token')
90
+ end
91
+ end
92
+
93
+ def handle_access_token_response response
94
+ error = Error.new(response)
95
+ fail(error) if client.options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
96
+ ::OAuth2::AccessToken.from_hash(client, response.parsed.merge(deep_symbolize(options.auth_token_params)))
97
+ end
98
+
99
+ def build_access_token
100
+ handle_access_token_response(request_access_token)
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,2 @@
1
+ require "omniauth_qiita/version"
2
+ require 'omniauth/strategies/qiita'
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Qiita
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth_qiita/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "omniauth_qiita"
8
+ spec.version = Omniauth::Qiita::VERSION
9
+ spec.authors = ["Takuya Miyamoto"]
10
+ spec.email = ["miyamototakuya@gmail.com"]
11
+ spec.summary = 'Qiita Oauth2 Strategy for OmniAuth'
12
+ spec.homepage = "https://github.com/tmiyamon/omniauth_qiita"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.2'
21
+ spec.add_runtime_dependency 'multi_json', '~> 1.10'
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency 'rspec', '~> 3.1'
25
+ spec.add_development_dependency 'rack-test'
26
+ spec.add_development_dependency 'simplecov'
27
+ spec.add_development_dependency 'webmock'
28
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Qiita do
4
+ let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) }
5
+
6
+ subject do
7
+ args = ['qiita', 'appid', 'secret', @options || {}].compact
8
+ OmniAuth::Strategies::Qiita.new(*args).tap do |strategy|
9
+ allow(strategy).to receive(:request) { request }
10
+ end
11
+ end
12
+
13
+ describe 'client options' do
14
+ it 'should have correct name' do
15
+ expect(subject.options.name).to eq('qiita')
16
+ end
17
+
18
+ it 'should have correct site' do
19
+ expect(subject.options.client_options.site).to eq('https://qiita.com')
20
+ end
21
+
22
+ it 'should have correct authorize url' do
23
+ expect(subject.options.client_options.authorize_url).to eq('https://qiita.com/api/v2/oauth/authorize')
24
+ end
25
+
26
+ it 'should have correct token url' do
27
+ expect(subject.options.client_options.token_url).to eq('/api/v2/access_tokens')
28
+ end
29
+ end
30
+
31
+ describe '#set_scope' do
32
+ it 'does nothing if a scope option is passed' do
33
+ params = { scope: 'a b c' }
34
+ subject.set_scope(params)
35
+
36
+ expect(params[:scope]).to eq 'a b c'
37
+ end
38
+
39
+ it 'sets read_qiita as default scope to scope in params' do
40
+ params = {}
41
+ subject.set_scope(params)
42
+
43
+ expect(params[:scope]).to eq 'read_qiita'
44
+ end
45
+ end
46
+
47
+ describe '#build_params_for_access_token' do
48
+ it "returns obtained code, client id and client secret" do
49
+ expect(subject.request).to receive(:params).and_return({ 'code' => '12345' })
50
+ expect(subject.build_params_for_access_token).to eq({ code: '12345', client_id: 'appid', client_secret: 'secret' })
51
+ end
52
+ end
53
+
54
+ describe '#build_request_option_for_access_token' do
55
+ it "returns request options for json request and response" do
56
+ params = { code: '12345', client_id: 'appid', client_secret: 'secret' }
57
+ response_option = { parse: :json }
58
+
59
+ expect(subject).to receive(:build_params_for_access_token).and_return(params)
60
+ expect(subject).to receive(:options).and_return({ 'token_params' => response_option })
61
+
62
+ expect(subject.build_request_option_for_access_token).to eq( {
63
+ headers: { 'Content-Type' => 'application/json' },
64
+ body: MultiJson.dump(params)
65
+ }.merge(response_option)
66
+ )
67
+ end
68
+ end
69
+
70
+ describe '#urls' do
71
+ it "returns facebook url if facebook_id is in raw_info" do
72
+ expect(subject.urls({ 'facebook_id' => 'test'})).to eq({'Facebook' => 'https://www.facebook.com/test'})
73
+ end
74
+ it "returns github url if github_login_name is in raw_info" do
75
+ expect(subject.urls({ 'github_login_name' => 'test'})).to eq({'Github' => 'https://github.com/test'})
76
+ end
77
+ it "returns linkedin url if linkedin_id is in raw_info" do
78
+ expect(subject.urls({ 'linkedin_id' => 'test'})).to eq({'LinkedIn' => 'https://www.linkedin.com/in/test'})
79
+ end
80
+ it "returns twitter url if twitter_screen_name is in raw_info" do
81
+ expect(subject.urls({ 'twitter_screen_name' => 'test'})).to eq({'Twitter' => 'https://twitter.com/test'})
82
+ end
83
+ it "returns website url if website_url is in raw_info" do
84
+ expect(subject.urls({ 'website_url' => 'http://test.com'})).to eq({'Website' => 'http://test.com'})
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'webmock/rspec'
8
+ require 'omniauth'
9
+ require 'omniauth_qiita'
10
+
11
+ RSpec.configure do |config|
12
+ config.include WebMock::API
13
+ config.include Rack::Test::Methods
14
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
+ config.expect_with :rspec do |c|
16
+ c.syntax = :expect
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth_qiita
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takuya Miyamoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - miyamototakuya@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - lib/omniauth/strategies/qiita.rb
140
+ - lib/omniauth_qiita.rb
141
+ - lib/omniauth_qiita/version.rb
142
+ - omniauth_qiita.gemspec
143
+ - spec/omniauth/strategies/qiita_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: https://github.com/tmiyamon/omniauth_qiita
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.2.2
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Qiita Oauth2 Strategy for OmniAuth
169
+ test_files:
170
+ - spec/omniauth/strategies/qiita_spec.rb
171
+ - spec/spec_helper.rb
172
+ has_rdoc: