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 support for Vend #549

Merged
merged 1 commit into from
Mar 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions social/backends/vend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Vend OAuth2 backend:

"""
from requests import HTTPError
from social.backends.oauth import BaseOAuth2
from social.exceptions import AuthCanceled, AuthUnknownError


class VendOAuth2(BaseOAuth2):
name = 'vend'
AUTHORIZATION_URL = 'https://secure.vendhq.com/connect'
ACCESS_TOKEN_URL = ''
SCOPE_SEPARATOR = ' '
REDIRECT_STATE = False
REDIRECT_URI_PARAMETER_NAME = 'redirect_uri'
EXTRA_DATA = [
('refresh_token', 'refresh_token'),
('domain_prefix','domain_prefix')
]
def get_user_id(self, details, response):
return None
Copy link
Owner

Choose a reason for hiding this comment

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

This is required to later identify the account association on login.

def get_user_details(self, response):
return {}

def user_data(self, access_token, *args, **kwargs):

return None


def access_token_url(self):
return self.ACCESS_TOKEN_URL
Copy link
Owner

Choose a reason for hiding this comment

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

This is the default behavior, why override? I see that it requires a domain_prefix a few lines below, if that's the case, then that logic should be put here.





Copy link
Owner

Choose a reason for hiding this comment

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

Too many blank lines, PEP8?

def process_error(self, data):
error = data.get('error')
if error:
if error == 'access_denied':
raise AuthCanceled(self)
else:
raise AuthUnknownError(self, 'Vend error was {0}'.format(
error
))
return super(VendOAuth2, self).process_error(data)

def auth_complete_params(self, state=None):
client_id, client_secret = self.get_key_and_secret()
return {
'code': self.data.get('code', '').encode('ascii', 'ignore'), # server response code
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'authorization_code', # request auth code
'redirect_uri': self.get_redirect_uri(state)
}

def auth_complete(self, *args, **kwargs):
"""Completes loging process, must return user instance"""

#Handle dynamic login access_token_url
self.ACCESS_TOKEN_URL = 'https://{0}.vendhq.com/api/1.0/token'.format(self.data["domain_prefix"])
Copy link
Owner

Choose a reason for hiding this comment

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

If this is moved to the access_token_url then this whole method override can be removed.


self.process_error(self.data)
try:
response = self.request_access_token(
self.ACCESS_TOKEN_URL,
params=self.auth_complete_params(self.validate_state()),
headers=self.auth_headers(),
method='POST',

)
except HTTPError as err:
if err.response.status_code == 400:
raise AuthCanceled(self)
else:

raise
except KeyError:
raise AuthUnknownError(self)
self.process_error(response)

return self.do_auth(response['access_token'], response=response,
*args, **kwargs)