Skip to content

Commit

Permalink
Import strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
jmckible committed Apr 13, 2016
1 parent 573f703 commit 41f25b5
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in omniauth-surveymonkey.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/omniauth-surveymonkey/omniauth-surveymonkey.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'omniauth-surveymonkey/version'
require 'omniauth/strategies/github'

OmniAuth.config.add_camelization 'surveymonkey', 'SurveyMonkey'
5 changes: 5 additions & 0 deletions lib/omniauth-surveymonkey/omniauth-surveymonkey/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OmniAuth
module SurveyMonkey
VERSION = '0.0.1'
end
end
71 changes: 71 additions & 0 deletions lib/omniauth-surveymonkey/omniauth/strategies/surveymonkey.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions omniauth-surveymonkey.gemspec
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'
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

0 comments on commit 41f25b5

Please sign in to comment.