Skip to content

Commit ce4225a

Browse files
committed
omniauth OAuth2 handshake for Fitbit
1 parent c00b617 commit ce4225a

File tree

6 files changed

+129
-2
lines changed

6 files changed

+129
-2
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1-
# omniauth-fitbit-oauth2
2-
OmniAuth OAuth2 strategy for Fitbit
1+
# OmniAuth OAuth2 strategy for Fitbit
2+
This gem is an OAuth2 OmniAuth Strategy for the [Fitbit API](https://wiki.fitbit.com/display/API/OAuth+2.0).
3+
4+
## Installing
5+
6+
Add to your `Gemfile`:
7+
8+
```ruby
9+
gem 'omniauth-fitbit-oauth2'
10+
```
11+
12+
Then `bundle install`.
13+
14+
## Usage
15+
16+
`OmniAuth::Strategies::FitbitOauth2` is simply a Rack middleware. Read [the OmniAuth 2.0 docs](https://github.com/intridea/omniauth-oauth2) for detailed instructions.
17+
18+
Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
19+
20+
```ruby
21+
Rails.application.config.middleware.use OmniAuth::Builder do
22+
provider :fitbit_oauth2, ENV['FITBIT_CLIENT_ID'], ENV['FITBIT_CLIENT_SECRET']
23+
end
24+
```

lib/omniauth-fitbit-oauth2.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'omniauth-fitbit-oauth2/version'
2+
require 'omniauth/strategies/fitbit_oauth2'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module OmniAuth
2+
module FitbitOauth2
3+
VERSION = '0.0.1'
4+
end
5+
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require 'omniauth-oauth2'
2+
require 'base64'
3+
4+
module OmniAuth
5+
module Strategies
6+
class FitbitOauth2 < OmniAuth::Strategies::OAuth2
7+
option :name, 'fitbit_oauth2'
8+
9+
option :client_options,
10+
{
11+
:site => 'https://api.fitbit.com',
12+
:authorize_url => 'https://www.fitbit.com/oauth2/authorize',
13+
:token_url => 'https://api.fitbit.com/oauth2/token'
14+
}
15+
16+
option :authorize_options, [:scope, :response_type]
17+
option :response_type, 'code'
18+
19+
def build_access_token
20+
options.token_params.merge!(:headers =>
21+
{'Authorization' => basic_auth_header})
22+
super
23+
end
24+
25+
def basic_auth_header
26+
'Basic ' + Base64.encode64(options[:client_id] + ':' +
27+
options[:client_secret]).gsub("\n", '')
28+
end
29+
30+
uid do
31+
raw_info['user']['encodedId']
32+
end
33+
34+
info do
35+
{
36+
:name => raw_info['user']['displayName'],
37+
:full_name => raw_info['user']['fullName'],
38+
:display_name => raw_info['user']['displayName'],
39+
:nickname => raw_info['user']['nickname'],
40+
:gender => raw_info['user']['gender'],
41+
:about_me => raw_info['user']['aboutMe'],
42+
:city => raw_info['user']['city'],
43+
:state => raw_info['user']['state'],
44+
:country => raw_info['user']['country'],
45+
:dob => !raw_info['user']['dateOfBirth'].empty? ? Date.strptime(raw_info['user']['dateOfBirth'], '%Y-%m-%d'):nil,
46+
:member_since => Date.strptime(raw_info['user']['memberSince'], '%Y-%m-%d'),
47+
:locale => raw_info['user']['locale'],
48+
:timezone => raw_info['user']['timezone']
49+
}
50+
end
51+
52+
extra do
53+
{
54+
:raw_info => raw_info
55+
}
56+
end
57+
58+
def raw_info
59+
if options[:use_english_measure] == 'true'
60+
@raw_info ||= MultiJson.load(access_token.
61+
request('get', 'https://api.fitbit.com/1/user/-/profile.json',
62+
{ 'Accept-Language' => 'en_US' }).body)
63+
else
64+
@raw_info ||= MultiJson.load(access_token.
65+
get('https://api.fitbit.com/1/user/-/profile.json').body)
66+
end
67+
end
68+
end
69+
end
70+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'spec_helper'
2+
3+
describe "OmniAuth::Strategies::FitbitOauth2" do
4+
subject do
5+
OmniAuth::Strategies::FitbitOauth2.new(nil, @options || {})
6+
end
7+
8+
context 'client options' do
9+
it 'has correct API site' do
10+
expect(subject.options.client_options.site).
11+
to eq('https://api.fitbit.com')
12+
end
13+
14+
it 'has correct token url' do
15+
expect(subject.options.client_options.token_url).
16+
to eq('https://api.fitbit.com/oauth2/token')
17+
end
18+
19+
it 'has correct authorize url' do
20+
expect(subject.options.client_options.authorize_url).
21+
to eq('https://www.fitbit.com/oauth2/authorize')
22+
end
23+
end
24+
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'bundler/setup'
2+
Bundler.setup
3+
4+
require 'omniauth-fitbit-oauth2'

0 commit comments

Comments
 (0)