客户注册失败

时间:2016-06-15 14:59:18

标签: android ibm-mobilefirst

我已经开发了一个带有适配器授权和挑战处理程序的ibm移动第一个应用程序。我已经添加了一个Android环境,并希望针对生产服务器进行测试,所以我在我的开发平板电脑上部署了apk。

我已经看到,当尝试调用远程适配器时,worklight尝试通过执行cordova方法" WLAuthorizationManagerPlugingetClientInstanceIdHeader"来获取ClientInstanceId。这个请求总是在超时时间内产生,我可以在控制台中看到一个新的entru:" Uncaught(在promise中)adapter-communication-error"。该方法以" WL.Logger.debug退出('客户端注册失败,错误:' + JSON.stringify(错误));"

编辑:登录流程:

登录实施:

import {UserModel} from 'app-user';
import {UserService} from 'app-user-service';
import {AppSettingsService} from 'app-settings';
import {AppMasterDataService} from "../master-data/app-master-data-service";
import jsSha from 'js-sha';
import {UserRepository} from "user-repository";
import {NetworkInformation} from     ../../infrastructure/phonegap/plugins/network-information';

 export class LoggedInUser{
 public static inject = [UserService, AppSettingsService,      AppMasterDataService, UserRepository];

public info: UserModel;

public isLoggedIn: boolean;

private singleStepAuthRealmChallengeHandler: any;

private _getUserInfoPromise: Promise<UserModel>;

private _listeners: Array<(success: boolean, error: string) => void> = [];

private lastCredentials: any;

constructor(private _userService: UserService, private _settingsService: AppSettingsService, private _masterDataService: AppMasterDataService, private _userRepo: UserRepository){
if(typeof WL != "undefined"){
  this.singleStepAuthRealmChallengeHandler = WL.Client.createChallengeHandler('GicarAuthRealm');
  this.singleStepAuthRealmChallengeHandler.isCustomResponse = this.isCustomResponse.bind(this);
  this.singleStepAuthRealmChallengeHandler.handleChallenge = this.handleChallenge.bind(this);
 }
}

public async initializeUserInfo(): Promise<void>{
  return this.executeLogOut(null, null);
}

public addLoginEventListener(callback: (success: boolean, error: string) =>  void){
 if(!this._listeners.some(x => x === callback)){
   this._listeners.push(callback);
 }
}
 public removeLoginEventListener(callback: (success: boolean, error: string) => void){
 let index = this._listeners.indexOf(callback);
 if(index >= 0){
   this._listeners.splice(index, 1);
 }
}

private raiseLoginEvent(success: boolean, error?: string){
 for (var listener of this._listeners){
   try {
     listener(success, error);
   }catch (e){
    this.removeLoginEventListener(listener);
   }
}
}
public prepareLogin(){
  if(NetworkInformation.connected())
   this._getUserInfoPromise = this._userService.getUserInfo();
}

public async executeLogIn(userName: string, password: string){//: Promise<boolean>{
await this.executeLogOut(userName, password);
if(this.singleStepAuthRealmChallengeHandler){
  if(NetworkInformation.connected()){
    var userNameSha = new jsSha("SHA-512", "TEXT");
    userNameSha.update(userName);
    var userNameHash = userNameSha.getHash("B64");

    var passwordSha = new jsSha("SHA-512", "TEXT");
    passwordSha.update(password);
    var passwordHash = passwordSha.getHash("B64");

    this.lastCredentials = {
      userName: userNameHash,
      password: passwordHash
    };
    var invocationData = {
      adapter : "GicarAuthAdapter",
      procedure : "submitAuthentication",
      parameters : [ userName, window.btoa(userName + ':' + password) ]
    };
    this.singleStepAuthRealmChallengeHandler.submitAdapterAuthentication(invocationData);
  }else {
    this.doDisconnectedLogin(userName, password);
  }

}
else{
  this._userService.logIn(userName, password)
  .then(info =>{
    this.info = info;
    this.isLoggedIn = typeof info !== 'undefined' && info != null;
    this.raiseLoginEvent(this.isLoggedIn);
  }).catch(e =>{
    this.isLoggedIn = false;
    this.info = null;
    this.raiseLoginEvent(false, e.message ? e.message : e.toString())
  });
 }
}

private async doDisconnectedLogin(userName: string, password: string){
 var userNameSha = new jsSha("SHA-512", "TEXT");
 userNameSha.update(userName);
 var userNameHash = userNameSha.getHash("B64");

var passwordSha = new jsSha("SHA-512", "TEXT");
passwordSha.update(password);
var passwordHash = passwordSha.getHash("B64");

var persisted = await this._userRepo.findUserByUserName(userNameHash);
let success = persisted && persisted.password == passwordHash;
this.info = persisted;
this.isLoggedIn = success;
this.raiseLoginEvent(success, success ? null : 'user-invalid-credentials');
}

public executeLogOut(userName: string, password: string): Promise<void>{
 this.lastCredentials = null;
 if(NetworkInformation.connected()){
   return this._userService.logOut(this.info).then(() =>{
     this.isLoggedIn = false;
     this.info = null;
   });
 }
else
  return Promise.resolve();

}

private isCustomResponse(response: any): boolean{
 if (!response || !response.responseJSON || response.responseText === null)  {
  return false;
}
if (typeof(response.responseJSON.authRequired) !== 'undefined'){
  return true;
} else {
  return false;
}
}

private async handleChallenge(response: JQueryXHR){
var authRequired = response.responseJSON.authRequired;
if(authRequired == true){
  this.info == null;
  this.raiseLoginEvent(false, response.responseJSON.errorMessage);
}else {
  try {
    if(this.info == null){
      this.singleStepAuthRealmChallengeHandler.submitSuccess();
      this.info = await this._getUserInfoPromise;
      await this._masterDataService.initializeMasterData(false);
      if(this.lastCredentials){
        await this._userRepo.saveUser({
          entityId: this.lastCredentials.userName,
          firstName: this.info.firstName,
          lastName: this.info.lastName,
          fullName: this.info.fullName,
          email: this.info.email,
          nif: this.info.nif,
          address: this.info.address,
          userName: this.lastCredentials.userName,
          password: this.lastCredentials.password
        });
      }
      this.isLoggedIn = true;
      this.raiseLoginEvent(true);
    }

  }catch (error){
    this.raiseLoginEvent(false, error.message ? error.message : error.toString());
  }
}
}
}

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题。

为了支持多种环境,包括desktopbrowser的一个,我使用&#34; old&#34;来调用MFP适配器。方法:`

WL.Client.invokeProcedure({
    adapter: "UserServiceAdapter",
    procedure: 'getUserInfo'
    parameters: [],
    compressResponse: false
  },{
    timeout:30000,
    onSuccess: handleSuccess,
    onFailure: handleFailure
  });

如果我将以上代码替换为:

var resourceRequest = new WLResourceRequest("/adapters/UserServiceAdapter/getUserInfo", WLResourceRequest.GET, 30000);
  resourceRequest.send().then(
    handleSuccess,
    handleFailure
);

它在Android上正常运行。

似乎我必须检查环境才能决定适配器调用方法