|
| 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 |
0 commit comments