alenny / angular2-adal-example

MIT License
22 stars 10 forks source link

Example for acquireToken? #5

Open spalt opened 8 years ago

spalt commented 8 years ago

Is this the correct usage of this function? I seem to be getting an error " Build: Property 'subscribe' does not exist on type '() => Observable<{}>'"

this.adalService.acquireToken("http://whatever") .subscribe( data => { alert(data); }, error => { alert('error!'); } );

jvciii commented 8 years ago

Did you get this.adalService.acquireToken to work?

spalt commented 8 years ago

Sort of. It works, but is it the right way? No clue. I'm doing this:

let adlContext = (this.adalService as any).adalContext;

adalService.config.redirectUri = window.location.origin + '/processtoken.html';
this.adlContext.acquireToken(url, function (error, null) { });
this.timer = setInterval(() => {
     var iframe = (document.getElementById("adalRenewFrame" + url) as any);
     if (iframe) {
     var idoc = iframe.contentDocument || iframe.contentWindow.document;
     if (iframe.src != '' && idoc.readyState == 'complete' && idoc.getElementById("access_token") && idoc.getElementById("access_token").innerHTML != '') {
          clearInterval(this.timer);
          this.adlContext._saveItem("access_token", idoc.getElementById("access_token").innerHTML);
     }
}}, 100);

Note the "processtoken.html". You're telling azure to send a new token, and then on the return, hit it, which decodes the token from the query string and puts it in the hidden iframe, in the "access_token" div. This file looks like this:

<!DOCTYPE html>
<html>
<body>
    <div id="access_token"></div>

    <script language="javascript">
        _getHash = function (hash) {
            if (hash.indexOf('#/') > -1) {
                hash = hash.substring(hash.indexOf('#/') + 2);
            } else if (hash.indexOf('#') > -1) {
                hash = hash.substring(1);
            }
            return hash;
        };

        _deserialize = function (query) {
            var match,
                pl = /\+/g,  // Regex for replacing addition symbol with a space
                search = /([^&=]+)=?([^&]*)/g,
                decode = function (s) {
                    return decodeURIComponent(s.replace(pl, ' '));
                },
                obj = {};
            match = search.exec(query);
            while (match) {
                obj[decode(match[1])] = decode(match[2]);
                match = search.exec(query);
            }
            return obj;
        };

        var hash = _getHash(window.location.hash);
        var params = _deserialize(hash);

        document.getElementById("access_token").innerHTML = params["access_token"];
    </script>
</body>
</html>
nikhil263 commented 7 years ago

But, this process won't work when we need to renew the token.

ClaytonBrawley commented 7 years ago

Has anyone made any progress on this?