omniauth-ihealth-oauth2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 85efaf3d91a90fe040fe8ed0ea100f3e255bea38f87ccf88182b4a89c423ee65
4
+ data.tar.gz: 99e4c5f47334f80f2793c1e711190775873d9c436bc11b128bfce2106f554fb5
5
+ SHA512:
6
+ metadata.gz: e3a3d42825d36f1ee3ca712de7b98e4535b0de5a8505cdde6e560e31c928fb79383d15047f15ccf7e68afb6a77d8be2c820cab1ef734f44d7f61da2836f1df83
7
+ data.tar.gz: 79b568739f5f056f63adfea897192379aeb928c97fab4598c7c55bef7d2f8a6b53f8d576a64b3a10cd78f5bd88b2d12389b3efae90814e814091ed94cd3555e9
@@ -0,0 +1,19 @@
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
+ .ruby-gemset
19
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-ihealth-oauth2.gemspec
4
+ gemspec
@@ -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-ihealth.gemspec')
10
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 Eric Shelley
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,54 @@
1
+ # OmniAuth iHealth OAuth2 Strategy
2
+
3
+ A iHealth OAuth2 strategy for OmniAuth.
4
+
5
+ For more details, read the iHealth documentation: https://developer.ihealthlabs.com
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'omniauth-ihealth-oauth2'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install omniauth-ihealth-oauth2
20
+
21
+ ## Usage
22
+
23
+ Register your application with iHealth to receive API credentials: https://developer.ihealthlabs.com
24
+
25
+ This is an example that you might put into a Rails initializer at `config/initializers/omniauth.rb`:
26
+
27
+ ```ruby
28
+ Rails.application.config.middleware.use OmniAuth::Builder do
29
+ provider :ihealth, ENV['IHEALTH_CLIENT_ID'], ENV['IHEALTH_CLIENT_SECRET'], :scope => 'OpenApiUserInfo'
30
+ end
31
+ ```
32
+
33
+ You can now access the OmniAuth iHealth OAuth2 URL: `/auth/ihealth`.
34
+
35
+ ## Granting Member Permissions to Your Application
36
+
37
+ With the iHealth API, you have the ability to specify which permissions you want users to grant your application.
38
+ For more details, read the iHealth documentation: https://developer.ihealthlabs.com.
39
+
40
+ Available scopes: `OpenApiActivity OpenApiBG OpenApiBP OpenApiSleep OpenApiSpO2 OpenApiSport OpenApiUserInfo OpenApiWeight`
41
+
42
+ You can configure the scope option:
43
+
44
+ ```ruby
45
+ provider :ihealth, ENV['IHEALTH_CLIENT_ID'], ENV['IHEALTH_CLIENT_SECRET'], :scope => 'OpenApiActivity OpenApiUserInfo'
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require "omniauth-ihealth-oauth2/version"
2
+ require 'omniauth/strategies/ihealth'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module IHealthOauth2
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,118 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class IHealth < OmniAuth::Strategies::OAuth2
6
+ # AVAILABLE_API_NAMES = "OpenApiActivity OpenApiBG OpenApiBP OpenApiSleep OpenApiSpO2 OpenApiSport OpenApiUserInfo OpenApiWeight"
7
+ DEFAULT_SCOPE = 'OpenApiUserInfo'
8
+
9
+ option :name, 'ihealth'
10
+ option :provider_ignores_state, true
11
+
12
+ option :client_options, {
13
+ :site => 'https://api.ihealthlabs.com:8443',
14
+ :authorize_url => '/OpenApiV2/OAuthv2/userauthorization/',
15
+ :token_url => '/OpenApiV2/OAuthv2/userauthorization/',
16
+ :token_method => :get,
17
+ :parse => :json
18
+ }
19
+
20
+ def authorize_params
21
+ super.tap do |params|
22
+ params[:response_type] = 'code'
23
+ params[:APIName] = get_scope(params)
24
+ params[:scope] = get_scope(params)
25
+ end
26
+ end
27
+
28
+ def token_params
29
+ super.tap do |params|
30
+ params[:client_id] = client.id
31
+ params[:client_secret] = client.secret
32
+ params[:grant_type] = "authorization_code"
33
+ end
34
+ end
35
+
36
+ def build_access_token
37
+ token_url_params = {:code => request.params['code'], :redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true))
38
+ parsed_response = client.request(options.client_options.token_method, client.token_url(token_url_params), parse: :json).parsed
39
+ hash = {
40
+ :access_token => parsed_response["AccessToken"],
41
+ :expires_in => parsed_response["Expires"],
42
+ :refresh_token => parsed_response["RefreshToken"],
43
+ :user_id => parsed_response["UserID"],
44
+ :api_name => parsed_response["APIName"],
45
+ :client_para => parsed_response["client_para"]
46
+ }
47
+ ::OAuth2::AccessToken.from_hash(client, hash)
48
+ end
49
+
50
+ uid { access_token[:user_id] }
51
+
52
+ info { user_data.slice(:name, :nickname, :image) }
53
+
54
+ extra do
55
+ { :user_info => user_data, :raw_info => raw_info }
56
+ end
57
+
58
+ def raw_info
59
+ access_token.options[:mode] = :query
60
+ user_profile_params = {:client_id => client.id, :client_secret => client.secret, :access_token => access_token.token}
61
+ user_profile_params.merge!({:sc => options.sc, :sv => options.sv}) if options.sc && options.sv
62
+ @raw_info ||= access_token.get("/openapiv2/user/#{access_token[:user_id]}.json/?#{user_profile_params.to_param}", parse: :json).parsed
63
+ end
64
+
65
+ def user_data
66
+ info = raw_info
67
+ user_data ||= {
68
+ :name => info["nickname"],
69
+ :gender => info["gender"]&.downcase,
70
+ :birthday => Time.at(info["dateofbirth"]).to_date.strftime("%Y-%m-%d"),
71
+ :image => URI.unescape(info["logo"]),
72
+ :nickname => info["nickname"],
73
+ :height => calc_height(info["height"], info["HeightUnit"]),
74
+ :weight => calc_weight(info["weight"], info["WeightUnit"])
75
+ }
76
+ end
77
+
78
+ protected
79
+
80
+ CM_TO_IN_CONVERSION = 0.393701
81
+ FT_TO_IN_CONVERSION = 12
82
+ def calc_height(value, unit)
83
+ case(unit)
84
+ when 0 # value is in cm
85
+ return value * CM_TO_IN_CONVERSION
86
+ when 1 # value is in feet
87
+ return value * FT_TO_IN_CONVERSION
88
+ else # unrecognized unit
89
+ return value
90
+ end
91
+ end
92
+
93
+ KG_TO_LBS_CONVERSION = 2.20462
94
+ STONE_TO_LBS_CONVERSION = 14
95
+ def calc_weight(value, unit)
96
+ case(unit)
97
+ when 0 # value is in kg
98
+ return value * KG_TO_LBS_CONVERSION
99
+ when 1 # value is in lbs
100
+ return value
101
+ when 2 # value is in stone
102
+ return value * STONE_TO_LBS_CONVERSION
103
+ else # unrecognized unit
104
+ return value
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def get_scope(params)
111
+ params[:scope] || DEFAULT_SCOPE
112
+ end
113
+
114
+ end
115
+ end
116
+ end
117
+
118
+ OmniAuth.config.add_camelization 'ihealth', 'iHealth'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth-ihealth-oauth2/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "omniauth-ihealth-oauth2"
8
+ gem.version = OmniAuth::IHealthOauth2::VERSION
9
+ gem.authors = ["Hunter Spinks", "Artur Karbone", "Victor Vargas", "Eric Shelley"]
10
+ gem.email = ["eirc@webdesignbakery.com"]
11
+ gem.description = %q{OmniAuth strategy for iHealth.}
12
+ gem.summary = %q{OmniAuth strategy for iHealth.}
13
+ gem.homepage = "https://github.com/bartimaeus/omniauth-ihealth-oauth2"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_runtime_dependency 'omniauth-oauth2', '~> 1.2'
22
+
23
+ gem.add_development_dependency 'bundler', '>= 1.16', '< 3.0'
24
+ gem.add_development_dependency 'rake', '~> 12.3'
25
+ gem.add_development_dependency 'rspec', '~> 3.6'
26
+ gem.add_development_dependency 'simplecov', '~> 0.16'
27
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-ihealth-oauth2'
3
+ require 'pry'
4
+
5
+ describe OmniAuth::Strategies::IHealth do
6
+ let(:request) { double('Request', params: {}, cookies: {}, env: {}) }
7
+ let(:app) do
8
+ lambda do
9
+ [200, {}, ['Hello.']]
10
+ end
11
+ end
12
+
13
+ subject do
14
+ OmniAuth::Strategies::IHealth.new(app, 'appid', 'secret', @options || {}).tap do |strategy|
15
+ allow(strategy).to receive(:request) do
16
+ request
17
+ end
18
+ end
19
+ end
20
+
21
+ before do
22
+ OmniAuth.config.test_mode = true
23
+ end
24
+
25
+ after do
26
+ OmniAuth.config.test_mode = false
27
+ end
28
+
29
+ it 'adds camelization for itself' do
30
+ expect(OmniAuth::Utils.camelize('ihealth')).to eq('iHealth')
31
+ end
32
+
33
+ describe '#client' do
34
+ it 'has correct iHealth site' do
35
+ expect(subject.client.site).to eq('https://api.ihealthlabs.com:8443')
36
+ end
37
+
38
+ it 'has correct `authorize_url`' do
39
+ expect(subject.client.options[:authorize_url]).to eq('/OpenApiV2/OAuthv2/userauthorization/')
40
+ end
41
+
42
+ it 'has correct `token_url`' do
43
+ expect(subject.client.options[:token_url]).to eq('/OpenApiV2/OAuthv2/userauthorization/')
44
+ end
45
+ end
46
+
47
+ describe '#callback_path' do
48
+ it 'has the correct callback path' do
49
+ expect(subject.callback_path).to eq('/auth/ihealth/callback')
50
+ end
51
+ end
52
+
53
+ describe '#uid' do
54
+ before :each do
55
+ allow(subject).to receive(:access_token) { Hash[user_id: 'uid'] }
56
+ end
57
+
58
+ it 'returns the id from raw_info' do
59
+ expect(subject.uid).to eq('uid')
60
+ end
61
+ end
62
+
63
+ describe '#access_token' do
64
+ let(:expires_in) { 3600 }
65
+ let(:token) { 'token' }
66
+ let(:access_token) do
67
+ instance_double OAuth2::AccessToken, :expires_in => expires_in, :token => token
68
+ end
69
+
70
+ before :each do
71
+ allow(subject).to receive(:access_token).and_return access_token
72
+ end
73
+
74
+ specify { expect(subject.access_token.expires_in).to eq expires_in }
75
+ end
76
+
77
+ describe '#authorize_params' do
78
+ describe 'scope' do
79
+ it 'sets default scope' do
80
+ expect(subject.authorize_params['scope']).to eq('OpenApiUserInfo')
81
+ end
82
+
83
+ it 'sets custom scope' do
84
+ @options = { scope: 'OpenApiActivity OpenApiUserInfo' }
85
+ expect(subject.authorize_params['scope']).to eq('OpenApiActivity OpenApiUserInfo')
86
+ end
87
+ end
88
+
89
+ describe 'APIName' do
90
+ it 'sets default APIName' do
91
+ expect(subject.authorize_params['APIName']).to eq('OpenApiUserInfo')
92
+ end
93
+
94
+ it 'sets custom APIName' do
95
+ @options = { scope: 'OpenApiActivity OpenApiUserInfo' }
96
+ expect(subject.authorize_params['APIName']).to eq('OpenApiActivity OpenApiUserInfo')
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,19 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-ihealth-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hunter Spinks
8
+ - Artur Karbone
9
+ - Victor Vargas
10
+ - Eric Shelley
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2019-06-25 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: omniauth-oauth2
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '1.16'
37
+ - - "<"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - - "<"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ requirement: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '12.3'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '12.3'
64
+ - !ruby/object:Gem::Dependency
65
+ name: rspec
66
+ requirement: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '3.6'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '3.6'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '0.16'
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '0.16'
92
+ description: OmniAuth strategy for iHealth.
93
+ email:
94
+ - eirc@webdesignbakery.com
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - ".gitignore"
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - Guardfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/omniauth-ihealth-oauth2.rb
107
+ - lib/omniauth-ihealth-oauth2/version.rb
108
+ - lib/omniauth/strategies/ihealth.rb
109
+ - omniauth-ihealth-oauth2.gemspec
110
+ - spec/omniauth/strategies/ihealth_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/bartimaeus/omniauth-ihealth-oauth2
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.0.3
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: OmniAuth strategy for iHealth.
135
+ test_files:
136
+ - spec/omniauth/strategies/ihealth_spec.rb
137
+ - spec/spec_helper.rb