[QRTR-77] Updated plaid client to properly authenticate with given token.

This commit is contained in:
DJ Gillespie 2020-09-09 18:05:41 -06:00
parent 31cc9078d4
commit 24148bbaf4
3 changed files with 17 additions and 227 deletions

View File

@ -1,79 +0,0 @@
from .abstract import AbstractConnectionClient
import plaid
import os
import datetime
def format_error(e):
return {
'error': {
'display_message': e.display_message,
'error_code': e.code,
'error_type': e.type,
'error_message': e.message}}
class Connection(AbstractConnectionClient):
def __init__(self, credentials):
self.credentials = credentials
# Fill in your Plaid API keys -
# https://dashboard.plaid.com/account/keys
self.PLAID_CLIENT_ID = os.getenv('PLAID_CLIENT_ID')
self.PLAID_SECRET = os.getenv('PLAID_SECRET')
self.PLAID_PUBLIC_KEY = os.getenv('PLAID_PUBLIC_KEY')
# Use 'sandbox' to test with Plaid's Sandbox environment (username: user_good,
# password: pass_good)
# Use `development` to test with live users and credentials and `production`
# to go live
self.PLAID_ENV = os.getenv('PLAID_ENV', 'sandbox')
# PLAID_PRODUCTS is a comma-separated list of products to use when initializing
# Link. Note that this list must contain 'assets' in order for the app to be
# able to create and retrieve asset reports.
self.PLAID_PRODUCTS = os.getenv('PLAID_PRODUCTS', 'transactions')
# PLAID_COUNTRY_CODES is a comma-separated list of countries for which users
# will be able to select institutions from.
self.PLAID_COUNTRY_CODES = os.getenv(
'PLAID_COUNTRY_CODES', 'US,CA,GB,FR,ES')
self.client = plaid.Client(
client_id=self.PLAID_CLIENT_ID,
secret=self.PLAID_SECRET,
public_key=self.PLAID_PUBLIC_KEY,
environment=self.PLAID_ENV,
api_version='2019-05-29')
public_key = self.credentials.get('public_key')
if not self.credentials.get('auth_token') and public_key:
self.credentials['auth_token'] = self.get_auth_token(public_key)
def get_auth_token(self, public_token):
try:
exchange_response = self.client.Item.public_token.exchange(
public_token)
except plaid.errors.PlaidError as e:
return format_error(e)
access_token = exchange_response['access_token']
return access_token
def get_transactions(
self,
start_date=None,
end_date=None,
auth_token=None):
if not auth_token:
auth_token = self.credentials.get('auth_token')
if not auth_token:
raise Exception("Missing Auth Token")
if not start_date:
start_date = '{:%Y-%m-%d}'.format(
datetime.datetime.now() + datetime.timedelta(-30))
if not end_date:
end_date = '{:%Y-%m-%d}'.format(datetime.datetime.now())
try:
transactions_resp = self.client.Transactions.get(
auth_token, start_date, end_date)
except plaid.errors.PlaidError as e:
return format_error(e)
return transactions_resp

View File

@ -1,10 +1,10 @@
from django.shortcuts import render
from rest_framework import status, viewsets
from rest_framework.response import Response
from .models import Connection
from .models import Connection, ConnectionType
from .serializers import ConnectionSerializer
from rest_framework.decorators import action
import plaid
import importlib
# Create your views here.
@ -21,11 +21,22 @@ class ConnectionViewSet(viewsets.ModelViewSet):
'delete',
'options']
@action(detail=False, methods=['post'], url_path='oauth/plaid')
def oauth(self, request, public_token=None):
if public_token is None:
@action(detail=False, methods=['post'], url_path='plaid')
def authenticate(self, request):
print(request.data)
print(request.data.keys())
public_key = request.data.get("public_key")
name = request.data.get("name", "dummyName")
if public_key is None:
return Response(
status=status.HTTP_400_BAD_REQUEST,
data="ERROR: missing public_token")
data="ERROR: missing public_key")
print(request)
plaid = importlib.import_module(f"connection.connections.plaid_client")
conn_type = ConnectionType.objects.get(name="Plaid")
conn = Connection.objects.create(name=name, type=conn_type,
credentials=request.data)
plaid_client = plaid.Connection(request.data)
conn.credentials = plaid_client.credentials
conn.save()
return Response(200)

View File

@ -1,142 +0,0 @@
"""
Django settings for qrtr_services project.
Generated by 'django-admin startproject' using Django 2.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_CONNECTION = dj_database_url.parse(os.environ.get("DATABASE_URL"))
DATABASES = {"default": DEFAULT_CONNECTION,}
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'jc@r$_x4$mp-b84&+m3s@hm7kpl$br-wa&50*&xjx^^fddg6q$'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'dj_rest_auth',
'django.contrib.sites',
'api',
'user',
'connection',
'qrtr_account',
'corsheaders',
]
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SITE_ID = 1
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
CORS_ORIGIN_WHITELIST = [
'http://localhost:3000',
'https://localhost:3000'
]
ROOT_URLCONF = 'core.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'core.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
AUTH_USER_MODEL = 'user.User'
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'