Skip to content

Commit

Permalink
Initial implementation of comparision nomadhelper.html deployed vs lo…
Browse files Browse the repository at this point in the history
…cal one

- Logic generate hashes and compare both content hashes for each file (reference #57)
  • Loading branch information
piotrzarzycki21 committed May 27, 2024
1 parent 8d9aca5 commit 10c1731
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package classes.managers
{
public static const LOGIN_REDIRECTION:String = "SuperHumanPortal.nsf/XMLAuthenticationTest?OpenAgent"; // Authentication Check
public static const APP_LOCAL_VERSION_URL:String = "resources/version.xml";
public static const NOMAD_HELPER_FILE_URL:String = "resources/nomadhelper.html";

private static var _instance:UrlProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ package model.proxy.login
import org.apache.royale.net.events.FaultEvent;
import org.puremvc.as3.multicore.patterns.proxy.Proxy;

import services.NomadHelperUrlDelegate;
import services.login.LoginServiceDelegate;

import utils.UtilsCore;
import org.apache.royale.utils.async.PromiseTask;
import utils.NomadHelperUrlTasks;
import org.apache.royale.utils.async.SequentialAsyncTask;

public class ProxyLogin extends Proxy
{
Expand All @@ -27,7 +33,8 @@ package model.proxy.login
public static const NOTE_ACCOUNTS_LOAD_FAILED:String = NAME + "NoteAccountsLoadFailed";

protected var loginServiceDelegate:LoginServiceDelegate;

private var nomadHelperUrlDelegate:NomadHelperUrlDelegate;

private var username:String;
private var proxyUrlParams:ProxyUrlParameters;
private var busyManagerProxy:ProxyBusyManager;
Expand All @@ -37,6 +44,7 @@ package model.proxy.login
super(NAME);

loginServiceDelegate = new LoginServiceDelegate();
nomadHelperUrlDelegate = new NomadHelperUrlDelegate();
}

override public function onRegister():void
Expand Down Expand Up @@ -166,10 +174,38 @@ package model.proxy.login

_config = config;

sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, this.getData() as UserVO);
if (this.isNomadHelperUrlExists())
{
var nomadHelperUrlTasks:NomadHelperUrlTasks = new NomadHelperUrlTasks();
nomadHelperUrlTasks.done(function(task:PromiseTask):void {
if (nomadHelperUrlTasks.failed)
{
sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, getData() as UserVO);
}
else if (nomadHelperUrlTasks.completed)
{
compareNomadHelperUrl(nomadHelperUrlTasks.completedTasks[0], nomadHelperUrlTasks.completedTasks[1]);
}
});
nomadHelperUrlTasks.run(config.config.nomad_helper_url);
}
else
{
sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, this.getData() as UserVO);
}
//ParseCentral.parseAppConfig(new XML(event.target["data"]));
}

private function onNomadHelperLoadSuccess(event:Event):void
{
nomadHelperUrlDelegate.getLocalNomadHelper(function onNomadHelperLocalSuccess(event:Event):void {
sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, this.getData() as UserVO);
},
function onFault(event:FaultEvent):void{
sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, this.getData() as UserVO);
});
}

private function onAccountsLoadSuccess(event:Event):void
{
ParseCentral.parseAccounts(new XML(event.target["data"]));
Expand Down Expand Up @@ -197,6 +233,35 @@ package model.proxy.login
this.testAuthenticationWithoutBusyIndicator();
}

private function compareNomadHelperUrl(nomadHelperUrlTask:PromiseTask, nomadHelperUrlTask2:PromiseTask):void
{
var nomadHelperUrlHash:PromiseTask = UtilsCore.computeHash(nomadHelperUrlTask.result.target.responseText);
var nomadHelperUrlHash2:PromiseTask = UtilsCore.computeHash(nomadHelperUrlTask2.result.target.responseText);

var sequentialHashTask:SequentialAsyncTask = new SequentialAsyncTask([
nomadHelperUrlHash,
nomadHelperUrlHash2
]);

sequentialHashTask.done(function(task:PromiseTask) {
if (sequentialHashTask.failed)
{
sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, getData() as UserVO);
}
else
{
if (sequentialHashTask.completed)
{
var hash1:String = sequentialHashTask.completedTasks[0].data;
var hash2:String = sequentialHashTask.completedTasks[1].data;

sendNotification(ProxyLogin.NOTE_LOGIN_SUCCESS, getData() as UserVO);
}
}
})
sequentialHashTask.run();
}

private function failOnServer(data:Object):void
{
var eventData:String = String(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package services
{
import classes.managers.UrlProvider;

import org.apache.royale.html.Alert;
import org.apache.royale.net.HTTPService;
import org.apache.royale.net.beads.CORSCredentialsBead;
import org.apache.royale.net.events.FaultEvent;

public class NomadHelperUrlDelegate
{
public function getNomadHelper(nomadHelperUrl:String, resultCallback:Function, faultCallback:Function=null):void
{
if (faultCallback == null)
{
faultCallback = onFault;
}

var service:HTTPService = new HTTPService();
service.addBead(new CORSCredentialsBead(true));
service.url = nomadHelperUrl;
service.method = "GET";
service.addEventListener("complete", resultCallback);
service.addEventListener("ioError", faultCallback);
service.send();
}

public function getLocalNomadHelper(resultCallback:Function, faultCallback:Function=null):void
{
if (faultCallback == null)
{
faultCallback = onFault;
}

var service:HTTPService = new HTTPService();
service.addBead(new CORSCredentialsBead(true));
service.url = UrlProvider.NOMAD_HELPER_FILE_URL;
service.method = "GET";
service.addEventListener("complete", resultCallback);
service.addEventListener("ioError", faultCallback);
service.send();
}

public function onFault(event:FaultEvent):void
{
Alert.show(event.message.toLocaleString(), this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package utils
{
import org.apache.royale.utils.async.SequentialAsyncTask;
import services.NomadHelperUrlDelegate;
import org.apache.royale.utils.async.AsyncTask;
import org.apache.royale.utils.async.PromiseTask;
import org.apache.royale.net.events.FaultEvent;

public class NomadHelperUrlTasks extends SequentialAsyncTask
{
public function NomadHelperUrlTasks(tasks:Array=null)
{
super(tasks);


}

override public function run(data:Object=null):void
{
if (data == null)
{
cancel();
return;
}

var localNomadHelperTask:PromiseTask = new PromiseTask(new Promise(function(resolve:Function, reject:Function){
var localNomadHelper:NomadHelperUrlDelegate = new NomadHelperUrlDelegate();
localNomadHelper.getLocalNomadHelper(function(event:Event):void{
resolve(event);
}, function onFault(fault:FaultEvent):void{
reject(fault);
})
}));
this.addTask(localNomadHelperTask);

var remoteNomadHelperTask:PromiseTask = new PromiseTask(new Promise(function(resolve:Function, reject:Function){
var localNomadHelper:NomadHelperUrlDelegate = new NomadHelperUrlDelegate();
localNomadHelper.getNomadHelper(String(data), function(event:Event):void{
resolve(event);
}, function onFault(fault:FaultEvent):void{
reject(fault);
})
}));
this.addTask(remoteNomadHelperTask);

super.run(data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils
{
import org.apache.royale.collections.ArrayList;
import org.apache.royale.collections.CompareUtils;
import org.apache.royale.utils.async.PromiseTask;

public class UtilsCore
{
Expand Down Expand Up @@ -44,5 +45,31 @@ package utils
{
collection.sortOn(fieldName, (descending ? Array.DESCENDING : null) | (numeric ? Array.NUMERIC : null));
}

public static function computeHash(content:String):PromiseTask {
var resultPromise:PromiseTask = new PromiseTask(new Promise(function(resolve:Function, reject:Function){
var encoder:TextEncoder = new TextEncoder();
var data:Uint8Array = encoder.encode(content);
var promise:PromiseTask = new PromiseTask(window["crypto"].subtle.digest('SHA-256', data));
promise.done(function digestDone(p:PromiseTask):void{
if (!p.failed)
{
var hashArray:Array = Array.from(new Uint8Array(p.result));
resultPromise.data = hashArray.map(function(element:*, index:int, arr:Array):String{
return element.toString(16).padStart(2, '0');
}).join('');

resolve();
}
else
{
reject();
}
})
promise.run();
}))

return resultPromise;
}
}
}

0 comments on commit 10c1731

Please sign in to comment.