rojabo token exchange works, refresh not
This commit is contained in:
@@ -49,7 +49,9 @@ class RowerInline(admin.StackedInline):
|
|||||||
'stravatoken', 'stravatokenexpirydate', 'stravarefreshtoken',
|
'stravatoken', 'stravatokenexpirydate', 'stravarefreshtoken',
|
||||||
'stravaexportas', 'strava_auto_export',
|
'stravaexportas', 'strava_auto_export',
|
||||||
'strava_auto_import',
|
'strava_auto_import',
|
||||||
'garmintoken', 'garminrefreshtoken')}),
|
'garmintoken', 'garminrefreshtoken',
|
||||||
|
'nktoken','nkrefreshtoken','nktokenexpirydate',
|
||||||
|
'rojabo_token','rojabo_refreshtoken','rojabo_tokenexpirydate')}),
|
||||||
('Team',
|
('Team',
|
||||||
{'fields': ('friends', 'privacy', 'team')}),
|
{'fields': ('friends', 'privacy', 'team')}),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1048,6 +1048,12 @@ class Rower(models.Model):
|
|||||||
|
|
||||||
rp3_auto_import = models.BooleanField(default=False)
|
rp3_auto_import = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
rojabo_token = models.CharField(
|
||||||
|
default='', max_length=200, blank=True, null=True)
|
||||||
|
rojabo_refreshtoken = models.CharField(
|
||||||
|
default='', max_length=200, blank=True, null=True)
|
||||||
|
rojabo_tokenexpirydate = models.DateTimeField(blank=True, null=True)
|
||||||
|
|
||||||
nktoken = models.TextField(
|
nktoken = models.TextField(
|
||||||
default='', max_length=1000, blank=True, null=True)
|
default='', max_length=1000, blank=True, null=True)
|
||||||
nktokenexpirydate = models.DateTimeField(blank=True, null=True)
|
nktokenexpirydate = models.DateTimeField(blank=True, null=True)
|
||||||
|
|||||||
@@ -1,2 +1,169 @@
|
|||||||
from rowers.models import Rower, Workout, TombStone
|
from rowers.models import Rower, Workout, TombStone
|
||||||
from rowers import utils
|
from rowers import utils
|
||||||
|
|
||||||
|
from rowers.imports import *
|
||||||
|
from rowsandall_app.settings import (
|
||||||
|
ROJABO_CLIENT_ID, ROJABO_REDIRECT_URI, ROJABO_CLIENT_SECRET,
|
||||||
|
SITE_URL, ROJABO_OAUTH_LOCATION,
|
||||||
|
UPLOAD_SERVICE_URL, UPLOAD_SERVICE_SECRET,
|
||||||
|
)
|
||||||
|
import gzip
|
||||||
|
import rowers.mytypes as mytypes
|
||||||
|
from rowers.utils import myqueue
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import base64
|
||||||
|
|
||||||
|
from rowers.utils import dologging
|
||||||
|
from json.decoder import JSONDecodeError
|
||||||
|
|
||||||
|
import django_rq
|
||||||
|
queue = django_rq.get_queue('default')
|
||||||
|
queuelow = django_rq.get_queue('low')
|
||||||
|
queuehigh = django_rq.get_queue('low')
|
||||||
|
|
||||||
|
oauth_data = {
|
||||||
|
'client_id': ROJABO_CLIENT_ID,
|
||||||
|
'client_secret': ROJABO_CLIENT_SECRET,
|
||||||
|
'redirect_uri': ROJABO_REDIRECT_URI,
|
||||||
|
'autorization_uri': ROJABO_OAUTH_LOCATION+"oauth/authorize",
|
||||||
|
'content_type': 'application/json',
|
||||||
|
'tokenname': 'nktoken',
|
||||||
|
'refreshtokenname': 'nkrefreshtoken',
|
||||||
|
'expirydatename': 'nktokenexpirydate',
|
||||||
|
'bearer_auth': True,
|
||||||
|
'base_url': ROJABO_OAUTH_LOCATION+"oauth/token",
|
||||||
|
'scope': 'read',
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_token(code): # pragma: no cover
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
"grant_type": "authorization_code",
|
||||||
|
"code": code,
|
||||||
|
"redirect_uri": oauth_data['redirect_uri'],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auth_string = '{id}:{secret}'.format(
|
||||||
|
id=ROJABO_CLIENT_ID,
|
||||||
|
secret=ROJABO_CLIENT_SECRET
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
headers = {'Authorization': 'Basic %s' % base64.b64encode(auth_string),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
except TypeError:
|
||||||
|
headers = {'Authorization': 'Basic %s' % base64.b64encode(
|
||||||
|
bytes(auth_string, 'utf-8')).decode('utf-8'),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
response = requests.post(oauth_data['base_url'],
|
||||||
|
data=post_data,
|
||||||
|
headers=headers)
|
||||||
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
return (0,response.reason)
|
||||||
|
|
||||||
|
try:
|
||||||
|
token_json = response.json()
|
||||||
|
thetoken = token_json['access_token']
|
||||||
|
expires_in = token_json['expires_in']
|
||||||
|
refresh_token = token_json['refresh_token']
|
||||||
|
except (KeyError, JSONDecodeError) as e: # pragma: no cover
|
||||||
|
thetoken = 0
|
||||||
|
expires_in = 0
|
||||||
|
refresh_token = 0
|
||||||
|
|
||||||
|
return [thetoken, expires_in, refresh_token]
|
||||||
|
|
||||||
|
def rojabo_open(user):
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
if (r.rojabo_token == '') or (r.rojabo_token is None):
|
||||||
|
raise NoTokenError("User has no token")
|
||||||
|
else:
|
||||||
|
if (timezone.now() > r.tokenexpirydate):
|
||||||
|
res = rower_rojabo_token_refresh(user)
|
||||||
|
if res is None: # pragma: no cover
|
||||||
|
raise NoTokenError("User has no token")
|
||||||
|
if res[0] is not None:
|
||||||
|
thetoken = res[0]
|
||||||
|
else: # pragma: no cover
|
||||||
|
raise NoTokenError("User has no token")
|
||||||
|
else:
|
||||||
|
thetoken = r.rojabo_token
|
||||||
|
|
||||||
|
return thetoken
|
||||||
|
|
||||||
|
def rower_rojabo_token_refresh(user):
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
res = do_refresh_token(r.rojabo_refreshtoken)
|
||||||
|
if res[0]:
|
||||||
|
access_token = res[0]
|
||||||
|
expires_in = res[1]
|
||||||
|
refresh_token = res[2]
|
||||||
|
expirydatetime = timezone.now()+datetime.timedelta(seconds=expires_in)
|
||||||
|
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
r.rojabo_token = access_token
|
||||||
|
r.rojabo_tokenexpirydate = expirydatetime
|
||||||
|
r.rojabo_refreshtoken = refresh_token
|
||||||
|
|
||||||
|
r.save()
|
||||||
|
return r.rojabo_token
|
||||||
|
else: # pragma: no cover
|
||||||
|
return None
|
||||||
|
|
||||||
|
def do_refresh_token(refreshtoken):
|
||||||
|
post_data = {
|
||||||
|
"grant_type": "refresh_token",
|
||||||
|
"refresh_token": refreshtoken,
|
||||||
|
#"redirect_uri": oauth_data['redirect_uri'],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auth_string = '{id}:{secret}'.format(
|
||||||
|
id=ROJABO_CLIENT_ID,
|
||||||
|
secret=ROJABO_CLIENT_SECRET
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
headers = {'Authorization': 'Basic %s' % base64.b64encode(auth_string),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
except TypeError:
|
||||||
|
headers = {'Authorization': 'Basic %s' % base64.b64encode(
|
||||||
|
bytes(auth_string, 'utf-8')).decode('utf-8'),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
response = requests.post(oauth_data['base_url'],
|
||||||
|
data=post_data,
|
||||||
|
headers=headers)
|
||||||
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
print(oauth_data['base_url'])
|
||||||
|
print(post_data)
|
||||||
|
print(auth_string)
|
||||||
|
print(headers)
|
||||||
|
print(response.status_code)
|
||||||
|
print(response.reason)
|
||||||
|
return (0,response.reason)
|
||||||
|
|
||||||
|
try:
|
||||||
|
token_json = response.json()
|
||||||
|
thetoken = token_json['access_token']
|
||||||
|
expires_in = token_json['expires_in']
|
||||||
|
refresh_token = token_json['refresh_token']
|
||||||
|
except (KeyError, JSONDecodeError) as e: # pragma: no cover
|
||||||
|
thetoken = 0
|
||||||
|
expires_in = 0
|
||||||
|
refresh_token = 0
|
||||||
|
|
||||||
|
return [thetoken, expires_in, refresh_token]
|
||||||
|
|||||||
@@ -32,6 +32,10 @@
|
|||||||
{% if rower.rp3token is not None and rower.rp3token != '' %}
|
{% if rower.rp3token is not None and rower.rp3token != '' %}
|
||||||
RP3
|
RP3
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if rower.rojabo_token is not None and rower.rojabo_token != '' %}
|
||||||
|
Rojabo
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{% if form.errors %}
|
{% if form.errors %}
|
||||||
@@ -80,8 +84,10 @@
|
|||||||
|
|
||||||
<p><a href="/rowers/me/garminauthorize"><img src="/static/img/garmin_badge_130.png"
|
<p><a href="/rowers/me/garminauthorize"><img src="/static/img/garmin_badge_130.png"
|
||||||
alt="connect with Garmin" width="130"></a></p>
|
alt="connect with Garmin" width="130"></a></p>
|
||||||
<p><a href="/rowers/me/rp3authorize"><img src="/static/img/logo-rp3-full-black.png"
|
<p><a href="/rowers/me/rp3authorize"><img src="/static/img/logo-rp3-full-black.png"
|
||||||
alt="connect with RP3" width="130"></a></p>
|
alt="connect with RP3" width="130"></a></p>
|
||||||
|
<p><a href="/rowers/me/rojaboauthorize"><img src="/static/img/rojabo.png"
|
||||||
|
alt="connect with Rojabo" width="130"></a></p>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -757,6 +757,8 @@ urlpatterns = [
|
|||||||
name='rower_c2_authorize'),
|
name='rower_c2_authorize'),
|
||||||
re_path(r'^me/nkauthorize/$', views.rower_nk_authorize,
|
re_path(r'^me/nkauthorize/$', views.rower_nk_authorize,
|
||||||
name='rower_nk_authorize'),
|
name='rower_nk_authorize'),
|
||||||
|
re_path(r'^me/rojaboauthorize/$', views.rower_rojabo_authorize,
|
||||||
|
name='rower_rojabo_authorize'),
|
||||||
re_path(r'^me/polarauthorize/$', views.rower_polar_authorize,
|
re_path(r'^me/polarauthorize/$', views.rower_polar_authorize,
|
||||||
name='rower_polar_authorize'),
|
name='rower_polar_authorize'),
|
||||||
re_path(r'^me/revokeapp/(?P<id>\d+)/$',
|
re_path(r'^me/revokeapp/(?P<id>\d+)/$',
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from rowsandall_app.settings import NK_OAUTH_LOCATION
|
from rowsandall_app.settings import NK_OAUTH_LOCATION, ROJABO_OAUTH_LOCATION
|
||||||
|
|
||||||
from rowers.views.statements import *
|
from rowers.views.statements import *
|
||||||
from rowers.plannedsessions import get_dates_timeperiod
|
from rowers.plannedsessions import get_dates_timeperiod
|
||||||
@@ -139,6 +139,23 @@ def workout_sporttracks_upload_view(request, id=0):
|
|||||||
|
|
||||||
return HttpResponseRedirect(url) # pragma: no cover
|
return HttpResponseRedirect(url) # pragma: no cover
|
||||||
|
|
||||||
|
# ROJABO authorization
|
||||||
|
def rower_rojabo_authorize(request): # pragma: no cover
|
||||||
|
state = str(uuid4())
|
||||||
|
scope = "read"
|
||||||
|
params = {
|
||||||
|
# "grant_type": "authorization_code",
|
||||||
|
# "response_type": "code",
|
||||||
|
"client_id": ROJABO_CLIENT_ID,
|
||||||
|
#"client_secret": ROJABO_CLIENT_SECRET,
|
||||||
|
# "scope": scope,
|
||||||
|
#"state": state,
|
||||||
|
"redirect_uri": ROJABO_REDIRECT_URI,
|
||||||
|
}
|
||||||
|
|
||||||
|
url = ROJABO_OAUTH_LOCATION+'oauth/authorize?'+urllib.parse.urlencode(params)
|
||||||
|
|
||||||
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
# NK Logbook authorization
|
# NK Logbook authorization
|
||||||
@login_required()
|
@login_required()
|
||||||
@@ -496,9 +513,47 @@ def rower_process_garmincallback(request): # pragma: no cover
|
|||||||
url = reverse('rower_exportsettings_view')
|
url = reverse('rower_exportsettings_view')
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
|
# Process Rojabo callback
|
||||||
|
@login_required()
|
||||||
|
def rower_process_rojabocallback(request): # prgrma: no cover
|
||||||
|
# do stuff
|
||||||
|
try:
|
||||||
|
code = request.GET.get('code', None)
|
||||||
|
res = rojabo_stuff.get_token(code)
|
||||||
|
except MultiValueDictKeyError:
|
||||||
|
message = "The resource owner or authorization server denied the request"
|
||||||
|
messages.error(request, message)
|
||||||
|
|
||||||
|
url = reverse('rower_exportsettings_view')
|
||||||
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
|
access_token = res[0]
|
||||||
|
if access_token == 0:
|
||||||
|
message = res[1]
|
||||||
|
message += ' Contact support@rowsandall.com if this behavior persists'
|
||||||
|
messages.error(request, message)
|
||||||
|
|
||||||
|
url = reverse('rower_exportsettings_view')
|
||||||
|
|
||||||
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
|
expires_in = res[1]
|
||||||
|
refresh_token = res[2]
|
||||||
|
expirydatetime = timezone.now()+datetime.timedelta(seconds=expires_in)
|
||||||
|
|
||||||
|
r = getrower(request.user)
|
||||||
|
r.rojabo_token = access_token
|
||||||
|
r.rojabo_tokenexpirydate = expirydatetime
|
||||||
|
r.rojabo_refreshtoken = refresh_token
|
||||||
|
|
||||||
|
r.save()
|
||||||
|
|
||||||
|
successmessage = "Tokens stored. Good to go. Please check your import/export settings"
|
||||||
|
messages.info(request, successmessage)
|
||||||
|
url = reverse('rower_exportsettings_view')
|
||||||
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
# Process NK Callback
|
# Process NK Callback
|
||||||
|
|
||||||
|
|
||||||
@login_required()
|
@login_required()
|
||||||
def rower_process_nkcallback(request): # pragma: no cover
|
def rower_process_nkcallback(request): # pragma: no cover
|
||||||
# do stuff
|
# do stuff
|
||||||
|
|||||||
@@ -190,6 +190,7 @@ import datetime
|
|||||||
import iso8601
|
import iso8601
|
||||||
import rowers.c2stuff as c2stuff
|
import rowers.c2stuff as c2stuff
|
||||||
import rowers.nkstuff as nkstuff
|
import rowers.nkstuff as nkstuff
|
||||||
|
import rowers.rojabo_stuff as rojabo_stuff
|
||||||
from rowers.c2stuff import c2_open
|
from rowers.c2stuff import c2_open
|
||||||
from rowers.nkstuff import nk_open
|
from rowers.nkstuff import nk_open
|
||||||
from rowers.rp3stuff import rp3_open
|
from rowers.rp3stuff import rp3_open
|
||||||
@@ -218,7 +219,8 @@ from rowsandall_app.settings import (
|
|||||||
BRAINTREE_MERCHANT_ID, BRAINTREE_PUBLIC_KEY, BRAINTREE_PRIVATE_KEY,
|
BRAINTREE_MERCHANT_ID, BRAINTREE_PUBLIC_KEY, BRAINTREE_PRIVATE_KEY,
|
||||||
PAYMENT_PROCESSING_ON,
|
PAYMENT_PROCESSING_ON,
|
||||||
RECAPTCHA_SITE_KEY, RECAPTCHA_SITE_SECRET,
|
RECAPTCHA_SITE_KEY, RECAPTCHA_SITE_SECRET,
|
||||||
NK_REDIRECT_URI, NK_CLIENT_ID, NK_CLIENT_SECRET
|
NK_REDIRECT_URI, NK_CLIENT_ID, NK_CLIENT_SECRET,
|
||||||
|
ROJABO_REDIRECT_URI, ROJABO_CLIENT_ID, ROJABO_CLIENT_SECRET,
|
||||||
)
|
)
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
|||||||
@@ -285,7 +285,8 @@ STRAVA_REDIRECT_URI = CFG['strava_callback']
|
|||||||
|
|
||||||
ROJABO_CLIENT_ID = CFG['rojabo_client_id']
|
ROJABO_CLIENT_ID = CFG['rojabo_client_id']
|
||||||
ROJABO_CLIENT_SECRET = CFG['rojabo_client_secret']
|
ROJABO_CLIENT_SECRET = CFG['rojabo_client_secret']
|
||||||
ROJABO_CALLBACK = CFG['rojabo_callback']
|
ROJABO_REDIRECT_URI = CFG['rojabo_callback']
|
||||||
|
ROJABO_OAUTH_LOCATION = CFG['rojabo_oauth_location']
|
||||||
|
|
||||||
# Garmin
|
# Garmin
|
||||||
GARMIN_CLIENT_KEY = CFG["garmin_client_key"]
|
GARMIN_CLIENT_KEY = CFG["garmin_client_key"]
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ urlpatterns += [
|
|||||||
# re_path(r'^admin/rq/',include('django_rq_dashboard.urls')),
|
# re_path(r'^admin/rq/',include('django_rq_dashboard.urls')),
|
||||||
re_path(r'^call\_back', rowersviews.rower_process_callback),
|
re_path(r'^call\_back', rowersviews.rower_process_callback),
|
||||||
re_path(r'^nk\_callback', rowersviews.rower_process_nkcallback),
|
re_path(r'^nk\_callback', rowersviews.rower_process_nkcallback),
|
||||||
|
re_path(r'^rojabo\_callback', rowersviews.rower_process_rojabocallback),
|
||||||
re_path(r'^stravacall\_back', rowersviews.rower_process_stravacallback),
|
re_path(r'^stravacall\_back', rowersviews.rower_process_stravacallback),
|
||||||
re_path(r'^garmin\_callback', rowersviews.rower_process_garmincallback),
|
re_path(r'^garmin\_callback', rowersviews.rower_process_garmincallback),
|
||||||
re_path(r'^sporttracks\_callback', rowersviews.rower_process_sporttrackscallback),
|
re_path(r'^sporttracks\_callback', rowersviews.rower_process_sporttrackscallback),
|
||||||
|
|||||||
BIN
static/img/rojabo.png
Normal file
BIN
static/img/rojabo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 37 KiB |
Reference in New Issue
Block a user