How to log out user from web site using BASIC authentication?

后端 未结 22 1420
感情败类
感情败类 2020-11-22 04:00

Is it possible to log out user from a web site if he is using basic authentication?

Killing session is not enough, since, once user is authenticated, each request co

相关标签:
22条回答
  • 2020-11-22 04:31

    Just for the record, there is a new HTTP Response Header called Clear-Site-Data. If your server reply includes a Clear-Site-Data: "cookies" header, then the authentication credentials (not only cookies) should be removed. I tested it on Chrome 77 but this warning shows on the console:

    Clear-Site-Data header on 'https://localhost:9443/clear': Cleared data types:
    "cookies". Clearing channel IDs and HTTP authentication cache is currently not
    supported, as it breaks active network connections.
    

    And the auth credentials aren't removed, so this doesn't works (for now) to implement basic auth logouts, but maybe in the future will. Didn't test on other browsers.

    References:

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data

    https://www.w3.org/TR/clear-site-data/

    https://github.com/w3c/webappsec-clear-site-data

    https://caniuse.com/#feat=mdn-http_headers_clear-site-data_cookies

    0 讨论(0)
  • 2020-11-22 04:32

    add this to your application :

    @app.route('/logout')
    def logout():
        return ('Logout', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})
    
    0 讨论(0)
  • 2020-11-22 04:34

    Sending https://invalid_login@hostname works fine everywhere except Safari on Mac (well, not checked Edge but should work there too).

    Logout doesn't work in Safari when a user selects 'remember password' in the HTTP Basic Authentication popup. In this case the password is stored in Keychain Access (Finder > Applications > Utilities > Keychain Access (or CMD+SPACE and type "Keychain Access")). Sending https://invalid_login@hostname doesn't affect Keychain Access, so with this checkbox it is not possible to logout on Safari on Mac. At least it is how it works for me.

    MacOS Mojave (10.14.6), Safari 12.1.2.

    The code below works fine for me in Firefox (73), Chrome (80) and Safari (12). When a user navigates to a logout page the code is executed and drops the credentials.

        //It should return 401, necessary for Safari only
        const logoutUrl = 'https://example.com/logout'; 
        const xmlHttp = new XMLHttpRequest();
        xmlHttp.open('POST', logoutUrl, true, 'logout');
        xmlHttp.send();
    

    Also for some reason Safari doesn't save credentials in the HTTP Basic Authentication popup even when the 'remember password' is selected. The other browsers do this correctly.

    0 讨论(0)
  • This JavaScript must be working for all latest version browsers:

    //Detect Browser
    var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
        // Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
    var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
    var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
        // At least Safari 3+: "[object HTMLElementConstructor]"
    var isChrome = !!window.chrome && !isOpera;              // Chrome 1+
    var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
    var Host = window.location.host;
    
    
    //Clear Basic Realm Authentication
    if(isIE){
    //IE
        document.execCommand("ClearAuthenticationCache");
        window.location = '/';
    }
    else if(isSafari)
    {//Safari. but this works mostly on all browser except chrome
        (function(safeLocation){
            var outcome, u, m = "You should be logged out now.";
            // IE has a simple solution for it - API:
            try { outcome = document.execCommand("ClearAuthenticationCache") }catch(e){}
            // Other browsers need a larger solution - AJAX call with special user name - 'logout'.
            if (!outcome) {
                // Let's create an xmlhttp object
                outcome = (function(x){
                    if (x) {
                        // the reason we use "random" value for password is 
                        // that browsers cache requests. changing
                        // password effectively behaves like cache-busing.
                        x.open("HEAD", safeLocation || location.href, true, "logout", (new Date()).getTime().toString())
                        x.send("");
                        // x.abort()
                        return 1 // this is **speculative** "We are done." 
                    } else {
                        return
                    }
                })(window.XMLHttpRequest ? new window.XMLHttpRequest() : ( window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : u )) 
            }
            if (!outcome) {
                m = "Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser."
            }
            alert(m);
            window.location = '/';
            // return !!outcome
        })(/*if present URI does not return 200 OK for GET, set some other 200 OK location here*/)
    }
    else{
    //Firefox,Chrome
        window.location = 'http://log:out@'+Host+'/';
    }
    
    0 讨论(0)
  • 2020-11-22 04:36
    function logout() {
      var userAgent = navigator.userAgent.toLowerCase();
    
      if (userAgent.indexOf("msie") != -1) {
        document.execCommand("ClearAuthenticationCache", false);
      }
    
      xhr_objectCarte = null;
    
      if(window.XMLHttpRequest)
        xhr_object = new XMLHttpRequest();
      else if(window.ActiveXObject)
        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
      else
        alert ("Your browser doesn't support XMLHTTPREQUEST");
    
      xhr_object.open ('GET', 'http://yourserver.com/rep/index.php', false, 'username', 'password');
      xhr_object.send ("");
      xhr_object = null;
    
      document.location = 'http://yourserver.com'; 
      return false;
    }
    
    0 讨论(0)
  • 2020-11-22 04:37

    Basic Authentication wasn't designed to manage logging out. You can do it, but not completely automatically.

    What you have to do is have the user click a logout link, and send a ‘401 Unauthorized’ in response, using the same realm and at the same URL folder level as the normal 401 you send requesting a login.

    They must be directed to input wrong credentials next, eg. a blank username-and-password, and in response you send back a “You have successfully logged out” page. The wrong/blank credentials will then overwrite the previous correct credentials.

    In short, the logout script inverts the logic of the login script, only returning the success page if the user isn't passing the right credentials.

    The question is whether the somewhat curious “don't enter your password” password box will meet user acceptance. Password managers that try to auto-fill the password can also get in the way here.

    Edit to add in response to comment: re-log-in is a slightly different problem (unless you require a two-step logout/login obviously). You have to reject (401) the first attempt to access the relogin link, than accept the second (which presumably has a different username/password). There are a few ways you could do this. One would be to include the current username in the logout link (eg. /relogin?username), and reject when the credentials match the username.

    0 讨论(0)
提交回复
热议问题