def get_my_apps(self): """ Returns the list of registered apps """ user = pylons.request.environ['fts3.User.Credentials'] my_apps = Session.query(OAuth2Application).filter(OAuth2Application.owner == user.user_dn).all() authorized_apps = Session.query( OAuth2Application.client_id, OAuth2Application.name, OAuth2Application.website, OAuth2Application.description, OAuth2Token.refresh_token, OAuth2Token.scope, OAuth2Token.expires, OAuth2Application.scope ).filter((OAuth2Token.dlg_id == user.delegation_id) & (OAuth2Token.client_id == OAuth2Application.client_id)) response = {'apps': my_apps, 'authorized': authorized_apps} if _accept_html(pylons.request.accept): pylons.response.headers['Content-Type'] = 'text/html; charset=UTF-8' response['user'] = user response['site'] = pylons.config['fts3.SiteName'] return render('/apps.html', extra_vars=response) else: pylons.response.headers['Content-Type'] = 'application/json' # Better serialization for authorized apps authorized = list() for auth in authorized_apps: authorized.append({ 'name': auth.name, 'website': auth.website, 'description': auth.description, 'scope': auth.scope, 'expires': auth.expires }) response['authorized'] = authorized return [to_json(response)]
def get_my_apps(self): """ Returns the list of registered apps """ user = pylons.request.environ['fts3.User.Credentials'] my_apps = Session.query(OAuth2Application).filter(OAuth2Application.owner == user.user_dn).all() authorized_apps = Session.query( OAuth2Application.client_id, OAuth2Application.name, OAuth2Application.website, OAuth2Application.description, OAuth2Token.refresh_token, OAuth2Token.scope, OAuth2Token.expires ).filter((OAuth2Token.dlg_id == user.delegation_id) & (OAuth2Token.client_id == OAuth2Application.client_id)) response = {'apps': my_apps, 'authorized': authorized_apps} if _accept_html(pylons.request.accept): pylons.response.headers['Content-Type'] = 'text/html; charset=UTF-8' response['user'] = user response['site'] = pylons.config['fts3.SiteName'] return render('/apps.html', extra_vars=response) else: pylons.response.headers['Content-Type'] = 'application/json' # Better serialization for authorized apps authorized = list() for auth in authorized_apps: authorized.append({ 'name': auth.name, 'website': auth.website, 'description': auth.description, 'scope': auth.scope, 'expires': auth.expires }) response['authorized'] = authorized return to_json(response)
def register(self): """ Register a new third party application """ if pylons.request.content_type.split( ';')[0].strip() == 'application/json': req = json.loads(pylons.request.body) scopes = req.get('scope', list()) else: req = pylons.request.POST scopes = req.getall('scope') if isinstance(scopes, basestring): scopes = scopes.split(',') if not req.get('name', None): raise HTTPBadRequest('Missing application name') if not req.get('website', None): raise HTTPBadRequest('Missing application website') if not req.get('redirect_to', None): raise HTTPBadRequest('Missing redirect urls') for s in scopes: if str(s) not in VALID_OPERATIONS: raise HTTPBadRequest('Invalid scope (%s)' % s) user = pylons.request.environ['fts3.User.Credentials'] app_id = _generate_app_id() app = OAuth2Application(client_id=app_id, client_secret=_generate_app_secret(), name=req['name'], description=req.get('description', ''), website=req['website'], scope=scopes, redirect_to=req['redirect_to'], owner=user.user_dn) try: Session.merge(app) Session.commit() except IntegrityError: Session.rollback() raise HTTPForbidden('The name already exists') except: Session.rollback() raise log.info("New application registered: %s (%s)" % (req['name'], app_id)) if _accept_html(pylons.request.accept): redirect(url_for(controller='oauth2', action='get_my_apps'), code=HTTPSeeOther.code) else: pylons.response.status_int = HTTPCreated.code pylons.response.headers['Content-Type'] = 'application/json' return [to_json(app.client_id)]
def get_app(self, client_id): """ Return information about a given app """ user = pylons.request.environ['fts3.User.Credentials'] app = Session.query(OAuth2Application).get(client_id) if not app: raise HTTPNotFound('Application not found') if app.owner != user.user_dn: raise HTTPForbidden() if _accept_html(pylons.request.accept): pylons.response.headers['Content-Type'] = 'text/html; charset=UTF-8' return render('/app.html', extra_vars={'app': app, 'user': user, 'site': pylons.config['fts3.SiteName']}) else: pylons.response.headers['Content-Type'] = 'application/json' return [to_json(app)]
def get_app(self, client_id): """ Return information about a given app """ user = pylons.request.environ['fts3.User.Credentials'] app = Session.query(OAuth2Application).get(client_id) if not app: raise HTTPNotFound('Application not found') if app.owner != user.user_dn: raise HTTPForbidden() if _accept_html(pylons.request.accept): pylons.response.headers['Content-Type'] = 'text/html; charset=UTF-8' return render('/app.html', extra_vars={'app': app, 'user': user, 'site': pylons.config['fts3.SiteName']}) else: pylons.response.headers['Content-Type'] = 'application/json' return to_json(app)
def register(self): """ Register a new third party application """ if pylons.request.content_type.split(';')[0].strip() == 'application/json': req = json.loads(pylons.request.body) else: req = pylons.request.POST if not req.get('name', None): raise HTTPBadRequest('Missing application name') if not req.get('website', None): raise HTTPBadRequest('Missing application website') if not req.get('redirect_to', None): raise HTTPBadRequest('Missing redirect urls') user = pylons.request.environ['fts3.User.Credentials'] app_id = _generate_app_id() app = OAuth2Application( client_id=app_id, client_secret=_generate_app_secret(), name=req['name'], description=req.get('description', ''), website=req['website'], redirect_to=req['redirect_to'], owner=user.user_dn ) try: Session.merge(app) Session.commit() except IntegrityError: Session.rollback() raise HTTPForbidden('The name already exists') except: Session.rollback() raise log.info("New application registered: %s (%s)" % (req['name'], app_id)) if _accept_html(pylons.request.accept): redirect(url_for(controller='oauth2', action='get_my_apps'), code=HTTPSeeOther.code) else: pylons.response.status_int = HTTPCreated.code pylons.response.headers['Content-Type'] = 'application/json' return to_json(app.client_id)
def set_global_config(self): """ Set the global configuration """ cfg = get_input_as_dict(request) vo_name = cfg.get('vo_name', '*') db_cfg = Session.query(ServerConfig).get(vo_name) if not db_cfg: db_cfg = ServerConfig(vo_name=vo_name) for key, value in cfg.iteritems(): value = validate_type(ServerConfig, key, value) setattr(db_cfg, key, value) Session.merge(db_cfg) audit_configuration('set-globals', to_json(db_cfg, indent=None)) try: Session.commit() except: Session.rollback() raise return self.get_global_config()