omniauth-ncu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 67758a1f70dcf09ba436e5b8e2a227e7dc264ff0
4
+ data.tar.gz: 24bed3669869d199128bda950ce4ee926dc3c636
5
+ SHA512:
6
+ metadata.gz: 0406a550b064a1a7cb4b78fee4e40131e986b3983db0417e369084a69f023386be2b7ae11dbca4557990d7a960e0e779647cbe15ef362f0233708b51cc067ef4
7
+ data.tar.gz: 492c7385bcad6f9f7f859dd589be9850ea1c721864f917ac6c1219f8768ee90d9c86a5e34c1049fd952532aa4759a62d5a031a4e0f6596cc68a81c9b341d7b5f
@@ -0,0 +1,10 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ root = true
4
+
5
+ [*]
6
+ end_of_line = lf
7
+ insert_final_newline = true
8
+ charset = utf-8
9
+ indent_style = space
10
+ indent_size = 2
@@ -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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'guard-bundler'
9
+ gem 'rb-fsevent'
10
+ gem 'growl'
11
+ end
@@ -0,0 +1,10 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
7
+ guard 'bundler' do
8
+ watch('Gemfile')
9
+ watch('omniauth-ncu.gemspec')
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+
2
+ Copyright (c) 2015- Community of Networking and Open Source
3
+
4
+ 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:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ 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.
@@ -0,0 +1,38 @@
1
+
2
+ # Omniauth::NCU
3
+ > English | [繁體中文](README.zh-tw.md)
4
+
5
+ This is the community version OmniAuth strategy for authenticating to [NCU](http://www.ncu.edu.tw) (National Central University, Taiwan). To use it, you'll need to sign up for an OAuth Client ID and Secret on the [NCU OAuth Applications Page](https://api.cc.ncu.edu.tw/manage/developer/client/list).
6
+
7
+ ## Basic Usage
8
+
9
+ ```
10
+ use OmniAuth::Builder do
11
+ provider :ncu, ENV['NCU_CLIENT_ID'], ENV['NCU_CLIENT_SECRET']
12
+ end
13
+ ```
14
+
15
+ ## Scopes
16
+
17
+ NCU OAuth API lets you set scopes to provide granular access to different types of data:
18
+
19
+ ```
20
+ use OmniAuth::Builder do
21
+ provider :ncu, ENV['NCU_CLIENT_ID'], ENV['NCU_CLIENT_SECRET'], scope: %w(user.info.basic.read course.schedule.read)
22
+ end
23
+ ```
24
+
25
+ More info on [Scopes](https://github.com/NCU-CC/API-Documentation/blob/master/oauth-service/scope.md).
26
+
27
+ ## License
28
+
29
+ Under MIT License.
30
+
31
+ > Copyright (c) 2015- Community of Networking and Open Source
32
+ >
33
+ > 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:
34
+ >
35
+ > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36
+ >
37
+ > 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.
38
+
@@ -0,0 +1,38 @@
1
+
2
+ # Omniauth::NCU
3
+ > [English](README.md) | 繁體中文
4
+
5
+ 此為社群版本之可用於[中央大學](http://www.ncu.edu.tw)認證之 OmniAuth strategy。 使用前你可能需要先前往[中央大學 OAuth 應用服務管理](https://api.cc.ncu.edu.tw/manage/developer/client/list)申請 Client ID 以及 Client Secret。
6
+
7
+ ## 基礎用法
8
+
9
+ ```
10
+ use OmniAuth::Builder do
11
+ provider :ncu, ENV['NCU_CLIENT_ID'], ENV['NCU_CLIENT_SECRET']
12
+ end
13
+ ```
14
+
15
+ ## Scope
16
+
17
+ NCU OAuth API 提供你設定 scopes 來取得不同資料的存取權限:
18
+
19
+ ```
20
+ use OmniAuth::Builder do
21
+ provider :ncu, ENV['NCU_CLIENT_ID'], ENV['NCU_CLIENT_SECRET'], scope: %w(user.info.basic.read course.schedule.read)
22
+ end
23
+ ```
24
+
25
+ 更多資訊請上 [Scopes](https://github.com/NCU-CC/API-Documentation/blob/master/oauth-service/scope.md)。
26
+
27
+ ## 授權條款
28
+
29
+ 以 MIT License 釋出。
30
+
31
+ > Copyright (c) 2015- Community of Networking and Open Source
32
+ >
33
+ > 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:
34
+ >
35
+ > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36
+ >
37
+ > 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.
38
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Run specs'
8
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require 'omniauth-ncu/version'
2
+ require 'omniauth/strategies/ncu'
@@ -0,0 +1,6 @@
1
+ module OmniAuth
2
+ module NCU
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
6
+
@@ -0,0 +1,80 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class NCU < OmniAuth::Strategies::OAuth2
6
+ DEFAULT_SCOPES = %w(user.info.basic.read)
7
+
8
+ option :client_options, {
9
+ :site => 'https://api.cc.ncu.edu.tw',
10
+ :authorize_url => 'https://api.cc.ncu.edu.tw/oauth/oauth/authorize',
11
+ :token_url => 'https://api.cc.ncu.edu.tw/oauth/oauth/token'
12
+ }
13
+
14
+ option :token_params, {
15
+ :parse => :json
16
+ }
17
+
18
+ option :authorize_params, {
19
+ :scope => DEFAULT_SCOPES
20
+ }
21
+
22
+ def request_phase
23
+ super
24
+ end
25
+
26
+ def authorize_params
27
+ super.tap do |params|
28
+ %w[scope client_options].each do |v|
29
+ if request.params[v]
30
+ params[v.to_sym] = request.params[v]
31
+ end
32
+ end
33
+ params[:scope] = params[:scope].join ' ' if params[:scope]
34
+ end
35
+ end
36
+
37
+ uid {
38
+ #raw_info['id'].to_s
39
+ info.hash
40
+ }
41
+
42
+ info do
43
+ next {} unless info_allowed?
44
+ {
45
+ 'name' => raw_info['name'],
46
+ 'type' => raw_info['type'],
47
+ 'unit' => raw_info['unit']
48
+ }.merge(raw_info['type'] == 'STUDENT' ? {
49
+ 'group' => raw_info['group'],
50
+ 'number' => raw_info['number']
51
+ } : {
52
+ 'title' => raw_info['title']
53
+ })
54
+ end
55
+
56
+ extra do
57
+ {
58
+ :raw_info => raw_info
59
+ }
60
+ end
61
+
62
+ def raw_info
63
+ return {} unless info_allowed?
64
+ access_token.options[:mode] = :header
65
+ @raw_info ||= access_token.get('personnel/v1/info').parsed
66
+ rescue ::OAuth2::Error => e
67
+ Logger.new(STDOUT).info e.to_s
68
+ {}
69
+ end
70
+
71
+ def info_allowed?
72
+ info_scopes = %w(user.info.basic.read)
73
+ scope = (options['scope'] || DEFAULT_SCOPES)
74
+ (info_scopes & scope).any?
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ OmniAuth.config.add_camelization 'ncu', 'NCU'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-ncu/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Kuo"]
6
+ gem.email = ["me@davy.tw"]
7
+ gem.description = %q{OmniAuth strategy for NCU OAuth.}
8
+ gem.summary = %q{OmniAuth strategy for NCU OAuth.}
9
+ gem.homepage = "https://github.com/ncunos/omniauth-ncu"
10
+ gem.license = "MIT"
11
+
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "omniauth-ncu"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = OmniAuth::NCU::VERSION
18
+
19
+ gem.add_dependency 'omniauth', '~> 1.0'
20
+ # Nothing lower than omniauth-oauth2 1.1.1
21
+ # http://www.rubysec.com/advisories/CVE-2012-6134/
22
+ gem.add_dependency 'omniauth-oauth2', '>= 1.1.1', '< 2.0'
23
+ gem.add_development_dependency 'rspec', '~> 2.99'
24
+ gem.add_development_dependency 'rack-test', '~> 0.6'
25
+ gem.add_development_dependency 'simplecov', '~> 0.10'
26
+ gem.add_development_dependency 'webmock', '~> 1.21'
27
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::NCU do
4
+ let!(:info_endpoint) { 'personnel/v1/info' }
5
+ let(:access_token) { double('AccessToken', :options => {}) }
6
+ let(:response) { double('Response', :parsed => {}) }
7
+ let(:student_response) { double('Response', :parsed => {
8
+ 'name' => 'Steve',
9
+ 'type' => 'STUDENT',
10
+ 'unit' => 'test unit',
11
+ 'group' => 'test group',
12
+ 'number' => '123456789'
13
+ }) }
14
+ let(:faculty_response) { double('Response', :parsed => {
15
+ 'name' => 'Steve',
16
+ 'type' => 'FACULTY',
17
+ 'unit' => 'test unit',
18
+ 'title' => 'test title'
19
+ }) }
20
+
21
+ subject do
22
+ OmniAuth::Strategies::NCU.new({})
23
+ end
24
+
25
+ before :each do
26
+ allow(subject).to receive(:access_token).and_return(access_token)
27
+ end
28
+
29
+ context "client options" do
30
+ it 'should have correct site' do
31
+ expect(subject.options.client_options.site).to eq("https://api.cc.ncu.edu.tw")
32
+ end
33
+
34
+ it 'should have correct authorize url' do
35
+ expect(subject.options.client_options.authorize_url).to eq('https://api.cc.ncu.edu.tw/oauth/oauth/authorize')
36
+ end
37
+
38
+ it 'should have correct token url' do
39
+ expect(subject.options.client_options.token_url).to eq('https://api.cc.ncu.edu.tw/oauth/oauth/token')
40
+ end
41
+ end
42
+
43
+ context "#info_allowed?" do
44
+ it "should allow raw_info if scope is nil by filling default scope" do
45
+ expect(subject.options['scope']).to be_nil
46
+ expect(subject.info_allowed?).to be true
47
+ end
48
+
49
+ it "should allow raw_info if scope is user.info.basic.read" do
50
+ allow(subject.options).to receive(:[]).with('scope').and_return %w(user.info.basic.read)
51
+ expect(subject.info_allowed?).to be true
52
+ end
53
+
54
+ it "should allow raw_info if scope has included user.info.basic.read" do
55
+ allow(subject.options).to receive(:[]).with('scope').and_return %w(user.info.basic.read course.schedule.read)
56
+ expect(subject.info_allowed?).to be true
57
+ end
58
+
59
+ it "should not allow raw_info if scope does not grant email access" do
60
+ allow(subject.options).to receive(:[]).with('scope').and_return %w(course.schedule.read)
61
+ expect(subject.info_allowed?).to be false
62
+ end
63
+
64
+ it "should assume raw_info access not allowed if scope is something currently not documented " do
65
+ allow(subject.options).to receive(:[]).with('scope').and_return %w(currently_not_documented)
66
+ expect(subject.info_allowed?).to be false
67
+ end
68
+ end
69
+
70
+ context "#raw_info" do
71
+ it "should return empty Hash if info not allowed" do
72
+ allow(subject).to receive(:info_allowed?).and_return false
73
+ expect(subject.raw_info.class).to be Hash
74
+ expect(subject.raw_info).to be_empty
75
+ end
76
+ it "should return correct info" do
77
+ allow(subject).to receive(:info_allowed?).and_return true
78
+ expect(access_token).to receive(:get).with(info_endpoint).and_return response
79
+ expect(subject.raw_info).to eq(response.parsed)
80
+ end
81
+ end
82
+
83
+ context "#info" do
84
+ before :each do
85
+ allow(subject).to receive(:info_allowed?).and_return true
86
+ end
87
+ it "should return student info if type is STUDENT" do
88
+ allow(subject).to receive(:raw_info).and_return student_response.parsed
89
+ expect(subject.info).to eq(student_response.parsed)
90
+ end
91
+ it "should return faculty info if type is FACULTY" do
92
+ allow(subject).to receive(:raw_info).and_return faculty_response.parsed
93
+ expect(subject.info).to eq(faculty_response.parsed)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,15 @@
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-ncu'
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
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-ncu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Kuo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.1
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '2.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.1
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.99'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.99'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rack-test
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.6'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.6'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.10'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.10'
89
+ - !ruby/object:Gem::Dependency
90
+ name: webmock
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.21'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.21'
103
+ description: OmniAuth strategy for NCU OAuth.
104
+ email:
105
+ - me@davy.tw
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".editorconfig"
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - Gemfile
114
+ - Guardfile
115
+ - LICENSE
116
+ - README.md
117
+ - README.zh-tw.md
118
+ - Rakefile
119
+ - lib/omniauth-ncu.rb
120
+ - lib/omniauth-ncu/version.rb
121
+ - lib/omniauth/strategies/ncu.rb
122
+ - omniauth-ncu.gemspec
123
+ - spec/omniauth/strategies/ncu_spec.rb
124
+ - spec/spec_helper.rb
125
+ homepage: https://github.com/ncunos/omniauth-ncu
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.8
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: OmniAuth strategy for NCU OAuth.
149
+ test_files:
150
+ - spec/omniauth/strategies/ncu_spec.rb
151
+ - spec/spec_helper.rb
152
+ has_rdoc: