def email_str(request): authenticator = Authenticator() try: authenticator.validate(request) except AuthenticationException: return authenticator.error_response() return HttpResponse(authenticator.user.email)
def setUp(self): self.authenticator = Authenticator(authentication_method='') class Request(object): pass self.request = Request()
def write_key(request): '''write the password used to encrypt funf database files to your PDS''' response = None try: token = request.GET['bearer_token'] scope = "funf_write" scope = AccessRange.objects.get(key="funf_write") authenticator = Authenticator(scope=scope) try: # Validate the request. authenticator.validate(request) except AuthenticationException: # Return an error response. return authenticator.error_response( content="You didn't authenticate.") profile = authenticator.user.get_profile() profile.funf_password = json.loads(request.raw_post_data)['key'] profile.save() response_content = json.dumps({'status': 'success'}) response = HttpResponse(content=response_content) except Exception as ex: print "EXCEPTION:" print ex response = HttpResponseBadRequest('failed to write funf key') return response
def last_name_str(request): scope = AccessRange.objects.get(key="last_name") authenticator = Authenticator(scope=scope) try: authenticator.validate(request) except AuthenticationException: return authenticator.error_response() return HttpResponse(authenticator.user.last_name)
def first_and_last_name_str(request): scope = AccessRange.objects.filter(key__in=["first_name", "last_name"]) authenticator = Authenticator(scope=scope) try: authenticator.validate(request) except AuthenticationException: return authenticator.error_response() return HttpResponse(authenticator.user.first_name + " " + authenticator.user.last_name)
def check(request): """"Page accessed by check script to verify router has token. """ authenticator = Authenticator() try: # Validate the request. authenticator.validate(request) except AuthenticationException: # Return an error response. return authenticator.error_response(content="You didn't authenticate.") username = authenticator.user.email return HttpResponse(content="good token %s" % username)
def authenticate_token(request, scope=None, client_id=None): authenticator = Authenticator() try: authenticator.validate(request) except AuthenticationException: return {'error': authenticator.error.message} auth_client_id = AccessToken.objects.get(token=request.REQUEST.get('bearer_token')).client.key auth_scope = [x.scope for x in authenticator.scope] if type(scope) == str: scope = [scope] if not scope == None: if not set(scope).issubset(set(auth_scope)): return {'error':'token not authorized for this scope'} if not client_id == None: if not client_id == auth_client_id: return {'error':'token not authorized for this client_id'} return {'ok': 'success', 'user': authenticator.user, 'scope': auth_scope, 'client_id': auth_client_id}
def get_system_entity_connection(request): response_content = {} try: scope = AccessRange.objects.get(key="system_entity") authenticator = Authenticator(scope=scope) authenticator.validate(request) if scope not in authenticator.scope: raise Exception("Access token is insufficient to get a system entity connection") pdslocationlist = list() for user in User.objects.all(): pdslocationlist.append(user.get_profile().pds_location) response_content['pds_locations']=pdslocationlist response_content['status']="success" except Exception as e: response_content['status']="error" response_content['message']="failed to connect as system entity" logging.debug(e) return HttpResponse(json.dumps(response_content), mimetype="application/json")
def attributes(request): authenticator = Authenticator() requested_attributes = request.REQUEST.get('attributes', '').split(',') try: authenticator.validate(request) except AuthenticationException: return authenticator.error_response(content="You didn't authenticate.") scope = authenticator.scope user = authenticator.user response = {} for attribute in requested_attributes: try: a = Attribute.objects.get(attribute=attribute) except Attribute.DoesNotExist: continue if a.scope in scope: try: response[attribute] = eval('user.' + attribute) except: pass return HttpResponse(json.dumps(response))
def automatic_error_str(request): authenticator = Authenticator() return authenticator.error_response()