From 41f25b526ada596733e474cb3a1947978317913f Mon Sep 17 00:00:00 2001 From: Jordan McKible Date: Wed, 13 Apr 2016 09:59:42 -0700 Subject: [PATCH] Import strategy --- .gitignore | 9 +++ Gemfile | 4 ++ LICENSE.txt | 21 ++++++ README.md | 10 ++- .../omniauth-surveymonkey.rb | 4 ++ .../omniauth-surveymonkey/version.rb | 5 ++ .../omniauth/strategies/surveymonkey.rb | 71 +++++++++++++++++++ omniauth-surveymonkey.gemspec | 17 +++++ 8 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 lib/omniauth-surveymonkey/omniauth-surveymonkey.rb create mode 100644 lib/omniauth-surveymonkey/omniauth-surveymonkey/version.rb create mode 100644 lib/omniauth-surveymonkey/omniauth/strategies/surveymonkey.rb create mode 100644 omniauth-surveymonkey.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cb6eeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b60a6cd --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in omniauth-surveymonkey.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..754000a --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Jordan McKible + +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: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +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. diff --git a/README.md b/README.md index 1e12a8a..f029289 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -The _official_ SurveyMonkey Omniauth strategy +# OmniAuth SurveyMonkey + +This is the official OmniAuth strategy for authenticating to SurveyMonkey's v3 API. To use it, you'll need to sign up and create an app at the [SurveyMonkey Developer homepage](https://developer.surveymonkey.com/). + +## Basic Usage + + Rails.application.config.middleware.use OmniAuth::Builder do + provider :surveymonkey, ENV['SURVEYMONKEY_CLIENT_ID'], ENV['SURVEYMONKEY_API_KEY'], ENV['SURVEYMONKEY_SECRET'] + end diff --git a/lib/omniauth-surveymonkey/omniauth-surveymonkey.rb b/lib/omniauth-surveymonkey/omniauth-surveymonkey.rb new file mode 100644 index 0000000..bdb2eaf --- /dev/null +++ b/lib/omniauth-surveymonkey/omniauth-surveymonkey.rb @@ -0,0 +1,4 @@ +require 'omniauth-surveymonkey/version' +require 'omniauth/strategies/github' + +OmniAuth.config.add_camelization 'surveymonkey', 'SurveyMonkey' diff --git a/lib/omniauth-surveymonkey/omniauth-surveymonkey/version.rb b/lib/omniauth-surveymonkey/omniauth-surveymonkey/version.rb new file mode 100644 index 0000000..1011780 --- /dev/null +++ b/lib/omniauth-surveymonkey/omniauth-surveymonkey/version.rb @@ -0,0 +1,5 @@ +module OmniAuth + module SurveyMonkey + VERSION = '0.0.1' + end +end diff --git a/lib/omniauth-surveymonkey/omniauth/strategies/surveymonkey.rb b/lib/omniauth-surveymonkey/omniauth/strategies/surveymonkey.rb new file mode 100644 index 0000000..438910c --- /dev/null +++ b/lib/omniauth-surveymonkey/omniauth/strategies/surveymonkey.rb @@ -0,0 +1,71 @@ +module OmniAuth + module Strategies + + class SurveyMonkey + include OmniAuth::Strategy + + configure url: 'https://www.surveymonkey.com/oauth/authorize' + + args [:client_id, :api_key, :client_secret] + + def request_phase + hash = { client_id: options.client_id, api_key: options.api_key, redirect_uri: callback_url, response_type: 'code' } + redirect "#{options.url}?#{hash.to_query}" + end + + def callback_phase + connection = Faraday.new url: 'https://api.surveymonkey.net' do |faraday| + faraday.request :url_encoded + faraday.response :logger + faraday.adapter Faraday.default_adapter + end + + form_fields = { + client_id: options.client_id, + client_secret: options.client_secret, + code: request.params['code'], + redirect_uri: (full_host + callback_path), + grant_type: 'authorization_code' + } + + response = connection.post "/oauth/token?api_key=#{options.api_key}", form_fields + json = MultiJson.load response.body + options.access_token = json['access_token'] + + connection.authorization :Bearer, options.access_token + info = connection.get "/v3/users/me?api_key=#{options.api_key}" + json = MultiJson.load info.body + + options.username = json['username'] + options.first_name = json['first_name'] + options.last_name = json['last_name'] + options.account_type = json['account_type'] + options.language = json['language'] + options.email = json['email'] + options.surveymonkey_id = json['id'].to_i + + super + end + + uid do + options.surveymonkey_id + end + + info do + { + account_type: options.account_type, + first_name: options.first_name, + last_name: options.last_name, + username: options.username, + language: options.language, + email: options.email + } + end + + credentials do + { token: options.access_token } + end + + end + end +end diff --git a/omniauth-surveymonkey.gemspec b/omniauth-surveymonkey.gemspec new file mode 100644 index 0000000..1352138 --- /dev/null +++ b/omniauth-surveymonkey.gemspec @@ -0,0 +1,17 @@ +require File.expand_path('../lib/omniauth-surveymonkey/version', __FILE__) + +Gem::Specification.new do |s| + s.name = 'omniauth-surveymonkey' + s.version = '0.0.1' + s.date = '2016-04-13' + s.authors = ['Jordan McKible'] + s.email = 'jordanm@surveymonkey.com' + s.license = 'MIT' + + gem.require_paths = ['lib'] + gem.version = OmniAuth::SurveyMonkey::VERSION + + gem.add_dependency 'omniauth', '~> 1.0' + gem.add_dependency 'faraday' + gem.add_dependency 'multi_json' +end