-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from Netflix/simpleclient-modules
Add module support to SimpleClient
- Loading branch information
Showing
18 changed files
with
417 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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> | ||
* | ||
|
@@ -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) { | ||
|
@@ -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 }, | ||
|
@@ -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); | ||
|
@@ -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> | ||
*/ | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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) | ||
|
@@ -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) { | ||
|
@@ -205,7 +225,7 @@ var SimpleClient$create; | |
}); | ||
}, self); | ||
}, | ||
|
||
/** | ||
* <p>Cancel any outstanding request. This method does nothing if there | ||
* is no such request.</p> | ||
|
@@ -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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.