-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added api exposure for logging in with an oauth access token. #237
base: 3-1-stable
Are you sure you want to change the base?
Changes from 3 commits
a2d8813
0c5ddd8
b47824f
4f1cff8
113e259
2770f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Spree | ||
module Api | ||
module V1 | ||
module UsersControllerDecorator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/Documentation: Missing top-level module documentation comment. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundModuleBody: Extra empty line detected at module body beginning. |
||
def social_login | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/AbcSize: Assignment Branch Condition size for social_login is too high. [32.95/15] |
||
authentication_method = Spree::AuthenticationMethod.find_by_provider(params[:provider]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [97/80] |
||
render json: {exception: 'Unsupported provider'}, status: 422 and return unless authentication_method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideHashLiteralBraces: Space inside { missing. |
||
omniauth_hash = authentication_method.get_omniauth_hash(params[:oauth_token]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [87/80] |
||
authentication = Spree::UserAuthentication.find_by_provider_and_uid(params[:provider], omniauth_hash['uid']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [118/80] |
||
|
||
if authentication.present? and authentication.try(:user).present? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/AndOr: Use && instead of and. |
||
render_user_login(authentication.user) | ||
elsif @current_api_user | ||
@current_api_user.apply_omniauth(omniauth_hash) | ||
@current_api_user.save! | ||
render_user_login(@current_api_user) | ||
else | ||
user = Spree::User.find_by_email(params[:email]) || Spree::User.new | ||
user.apply_omniauth(omniauth_hash) | ||
if user.save! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. |
||
render_user_login(user) | ||
end | ||
end | ||
|
||
if @order | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression. |
||
user = @current_api_user || authentication.user | ||
@order.associate_user!(user) | ||
end | ||
end | ||
|
||
def oauth_providers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/MethodLength: Method has too many lines. [11/10] |
||
auth_methods = Spree::AuthenticationMethod.active_authentication_methods.pluck(:provider) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [99/80] |
||
auth_methods.map! do |provider| | ||
oauth_provider = SpreeSocial::OAUTH_PROVIDERS.detect {|p| p[1] == provider} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | missing. |
||
{ | ||
name: oauth_provider[0], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. |
||
provider: provider, | ||
signup_support: oauth_provider[2] | ||
} | ||
end | ||
render json: auth_methods, status: :ok | ||
end | ||
|
||
private | ||
|
||
def render_user_login(user) | ||
render :json => {:result => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
:user => "#{user.login}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. |
||
:api_key => "#{user.spree_api_key}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
:user_id => "#{user.id}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideHashLiteralBraces: Space inside } missing. |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundModuleBody: Extra empty line detected at module body end. |
||
end | ||
end | ||
end | ||
end | ||
|
||
Spree::Api::V1::UsersController.prepend(Spree::Api::V1::UsersControllerDecorator) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [81/80] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,53 @@ | ||
class Spree::AuthenticationMethod < ActiveRecord::Base | ||
validates :provider, :api_key, :api_secret, presence: true | ||
|
||
validate :provider_must_be_backed_by_omniauth_strategy | ||
|
||
def self.active_authentication_methods | ||
where(environment: ::Rails.env, active: true) | ||
end | ||
|
||
def self.active_authentication_methods? | ||
where(environment: ::Rails.env, active: true).exists? | ||
active_authentication_methods.exists? | ||
end | ||
|
||
scope :available_for, lambda { |user| | ||
scope :available_for, lambda {|user| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | missing. |
||
sc = where(environment: ::Rails.env) | ||
sc = sc.where.not(provider: user.user_authentications.pluck(:provider)) if user && !user.user_authentications.empty? | ||
sc | ||
} | ||
|
||
def get_omniauth_hash(token) | ||
strategy(token).auth_hash | ||
end | ||
|
||
def provider_must_be_backed_by_omniauth_strategy | ||
begin | ||
strategy_class | ||
rescue NameError => e | ||
errors.add(:provider, 'must be backed by an omniauth strategy') | ||
end | ||
end | ||
|
||
private | ||
|
||
def strategy_class | ||
"::OmniAuth::Strategies::#{provider.classify}".constantize | ||
end | ||
|
||
def client | ||
::OAuth2::Client.new(api_key, api_secret, strategy_class.default_options.client_options.to_h).tap do |c| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [108/80] |
||
c.site = strategy_class.default_options.client_options['site'] | ||
end | ||
end | ||
|
||
def access_token(token) | ||
::OAuth2::AccessToken.new(client, token) | ||
end | ||
|
||
def strategy(token) | ||
app = lambda {|env| [200, {}, ["Hello World."]]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/Lambda: Use the -> { ... } lambda literal syntax for single line lambdas. |
||
options = [api_key, api_secret] | ||
strategy_class.new(app, *options).tap {|s| s.access_token = access_token(token)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | missing. |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,13 @@ def apply_omniauth(omniauth) | |
def password_required? | ||
(user_authentications.empty? || !password.blank?) && super | ||
end | ||
|
||
def oauth_providers | ||
user_authentications.map do |user_authentication| | ||
{ | ||
provider: user_authentication.provider, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. |
||
uid: user_authentication.uid | ||
} | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.