def authorize_evernote_done(request): request_token = request.session.pop('evernote_request_token', None) if not request_token: return redirect('evernote_sync:authorize_evernote') error = None access_token = None oauth_token = request.GET.get('oauth_token') oauth_verifier = request.GET.get('oauth_verifier') if not oauth_token: error = u'Invalid response from Evernote' elif oauth_token != request_token.get('oauth_token'): error = u'Unexpected response from Evernote' elif not oauth_verifier: error = u'Access to Evernote rejected' if not error: client = utils.get_unauthorized_evernote_client() oauth_token_secret = request_token['oauth_token_secret'] try: access_token = client.get_access_token(oauth_token, oauth_token_secret, oauth_verifier) except ValueError as e: error = strip_tags(str(e)) if error: return render(request, 'evernote_sync/authorize_evernote_done.html', {'error': error}) OAuthToken.register(request.user, utils.ACCESS_TOKEN_CLIENT, access_token) return redirect('evernote_sync:add_integration')
def authorize_github_done(request): received_state = request.GET['state'] expected_state = request.session.pop(OAUTH_STATE_SESSION_KEY, None) if not received_state == expected_state: raise PowerAppError("Invalid state") authorization_code = request.GET['code'] redirect_uri = build_absolute_uri(reverse('github:authorize_github_done')) resp = requests.post(GITHUB_ACCESS_TOKEN_ENDPOINT, data={ 'client_id': settings.GITHUB_CLIENT_ID, 'client_secret': settings.GITHUB_CLIENT_SECRET, 'code': authorization_code, 'redirect_url': redirect_uri }, headers={'Accept': 'application/json'}) if resp.status_code != 200: error = resp.headers.get('X-Error', 'Unknown Error') return render(request, 'github/authorize_github_done.html', {'error': error}) access_token = resp.json()['access_token'] OAuthToken.register(request.user, ACCESS_TOKEN_CLIENT, access_token) redirect_target = request.session.pop('github_auth_redirect', None) if not redirect_target: redirect_target = 'github:add_integration' return redirect(redirect_target)
def save_token(self, user, token): kwargs = { 'user': user, 'client': self.name, 'access_token': token['access_token'], } for extra_field in ['expires_in', 'refresh_token', 'token_type']: if extra_field in token: kwargs[extra_field] = token[extra_field] return OAuthToken.register(**kwargs)
def authorize_pocket_done(request): request_token = request.session.pop('pocket_request_token', None) if not request_token: return redirect('powerapp_pocket:authorize_pocket') resp = requests.post(POCKET_ACCESS_TOKEN_ENDPOINT, data={ 'consumer_key': settings.POCKET_CONSUMER_KEY, 'code': request_token, }, headers={'X-Accept': 'application/json'}) if resp.status_code != 200: error = resp.headers.get('X-Error', 'Unknown Error') return render(request, 'powerapp_pocket/authorize_pocket_done.html', {'error': error}) access_token = resp.json()['access_token'] OAuthToken.register(request.user, 'pocket', access_token) redirect_target = request.session.pop('pocket_auth_redirect', None) if not redirect_target: redirect_target = 'powerapp_pocket:add_integration' return redirect(redirect_target)