Commit bfe237a2 authored by Sofia Papagiannaki's avatar Sofia Papagiannaki

astakos: Introduce CRUD management commands for oa2 clients

parent 779572c2
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from django.db import transaction
from django.core.management.base import CommandError
from snf_django.management.commands import SynnefoCommand
from astakos.oa2.models import Client, RedirectUrl
class Command(SynnefoCommand):
args = "<identfier>"
help = "Create a oauth2 client"
option_list = SynnefoCommand.option_list + (
make_option('--secret',
dest='secret',
metavar='SECRET',
action='store',
default=None,
help="Set client's secret"),
make_option('--is-trusted',
action='store_true',
dest='is_trusted',
default=False,
help="Whether client is trusted or not"),
make_option('--type',
action='store',
dest='type',
default='confidential',
help="Set client's type"),
make_option('--url',
action='append',
dest='urls',
default=[],
help="Set client's redirect URLs"),
)
@transaction.commit_on_success
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Invalid number of arguments")
identifier = args[0].decode('utf8')
try:
c = Client(identifier=identifier, secret=options['secret'],
type=options['type'], is_trusted=options['is_trusted'])
c.save()
c.redirecturl_set.bulk_create((RedirectUrl(client=c, url=url) for
url in options['urls']))
c.save()
except BaseException, e:
import traceback
traceback.print_exc()
raise CommandError(e)
else:
self.stdout.write('Client created successfully\n')
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from snf_django.management.commands import ListCommand
from astakos.oa2.models import Client
def get_redirect_urls(client):
return ','.join(client.redirecturl_set.values_list('url', flat=True))
class Command(ListCommand):
help = "List oauth2 clients"
object_class = Client
FIELDS = {
'id': ('id', ('The id of the client')),
'name': ('name', 'The name of the client'),
'identifier': ('identifier', 'The unique client identifier'),
'type': ('type', 'The client type'),
'is_trusted': ('is_trusted', 'Whether the client is trusted or not'),
'redirect_urls': (get_redirect_urls, 'The registered redirect URLs')
}
fields = ['id', 'identifier', 'type', 'is_trusted']
option_list = ListCommand.option_list + (
make_option('--confidential',
action='store_true',
dest='confidential',
default=False,
help="Display only confidential clients"),
make_option('--public',
action='store_true',
dest='public',
default=False,
help="Display only public clients"),
make_option('--trusted',
action='store_true',
dest='trusted',
default=False,
help="Display only trusted clients"),
)
def handle_args(self, *args, **options):
if options['confidential']:
self.filters['type'] = 'confidential'
if options['public']:
self.filters['type'] = 'public'
if options['trusted']:
self.filters['is_trusted'] = True
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from astakos.oa2.models import Client
class Command(BaseCommand):
args = "<client ID or identifier>"
help = "Remove an oauth2 client along with its registered redirect urls"
@transaction.commit_on_success
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a client ID or identifier")
ident = args[0]
try:
try:
ident = int(ident)
client = Client.objects.get(id=ident)
except ValueError:
client = Client.objects.get(identifier=ident)
except Client.DoesNotExist:
raise CommandError(
"Client does not exist. You may run snf-manage "
"oa2-client-list for available client IDs.")
client.redirecturl_set.all().delete()
client.delete()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment