2017-10-02 10 views
5

セキュリティで保護されたストレージプラグインを初期化しようとしています。これが失敗すると、ユーザーには安全なロック画面が設定されていないことを意味します。私は提供されたサンプルを再作成しようとしているgithubのページを使用する:Ionic secure storage - ユーザーにロック画面の設定を依頼する

var ss; 
var _init = function() { 
    ss = new cordova.plugins.SecureStorage(
     function() { 
      console.log('OK'); 
     }, 
     function() { 
      navigator.notification.alert(
       'Please enable the screen lock on your device. This app cannot operate securely without it.', 
       function() { 
        ss.secureDevice(
         function() { 
          _init(); 
         }, 
         function() { 
          _init(); 
         } 
        ); 
       }, 
       'Screen lock is disabled' 
      ); 
     }, 
     'my_app'); 
}; 
_init(); 

これは私の試みです:

private createSecureStorage() { 
    this.secureStorageAPI.create(this.storeName).then( 
     (storage: SecureStorageObject) => { 
      this.secureStorage = storage; 
    }).catch( 
     (error) => { 
      this.dialogs.alert('Please enable the screen lock on your device. This app cannot operate securely without it.').then( 
       () => { 
       // Alert Dismissed, should open the secure lockscreen settings here 
        this.secureStorage.secureDevice().then( 
        () => { 
          // Try again 
          this.createSecureStorage(); 
         } 
       ).catch(() => { 
        // Try again 
        this.createSecureStorage(); 
        }) 
       }) 
     }); 
    } 

私がいる問題は、secureStorageApi.create呼び出しが失敗したときにということsecureStorageです私はそれを呼び出してsecureDevice()を呼び出すために使用することはできません。

ご協力いただければ幸いです。

+0

この1を支援することはできません1?私は、secureDevice()を呼び出すためにSecureStorageオブジェクトが必要なので、これは誤ったデザインであると思います。 – Juxture

答えて

6

このように:

var __extends = (this && this.__extends) || (function() { 
    var extendStatics = Object.setPrototypeOf || 
     ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || 
     function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; 
    return function (d, b) { 
     extendStatics(d, b); 
     function __() { this.constructor = d; } 
     d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 
    }; 
})(); 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { 
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; 
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); 
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; 
    return c > 3 && r && Object.defineProperty(target, key, r), r; 
}; 
var __metadata = (this && this.__metadata) || function (k, v) { 
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); 
}; 
import { Injectable } from '@angular/core'; 
import { CordovaInstance, Plugin, CordovaCheck, IonicNativePlugin } from '@ionic-native/core'; 
/** 
* @hidden 
*/ 
var SecureStorageObject = (function() { 
    function SecureStorageObject(_objectInstance) { 
     this._objectInstance = _objectInstance; 
    } 
    /** 
    * Gets a stored item 
    * @param key {string} 
    * @returns {Promise<string>} 
    */ 
    SecureStorageObject.prototype.get = function (key) { return; }; 
    /** 
    * Stores a value 
    * @param key {string} 
    * @param value {string} 
    * @returns {Promise<any>} 
    */ 
    SecureStorageObject.prototype.set = function (key, value) { return; }; 
    /** 
    * Removes a single stored item 
    * @param key {string} 
    * @returns {Promise<string>} returns a promise that resolves with the key that was removed 
    */ 
    SecureStorageObject.prototype.remove = function (key) { return; }; 
    /** 
    * Get all references from the storage. 
    * @returns {Promise<string[]>} returns a promise that resolves with array of keys storage 
    */ 
    SecureStorageObject.prototype.keys = function() { return; }; 
    /** 
    * Clear all references from the storage. 
    * @returns {Promise<any>} 
    */ 
    SecureStorageObject.prototype.clear = function() { return; }; 
    return SecureStorageObject; 
}()); 
/** 
* @hidden 
*/ 
var SecureDeviceObject = (function() { 
    function SecureDeviceObject(_objectInstance) { 
     this._objectInstance = _objectInstance; 
    } 

    /** 
     * Brings up the screen-lock settings 
     * @returns {Promise<any>} 
     */ 
    SecureStorageObject.prototype.secureDevice = function() { return; }; 
    return SecureDeviceObject; 
}()); 
export { SecureStorageObject, SecureDeviceObject }; 
__decorate([ 
    CordovaInstance({ 
     callbackOrder: 'reverse' 
    }), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", [String]), 
    __metadata("design:returntype", Promise) 
], SecureStorageObject.prototype, "get", null); 
__decorate([ 
    CordovaInstance({ 
     callbackOrder: 'reverse' 
    }), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", [String, String]), 
    __metadata("design:returntype", Promise) 
], SecureStorageObject.prototype, "set", null); 
__decorate([ 
    CordovaInstance({ 
     callbackOrder: 'reverse' 
    }), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", [String]), 
    __metadata("design:returntype", Promise) 
], SecureStorageObject.prototype, "remove", null); 
__decorate([ 
    CordovaInstance({ 
     callbackOrder: 'reverse' 
    }), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", []), 
    __metadata("design:returntype", Promise) 
], SecureStorageObject.prototype, "keys", null); 
__decorate([ 
    CordovaInstance({ 
     callbackOrder: 'reverse' 
    }), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", []), 
    __metadata("design:returntype", Promise) 
], SecureStorageObject.prototype, "clear", null); 
__decorate([ 
    CordovaInstance(), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", []), 
    __metadata("design:returntype", Promise) 
], SecureDeviceObject.prototype, "secureDevice", null); 
/** 
* @name Secure Storage 
* @description 
* This plugin gets, sets and removes key,value pairs from a device's secure storage. 
* 
* Requires Cordova plugin: `cordova-plugin-secure-storage`. For more info, please see the [Cordova Secure Storage docs](https://github.com/Crypho/cordova-plugin-secure-storage). 
* 
* The browser platform is supported as a mock only. Key/values are stored unencrypted in localStorage. 
* 
* @usage 
* 
* ```typescript 
* import { SecureStorage, SecureStorageObject } from '@ionic-native/secure-storage'; 
* 
* constructor(private secureStorage: SecureStorage) { } 
* 
* ... 
* 
* this.secureStorage.create('my_store_name') 
* .then((storage: SecureStorageObject) => { 
* 
*  storage.get('key') 
*  .then(
*   data => console.log(data), 
*   error => console.log(error) 
*  ); 
* 
*  storage.set('key', 'value') 
*  .then(
*   data => console.log(data), 
*   error => console.log(error) 
*  ); 
* 
*  storage.remove('key') 
*  .then(
*   data => console.log(data), 
*   error => console.log(error) 
*  ); 
* 
* }); 
* 
* 
* ``` 
* @classes 
* SecureStorageObject 
*/ 
var SecureStorage = SecureStorage_1 = (function (_super) { 
    __extends(SecureStorage, _super); 
    function SecureStorage() { 
     return _super !== null && _super.apply(this, arguments) || this; 
    } 
    /** 
    * Creates a namespaced storage. 
    * @param store {string} 
    * @returns {Promise<SecureStorageObject>} 
    */ 
    SecureStorage.prototype.create = function (store) { 
     return new Promise(function (res, rej) { 
      var instance = new (SecureStorage_1.getPlugin())(
       function() { 
        res(new SecureStorageObject(instance)); 
       }, 
       function() { 
        rej(new SecureDeviceObject(instance)); 
       }, 
       store); 
     }); 
    }; 
    return SecureStorage; 
}(IonicNativePlugin)); 
SecureStorage.decorators = [ 
    { type: Injectable }, 
]; 
/** @nocollapse */ 
SecureStorage.ctorParameters = function() { return []; }; 
__decorate([ 
    CordovaCheck(), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", [String]), 
    __metadata("design:returntype", Promise) 
], SecureStorage.prototype, "create", null); 
SecureStorage = SecureStorage_1 = __decorate([ 
    Plugin({ 
     pluginName: 'SecureStorage', 
     plugin: 'cordova-plugin-secure-storage', 
     pluginRef: 'cordova.plugins.SecureStorage', 
     repo: 'https://github.com/Crypho/cordova-plugin-secure-storage', 
     platforms: ['Android', 'Browser', 'iOS', 'Windows'] 
    }) 
], SecureStorage); 
export { SecureStorage }; 
var SecureStorage_1; 
//# sourceMappingURL=index.js.map 

次に使用できるようになります:

private createSecureStorage() { 
     this.secureStorageAPI.create(this.storeName).then( 
      (storage: SecureStorageObject) => { 
       console.log("secure"); 
       this.secureStorage = storage; 
     }).catch( 
      (secureDeviceObject) => { 
       this.dialogs.alert('Please enable the screen lock on your device. This app cannot operate securely without it.').then( 
        () => { 
         // Alert Dismissed, should open the secure lockscreen settings here 
         secureDeviceObject.secureDevice().then( 
         () => { 
          // Try again 
          console.log("Success"); 
          this.createSecureStorage(); 
         }).catch(() => { 
          // Try again 
          console.log(" Error ") 
          this.createSecureStorage(); 
         }) 
        }); 
      }); 
    } 

変更された機能

私がやったことは、secureDevice関数をSecureDeviceObjectという新しいオブジェクトに移動し、デコレータを変更したことです。これにより、あなたは等を取得および設定機能を呼び出そうとするために、このオブジェクトを使用することはできません

これは、新しいオブジェクトです:

__decorate([ 
    CordovaInstance(), 
    __metadata("design:type", Function), 
    __metadata("design:paramtypes", []), 
    __metadata("design:returntype", Promise) 
], SecureDeviceObject.prototype, "secureDevice", null); 

var SecureDeviceObject = (function() { 
    function SecureDeviceObject(_objectInstance) { 
     this._objectInstance = _objectInstance; 
    } 

    /** 
     * Brings up the screen-lock settings 
     * @returns {Promise<any>} 
     */ 
    SecureStorageObject.prototype.secureDevice = function() { return; }; 
    return SecureDeviceObject; 
}()); 

は、私はdecoraterを変更しました

最後の変更がsecureDeviceObjectを返す約束を拒否作っている:

SecureStorage.prototype.create = function (store) { 
     return new Promise(function (res, rej) { 
      var instance = new (SecureStorage_1.getPlugin())(
       function() { 
        res(new SecureStorageObject(instance)); 
       }, 
       function() { 
        rej(new SecureDeviceObject(instance)); 
       }, 
       store); 
     }); 
    }; 

私はこれを仮定最高の可能な修正ではありませんが、それは私が行うことができる最高でした:D アンドロイド4〜8でテスト済みです。これらのすべてに取り組む!

はそれが正しい方向に私を指しているため、誰か:)

おかげ@JudgeFudgeを役に立てば幸い

+0

こんにちは、良い仕事、コードでこのバグを閉じてください。 :) https://github.com/ionic-team/ionic-native/issues/1625 – Servuc

2

この問題はすでに問題として追跡されています。https://github.com/ionic-team/ionic-native/issues/1944を参照してください。

あなたは、このための迅速な解決が必要な場合は、次のいずれかの手順を試すことができます。

1)多分、この問題は、以前のバージョンでは発生しません、イオンSecureStorageプラグインをダウングレードします。

2)自分で問題を解決してください。あなたはここにnode_modulesフォルダ内のソースを見つけることができます(それについて助けが必要な場合は、後で見てみることができます)。

node_modules \ cordova-plugin-secure-storage \ src \ android \ SecureStorage .java node_modules \ cordova-plugin-secure-storage \ src \ ios \ SecureStorage.m

。それはなっているはずですコード

作業

index.jsイオン-ネイティブ\セキュアストレージ\ @

をnode_modules \:これは今変更を作業する必要があるすべての人のために

+0

Thnx!私はその問題を見つけることができませんでした。私はプラグインを自分で修正しようとします。私は何か質問があるなら、私はあなたに知らせるでしょう。 – Juxture

+0

いくつかの調査の後、私は問題を解決することができました。私はいくつかの広範なテストを行い、ここにコードを投稿します。 – Juxture

関連する問題