@@ -799,11 +799,13 @@ define('Config',[],function() {
799799 return Config ;
800800} ) ;
801801/**
802- * JSO - Javascript JSO Library
803- * Version 2.0
804- * UNINETT AS - http://uninett.no
802+ * JSO - Javascript OAuth Library
803+ * Version 2.0
804+ * UNINETT AS - http://uninett.no
805+ * Author: Andreas Åkre Solberg <andreas.solberg@uninett.no>
806+ * Licence:
805807 *
806- * Documentation available at: https://github.com/andreassolberg/jso
808+ * Documentation available at: https://github.com/andreassolberg/jso
807809 */
808810
809811define ( 'jso' , [ 'require' , 'exports' , 'module' , './store' , './utils' , './Config' ] , function ( require , exports , module ) {
@@ -821,18 +823,99 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
821823 var utils = require ( './utils' ) ;
822824 var Config = require ( './Config' ) ;
823825
826+
827+
828+
829+
824830 var JSO = function ( config ) {
825831
826832 this . config = new Config ( default_config , config ) ;
827833 this . providerID = this . getProviderID ( ) ;
828834
829835 JSO . instances [ this . providerID ] = this ;
830836
837+ this . callbacks = { } ;
838+
839+ this . callbacks . redirect = JSO . redirect ;
840+
831841 // console.log("Testing configuration object");
832842 // console.log("foo.bar.baz (2,false)", this.config.get('foo.bar.baz', 2 ) );
833843 // console.log("foo.bar.baz (2,true )", this.config.get('foo.bar.baz', 2, true ) );
834844 } ;
835845
846+ JSO . internalStates = [ ] ;
847+ JSO . instances = { } ;
848+ JSO . store = store ;
849+
850+ console . log ( "RESET internalStates array" ) ;
851+
852+
853+ JSO . enablejQuery = function ( $ ) {
854+ JSO . $ = $ ;
855+ } ;
856+
857+
858+ JSO . redirect = function ( url , callback ) {
859+ window . location = url ;
860+ } ;
861+
862+ JSO . prototype . inappbrowser = function ( params ) {
863+ var that = this ;
864+ return function ( url , callback ) {
865+
866+
867+ var onNewURLinspector = function ( ref ) {
868+ return function ( inAppBrowserEvent ) {
869+
870+ // we'll check the URL for oauth fragments...
871+ var url = inAppBrowserEvent . url ;
872+ utils . log ( "loadstop event triggered, and the url is now " + url ) ;
873+
874+ if ( that . URLcontainsToken ( url ) ) {
875+ // ref.removeEventListener('loadstop', onNewURLinspector);
876+ setTimeout ( function ( ) {
877+ ref . close ( ) ;
878+ } , 500 ) ;
879+
880+
881+ that . callback ( url , function ( ) {
882+ // When we've found OAuth credentials, we close the inappbrowser...
883+ utils . log ( "Closing window " , ref ) ;
884+ if ( typeof callback === 'function' ) callback ( ) ;
885+ } ) ;
886+ }
887+
888+ } ;
889+ } ;
890+
891+ var target = '_blank' ;
892+ if ( params . hasOwnProperty ( 'target' ) ) {
893+ target = params . target ;
894+ }
895+ var options = { } ;
896+
897+ utils . log ( "About to open url " + url ) ;
898+
899+ var ref = window . open ( url , target , options ) ;
900+ utils . log ( "URL Loaded... " ) ;
901+ ref . addEventListener ( 'loadstart' , onNewURLinspector ( ref ) ) ;
902+ utils . log ( "Event listeren ardded... " , ref ) ;
903+
904+
905+ // Everytime the Phonegap InAppBrowsers moves to a new URL,
906+
907+
908+
909+ } ;
910+ } ;
911+
912+ JSO . prototype . on = function ( eventid , callback ) {
913+ if ( typeof eventid !== 'string' ) throw new Error ( 'Registering triggers on JSO must be identified with an event id' ) ;
914+ if ( typeof callback !== 'function' ) throw new Error ( 'Registering a callback on JSO must be a function.' ) ;
915+
916+ this . callbacks [ eventid ] = callback ;
917+ } ;
918+
836919
837920 /**
838921 * We need to get an identifier to represent this OAuth provider.
@@ -852,14 +935,29 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
852935 } ;
853936
854937
855- JSO . internalStates = [ ] ;
856- JSO . instances = { } ;
857- JSO . store = store ;
858938
859- JSO . enablejQuery = function ( $ ) {
860- JSO . $ = $ ;
861- } ;
862939
940+ /**
941+ * Do some sanity checking whether an URL contains a access_token in an hash fragment.
942+ * Used in URL change event trackers, to detect responses from the provider.
943+ * @param {[type] } url [description]
944+ */
945+ JSO . prototype . URLcontainsToken = function ( url ) {
946+ // If a url is provided
947+ if ( url ) {
948+ // utils.log('Hah, I got the url and it ' + url);
949+ if ( url . indexOf ( '#' ) === - 1 ) return false ;
950+ h = url . substring ( url . indexOf ( '#' ) ) ;
951+ // utils.log('Hah, I got the hash and it is ' + h);
952+ }
953+
954+ /*
955+ * Start with checking if there is a token in the hash
956+ */
957+ if ( h . length < 2 ) return false ;
958+ if ( h . indexOf ( "access_token" ) === - 1 ) return false ;
959+ return true ;
960+ } ;
863961
864962 /**
865963 * Check if the hash contains an access token.
@@ -878,7 +976,7 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
878976 state ,
879977 instance ;
880978
881- utils . log ( "JSO.prototype.callback()" ) ;
979+ utils . log ( "JSO.prototype.callback() " + url + " callback=" + typeof callback ) ;
882980
883981 // If a url is provided
884982 if ( url ) {
@@ -964,21 +1062,34 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
9641062
9651063 utils . log ( atoken ) ;
9661064
1065+ utils . log ( "Looking up internalStates storage for a stored callback... " , "state=" + atoken . state , JSO . internalStates ) ;
1066+
9671067 if ( JSO . internalStates [ atoken . state ] && typeof JSO . internalStates [ atoken . state ] === 'function' ) {
968- // log("InternalState is set, calling it now!");
969- JSO . internalStates [ atoken . state ] ( ) ;
1068+ utils . log ( "InternalState is set, calling it now!" ) ;
1069+ JSO . internalStates [ atoken . state ] ( atoken ) ;
9701070 delete JSO . internalStates [ atoken . state ] ;
9711071 }
9721072
9731073
1074+ utils . log ( "Successfully obtain a token, now call the callback, and may be the window closes" , callback ) ;
1075+
9741076 if ( typeof callback === 'function' ) {
975- callback ( ) ;
1077+ callback ( atoken ) ;
9761078 }
9771079
9781080 // utils.log(atoken);
9791081
9801082 } ;
9811083
1084+ JSO . prototype . dump = function ( ) {
1085+
1086+ var txt = '' ;
1087+ var tokens = store . getTokens ( this . providerID ) ;
1088+ txt += 'Tokens: ' + "\n" + JSON . stringify ( tokens , undefined , 4 ) + '\n\n' ;
1089+ txt += 'Config: ' + "\n" + JSON . stringify ( this . config , undefined , 4 ) + "\n\n" ;
1090+ return txt ;
1091+ } ;
1092+
9821093 JSO . prototype . _getRequestScopes = function ( opts ) {
9831094 var scopes = [ ] , i ;
9841095 /*
@@ -1021,6 +1132,27 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
10211132
10221133 } ;
10231134
1135+
1136+ // exp.jso_ensureTokens = function (ensure) {
1137+ // var providerid, scopes, token;
1138+ // for(providerid in ensure) {
1139+ // scopes = undefined;
1140+ // if (ensure[providerid]) scopes = ensure[providerid];
1141+ // token = store.getToken(providerid, scopes);
1142+
1143+ // utils.log("Ensure token for provider [" + providerid + "] ");
1144+ // utils.log(token);
1145+
1146+ // if (token === null) {
1147+ // jso_authrequest(providerid, scopes);
1148+ // }
1149+ // }
1150+
1151+
1152+ // return true;
1153+ // }
1154+
1155+
10241156 JSO . prototype . _authorize = function ( callback , opts ) {
10251157 var
10261158 request ,
@@ -1031,15 +1163,18 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
10311163 var client_id = this . config . get ( 'client_id' , null , true ) ;
10321164
10331165 utils . log ( "About to send an authorization request to this entry:" , authorization ) ;
1034- console . log ( "Options" , opts ) ;
1166+ utils . log ( "Options" , opts , "callback" , callback ) ;
10351167
10361168
10371169 request = {
10381170 "response_type" : "token" ,
10391171 "state" : utils . uuid ( )
10401172 } ;
10411173
1174+
1175+
10421176 if ( callback && typeof callback === 'function' ) {
1177+ utils . log ( "About to store a callback for later with state=" + request . state , callback ) ;
10431178 JSO . internalStates [ request . state ] = callback ;
10441179 }
10451180
@@ -1059,7 +1194,7 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
10591194 request . scope = utils . scopeList ( scopes ) ;
10601195 }
10611196
1062- console . log ( "DEBUG REQUEST" ) ; console . log ( request ) ;
1197+ utils . log ( "DEBUG REQUEST" ) ; utils . log ( request ) ;
10631198
10641199 authurl = utils . encodeURL ( authorization , request ) ;
10651200
@@ -1079,17 +1214,17 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
10791214 utils . log ( JSON . parse ( JSON . stringify ( request ) ) ) ;
10801215
10811216 store . saveState ( request . state , request ) ;
1082- this . gotoAuthorizeURL ( authurl ) ;
1217+ this . gotoAuthorizeURL ( authurl , callback ) ;
10831218 } ;
10841219
10851220
10861221 JSO . prototype . gotoAuthorizeURL = function ( url , callback ) {
10871222
1088- prompt ( 'Authorization url' , url ) ;
1089-
1090- setTimeout ( function ( ) {
1091- window . location = url ;
1092- } , 2000 ) ;
1223+
1224+ if ( ! this . callbacks . redirect || typeof this . callbacks . redirect !== 'function' )
1225+ throw new Error ( 'Cannot redirect to authorization endpoint because of missing redirect handler' ) ;
1226+
1227+ this . callbacks . redirect ( url , callback ) ;
10931228
10941229 } ;
10951230
@@ -1329,6 +1464,7 @@ define('jso',['require','exports','module','./store','./utils','./Config'],funct
13291464
13301465
13311466} ) ;
1467+
13321468 //The modules for your project will be inlined above
13331469 //this snippet. Ask almond to synchronously require the
13341470 //module value for 'main' here and return it as the
0 commit comments