Ejemplo n.º 1
0
    def test_if_user_get_called_with_osm_id(self, mock_user_get):
        # Arrange
        osm_response = get_canned_osm_user_details()

        # Act
        AuthenticationService.login_user(osm_response, '/test/redirect')

        # Assert
        mock_user_get.assert_called_with(7777777)
Ejemplo n.º 2
0
    def test_if_user_create_called_if_user_not_found(self, mock_user_get,
                                                     mock_user_register,
                                                     mock_message):
        # Arrange
        osm_response = get_canned_osm_user_details()
        mock_user_get.side_effect = NotFound()

        # Act
        AuthenticationService.login_user(osm_response, '/test/redirect')

        # Assert
        mock_user_register.assert_called_with(7777777, 'Thinkwhere Test', 16)
Ejemplo n.º 3
0
 def post(self):
     """
     Validates users credentials and returns token and relevant user details
     ---
     tags:
       - authentication
     produces:
       - application/json
     parameters:
         - in: header
           name: Authorization
           description: Base64 encoded user password
           required: true
           type: string
     responses:
       200:
         description: Login Successful
       401:
         description: Unauthorized, credentials are invalid
       500:
         description: Internal Server Error
     """
     try:
         session = AuthenticationService.login_user(
             dmis.authenticated_user_id)
         return session.to_primitive(), 200
     except Exception as e:
         current_app.logger.critical(
             'Unhandled exception when attempting to login, exception: {0}'.
             format(str(e)))
         return {'Error': 'Unhandled'}, 500
Ejemplo n.º 4
0
    def test_valid_auth_request_gets_token(self, mock_user_get):
        # Arrange
        osm_response = get_canned_osm_user_details()

        # Act
        redirect_url = AuthenticationService.login_user(
            osm_response, '/test/redirect')

        # Assert
        parsed_url = urlparse(redirect_url)
        query = parse_qs(parsed_url.query)

        self.assertEqual(query['username'][0], 'Thinkwhere Test')
        self.assertTrue(query['session_token'][0])
        self.assertEqual(query['redirect_to'][0], '/test/redirect')
Ejemplo n.º 5
0
    def get(self):
        """
        Handles the OSM OAuth callback
        ---
        tags:
          - authentication
        produces:
          - application/json
        responses:
          302:
            description: Redirects to login page, or login failed page
          500:
            description: A problem occurred authenticating the user
          502:
            description: A problem occurred negotiating with the OSM API
        """
        osm_resp = osm.authorized_response()
        if osm_resp is None:
            current_app.logger.critical('No response from OSM')
            return redirect(
                AuthenticationService.get_authentication_failed_url())
        else:
            session[
                'osm_oauth'] = osm_resp  # Set OAuth details in the session temporarily

        osm_response = osm.request(
            'user/details')  # Get details for the authenticating user

        if osm_response.status != 200:
            current_app.logger.critical('Error response from OSM')
            return redirect(
                AuthenticationService.get_authentication_failed_url())

        try:
            redirect_to = request.args.get('redirect_to')
            authorized_url = AuthenticationService.login_user(
                osm_response.data, redirect_to)
            return redirect(
                authorized_url
            )  # Redirect to Authentication page on successful authorization :)
        except AuthServiceError as e:
            return {"Error": str(e)}, 500