Skip to content
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

Open
wants to merge 6 commits into
base: 3-1-stable
Choose a base branch
from

Conversation

Oldharlem
Copy link

This PR adds the option to use a valid access token to authenticate with the application.
The omniauth strategies are used for the authentication but obtaining a token part is skipped. Instead, the token is directly fed to the strategy and the user info is retrieved via the strategy.

This feature would allow any API consumer with a valid token to register or login using this token.

Could not test since they seem broken on 3-1-stable. Is anybody else experiencing problems running the tests?

getting an undefined method 'load_defaults' for #<Rails::Application::Configuration:0x00007f8673c52e60> error

end
end

Spree::Api::V1::UsersController.prepend(Spree::Api::V1::UsersControllerDecorator)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]
Layout/TrailingBlankLines: Final newline missing.

:user_id => "#{user.id}"
}}
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EmptyLinesAroundModuleBody: Extra empty line detected at module body end.

:user => "#{user.login}",
:api_key => "#{user.spree_api_key}",
:user_id => "#{user.id}"
}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideHashLiteralBraces: Space inside } missing.

render :json => {:result => {
:user => "#{user.login}",
:api_key => "#{user.spree_api_key}",
:user_id => "#{user.id}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
Style/UnneededInterpolation: Prefer to_s over string interpolation.

def render_user_login(user)
render :json => {:result => {
:user => "#{user.login}",
:api_key => "#{user.spree_api_key}",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
Style/UnneededInterpolation: Prefer to_s over string interpolation.

else
user = Spree::User.find_by_email(params[:email]) || Spree::User.new
user.apply_omniauth(omniauth_hash)
if user.save!

Choose a reason for hiding this comment

The 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 &&/||.

omniauth_hash = authentication_method.get_omniauth_hash(params[:oauth_token])
authentication = Spree::UserAuthentication.find_by_provider_and_uid(params[:provider], omniauth_hash['uid'])

if authentication.present? and authentication.try(:user).present?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/AndOr: Use && instead of and.

authentication_method = Spree::AuthenticationMethod.find_by_provider(params[:provider])
render json: {exception: 'Unsupported provider'}, status: 422 and return unless authentication_method
omniauth_hash = authentication_method.get_omniauth_hash(params[:oauth_token])
authentication = Spree::UserAuthentication.find_by_provider_and_uid(params[:provider], omniauth_hash['uid'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [118/80]

def social_login
authentication_method = Spree::AuthenticationMethod.find_by_provider(params[:provider])
render json: {exception: 'Unsupported provider'}, status: 422 and return unless authentication_method
omniauth_hash = authentication_method.get_omniauth_hash(params[:oauth_token])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [87/80]


def social_login
authentication_method = Spree::AuthenticationMethod.find_by_provider(params[:provider])
render json: {exception: 'Unsupported provider'}, status: 422 and return unless authentication_method

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
Style/AndOr: Use && instead of and.
Metrics/LineLength: Line is too long. [111/80]

def oauth_providers
user_authentications.map do |user_authentication|
{
provider: user_authentication.provider,

Choose a reason for hiding this comment

The 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.

def strategy(token)
app = lambda {|env| [200, {}, ["Hello World."]]}
options = [api_key, api_secret]
strategy_class.new(app, *options).tap {|s| s.access_token = access_token(token)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideBlockBraces: Space between { and | missing.
Metrics/LineLength: Line is too long. [84/80]
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

def strategy(token)
app = lambda {|env| [200, {}, ["Hello World."]]}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Lambda: Use the -> { ... } lambda literal syntax for single line lambdas.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Lint/UnusedBlockArgument: Unused block argument - env. If it's necessary, use _ or _env as an argument name to indicate that it won't be used. Also consider using a proc without arguments instead of a lambda if you want it to accept any arguments but don't care about them.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

def client
::OAuth2::Client.new(api_key, api_secret, strategy_class.default_options.client_options.to_h).tap do |c|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [108/80]

end

def provider_must_be_backed_by_omniauth_strategy
errors.add(:provider, 'must be backed by an omniauth strategy') unless strategy_class.safe_constantize

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [106/80]

module UsersControllerDecorator

def social_login
authentication_method = Spree::AuthenticationMethod.find_by_provider(params[:provider])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [97/80]

module V1
module UsersControllerDecorator

def social_login

Choose a reason for hiding this comment

The 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]
Metrics/CyclomaticComplexity: Cyclomatic complexity for social_login is too high. [11/6]
Metrics/MethodLength: Method has too many lines. [22/10]
Metrics/PerceivedComplexity: Perceived complexity for social_login is too high. [12/7]

module Api
module V1
module UsersControllerDecorator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EmptyLinesAroundModuleBody: Extra empty line detected at module body beginning.

module Spree
module Api
module V1
module UsersControllerDecorator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Documentation: Missing top-level module documentation comment.

@@ -0,0 +1,64 @@
module Spree

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.

@Oldharlem
Copy link
Author

Is this repo still active?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants