omniauth-yahoo_auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5e125eca3d64d95d72823a151562846622645f4
4
+ data.tar.gz: 4d5d29ec9d5fa62f44d84782010b4fa586cf6233
5
+ SHA512:
6
+ metadata.gz: 62c8543dc77539c152a7d269b5442c813fecfa803877258bd6dcb5c300c73d8fcf19c724dcad09fb54b2da0459782853a3956de00b37f5865f82a0c8ce366caf
7
+ data.tar.gz: ec13f37df678478f612b5382831e04981a8c9302926a44beb8a715ccaf45402cb69f670de98efa3e761f2edd92c7661f92318a54804411cedaa645e7b3c70e34
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at karan150394@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Karan Pathak
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.
@@ -0,0 +1,182 @@
1
+ # OmniAuth Yahoo OAuth2 Strategy
2
+
3
+ Yahoo OAuth2 Strategy for OmniAuth. <br>
4
+ Supports OAuth 2.0 client-side flow. Read about it at: https://developer.yahoo.com/oauth2/guide/
5
+
6
+ ## Installation
7
+
8
+ Add to your `Gemfile`:
9
+
10
+ ```ruby
11
+ gem 'omniauth-yahoo_auth'
12
+ ```
13
+
14
+ Then `bundle install`.
15
+
16
+ ## Yahoo App Setup
17
+ * Go to https://developer.yahoo.com/apps/
18
+ * Click on `Create an app`. Give an application name, callback domain and Api Permissions.
19
+ * This gem is tested with an app that had contacts and profiles API enabled with read access.
20
+ * Then hit create app button.
21
+ * Yahoo will now give you your app's id and secret.
22
+
23
+ ## Usage
24
+
25
+ * Add a route `get 'auth/:provider/callback', to: 'sessions#custom'` in your routes.rb file
26
+ * Create a sessions controller and a custom method.
27
+ * Add the following in one of your initializer files or create a new one(say config/initializers/omniauth.rb)
28
+ ```ruby
29
+ Rails.application.config.middleware.use OmniAuth::Builder do
30
+ provider :yahoo_auth, ENV['YAHOO_APP_ID'], ENV['YAHOO_APP_SECRET']
31
+ end
32
+ ```
33
+ * You can now restart your server and go to `/auth/yahoo_auth`.
34
+ * Yahoo should now prompt the user to login using yahoo credentials. Enter them and hit login.
35
+ * You should be able to get access to an Auth hash using `env["omniauth.auth"]` inside your session#custom method.
36
+
37
+ **NOTE**: While developing your application, if you change the scope in the initializer you will need to restart your app server.
38
+
39
+ ## Configuring
40
+
41
+ You can configure several options, which you pass in to the `provider` method via a `Hash`:
42
+
43
+ Option name | Default | Explanation
44
+ --- | --- | ---
45
+ `name` | `yahoo_auth` | It can be changed to any value, for example `yahoo`. The OmniAuth URL will thus change to /auth/yahoo .
46
+ `redirect_uri` | `/auth/yahoo/callback` | Specify a custom callback URL used during the server-side flow. Default is `https://www.your_callback_domain/auth/yahoo/callback`
47
+ `image_size` | `192x192` | Set the size for the returned image in the auth hash. Valid options include sizes: 16x16, 24x24, 32x32, 48x48, 64x64, 96x96, 128x128, 192x192
48
+
49
+ For example:
50
+
51
+ ```ruby
52
+ Rails.application.config.middleware.use OmniAuth::Builder do
53
+ provider :yahoo_auth, ENV['YAHOO_APP_ID'], ENV['YAHOO_APP_SECRET'],
54
+ { name: "yahoo",
55
+ redirect_uri: "https://www.your_callback_domain/auth/yahoo/callback",
56
+ image_size: "96x96"}
57
+ end
58
+ ```
59
+
60
+ ## Auth Hash
61
+
62
+ Here's an example *Auth Hash* available in `request.env['omniauth.auth']`:
63
+
64
+ ```ruby
65
+ {
66
+ info: {
67
+ nickname: 'Harvey',
68
+ email: 'harvey@suits.com',
69
+ first_name: 'Harvey',
70
+ last_name: 'Specter',
71
+ image: 'https://s.yimg.com/wm/modern/images/default_user_profile_pic_192.png',
72
+ },
73
+ credentials: {
74
+ token: 'HnEU9cep1...', # OAuth 2.0 ACCESS_TOKEN.
75
+ refresh_token: 'AFBTm...', # REFRESH_TOKEN to to get a new OAuth 2.0 access_token when the previous one expires.
76
+ expires_at: 1503232413, # Time at which your OAuth 2.0 access_token expires.
77
+ expires: true # this will always be true.
78
+ },
79
+ extra: {
80
+ gender: 'M',
81
+ language: 'en-IN',
82
+ location: 'User Location',
83
+ birth_year: 'User birth year',
84
+ birth_date: 'User birth date',
85
+ addresses: 'User addresses',
86
+ urls: {
87
+ default_image: 'https://s.yimg.com/wm/modern/images/default_user_profile_pic_192.png',
88
+ profile: 'http://profile.yahoo.com/KBA...'
89
+ }
90
+ }
91
+ }
92
+ ```
93
+
94
+ The precise information available will depend on your request.
95
+
96
+ ## Integration with Devise
97
+
98
+ First define your application id and secret in `config/initializers/devise.rb`. Do not use the snippet mentioned in the [Usage](https://github.com/creative-karan/omniauth-yahoo_auth#usage) section.
99
+
100
+ Configuration options can be passed as the last parameter here as key/value pairs.
101
+
102
+ ```ruby
103
+ config.omniauth :yahoo_auth, 'YAHOO_APP_ID', 'YAHOO_APP_SECRET', {}
104
+ ```
105
+
106
+ Then add the following to 'config/routes.rb' so the callback routes are defined.
107
+
108
+ ```ruby
109
+ devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
110
+ ```
111
+
112
+ Make sure your model is omniauthable. Generally this is "/app/models/user.rb"
113
+
114
+ ```ruby
115
+ devise :omniauthable, omniauth_providers: [:yahoo_auth]
116
+ ```
117
+
118
+ Then make sure your callbacks controller is setup.
119
+
120
+ ```ruby
121
+ # app/controllers/users/omniauth_callbacks_controller.rb
122
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
123
+ def yahoo_auth
124
+ # You need to implement the method below in your model (e.g. app/models/user.rb)
125
+ @user = User.from_omniauth(request.env['omniauth.auth'])
126
+
127
+ if @user.persisted?
128
+ flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Yahoo'
129
+ sign_in_and_redirect @user, event: :authentication
130
+ else
131
+ session['devise.yahoo_data'] = request.env['omniauth.auth']
132
+ redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
133
+ end
134
+ end
135
+ end
136
+ ```
137
+
138
+ and bind to or create the user
139
+
140
+ ```ruby
141
+ # app/models/user.rb
142
+ def self.from_omniauth(access_token)
143
+ data = access_token.info
144
+ user = User.where(email: data['email']).first
145
+ # Uncomment the section below if you want users to be created if they don't exist
146
+ # unless user
147
+ # user = User.create(name: data['nickname'],
148
+ # email: data['email'],
149
+ # password: Devise.friendly_token[0,20]
150
+ # )
151
+ # end
152
+ user
153
+ end
154
+ ```
155
+
156
+ For your views you can login using:
157
+
158
+ ```erb
159
+ <%= link_to "Sign in with Yahoo", user_yahoo_auth_omniauth_authorize_path %>
160
+
161
+ <%# Devise prior 4.1.0: %>
162
+ <%= link_to "Sign in with Yahoo", user_omniauth_authorize_path(:yahoo_auth) %>
163
+ ```
164
+
165
+ ## Test this gem locally
166
+ Yahoo doesn't allow `localhost or 127.0.0.1` as callback domain while making an app. <br>
167
+ So, you can follow below steps to test this gem on your local environment.
168
+
169
+ * In case you are using unix or linux system, create a alias like `127.0.0.1 mywebsite.dev` in /etc/hosts
170
+ (you need have the line which is similar to the one mentioned here in the file)
171
+ * Use http://website.dev/callbackurl/for/app in call back URL during local testing.
172
+ * You will need to run your rails app at port 80.
173
+ * To forcefully run your app on port 80 use `rvmsudo rails s -p 80`
174
+
175
+ ## Further Reading
176
+ * Authorization flow of yahoo is described at : https://developer.yahoo.com/oauth2/guide/flows_authcode/.
177
+ * Yahoo social api documentation : https://developer.yahoo.com/social/rest_api_guide/
178
+ * Overview of devise and OAuth gems working together : https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
179
+
180
+ ## Contributing
181
+
182
+ Bug reports and pull requests are welcome on GitHub at https://github.com/creative-karan/omniauth-yahoo_auth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "omniauth/yahoo_auth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,110 @@
1
+ require 'omniauth-oauth2'
2
+ require 'base64'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ # Main class for Yahoo Auth Startegy
7
+ class YahooAuth < OmniAuth::Strategies::OAuth2
8
+ SOCIAL_API_URL = "https://social.yahooapis.com/v1/user/"
9
+
10
+ option :name, "yahoo_auth"
11
+
12
+ option :client_options, {
13
+ site: "https://api.login.yahoo.com",
14
+ authorize_url: "https://api.login.yahoo.com/oauth2/request_auth",
15
+ token_url: "https://api.login.yahoo.com/oauth2/get_token"
16
+ }
17
+
18
+ uid { access_token.params['xoauth_yahoo_guid'] }
19
+
20
+ info do
21
+ prune!(
22
+ nickname: raw_info['nickname'],
23
+ email: get_primary_email,
24
+ first_name: raw_info['givenName'],
25
+ last_name: raw_info['familyName'],
26
+ image: get_user_image
27
+ )
28
+ end
29
+
30
+ extra do
31
+ prune!(
32
+ gender: raw_info['gender'],
33
+ language: raw_info['lang'],
34
+ location: raw_info['location'],
35
+ birth_year: raw_info['birthYear'],
36
+ birth_date: raw_info['birthdate'],
37
+ addresses: raw_info['addresses'],
38
+ urls: {
39
+ default_image: raw_info['image']['imageUrl'],
40
+ profile: raw_info['profileUrl']
41
+ }
42
+ )
43
+ end
44
+
45
+ def raw_info
46
+ # This is a public API and does not need signing or authentication
47
+ raw_info_url = "#{SOCIAL_API_URL}#{uid}/profile?format=json"
48
+ @raw_info ||= access_token.get(raw_info_url).parsed['profile'] || {}
49
+ rescue ::Errno::ETIMEDOUT
50
+ raise ::Timeout::Error
51
+ end
52
+
53
+ def custom_build_access_token
54
+ get_access_token(request)
55
+ end
56
+ alias build_access_token custom_build_access_token
57
+
58
+ private
59
+
60
+ def callback_url
61
+ options[:redirect_uri] || (full_host + script_name + callback_path)
62
+ end
63
+
64
+ def prune!(hash)
65
+ hash.delete_if do |_, value|
66
+ prune!(value) if value.is_a?(Hash)
67
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
68
+ end
69
+ end
70
+
71
+ def get_primary_email
72
+ email = nil
73
+ email_hash = raw_info['emails']
74
+ if email_hash
75
+ email_info = email_hash.find{|e| e['primary']} || email_hash.first
76
+ email = email_info['handle']
77
+ end
78
+ email
79
+ end
80
+
81
+ def get_user_image
82
+ debugger
83
+ image_size = options[:image_size]
84
+ if image_size
85
+ image_url = "#{SOCIAL_API_URL}#{uid}/profile/image/#{image_size}?format=json"
86
+ image_hash = access_token.get(image_url).parsed["image"] || {}
87
+ image_hash["imageUrl"]
88
+ else
89
+ # Return default image
90
+ raw_info['image']['imageUrl']
91
+ end
92
+ end
93
+
94
+ def get_access_token(request)
95
+ verifier = request.params['code']
96
+ auth = "Basic #{Base64.strict_encode64("#{options.client_id}:#{options.client_secret}")}"
97
+ token = client.get_token(
98
+ { redirect_uri: callback_url,
99
+ code: verifier,
100
+ grant_type: 'authorization_code',
101
+ headers: { 'Authorization' => auth }
102
+ }.merge(token_params.to_hash(symbolize_keys: true)),
103
+ deep_symbolize(options.auth_token_params || {})
104
+ )
105
+ token
106
+ end
107
+
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,8 @@
1
+ require "omniauth/yahoo_auth/version"
2
+ require 'omniauth/strategies/yahoo_auth'
3
+
4
+ # module Omniauth
5
+ # module YahooAuth
6
+ # # Your code goes here...
7
+ # end
8
+ # end
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module YahooAuth
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth/yahoo_auth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "omniauth-yahoo_auth"
8
+ spec.version = Omniauth::YahooAuth::VERSION
9
+ spec.authors = ["Karan Pathak"]
10
+ spec.email = ["karan150394@gmail.com"]
11
+
12
+ spec.summary = "Yahoo OAuth2 Strategy for OmniAuth."
13
+ spec.description = "Yahoo OAuth2 Strategy. It lets you sign-in a rails app using yahoo login. Supports OAuth 2.0 client-side flow"
14
+ spec.homepage = "https://github.com/creative-karan/omniauth-yahoo_auth"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-yahoo_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karan Pathak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Yahoo OAuth2 Strategy. It lets you sign-in a rails app using yahoo login.
56
+ Supports OAuth 2.0 client-side flow
57
+ email:
58
+ - karan150394@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/omniauth/strategies/yahoo_auth.rb
74
+ - lib/omniauth/yahoo_auth.rb
75
+ - lib/omniauth/yahoo_auth/version.rb
76
+ - omniauth-yahoo_auth.gemspec
77
+ homepage: https://github.com/creative-karan/omniauth-yahoo_auth
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.12
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Yahoo OAuth2 Strategy for OmniAuth.
101
+ test_files: []