Skip to content

Commit

Permalink
Merge pull request #246 from Netflix/simpleclient-modules
Browse files Browse the repository at this point in the history
Add module support to SimpleClient
  • Loading branch information
wmiaw authored Feb 7, 2018
2 parents a02abfd + 662c1e5 commit e98fdb7
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 299 deletions.
36 changes: 28 additions & 8 deletions examples/simple/src/main/javascript/client/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
* limitations under the License.
*/

var KeyFormat = require('msl-core/crypto/KeyFormat.js');
var MslConstants = require('msl-core/MslConstants.js');
var PublicKey = require('msl-core/crypto/PublicKey.js');
var TextEncoding = require('msl-core/util/TextEncoding.js');
var WebCryptoAlgorithm = require('msl-core/crypto/WebCryptoAlgorithm.js');
var WebCryptoUsage = require('msl-core/crypto/WebCryptoUsage.js');

var AdvancedRequest = require('./msg/AdvancedRequest.js');
var SimpleClient$create = require('./SimpleClient.js').create;
var SimpleConstants = require('./SimpleConstants.js');
var SimpleEchoRequest = require('./msg/SimpleEchoRequest.js');
var SimpleFilterStreamFactory = require('./msg/SimpleFilterStreamFactory.js');
var SimpleLogRequest = require('./msg/SimpleLogRequest.js');
var SimpleMessageDebugContext = require('./msg/SimpleMessageDebugContext.js');
var SimpleProfileRequest = require('./msg/SimpleProfileRequest.js');
var SimpleQueryRequest = require('./msg/SimpleQueryRequest.js');
var SimpleQuitRequest = require('./msg/SimpleQuitRequest.js');

function getQueryParam(key, defaultValue) {
var queryString = window.location.search;
var regex = new RegExp(key + '=([^&?]*)');
Expand Down Expand Up @@ -174,9 +192,11 @@ function logoutUser() {
}

function setRequestType(type) {
var i;

// Unselect the request tabs.
var tabs = document.getElementsByClassName('tab');
for (var i = 0; i < tabs.length; ++i)
for (i = 0; i < tabs.length; ++i)
tabs[i].className = "";

// Select the request tab.
Expand All @@ -185,19 +205,19 @@ function setRequestType(type) {

// Disable all fields.
var fieldElements = document.getElementsByClassName('field');
for (var i = 0; i < fieldElements.length; ++i) {
for (i = 0; i < fieldElements.length; ++i) {
var field = fieldElements[i];
field.style.visibility = 'hidden';
field.style.position = 'absolute';
};
}

// Enable the selected field.
var enabledElements = document.getElementsByClassName(type);
for (var i = 0; i < enabledElements.length; ++i) {
var field = enabledElements[i];
field.style.position = 'relative';
field.style.visibility = 'visible';
};
for (i = 0; i < enabledElements.length; ++i) {
var enabled = enabledElements[i];
enabled.style.position = 'relative';
enabled.style.visibility = 'visible';
}

// Set the request type.
var typeField = document.getElementById('type');
Expand Down
75 changes: 49 additions & 26 deletions examples/simple/src/main/javascript/client/SimpleClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Copyright (c) 2014-2017 Netflix, Inc. All rights reserved.
*
* Copyright (c) 2014-2018 Netflix, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -13,19 +13,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var SimpleClient;
var SimpleClient$create;

(function() {
(function(require, module) {
"use strict";

var Class = require('msl-core/util/Class.js');
var AsyncExecutor = require('msl-core/util/AsyncExecutor.js');
var EmailPasswordAuthenticationData = require('msl-core/userauth/EmailPasswordAuthenticationData.js');
var KeyFormat = require('msl-core/crypto/KeyFormat.js');
var MslCrypto = require('msl-core/crypto/MslCrypto.js');
var MslIoException = require('msl-core/MslIoException.js');
var Url = require('msl-core/io/Url.js');
var WebCryptoAlgorithm = require('msl-core/crypto/WebCryptoAlgorithm.js');
var WebCryptoUsage = require('msl-core/crypto/WebCryptoUsage.js');
var Xhr = require('msl-core/io/Xhr.js');
var AsymmetricWrappedExchange = require('msl-core/keyx/AsymmetricWrappedExchange.js');
var MslControl = require('msl-core/msg/MslControl.js');
var PublicKey = require('msl-core/crypto/PublicKey.js');
var RsaStore = require('msl-core/entityauth/RsaStore.js');

var SimpleKeyxManager = require('./keyx/SimpleKeyxManager.js');
var AdvancedRequestMessageContext = require('./msg/AdvancedRequestMessageContext.js');
var SimpleRequestMessageContext = require('./msg/SimpleRequestMessageContext.js');
var SimpleRequest = require('./msg/SimpleRequest.js');
var SimpleMslContext = require('./util/SimpleMslContext.js');
var SimpleConstants = require('./SimpleConstants.js');

/**
* <p>An example JavaScript MSL client that sends requests to the example
* Java MSL server.</p>
*
* @author Wesley Miaw <[email protected]>
*/
SimpleClient = Class.create({
var SimpleClient = module.exports = Class.create({
/**
* <p>Create a new client.</p>
*
Expand All @@ -38,7 +58,7 @@ var SimpleClient$create;
*/
init: function init(identity, factory, callback) {
var self = this;

// Import the server RSA public key.
PublicKey.import(SimpleConstants.RSA_PUBKEY_B64, WebCryptoAlgorithm.RSASSA_SHA256, WebCryptoUsage.VERIFY, KeyFormat.SPKI, {
result: function(publicKey) {
Expand All @@ -47,20 +67,20 @@ var SimpleClient$create;
var mechanism = (MslCrypto.getWebCryptoVersion() == MslCrypto.WebCryptoVersion.LEGACY)
? AsymmetricWrappedExchange.Mechanism.JWE_RSA
: AsymmetricWrappedExchange.Mechanism.JWK_RSA;
SimpleKeyxManager$create(mechanism, {
SimpleKeyxManager.create(mechanism, {
result: function(keyxMgr) {
AsyncExecutor(callback, function() {
// Create the RSA key store.
var rsaStore = new RsaStore();
rsaStore.addPublicKey(SimpleConstants.SERVER_ID, publicKey);

// Set up the MSL context.
var ctx = new SimpleMslContext(identity, rsaStore, keyxMgr, callback.error);

// Create the MSL control.
var ctrl = new MslControl();
ctrl.setFilterFactory(factory);

// Set properties.
var props = {
_keyxMgr: { value: keyxMgr, writable: false, enumerable: false, configurable: false },
Expand All @@ -71,7 +91,7 @@ var SimpleClient$create;
_cancelFunc: { value: null, writable: true, enumerable: false, configurable: false },
};
Object.defineProperties(this, props);

// Return the client.
return this;
}, self);
Expand All @@ -83,18 +103,18 @@ var SimpleClient$create;
error: callback.error,
});
},

/**
* <p>Add an RSA public key to the RSA key store.</p>
*
*
* @param {string} identity the remote entity's identity (i.e. RSA key
* pair identity).
* @param {PublicKey} key the RSA public key.
*/
addRsaPublicKey: function addRsaPublicKey(identity, key) {
this._rsaStore.addPublicKey(identity, key);
},

/**
* <p>Reset all state data.</p>
*/
Expand All @@ -104,12 +124,12 @@ var SimpleClient$create;
store.clearUserIdTokens();
store.clearServiceTokens();
},

/**
* <p>Set the entity identity. If the identity has not changed then
* this method does nothing. If the identity has changed then all data
* is reset and the new entity identity will be used.</p>
*
*
* @param {string} identity the new entity identity.
* @param {function(msgOrError)}
* callback the callback that will any thrown exceptions.
Expand Down Expand Up @@ -143,10 +163,10 @@ var SimpleClient$create;
if (userIdToken)
store.removeUserIdToken(userIdToken);
},

/**
* <p>Send a request and receive the response.</p>
*
*
* @param {string} endpoint the HTTP endpoint to send the request to.
* @param {?string} username username or {@code null} if the request is
* not associated with a user.
Expand All @@ -162,7 +182,7 @@ var SimpleClient$create;
*/
send: function send(endpoint, username, password, request, dbgCtx, callback) {
var self = this;

AsyncExecutor(callback, function() {
// Build the message context.
var userAuthData = (username && password)
Expand All @@ -174,19 +194,19 @@ var SimpleClient$create;
else
callback.error(msgOrError);
};

// Simple or advanced request?
var msgCtx;
if (request instanceof SimpleRequest) {
msgCtx = new SimpleRequestMessageContext(username, userAuthData, request, this._keyxMgr, dbgCtx, errorCallback);
} else {
msgCtx = new AdvancedRequestMessageContext(username, userAuthData, request, this._keyxMgr, dbgCtx, errorCallback);
}

// Create the URL instance.
var xhr = new Xhr(endpoint);
var url = new Url(xhr, SimpleConstants.TIMEOUT_MS);

// Send the request.
this._cancelFunc = this._ctrl.request(this._ctx, msgCtx, url, SimpleConstants.TIMEOUT_MS, {
result: function(channel) {
Expand All @@ -205,7 +225,7 @@ var SimpleClient$create;
});
}, self);
},

/**
* <p>Cancel any outstanding request. This method does nothing if there
* is no such request.</p>
Expand All @@ -225,7 +245,10 @@ var SimpleClient$create;
* callback the callback that will receive the created client or
* any thrown exceptions.
*/
SimpleClient$create = function SimpleClient$create(identity, factory, callback) {
var SimpleClient$create = function SimpleClient$create(identity, factory, callback) {
new SimpleClient(identity, factory, callback);
};
})();

// Exports.
module.exports.create = SimpleClient$create;
})(require, (typeof module !== 'undefined') ? module : mkmodule('SimpleClient'));
88 changes: 45 additions & 43 deletions examples/simple/src/main/javascript/client/SimpleConstants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014 Netflix, Inc. All rights reserved.
* Copyright (c) 2014-2018 Netflix, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,52 +13,54 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var SimpleConstants = {
/** Default server port. */
SERVER_PORT: 8080,
/** MSL control timeout in milliseconds. */
TIMEOUT_MS: 120 * 1000,

/** Server entity ID. */
SERVER_ID: "SimpleMslServer",
/** Server 2048-bit RSA public key. */
RSA_PUBKEY_B64:
(function(require, module) {
var SimpleConstants = module.exports = {
/** Default server port. */
SERVER_PORT: 8080,
/** MSL control timeout in milliseconds. */
TIMEOUT_MS: 120 * 1000,

/** Server entity ID. */
SERVER_ID: "SimpleMslServer",
/** Server 2048-bit RSA public key. */
RSA_PUBKEY_B64:
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4feorj/FWQi8AhbvjK3G" +
"L31ct6N+Ad/3FwqNsa4vAsfPmilLRx0DWhkxRycetmQEAa+1THyNCzobIduQE3UY" +
"8NtdOiy1S3BtHDoiSNEITFPAs0l2OAZ2ZUv0KIr9sLWAznlXMclLOBXtLOQMCs2e" +
"Ey4MO1m9uLywwc2SuAfoZe+wjEIauyoQK/M5miA0fbaEn4H+3m5aiP3Lb1X5Ss4b" +
"4tuu0ENsO/ebgMx2ltZ4b9dkzA65DM6XxEC60jK1AW+/wvFb4+iPQqrA7mdiZWsp" +
"zqMRTaAUDHKJo2LFBc6N0/wuTsXczHx6TYz5b2hrI6N+O7EEuxirAaU+xU7XEqv2" +
"dQIDAQAB",


/** Client entity ID. */
CLIENT_ID: "SimpleMslClient",

/** Email/Password set. */
EMAIL_PASSWORDS: [
[ "kirito", "asuna" ],
[ "chie", "shuhei" ],
[ "hideki", "chi" ]
],
/** Server administrator. */
ADMIN_USERNAME: "kirito",

/**
* Query data: user, key.
*
* If the first value is not null, only the listed user has permission to
* access the data value.
*/
QUERY_DATA: [
[ null, "cat" ],
[ "chie", "alien" ],
[ "kirito", "sword" ],
[ null, "dog" ],
[ null, "bird" ],
[ null, "turtle" ],
[ null, "fish" ],
[ "chie", "bathhouse" ],
[ "hideki", "computer" ],
],
};


/** Client entity ID. */
CLIENT_ID: "SimpleMslClient",

/** Email/Password set. */
EMAIL_PASSWORDS: [
[ "kirito", "asuna" ],
[ "chie", "shuhei" ],
[ "hideki", "chi" ]
],
/** Server administrator. */
ADMIN_USERNAME: "kirito",

/**
* Query data: user, key.
*
* If the first value is not null, only the listed user has permission to
* access the data value.
*/
QUERY_DATA: [
[ null, "cat" ],
[ "chie", "alien" ],
[ "kirito", "sword" ],
[ null, "dog" ],
[ null, "bird" ],
[ null, "turtle" ],
[ null, "fish" ],
[ "chie", "bathhouse" ],
[ "hideki", "computer" ],
],
};
})(require, (typeof module !== 'undefined') ? module : mkmodule('SimpleConstants'));
Loading

0 comments on commit e98fdb7

Please sign in to comment.