예제 #1
0
def login(request):
    print("Login..")
    try:
	print("Starting login process..")
        username = request.POST['username']
        password = request.POST['password']
        master_server_url = request.POST['url']
	url = master_server_url
    except:
	authresponse = HttpResponse(status=400)
	authresponse.write("Bad request. Make sure username, password, and master server url are in this request as POST")
	return authresponse
	
    print("cechking master server  url..")
    if not master_server_url.endswith("/"):
	master_server_url = master_server_url + "/"
    master_server_url.strip('/') #Get rid of all slashes.. (Makes the above redundnat)
    user_destination = username + "@" + master_server_url

    print("Checking user: "******"Invalid login")
	return authresponse
    else:
	print("checking all cred against : " + user_destination)
        all_credentials = credentials.objects.values_list("user_destination", flat=True)
        if user_destination not in all_credentials:
            print("Creating new credential..")
            new_credential = credentials(username=username, endpoint=master_server_url, user=user, user_destination=user_destination, password=password)
            new_credential.save()
            print("Created new credential.")
	authresponse = HttpResponse(status=200)
	authresponse.write("Login OK")
	return authresponse
예제 #2
0
def auth_backend(username, password, master_server_url):
    try:
        print("Checking your Login..")
	user = User.objects.get(username=username)
    except User.DoesNotExist:
	print("User does not exist..")
	user = None

    #try:
    if True:
	user_destination = username + "@" + master_server_url
        #Check username and password here..
	user = authenticate(username=username, password=password)
	if user is not None:
	    # Yes? return the Django user object
	    print("Local LRS: Username and Password check success")
	    
	    #Check if internet is off / bad connectivity, it should let you through..
	    

	    print("Not so fast, it could be old password. Will check the same against the Master Server..")
	    xapi_status, code = xapi_login(username,password, master_server_url)
	    if xapi_status: 
		#Returns true if user is successfully authenticated, False if not
		print("Master Server: Username and Password check success.")
		print("   Nothing to update. Letting you through..")
	        return user
	    if not xapi_status and code:
		print("Master Server: Unable to check same credentials on Master Server. Either password is wrong on either master/local or no account created.")
		return None
	    if not xapi_status and not code:
		print("No internet. Giving user found locally..")
		return user
	else:
	    print("Local LRS: User auth failed.")
	    print("Local LRS: Checking if user exists at all on local LRS..")
	    user_count = User.objects.filter(username=username).count()
            if user_count > 0:
		print("Local LRS: Okay, so the user exists locally. The password given was locally wrong but may be okay on the Master Server") 
		xapi_status, code = xapi_login(username,password, master_server_url)
		if xapi_status:
		    print("Master Server: The password was correct here")
		    print("   got to update local's password")
		    user = User.objects.get(username=username)
		    user.set_password(password)
		    user.save()
		    print("     updated. Updating credentials")
		    #update credentials as well.
		    try:
		        credential_map = credentials.objects.get(user_destination=user_destination)
		        if credential_map is not None:
			    credential_map.password = password
			    credential_map.save()
			    print("         updated credentials.")
		    except credentials.DoesNotExist:
			print("Cred not existing. Making one..")
			new_credential = credentials(username=username, endpoint=master_server_url, user=user,user_destination=user_destination, password=password)
             		new_credential.save()
		    except Exception, e:
			print("Unable to get credential map")
			print("Reason : " + str(e))
		    
		return user		
	    else:
		print("Local LRS: Nope, no user locally. Checking on Master Server")
		xapi_status, code = xapi_login(username,password, master_server_url)
	        if xapi_status: 
		    #Returns true if user is successfully authenticated, False if not
		    print("Master Server: Username and Password check success for new user.")
		    print("Master Server: Will Create New User")
		    #Create user.
		
		    user_email = username + "@email.com"
		    user = User(username=username, email=user_email)
		    user.set_password(password)
		    user.is_active = True
		    user.save()
		    print("User created!")
		    return user;
	        else:
		    print("Master Server: Credential login failed / No user found")
		    return None