Skip to content

Prompt user for authentication when HTTP request needs it #27439

Closed

Description

@jdm

if !http_request.use_url_credentials || authentication_fetch_flag {
// FIXME: Prompt the user for username and password from the window
// Wrong, but will have to do until we are able to prompt the user
// otherwise this creates an infinite loop
// We basically pretend that the user declined to enter credentials
return response;
}
and
// Step 3
// FIXME: Prompt the user for proxy authentication credentials
// Wrong, but will have to do until we are able to prompt the user
// otherwise this creates an infinite loop
// We basically pretend that the user declined to enter credentials
return response;
require integration with the embedding layer in order to request user/password information from the browser UI.

We will need to store an EmbedderProxy in HttpState. This will allow us to send messages to the embedder from within the HTTP fetching code linked earlier.

We can then send two EmbedderMsg::Prompt messages and wait on the responses from the channel that is sent as part of that message, which will contain the username and password values chosen by the user.

To test this, run the following code under python3:

Details
import http.server
import cgi
import base64
import json
from urllib.parse import urlparse, parse_qs


class CustomServerHandler(http.server.BaseHTTPRequestHandler):

    def do_HEAD(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()

    def do_AUTHHEAD(self):
        self.send_response(401)
        self.send_header(
            'WWW-Authenticate', 'Basic realm="Demo Realm"')
        self.send_header('Content-type', 'application/json')
        self.end_headers()

    def do_GET(self):
        key = self.server.get_auth_key()

        ''' Present frontpage with user authentication. '''
        if self.headers.get('Authorization') == None:
            self.do_AUTHHEAD()

            response = {
                'success': False,
                'error': 'No auth header received'
            }

            self.wfile.write(bytes(json.dumps(response), 'utf-8'))

        elif self.headers.get('Authorization') == 'Basic ' + str(key):
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()

            getvars = self._parse_GET()

            response = {
                'path': self.path,
                'get_vars': str(getvars)
            }

            base_path = urlparse(self.path).path
            if base_path == '/path1':
                # Do some work
                pass
            elif base_path == '/path2':
                # Do some work
                pass

            self.wfile.write(bytes(json.dumps(response), 'utf-8'))
        else:
            self.do_AUTHHEAD()

            response = {
                'success': False,
                'error': 'Invalid credentials'
            }

            self.wfile.write(bytes(json.dumps(response), 'utf-8'))

    def do_POST(self):
        key = self.server.get_auth_key()

        ''' Present frontpage with user authentication. '''
        if self.headers.get('Authorization') == None:
            self.do_AUTHHEAD()

            response = {
                'success': False,
                'error': 'No auth header received'
            }

            self.wfile.write(bytes(json.dumps(response), 'utf-8'))

        elif self.headers.get('Authorization') == 'Basic ' + str(key):
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()

            postvars = self._parse_POST()
            getvars = self._parse_GET()

            response = {
                'path': self.path,
                'get_vars': str(getvars),
                'get_vars': str(postvars)
            }

            base_path = urlparse(self.path).path
            if base_path == '/path1':
                # Do some work
                pass
            elif base_path == '/path2':
                # Do some work
                pass

            self.wfile.write(bytes(json.dumps(response), 'utf-8'))
        else:
            self.do_AUTHHEAD()

            response = {
                'success': False,
                'error': 'Invalid credentials'
            }

            self.wfile.write(bytes(json.dumps(response), 'utf-8'))

        response = {
            'path': self.path,
            'get_vars': str(getvars),
            'get_vars': str(postvars)
        }

        self.wfile.write(bytes(json.dumps(response), 'utf-8'))

    def _parse_POST(self):
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        if ctype == 'multipart/form-data':
            postvars = cgi.parse_multipart(self.rfile, pdict)
        elif ctype == 'application/x-www-form-urlencoded':
            length = int(self.headers.getheader('content-length'))
            postvars = cgi.parse_qs(
                self.rfile.read(length), keep_blank_values=1)
        else:
            postvars = {}

        return postvars

    def _parse_GET(self):
        getvars = parse_qs(urlparse(self.path).query)

        return getvars


class CustomHTTPServer(http.server.HTTPServer):
    key = ''

    def __init__(self, address, handlerClass=CustomServerHandler):
        super().__init__(address, handlerClass)

    def set_auth(self, username, password):
        self.key = base64.b64encode(
            bytes('%s:%s' % (username, password), 'utf-8')).decode('ascii')

    def get_auth_key(self):
        return self.key


if __name__ == '__main__':
    server = CustomHTTPServer(('', 8888))
    server.set_auth('demo', 'demo')
    server.serve_forever()
Then load `http://localhost:8888` in Servo (and any other browser to verify). The username `demo` with password `demo` should successfully pass the authentication check; everything else should continue to show the authentication prompt.

Activity

  1. added
    E-more-complex
    on Jul 29, 2020
  2. mustafapc19 commented on Jul 30, 2020

    @mustafapc19
    Contributor

    Can I try this? I might be able to do this. With your help ofcourse 🙂

  3. jdm commented on Jul 30, 2020

    @jdm
    MemberAuthor

    Go for it! Please ask questions if anything is unclear!

  4. mustafapc19 commented on Jul 31, 2020

    @mustafapc19
    Contributor

    Yeah. Assign me 👍

  5. added
    C-assigned
    on Jul 31, 2020
  6. paulrouget commented on Jul 31, 2020

    @paulrouget
    Contributor

    We can then send two EmbedderMsg::Prompt messages and wait on the responses from the channel that is sent as part of that message, which will contain the username and password values chosen by the user.

    I think it wouldn't be too hard to also introduce a Prompt::Credential here:

    #[derive(Deserialize, Serialize)]

  7. jdm commented on Jul 31, 2020

    @jdm
    MemberAuthor

    Agreed, but it will be easiest for a new contributor to use the existing messages rather than also introducing new ones.

  8. mustafapc19 commented on Jul 31, 2020

    @mustafapc19
    Contributor

    I'll try to implement prompt:: Credentials and if I fail to. I'll do the sending twice thingy

  9. ritwickdsouza commented on Sep 4, 2020

    @ritwickdsouza

    Hey @mustafapc19, are you still working on this task? If not can I work on it?

  10. mustafapc19 commented on Sep 4, 2020

    @mustafapc19
    Contributor

    I kinda have the pr almost ready but then Mozilla defunded the project. Should send the pr @jdm ?

  11. ritwickdsouza commented on Sep 4, 2020

    @ritwickdsouza

    Ah, i see. thanks

  12. jdm commented on Sep 4, 2020

    @jdm
    MemberAuthor

    I would review it :)

  13. mustafapc19 commented on Sep 7, 2020

    @mustafapc19
    Contributor

    Yeah. The issue I was facing then was that I was inserting the (http:: Authentication and the required string) in the response object's header map. But in server the Authentication header for some reason is None. Sadly I still can't figure. If u feel nostalgic about servo, u can help ? 🥺

  14. jdm commented on Sep 7, 2020

    @jdm
    MemberAuthor

    The header needs to be added to the request, rather than the response.

  15. Wowo10 commented on Apr 1, 2021

    @Wowo10

    Since this looks to me like abandoned since half a year. Mind if I take over?

  16. mustafapc19 commented on Apr 1, 2021

    @mustafapc19
    Contributor

    Since this looks to me like abandoned since half a year. Mind if I take over?

    Yeah sure. I think I have implemented the feature but I didn't get the time to write the test for it. Go for it 🚀

  17. arthmis commented on Dec 12, 2024

    @arthmis
    Contributor

    @jdm can I take on this issue?

  18. jdm commented on Dec 12, 2024

    @jdm
    MemberAuthor

    @arthmis Yes, that would be great!

  19. arthmis commented on Dec 14, 2024

    @arthmis
    Contributor

    so I've run into an issue. It seems prompting on windows doesn't work. I get a prompt window, I enter demo for the username but then servo hangs/freezes. I tested the same code on my macbook and I can get through the username and then the password prompt and receive those values in the IpcReceiver. So I'll have to look into why it's hanging on the prompt on windows

  20. mukilan commented on Dec 14, 2024

    @mukilan
    Member

    It seems prompting on windows doesn't work.

    Just an FYI, I believe our Outreachy intern (@chickenleaf) also faced a similar issue recently. We are looking into replacing tinyfiledialog with egui-file-dialog and/or egui-modal. It is likely that the code that shows the dialog window and blocks the embedder thread will be removed soon. So I guess it should be fine to proceed with the changes for this issue, even if the problem with Windows can't be resolved and as long as it works on Linux and Mac.

    cc: @mrobinson

  21. chickenleaf commented on Dec 14, 2024

    @chickenleaf
    Contributor

    @mukilan is this issue mentioned in the internship tasks? For the mini browser. I feel like I added this in my proposal?

  22. mukilan commented on Dec 14, 2024

    @mukilan
    Member

    @mukilan is this issue mentioned in the internship tasks? For the mini browser. I feel like I added this in my proposal?

    Yes, I believe we so, but it should be fine as @arthmis has already started on the changes and we were likely not going to get around to it until later in the internship. Depending on how much of the implementation is complete at that point, we could probably work on enhancements or come up with other tasks for you.

  23. arthmis commented on Dec 14, 2024

    @arthmis
    Contributor

    I was going to continue development on my mac for this issue then look into the windows issue afterwards so that's good to hear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-embeddingA-networkC-assignedE-more-complex

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions