Skip to content

Commit

Permalink
Merge 531f89b into d00c5a5
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjoyce3 committed Oct 28, 2019
2 parents d00c5a5 + 531f89b commit 071dbc8
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 94 deletions.
16 changes: 8 additions & 8 deletions dist/appid.min.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions dist/appid.umd.min.js

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions docs/AppID.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h4 class="name" id="AppID"><span class="type-signature"></span>new AppID<span c

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line19">line 19</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line20">line 20</a>
</li></ul></dd>


Expand Down Expand Up @@ -188,7 +188,7 @@ <h4 class="name" id="init"><span class="type-signature">(async) </span>init<span

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line61">line 61</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line69">line 69</a>
</li></ul></dd>


Expand Down Expand Up @@ -601,7 +601,7 @@ <h4 class="name" id="signin"><span class="type-signature">(async) </span>signin<

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line96">line 96</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line104">line 104</a>
</li></ul></dd>


Expand Down Expand Up @@ -826,7 +826,7 @@ <h4 class="name" id="silentSignin"><span class="type-signature">(async) </span>s

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line115">line 115</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line123">line 123</a>
</li></ul></dd>


Expand Down Expand Up @@ -1053,7 +1053,7 @@ <h4 class="name" id="getUserInfo"><span class="type-signature">(async) </span>ge

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line153">line 153</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line161">line 161</a>
</li></ul></dd>


Expand Down Expand Up @@ -1255,7 +1255,7 @@ <h5>Returns:</h5>



<h4 class="name" id="changePassword"><span class="type-signature">(async) </span>changePassword<span class="signature">(idTokenPayload)</span><span class="type-signature"> &rarr; {Promise.&lt;<a href="global.html#Tokens">Tokens</a>>}</span></h4>
<h4 class="name" id="changePassword"><span class="type-signature">(async) </span>changePassword<span class="signature">(idToken)</span><span class="type-signature"> &rarr; {Promise.&lt;<a href="global.html#Tokens">Tokens</a>>}</span></h4>



Expand All @@ -1267,7 +1267,7 @@ <h4 class="name" id="changePassword"><span class="type-signature">(async) </span

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line176">line 176</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line185">line 185</a>
</li></ul></dd>


Expand Down Expand Up @@ -1307,7 +1307,8 @@ <h4 class="name" id="changePassword"><span class="type-signature">(async) </span


<div class="description">
<p>This method will open a popup to the change password widget for Cloud Directory users.</p>
<p>This method will open a popup to the change password widget for Cloud Directory users.<br>
You must enable users to manage their account from your app in Cloud Directory settings.</p>
</div>


Expand All @@ -1320,7 +1321,7 @@ <h4 class="name" id="changePassword"><span class="type-signature">(async) </span

<h5>Example</h5>

<pre class="prettyprint"><code>let tokens = await appID.changePassword(idTokenPayload);</code></pre>
<pre class="prettyprint"><code>let tokens = await appID.changePassword(idToken);</code></pre>



Expand Down Expand Up @@ -1350,7 +1351,7 @@ <h5>Parameters:</h5>

<tr>

<td class="name"><code>idTokenPayload</code></td>
<td class="name"><code>idToken</code></td>


<td class="type">
Expand All @@ -1366,7 +1367,7 @@ <h5>Parameters:</h5>



<td class="description last"><p>The id token payload.</p></td>
<td class="description last"><p>A JWT.</p></td>
</tr>


Expand Down Expand Up @@ -1447,7 +1448,7 @@ <h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<p>&quot;Missing id token payload&quot;</p>
<p>&quot;Missing id token string&quot;</p>
</div>
</dt>
<dd></dd>
Expand Down Expand Up @@ -1516,7 +1517,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Thu Oct 24 2019 14:14:28 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Mon Oct 28 2019 11:08:24 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
4 changes: 2 additions & 2 deletions docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ <h4 class="name" id="Tokens">Tokens</h4>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line77">line 77</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line85">line 85</a>
</li></ul></dd>


Expand Down Expand Up @@ -329,7 +329,7 @@ <h5>Type:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Thu Oct 24 2019 14:14:28 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Mon Oct 28 2019 11:08:24 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ <h3> </h3>

<section class="readme">
<article><p><a href="https://www.codacy.com/app/kajabfab/appid-clientsdk-js?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=ibm-cloud-security/appid-clientsdk-js&amp;utm_campaign=Badge_Grade"><img src="https://api.codacy.com/project/badge/Grade/a30e7499a5234d3494508b7050975beb" alt="Codacy Badge"></a></p>
<h1 id="ibm-cloud-app-id-js-client-sdk">IBM Cloud App ID JS Client SDK</h1>
<h1 id="ibm-cloud-app-id-javascript-sdk">IBM Cloud App ID JavaScript SDK</h1>
<p>Client-side javascript SDK for the IBM Cloud App ID service to use with single-page applications created in Angular, React, or other frontend frameworks.<br>
Read the <a href="https://cloud.ibm.com/docs/services/appid?topic=appid-single-page">documentation</a> for information on getting started with IBM Cloud App ID and single-page applications.<br>
Apps using Node.js should use the our <a href="https://github.com/ibm-cloud-security/appid-serversdk-nodejs">server-side SDK</a>.</p>
Apps using Node.js should use our <a href="https://github.com/ibm-cloud-security/appid-serversdk-nodejs">server-side SDK</a>.</p>
<h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li>
Expand Down Expand Up @@ -124,7 +124,7 @@ <h2 id="api-reference">API Reference</h2>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Thu Oct 24 2019 14:14:28 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Mon Oct 28 2019 11:08:24 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
49 changes: 32 additions & 17 deletions docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ <h1 class="page-title">index.js</h1>
const RequestHandler = require('./RequestHandler');
const PopupController = require('./PopupController');
const IFrameController = require('./IFrameController');
const OpenIdConfigurationResource = require('./OpenIDConfigurationResource');
const OpenIdConfigurationResource = require('./OpenIDConfigurationResource')
const TokenValidator = require('./TokenValidator');
const constants = require('./constants');
const AppIDError = require('./errors/AppIDError');
const jsrsasign = require('jsrsasign');
Expand All @@ -67,6 +68,7 @@ <h1 class="page-title">index.js</h1>
openIdConfigResource = new OpenIdConfigurationResource(),
utils,
requestHandler = new RequestHandler(),
tokenValidator = new TokenValidator(),
w = window,
url = URL
} = {}) {
Expand All @@ -76,8 +78,14 @@ <h1 class="page-title">index.js</h1>
this.openIdConfigResource = openIdConfigResource;
this.URL = url;
this.utils = utils;
this.tokenValidator = tokenValidator;
if (!utils) {
this.utils = new Utils({openIdConfigResource: this.openIdConfigResource, url: this.URL, popup: this.popup, jsrsasign});
this.utils = new Utils({
openIdConfigResource: this.openIdConfigResource,
url: this.URL,
popup: this.popup,
jsrsasign
});
}
this.request = requestHandler.request;
this.window = w;
Expand Down Expand Up @@ -209,31 +217,38 @@ <h1 class="page-title">index.js</h1>

/**
* This method will open a popup to the change password widget for Cloud Directory users.
* @param {string} idTokenPayload The id token payload.
* You must enable users to manage their account from your app in Cloud Directory settings.
* @param {string} idToken A JWT.
* @returns {Promise&lt;Tokens>} The tokens of the authenticated user.
* @throws {AppIDError} "Expect id token payload object to have identities field"
* @throws {AppIDError} "Must be a Cloud Directory user"
* @throws {AppIDError} "Missing id token payload"
* @throws {AppIDError} "Missing id token string"
* @example
* let tokens = await appID.changePassword(idTokenPayload);
* let tokens = await appID.changePassword(idToken);
*/
async changePassword(idTokenPayload) {
async changePassword(idToken) {
this._validateInitalize();
let userId;

if (!idTokenPayload){
throw new AppIDError(constants.MISSING_ID_TOKEN_PAYLOAD);
}
if (typeof idTokenPayload === 'string') {
throw new AppIDError(constants.INVALID_ID_TOKEN_PAYLOAD);
if (!idToken || typeof idToken !== 'string') {
throw new AppIDError(constants.MISSING_ID_TOKEN);
}
if(idTokenPayload.identities &amp;&amp; idTokenPayload.identities[0] &amp;&amp; idTokenPayload.identities[0].id) {
if (idTokenPayload.identities[0].provider !== 'cloud_directory') {

let userId;
const publicKeys = await this.openIdConfigResource.getPublicKeys();
let decodedToken = this.tokenValidator.decodeAndValidate({
token: idToken,
publicKeys,
issuer: this.openIdConfigResource.getIssuer(),
clientId: this.clientId
});

if (decodedToken.identities &amp;&amp; decodedToken.identities[0] &amp;&amp; decodedToken.identities[0].id) {
if (decodedToken.identities[0].provider !== 'cloud_directory') {
throw new AppIDError(constants.NOT_CD_USER);
}
userId = idTokenPayload.identities[0].id;
userId = decodedToken.identities[0].id;
} else {
throw new AppIDError(constants.INVALID_ID_TOKEN_PAYLOAD);
throw new AppIDError(constants.INVALID_ID_TOKEN);
}

const endpoint = this.openIdConfigResource.getIssuer() + constants.CHANGE_PASSWORD;
Expand Down Expand Up @@ -271,7 +286,7 @@ <h1 class="page-title">index.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Thu Oct 24 2019 14:14:28 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Mon Oct 28 2019 11:08:24 GMT-0500 (CDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion src/TokenValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TokenValidator {
throw new TokenError(constants.INVALID_AUDIENCE);
}

if (decoded.payloadObj.nonce && decoded.payloadObj.nonce !== nonce) {
if ((nonce && !decoded.payloadObj.nonce) || (decoded.payloadObj.nonce !== nonce)) {
throw new TokenError(constants.INVALID_NONCE);
}

Expand Down
4 changes: 2 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module.exports = {
INVALID_TOKEN: 'Invalid token',
MISSING_PUBLIC_KEY: 'Cannot find public key',
INVALID_ACCESS_TOKEN: 'Access token must be a string',
INVALID_ID_TOKEN_PAYLOAD: 'Expect id token payload object to have identities field',
INVALID_ID_TOKEN: 'Invalid id token',
NOT_CD_USER: 'Must be a Cloud Directory user',
MISSING_CLIENT_ID: 'Missing client ID',
MISSING_ID_TOKEN_PAYLOAD: 'Missing id token payload',
MISSING_ID_TOKEN: 'Missing id token string',
INVALID_DISCOVERY_ENDPOINT: 'Invalid discovery endpoint',
FAIL_TO_INITIALIZE: 'You must call and wait for init() before calling this function.',
RESPONSE_TYPE: 'code',
Expand Down
49 changes: 32 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const Utils = require('./utils');
const RequestHandler = require('./RequestHandler');
const PopupController = require('./PopupController');
const IFrameController = require('./IFrameController');
const OpenIdConfigurationResource = require('./OpenIDConfigurationResource');
const OpenIdConfigurationResource = require('./OpenIDConfigurationResource')
const TokenValidator = require('./TokenValidator');
const constants = require('./constants');
const AppIDError = require('./errors/AppIDError');
const jsrsasign = require('jsrsasign');
Expand All @@ -23,6 +24,7 @@ class AppID {
openIdConfigResource = new OpenIdConfigurationResource(),
utils,
requestHandler = new RequestHandler(),
tokenValidator = new TokenValidator(),
w = window,
url = URL
} = {}) {
Expand All @@ -32,8 +34,14 @@ class AppID {
this.openIdConfigResource = openIdConfigResource;
this.URL = url;
this.utils = utils;
this.tokenValidator = tokenValidator;
if (!utils) {
this.utils = new Utils({openIdConfigResource: this.openIdConfigResource, url: this.URL, popup: this.popup, jsrsasign});
this.utils = new Utils({
openIdConfigResource: this.openIdConfigResource,
url: this.URL,
popup: this.popup,
jsrsasign
});
}
this.request = requestHandler.request;
this.window = w;
Expand Down Expand Up @@ -165,35 +173,42 @@ class AppID {

/**
* This method will open a popup to the change password widget for Cloud Directory users.
* @param {string} idTokenPayload The id token payload.
* You must enable users to manage their account from your app in Cloud Directory settings.
* @param {string} idToken A JWT.
* @returns {Promise<Tokens>} The tokens of the authenticated user.
* @throws {AppIDError} "Expect id token payload object to have identities field"
* @throws {AppIDError} "Must be a Cloud Directory user"
* @throws {AppIDError} "Missing id token payload"
* @throws {AppIDError} "Missing id token string"
* @example
* let tokens = await appID.changePassword(idTokenPayload);
* let tokens = await appID.changePassword(idToken);
*/
async changePassword(idTokenPayload) {
async changePassword(idToken) {
this._validateInitalize();
let userId;

if (!idTokenPayload){
throw new AppIDError(constants.MISSING_ID_TOKEN_PAYLOAD);
}
if (typeof idTokenPayload === 'string') {
throw new AppIDError(constants.INVALID_ID_TOKEN_PAYLOAD);
if (!idToken || typeof idToken !== 'string') {
throw new AppIDError(constants.MISSING_ID_TOKEN);
}
if(idTokenPayload.identities && idTokenPayload.identities[0] && idTokenPayload.identities[0].id) {
if (idTokenPayload.identities[0].provider !== 'cloud_directory') {

let userId;
const publicKeys = await this.openIdConfigResource.getPublicKeys();
let decodedToken = this.tokenValidator.decodeAndValidate({
token: idToken,
publicKeys,
issuer: this.openIdConfigResource.getIssuer(),
clientId: this.clientId
});

if (decodedToken.identities && decodedToken.identities[0] && decodedToken.identities[0].id) {
if (decodedToken.identities[0].provider !== 'cloud_directory') {
throw new AppIDError(constants.NOT_CD_USER);
}
userId = idTokenPayload.identities[0].id;
userId = decodedToken.identities[0].id;
} else {
throw new AppIDError(constants.INVALID_ID_TOKEN_PAYLOAD);
throw new AppIDError(constants.INVALID_ID_TOKEN);
}

const endpoint = this.openIdConfigResource.getIssuer() + constants.CHANGE_PASSWORD;
return this.utils.performOAuthFlowAndGetTokens({
return await this.utils.performOAuthFlowAndGetTokens({
userId,
origin: this.window.origin,
clientId: this.clientId,
Expand Down
Loading

0 comments on commit 071dbc8

Please sign in to comment.