@@ -535,7 +535,92 @@ module.factory('ConfigurationService', function(Config, AccountService) {
535535 return SettingsService ;
536536} ) ;
537537
538- module . factory ( "AccountService" , function ( ) {
538+ module . factory ( 'Base64' , function ( ) {
539+ var keyStr = 'ABCDEFGHIJKLMNOP' +
540+ 'QRSTUVWXYZabcdef' +
541+ 'ghijklmnopqrstuv' +
542+ 'wxyz0123456789+/' +
543+ '=' ;
544+ return {
545+ encode : function ( input ) {
546+ var output = "" ;
547+ var chr1 , chr2 , chr3 = "" ;
548+ var enc1 , enc2 , enc3 , enc4 = "" ;
549+ var i = 0 ;
550+
551+ do {
552+ chr1 = input . charCodeAt ( i ++ ) ;
553+ chr2 = input . charCodeAt ( i ++ ) ;
554+ chr3 = input . charCodeAt ( i ++ ) ;
555+
556+ enc1 = chr1 >> 2 ;
557+ enc2 = ( ( chr1 & 3 ) << 4 ) | ( chr2 >> 4 ) ;
558+ enc3 = ( ( chr2 & 15 ) << 2 ) | ( chr3 >> 6 ) ;
559+ enc4 = chr3 & 63 ;
560+
561+ if ( isNaN ( chr2 ) ) {
562+ enc3 = enc4 = 64 ;
563+ } else if ( isNaN ( chr3 ) ) {
564+ enc4 = 64 ;
565+ }
566+
567+ output = output +
568+ keyStr . charAt ( enc1 ) +
569+ keyStr . charAt ( enc2 ) +
570+ keyStr . charAt ( enc3 ) +
571+ keyStr . charAt ( enc4 ) ;
572+ chr1 = chr2 = chr3 = "" ;
573+ enc1 = enc2 = enc3 = enc4 = "" ;
574+ } while ( i < input . length ) ;
575+
576+ return output ;
577+ } ,
578+
579+ decode : function ( input ) {
580+ var output = "" ;
581+ var chr1 , chr2 , chr3 = "" ;
582+ var enc1 , enc2 , enc3 , enc4 = "" ;
583+ var i = 0 ;
584+
585+ // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
586+ var base64test = / [ ^ A - Z a - z 0 - 9 \+ \/ \= ] / g;
587+ if ( base64test . exec ( input ) ) {
588+ alert ( "There were invalid base64 characters in the input text.\n" +
589+ "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
590+ "Expect errors in decoding." ) ;
591+ }
592+ input = input . replace ( / [ ^ A - Z a - z 0 - 9 \+ \/ \= ] / g, "" ) ;
593+
594+ do {
595+ enc1 = keyStr . indexOf ( input . charAt ( i ++ ) ) ;
596+ enc2 = keyStr . indexOf ( input . charAt ( i ++ ) ) ;
597+ enc3 = keyStr . indexOf ( input . charAt ( i ++ ) ) ;
598+ enc4 = keyStr . indexOf ( input . charAt ( i ++ ) ) ;
599+
600+ chr1 = ( enc1 << 2 ) | ( enc2 >> 4 ) ;
601+ chr2 = ( ( enc2 & 15 ) << 4 ) | ( enc3 >> 2 ) ;
602+ chr3 = ( ( enc3 & 3 ) << 6 ) | enc4 ;
603+
604+ output = output + String . fromCharCode ( chr1 ) ;
605+
606+ if ( enc3 != 64 ) {
607+ output = output + String . fromCharCode ( chr2 ) ;
608+ }
609+ if ( enc4 != 64 ) {
610+ output = output + String . fromCharCode ( chr3 ) ;
611+ }
612+
613+ chr1 = chr2 = chr3 = "" ;
614+ enc1 = enc2 = enc3 = enc4 = "" ;
615+
616+ } while ( i < input . length ) ;
617+
618+ return output ;
619+ }
620+ } ;
621+ } ) ;
622+
623+ module . factory ( "AccountService" , function ( $cookieStore ) {
539624 var username = null ;
540625 var accountURI = null ;
541626 var email = null ;
@@ -577,7 +662,9 @@ module.factory("AccountService", function() {
577662 } ;
578663
579664 var getAccount = function ( ) {
580- return { username : username , email : email } ;
665+ var user = $cookieStore . get ( 'User' ) ;
666+ var pass = $cookieStore . get ( 'Pass' ) ;
667+ return { username : username , email : email , user : user , pass : pass } ;
581668 } ;
582669
583670 var setAdmin = function ( adm ) {
@@ -603,8 +690,13 @@ module.factory("AccountService", function() {
603690 } ;
604691} ) ;
605692
606- module . factory ( "LoginService" , function ( $http , $location , AccountService , ConfigurationService ) {
693+ module . factory ( "LoginService" , function ( $http , $location , $cookieStore , AccountService , ConfigurationService , Base64 ) {
694+
607695 var login = function ( username , password ) {
696+
697+ var username = Base64 . decode ( username ) ;
698+ var password = Base64 . decode ( password ) ;
699+
608700 var postData = {
609701 username : username ,
610702 password : password ,
@@ -621,6 +713,14 @@ module.factory("LoginService", function($http, $location, AccountService, Config
621713 AccountService . setEmail ( response . data . email ) ;
622714 AccountService . setAdmin ( response . data . admin ) ;
623715 ConfigurationService . setSettingsGraph ( response . data . settingsGraph ) ;
716+
717+ var encodedUser = Base64 . encode ( username ) ;
718+ var encodedPass = Base64 . encode ( password ) ;
719+ $http . defaults . headers . common . Authorization = 'User ' + encodedUser + ' Pass ' + encodedPass ;
720+ $cookieStore . put ( 'User' , encodedUser ) ;
721+ $cookieStore . put ( 'Pass' , encodedPass ) ;
722+ console . log ( $http . defaults . headers . common ) ;
723+
624724 } , function ( response ) {
625725 //todo
626726 } ) ;
@@ -640,6 +740,14 @@ module.factory("LoginService", function($http, $location, AccountService, Config
640740 AccountService . clear ( ) ;
641741 ConfigurationService . restoreDefaultSettingsGraph ( ) ;
642742 $location . path ( "/home" ) ;
743+
744+ document . execCommand ( "ClearAuthenticationCache" ) ;
745+ $cookieStore . remove ( 'User' ) ;
746+ $cookieStore . remove ( 'Pass' ) ;
747+ $http . defaults . headers . common . Authorization = 'User Pass' ;
748+
749+ console . log ( $http . defaults . headers . common ) ;
750+
643751 } , function ( response ) {
644752 //todo
645753 } ) ;
0 commit comments