omniauth-withings2 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f2548e4dd3ea286bd7956e05ef9c23f9c85654a06cb92c6d2c22a95b1f33f8cf
4
+ data.tar.gz: 4f453612db759c47a6ea899b2a6db1798e1f3d30dbc2eb164a6998a9546d7ff6
5
+ SHA512:
6
+ metadata.gz: e475ca68c31802af693d9f8594532b74200933167cbb1eeb568a537e71fe371d4221f64975c4c3ebbb9523b7b7a1684b840c425973d8bd0c2079f693c2e18083
7
+ data.tar.gz: 4c8f3e4d2d77154a638f0c549681d1546182a7786c653270f1faa1f7a1c236102960910a20f364c0368a7a43c5f2785cd54cbc43291d524fcbe6f71fa7481d64
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea/*
6
+ *.iml
7
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.1.1"
4
+ - rbx
5
+ # run tests
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'omniauth-oauth2', '~> 1.4'
6
+
7
+ group :development, :test do
8
+ gem 'rspec'
9
+ gem 'rspec-mocks'
10
+ gem 'webmock'
11
+ gem 'rack-test'
12
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Daniel Nelson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # OmniAuth Withings2 Strategy
2
+
3
+ This is an OmniAuth OAuth 2.0 strategy for authenticating with the [Nokia OAuth 2.0 api](see https://developer.health.nokia.com/oauth2/#tag/OAuth-2.0).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'omniauth-withings2'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install omniauth-withings2
20
+
21
+
22
+ ## Get your Oauth credentials
23
+
24
+ To register your application with Nokia and obtain a client id and consumer secret, go to the [Nokia application registration](https://account.health.nokia.com/partner/add_oauth2).
25
+
26
+ ## Running the example
27
+
28
+ $ cd example
29
+ $ bundle
30
+ $ NOKIA_API_KEY=<your_nokia_api_key> NOKIA_API_SECRET=<your_nokia_api_secret> bundle exec ruby example.rb
31
+ Visit http://localhost:4567/ in a browser
32
+
33
+ ## Usage
34
+
35
+ Add the strategy to your `Gemfile`:
36
+
37
+ ```ruby
38
+ gem 'omniauth-withings2'
39
+ ```
40
+
41
+ Then integrate the strategy into your middleware:
42
+
43
+ ```ruby
44
+ use OmniAuth::Builder do
45
+ provider :withings2,
46
+ ENV['WITHINGS_CLIENT_ID'],
47
+ ENV['WITHINGS_CONSUMER_SECRET'],
48
+ scope: 'user.metrics'
49
+ end
50
+ ```
51
+
52
+ In Rails, create a new file under config/initializers called omniauth.rb to plug the strategy into your middleware stack.
53
+
54
+ ```ruby
55
+ Rails.application.config.middleware.use OmniAuth::Builder do
56
+ provider :withings2,
57
+ ENV['WITHINGS_CLIENT_ID'],
58
+ ENV['WITHINGS_CONSUMER_SECRET'],
59
+ scope: 'user.metrics'
60
+ end
61
+ ```
62
+
63
+ In your controller action (responding to /auth/nokia/callback), get the credentials and store them for later interaction with the API:
64
+
65
+ ```ruby
66
+ request.env['omniauth.auth']['credentials']
67
+ ```
68
+
69
+ ### TODO: update
70
+ To interact with the Nokia API (e.g. retrieve weight measurements recorded by a Nokia scale):
71
+
72
+ ```ruby
73
+ oauth_consumer = OAuth::Consumer.new(
74
+ ENV['NOKIA_API_KEY'],
75
+ ENV['NOKIA_API_SECRET'],
76
+ OmniAuth::Strategies::Nokia.consumer_options,
77
+ )
78
+
79
+ api_access_token = OAuth::AccessToken.from_hash(oauth_consumer, {
80
+ oauth_token: nokia_user_access_token,
81
+ oauth_token_secret: nokia_user_access_token_secret,
82
+ })
83
+
84
+ # Change the uri to access other Nokia API endpoints
85
+ uri = "https://api.health.nokia.com/measure?action=getmeas&userid=#{nokia_user_id}"
86
+
87
+ request = api_access_token.get(uri)
88
+ JSON.parse(request.body)
89
+ ```
90
+ ### END TODO: update
91
+
92
+ ## About OmniAuth
93
+
94
+ For additional information about OmniAuth, visit [OmniAuth wiki](https://github.com/intridea/omniauth/wiki).
95
+
96
+ For a short tutorial on how to use OmniAuth in your Rails application, visit [this tutsplus.com tutorial](http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/).
97
+
98
+ (The above adapted from [omniauth-fitbit](https://github.com/tkgospodinov/omniauth-fitbit))
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/omniauth-nokia.
103
+
104
+ ## Original License
105
+
106
+ Copyright (c) 2016 TK Gospodinov. See [LICENSE](https://github.com/tkgospodinov/omniauth-fitbit/blob/master/LICENSE.md) for details.
107
+
108
+ Permission is hereby granted, free of charge, to any person obtaining
109
+ a copy of this software and associated documentation files (the
110
+ "Software"), to deal in the Software without restriction, including
111
+ without limitation the rights to use, copy, modify, merge, publish,
112
+ distribute, sublicense, and/or sell copies of the Software, and to
113
+ permit persons to whom the Software is furnished to do so, subject to
114
+ the following conditions:
115
+
116
+ The above copyright notice and this permission notice shall be
117
+ included in all copies or substantial portions of the Software.
118
+
119
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
120
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
121
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
122
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
123
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
124
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
125
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/example/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'omniauth-withings2', path: '../'
4
+ gem 'pry'
5
+ gem 'pry-stack_explorer'
6
+ gem 'sinatra'
@@ -0,0 +1,20 @@
1
+ require 'sinatra'
2
+ require 'omniauth-withings2'
3
+ require 'pry'
4
+
5
+ use Rack::Session::Cookie
6
+ use OmniAuth::Builder do
7
+ provider :withings2, ENV['WITHINGS_CLIENT_ID'], ENV['WITHINGS_CONSUMER_SECRET'], { :scope => 'user.metrics', :redirect_uri => 'http://localhost:4567/auth/withings2/callback' }
8
+ end
9
+
10
+ get '/' do
11
+ <<-HTML
12
+ <a href='/auth/withings2'>Sign in with Withings</a>
13
+ HTML
14
+ end
15
+
16
+ get '/auth/withings2/callback' do
17
+ # Do whatever you want with the data
18
+ # MultiJson.encode(request.env['omniauth.auth'])
19
+ request.env['omniauth.auth'].to_json
20
+ end
@@ -0,0 +1,37 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Withings2 < OmniAuth::Strategies::OAuth2
6
+
7
+ option :name, 'withings2'
8
+ option :client_options, {
9
+ :site => 'https://account.health.nokia.com',
10
+ :authorize_url => 'https://account.health.nokia.com/oauth2_user/authorize2',
11
+ :token_url => 'https://account.health.nokia.com/oauth2/token'
12
+ }
13
+
14
+ option :response_type, 'code'
15
+ option :authorize_options, %i(scope response_type redirect_uri)
16
+
17
+ def build_access_token
18
+ options.token_params.merge!(:headers => {'Authorization' => basic_auth_header })
19
+ super
20
+ end
21
+
22
+ def basic_auth_header
23
+ "Basic " + Base64.strict_encode64("#{options[:client_id]}:#{options[:client_secret]}")
24
+ end
25
+
26
+ def query_string
27
+ # Using state and code params in the callback_url causes a mismatch with
28
+ # the value set in the withings2 application configuration, so we're skipping them
29
+ ''
30
+ end
31
+
32
+ uid do
33
+ access_token.params['user_id']
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Withings
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'omniauth-withings2/version'
2
+ require 'omniauth/strategies/withings2'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "omniauth-withings2/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "omniauth-withings2"
7
+ s.version = OmniAuth::Withings::VERSION
8
+ s.authors = ["Daniel Nelson"]
9
+ s.email = ["daniel@platejoy.com"]
10
+ s.homepage = "http://github.com/platejoy/omniauth-withings2"
11
+ s.summary = %q{OmniAuth OAuth2 strategy for Withings}
12
+ s.description = %q{OmniAuth OAuth2 strategy for Withings}
13
+
14
+ s.files = `git ls-files`.split($\)
15
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_runtime_dependency 'omniauth-oauth2', '~> 1.4'
20
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe "OmniAuth::Strategies::Withings2" do
4
+ subject do
5
+ OmniAuth::Strategies::Withings2.new(nil, @options || {})
6
+ end
7
+
8
+ describe 'response_type' do
9
+ it 'includes :code' do
10
+ expect(subject.options["response_type"]).to include('code')
11
+ end
12
+ end
13
+
14
+ describe 'authorize_options' do
15
+ it 'includes :scope' do
16
+ expect(subject.options["authorize_options"]).to include(:scope)
17
+ end
18
+
19
+ it 'includes :response_type' do
20
+ expect(subject.options["authorize_options"]).to include(:response_type)
21
+ end
22
+
23
+ it 'includes :redirect_uri' do
24
+ expect(subject.options["authorize_options"]).to include(:redirect_uri)
25
+ end
26
+ end
27
+
28
+ context 'client options' do
29
+ it 'has correct OAuth endpoint' do
30
+ expect(subject.options.client_options.site).to eq('https://account.health.nokia.com')
31
+ end
32
+
33
+ it 'has correct authorize url' do
34
+ expect(subject.options.client_options.authorize_url).to eq('https://account.health.nokia.com/oauth2_user/authorize2')
35
+ end
36
+
37
+ it 'has correct token url' do
38
+ expect(subject.options.client_options.token_url).to eq('https://account.health.nokia.com/oauth2/token')
39
+ end
40
+ end
41
+
42
+ context 'auth header' do
43
+ before :each do
44
+ subject.options.client_id = 'testclientid'
45
+ subject.options.client_secret = 'testclientsecret'
46
+ end
47
+
48
+ it 'returns the correct authorization header value' do
49
+ expect(subject.basic_auth_header).to eq('Basic ' + Base64.strict_encode64("testclientid:testclientsecret"))
50
+ end
51
+ end
52
+
53
+ context 'uid' do
54
+ before :each do
55
+ access_token = double('access_token')
56
+ allow(access_token).to receive('params') { { 'user_id' => '123ABC' } }
57
+ allow(subject).to receive(:access_token) { access_token }
58
+ end
59
+
60
+ it 'returns the correct id from raw_info' do
61
+ expect(subject.uid).to eq('123ABC')
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path('..', __FILE__)
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rspec'
5
+ require 'rack/test'
6
+ require 'webmock/rspec'
7
+ require 'omniauth-oauth2'
8
+ require 'omniauth-withings2'
9
+
10
+ RSpec.configure do |config|
11
+ config.include WebMock::API
12
+ config.include Rack::Test::Methods
13
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
14
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-withings2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Nelson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-08 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.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ description: OmniAuth OAuth2 strategy for Withings
28
+ email:
29
+ - daniel@platejoy.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - LICENSE.md
38
+ - README.md
39
+ - Rakefile
40
+ - example/Gemfile
41
+ - example/example.rb
42
+ - lib/omniauth-withings2.rb
43
+ - lib/omniauth-withings2/version.rb
44
+ - lib/omniauth/strategies/withings2.rb
45
+ - omniauth-withings2.gemspec
46
+ - spec/omniauth/strategies/withings2_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: http://github.com/platejoy/omniauth-withings2
49
+ licenses: []
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.7.6
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: OmniAuth OAuth2 strategy for Withings
71
+ test_files:
72
+ - spec/omniauth/strategies/withings2_spec.rb
73
+ - spec/spec_helper.rb