def invalidatetoken(client=None, user=None, **kwargs): """Invalidate a token. Provide at least a client or a user client client_id or client_secret of an existing client user username or email of an existing user """ if not client and not user: helpers.abort("""Provide at least a client (client_id or client_secret) or a user (username or email)""") if user: user_inst = User.first((User.username == user) | (User.email == user)) if not user_inst: return reporter.error('User not found', user) where_clause = (Session.user_id == user_inst.pk) else: client_inst = Client.first((Client.client_id == client) | (Client.client_secret == client)) if not client_inst: return reporter.error('Client not found', user) where_clause = (Session.client_id == client_inst.pk) tokens = Token.select().join(Session).where(where_clause).where( Token.expires > utcnow()) for token in tokens: token.expires = utcnow() token.save() reporter.notice('Invalidate {} tokens'.format(len(tokens)), tokens)
def listclients(**kwargs): """List existing clients with details.""" tpl = '{:<40} {:<40} {}' print(tpl.format('name', 'client_id', 'client_secret')) for client in Client.select(): print(tpl.format(client.name, str(client.client_id), client.client_secret))
def listclients(**kwargs): """List existing clients with details.""" tpl = '{:<40} {:<40} {}' print(tpl.format('name', 'client_id', 'client_secret')) for client in Client.select(): print( tpl.format(client.name, str(client.client_id), client.client_secret))
def session_client(func, *args, **kwargs): clientname = context.get('clientname') contributor_type = context.get('contributor_type') try: client = Client.select().where(Client.name == clientname).get() except Client.DoesNotExist: raise Exception('Client not found {}'.format(clientname or '')) session = Session.create(client=client, contributor_type=contributor_type) context.set('session', session) return func(*args, **kwargs)
def listclients(**kwargs): """List existing clients with details.""" tpl = '{:<50} {:<40} {:<40} {:<60} {:<200} {}' print( tpl.format('id', 'name', 'client_id', 'client_secret', 'scopes', 'contributor_types')) for client in Client.select(): print( tpl.format(client.id, client.name, str(client.client_id), client.client_secret, ' '.join(client.scopes or []), ' '.join(client.contributor_types or [])))
def createclient(name=None, user=None, **kwargs): """Create a client. name name of the client to create user username or email of an existing user """ if not name: name = helpers.prompt('Client name') if not user: user = helpers.prompt('User username or email') user_inst = User.first((User.username == user) | (User.email == user)) if not user_inst: return reporter.error('User not found', user) validator = Client.validator(name=name, user=user_inst) if validator.errors: return reporter.error('Errored', validator.errors) client = validator.save() reporter.notice('Created', client) listclients()
def createclient(name=None, user=None, scopes=[], contributor_types=[], **kwargs): """Create a client. name name of the client to create user username or email of an existing user """ if not name: name = helpers.prompt('Client name') if not user: user = helpers.prompt('User username or email') user_inst = User.first((User.username == user) | (User.email == user)) if not user_inst: return reporter.error('User not found', user) if not scopes: scopes = helpers.prompt('Scopes (separated by spaces)', default='').split() if not contributor_types: contributor_types = helpers.prompt( 'Contributor types (separated by spaces)', default='viewer').split() for ct in contributor_types: if ct not in Client.CONTRIBUTOR_TYPE: return reporter.error( '{} not in {}'.format(ct, str(Client.CONTRIBUTOR_TYPE)), contributor_types) if contributor_types != ['viewer']: contributor_types.append('viewer') validator = Client.validator(name=name, user=user_inst, scopes=scopes, contributor_types=contributor_types) if validator.errors: return reporter.error('Errored', validator.errors) client = validator.save() reporter.notice('Created', client) listclients()