i'm building first mobile app using cordova. back-end services live on azure i'm trying authentication working using adal plugin cordova.
first of found out library not intercepts adal library angular does. i'm using angular within cordova app, paired material design directives look-and-feel. have been nice have interception, understood it's not there @ moment (should find out how hard implement).
so instead wrote service take care of sending rest api requests azure, including correct authentication token. it's based on sample found here.
this came with:
var request = function(url) { createcontext() .then(function () { getauthtoken().then( function(token) { sendrequest(token, url); }) }, function (err) { $log.error("failed create context."); }); };
first create authentication context:
function createcontext () { return $q(function (resolve, reject) { var authenticationcontext = microsoft.adal.authenticationcontext; authenticationcontext.createasync(authority) .then(function (context) { authcontext = context; $log.log("created authentication context authority url: " + context.authority); resolve(); }, function (err) { $log.error("failed create authentication context: " + pre(err)) reject(); }); }); };
the using context should authentication token:
function getauthtoken() { if (authcontext == null) { $log.error('authentication context isn\'t created yet. create context first'); return; } return $q(function (resolve, reject) { authcontext.acquiretokenasync(resourceurl, appid, redirecturl) .then(function (authresult) { resolve(authresult.accesstoken); }, function (err) { $log.error("failed acquire token: " + pre(err)); reject(); }); }); }
and afterwards should send request i'll leave part out since never gets there anyway. feel need re-emphasize i'm complete n00b @ stuff, please easy on me , on code. there's lot of room improvement, that.
when run this, pops window need login using microsoft account, cool. got 2 factor authentication first time tried this, nice! log in , returned code. authresult variable has status of "failed" , there's no access token in result. unfortunately there's no indication of went wrong. first part of question is; have gone wrong here?
now second part of question; how debug these kinds of things? on desktop i'd run fiddler check out communication, don't know how android. i'm debugging on device btw, cause reason of emulators available me extremely slow (vs , google) though hardware specs should support them fine.
thanks pointers!
update 03-02-2016
fiddling around code bit, decided pack things in login function gives shorter sample:
var createcontext = function () { if (authcontext == null) { authcontext = new microsoft.adal.authenticationcontext(authority); } }; var getauthtoken = function () { if (authcontext == null) { $log.error('authentication context isn\'t created yet. create context first'); return; } return $q(function (resolve, reject) { authcontext.acquiretokenasync(endpointurl, appid, redirecturl) .then(function (authresult) { resolve(authresult.accesstoken); }, function (err) { $log.error("failed acquire token: " + pre(err)); reject(); }); }); } var login = function () { createcontext(); getauthtoken(); }
this code runs on following input vars:
var authority = 'https://login.windows.net/[tenantid]'; var resourceurl = 'https://graph.windows.net/'; var appid = '1ef41b17-0943-4359-bc12-014f4fd2d841'; var redirecturl = 'http://myapp';
i used chrome://inspect see going on wire. , big surprise, see valid saml token returned azure. has got name in , everything, i'd recon wouldn't send after failed authentication. seems though response ok, adal library doesn't give me proper response (status = failed). again no clue on how proceed :s
i solved it. , 1 expect, remedy simple get. configuring application in azure ad, chose "web application" type application, since web application angular , all. guess since cordova translates things native code, that's not correct option chose. created new application "native application" instead , used client id of one, started working.... sincerely hope else in future...!
Comments
Post a Comment