Skip to content

Commit

Permalink
make boa configurable via api v2
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Nov 21, 2023
1 parent 3141057 commit e969c3d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/base/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@
VARNISH_SERVERS = osf_settings.VARNISH_SERVERS
ESI_MEDIA_TYPES = osf_settings.ESI_MEDIA_TYPES

ADDONS_FOLDER_CONFIGURABLE = ['box', 'dropbox', 's3', 'googledrive', 'figshare', 'owncloud', 'onedrive']
ADDONS_OAUTH = ADDONS_FOLDER_CONFIGURABLE + ['dataverse', 'github', 'bitbucket', 'gitlab', 'mendeley', 'zotero', 'forward', 'boa']
ADDONS_FOLDER_CONFIGURABLE = ['box', 'dropbox', 's3', 'googledrive', 'figshare', 'owncloud', 'onedrive', 'boa']
ADDONS_OAUTH = ADDONS_FOLDER_CONFIGURABLE + ['dataverse', 'github', 'bitbucket', 'gitlab', 'mendeley', 'zotero', 'forward']

BYPASS_THROTTLE_TOKEN = 'test-token'

Expand Down
44 changes: 44 additions & 0 deletions api/nodes/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import connection
from distutils.version import StrictVersion
from addons.boa.models import BoaProvider

from boaapi.boa_client import BoaClient, BoaException, BOA_API_ENDPOINT
from api.base.exceptions import (
Conflict, EndpointNotImplementedError,
InvalidModelValueError,
Expand Down Expand Up @@ -1093,6 +1095,48 @@ def update(self, instance, validated_data):
return instance


class BoaNodeAddonSettingsSerializer(NodeAddonSettingsSerializer):
SHORT_NAME = 'boa'
FULL_NAME = 'Boa'

oauth_secret = ser.CharField(required=False, allow_null=True, write_only=True)
oauth_key = ser.CharField(required=False, allow_null=True, write_only=True)

def update(self, instance, validated_data):
instance = super().update(instance, validated_data)
username = validated_data.get('username')
password = validated_data.get('password')
user = self.context['request'].user

if username and password:
try:
boa_client = BoaClient(endpoint=BOA_API_ENDPOINT)
boa_client.login(username, password)
boa_client.close()
except BoaException:
raise exceptions.PermissionDenied('You do not have access to this addon')

provider = BoaProvider(
account=None,
host=BOA_API_ENDPOINT,
username=username,
password=password,
)
provider.account.save()
provider.account = ExternalAccount.objects.get(
provider=provider.short_name,
provider_id=f'{BOA_API_ENDPOINT}:{username}'.lower(),
)
if provider.account.oauth_key != password:
provider.account.oauth_key = password
provider.account.save()

if not user.external_accounts.filter(id=provider.account.id).exists():
user.external_accounts.add(provider.account)

return instance


class NodeDetailSerializer(NodeSerializer):
"""
Overrides NodeSerializer to make id required.
Expand Down

0 comments on commit e969c3d

Please sign in to comment.