def setUp(self):
        self.log = LogCapture()

        self.test_user = get_user_model().objects.create_user(
            ADMIN_ACCOUNT_NAME, "*****@*****.**", ADMIN_ACCOUNT_PASSWORD)
        self.test_user.save()
        self.test_user.profile.oauth_scope = 'read write'
        self.test_user.profile.accepted_terms = True
        self.test_user.profile.account_activated = True
        self.test_user.profile.save()

        self.application = Application(
            name="django_project",
            redirect_uris=
            "http://localhost http://example.com http://example.it",
            user=self.test_user,
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
        )
        self.application.save()

        mysite = Site.objects.get_current()
        mysite.name = 'Django-Project'
        mysite.save()

        oauth2_settings._SCOPES = ['read', 'write', 'groups']

        # Use the helper to create the access token and refresh token db records.
        self.token = create_access_token(self.test_user,
                                         self.test_user.profile.oauth_scope)
Esempio n. 2
0
def sdLogin(request, *args, **kwargs):

    # TODO: Support the "next" parameter in the template javascript redirect code.

    # This POST method is called by javascript and expects some JSON in return.
    # The goal here is to authenticate the user with oauth and then encrypt the
    # oauth information.  The encrypted information will be stored in a browser cookie,
    # to be later decrypted in the middleware level to set the "Authorization" header.
    if request.method == 'POST':

        if 'username' not in request.POST or 'password' not in request.POST:
            raise ValueError

        # Manually do django authentication.
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)

        if user is not None and user.is_active:

            # Log our user in to django
            login(request, user)

            # Create and encrypt the access token based on this user
            enc = encrypt_access_token(create_access_token(user, user.profile.oauth_scope))

            # Setup login redirect
            if 'next' in request.GET:
                redirect = request.GET['next']
            else:
                redirect = settings.LOGIN_REDIRECT_URL

            # Format our response
            response = JsonResponse(
                dict([('status', 'OK'), ('next', redirect)])
            )

            # Set the encrypted token in the response.
            response.set_cookie('token', enc.decode('UTF-8'))
            response.set_cookie('fade-page-in', 1)

        else:
            # Send our error message
            response = JsonResponse(
                dict([('status', 'ERROR')])
            )

        return response

    else:

        form = accounts.forms.SDAuthenticationForm()
        context = {
            'form': form,
            'next': request.GET['next'] if 'next' in request.GET else None,
        }

    return render(request, 'accounts/login.html', context)
Esempio n. 3
0
def login_view(request, *args, **kwargs):

    # TODO: Support the "next" parameter in the template javascript redirect code.

    # This POST method is called by javascript and expects some JSON in return.
    # The goal here is to authenticate the user with oauth and then encrypt the
    # oauth information.  The encrypted information will be stored in a browser cookie,
    # to be later decrypted in the middleware level to set the "Authorization" header.
    if request.method == 'POST':

        if 'username' not in request.POST or 'password' not in request.POST:
            raise ValueError

        # Manually do django authentication.
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(request=request,
                            username=username,
                            password=password)

        if user is not None and user.is_active:

            # Log our user in to django
            login(request, user)

            # Create and encrypt the access token based on this user
            enc = encrypt_access_token(
                create_access_token(user, user.profile.oauth_scope))

            # Setup login redirect
            if 'next' in request.GET:
                redirect = request.GET['next']
            else:
                redirect = settings.LOGIN_REDIRECT_URL

            # Format our response
            response = JsonResponse(
                dict([('status', 'OK'), ('next', redirect)]))

            # TODO: find out why this delays forever when celery service is not running
            # user_security_event.delay(username, 'login')

            # Set the encrypted token in the response.
            response.set_cookie('token', enc.decode('UTF-8'))
            response.set_cookie('fade-page-in', 1)

        else:
            # Send our error message
            response = JsonResponse(dict([('status', 'ERROR')]))
            # user_security_event.delay(username, 'login', success=False)

        return response

    else:

        form = forms.LoginForm()
        context = {
            'form': form,
            'next': request.GET['next'] if 'next' in request.GET else None,
        }

    return render(request, 'accounts/login.html', context)
Esempio n. 4
0
    def setUp(self):
        self.log = LogCapture()
        self.factory = APIRequestFactory()

        self.test_user = get_user_model().objects.create_user("tester", "*****@*****.**", "12341234")
        self.test_user.save()
        self.test_user.profile.oauth_scope = 'read write'
        self.test_user.profile.accepted_terms = True
        self.test_user.profile.account_activated = True
        self.test_user.profile.save()

        self.application = Application(
            name="silentdune",
            redirect_uris="http://localhost http://example.com http://example.it",
            user=self.test_user,
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
        )
        self.application.save()

        # self.application = ApplicationModel(
        #     name="silentdune",
        #     redirect_uris="http://localhost http://example.com http://example.it",
        #     user=self.test_user,
        #     client_type=ApplicationModel.CLIENT_CONFIDENTIAL,
        #     authorization_grant_type=ApplicationModel.GRANT_PASSWORD,
        # )
        # self.application.save()

        mysite = Site.objects.get_current()
        mysite.name = 'Silent Dune'
        mysite.save()

        oauth2_settings._SCOPES = ['read', 'write', 'groups']

        # Use the helper to create the access token and refresh token db records.
        self.token = create_access_token(self.test_user, self.test_user.profile.oauth_scope)

        # machine id used by tests
        self.machine_id = u'02f1ddb1415c4feba9880b2b8c4c5925'

        node = {
            u'id': 1,
            u'platform': u'iptables',
            u'os': u'linux',
            u'dist': u'redhat',
            u'dist_version': u'7.2',
            u'hostname': u'rhel7-test01.entpack.com',
            u'python_version': u'2.7.5 (default, Oct 11 2015, 17:47:16) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]',
            u'machine_id': self.machine_id,
            u'last_connection': u'2016-05-25T16:09:07Z',
            u'sync': False,
            u'notes': None,
            u'active': False,
            u'locked': False,
            u'polling_interval': 60,
            u'fernet_key': u'yMDxw75rA1aV4a-flT7EdoJTrL8WA5sXlcsAp3Uw0KQ='
        }

        self.node = Node.objects.create(**node)
        self.node.save()