Skip to content

Commit

Permalink
Merge 2a4f9a5 into d00c5a5
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjoyce3 authored Oct 25, 2019
2 parents d00c5a5 + 2a4f9a5 commit d123d51
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 20 deletions.
14 changes: 7 additions & 7 deletions dist/appid.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions dist/appid.umd.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class RequestHandler {
if (!response.ok || response.status > 300) {
throw new RequestError(`Failed to fetch ${url}. Response=${text}`, response.status);
}

try {
return JSON.parse(text);
} catch(err) {
if (text !== '') {
return text;
}
throw new RequestError(`Invalid response while trying to fetch ${url}. Response=${text}`, response.status, err);
}
};
Expand Down
5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module.exports = {
NOT_CD_USER: 'Must be a Cloud Directory user',
MISSING_CLIENT_ID: 'Missing client ID',
MISSING_ID_TOKEN_PAYLOAD: 'Missing id token payload',
MISSING_TOKENS: 'Missing tokens object',
MISSING_ACCESS_TOKEN: 'Missing access token string',
MISSING_ID_TOKEN: 'Missing access 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 All @@ -29,6 +32,8 @@ module.exports = {
TOKEN_ALG: 'RS256',
VERSION: 4,
CHANGE_PASSWORD: '/cloud_directory/change_password',
GENERATE_CODE: '/cloud_directory/generate_code',
CHANGE_DETAILS: '/cloud_directory/change_details',
OAUTH: '/oauth/v4/',
MALFORMED_ACCESS_TOKEN: 'eyJpc3MiOiJtb2JpbGVjbGllbnRhY2Nlc3Muc3RhZ2UxLm5nLmJsdWVtaXgubmV0IiwiZXhwIjoxNDg3MDg0ODc4LCJhdWQiO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additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ class AppID {
this.URL = url;
this.utils = utils;
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 @@ -177,13 +182,13 @@ class AppID {
this._validateInitalize();
let userId;

if (!idTokenPayload){
if (!idTokenPayload) {
throw new AppIDError(constants.MISSING_ID_TOKEN_PAYLOAD);
}
if (typeof idTokenPayload === 'string') {
throw new AppIDError(constants.INVALID_ID_TOKEN_PAYLOAD);
}
if(idTokenPayload.identities && idTokenPayload.identities[0] && idTokenPayload.identities[0].id) {
if (idTokenPayload.identities && idTokenPayload.identities[0] && idTokenPayload.identities[0].id) {
if (idTokenPayload.identities[0].provider !== 'cloud_directory') {
throw new AppIDError(constants.NOT_CD_USER);
}
Expand All @@ -201,6 +206,47 @@ class AppID {
});
}

/**
* This method will open a popup to the change details widget for Cloud Directory users.
* You must enable users to manage their account from your app in Cloud Directory settings.
* @param {Object} tokens App ID tokens
* @returns {Promise<Tokens>}
* @throws {AppIDError} "Missing id token string"
* @throws {AppIDError} "Missing access token string"
* @throws {AppIDError} "Missing tokens object"
* @example
* let tokens = {accessToken, idToken}
* let newTokens = await appID.changeDetails(tokens);
*/
async changeDetails(tokens) {
if (!tokens) {
throw new AppIDError(constants.MISSING_TOKENS);
}

if (!tokens.accessToken && typeof tokens.accessToken !== 'string') {
throw new AppIDError(constants.MISSING_ACCESS_TOKEN);
}

if (!tokens.idToken && typeof tokens.idToken !== 'string') {
throw new AppIDError(constants.MISSING_ID_TOKEN);
}

const generateCodeUrl = this.openIdConfigResource.getIssuer() + constants.GENERATE_CODE;
const changeDetailsCode = await this.request(generateCodeUrl, {
headers: {
'Authorization': 'Bearer ' + tokens.accessToken + ' ' + tokens.idToken
}
});
const endpoint = this.openIdConfigResource.getIssuer() + constants.CHANGE_DETAILS;

return this.utils.performOAuthFlowAndGetTokens({
origin: this.window.origin,
clientId: this.clientId,
endpoint,
changeDetailsCode
});
}

/**
*
* @private
Expand Down
10 changes: 7 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Utils {
return {codeVerifier, codeChallenge, state, nonce};
}

getAuthParamsAndUrl({clientId, origin, prompt, endpoint, userId}) {
getAuthParamsAndUrl({clientId, origin, prompt, endpoint, userId, changeDetailsCode}) {
const {codeVerifier, codeChallenge, state, nonce} = this.getPKCEFields();
let authParams = {
client_id: clientId,
Expand All @@ -67,6 +67,10 @@ class Utils {
authParams.user_id = userId;
}

if (changeDetailsCode) {
authParams.code = changeDetailsCode;
}

const url = endpoint + '?' + this.buildParams(authParams);
return {
codeVerifier,
Expand All @@ -76,8 +80,8 @@ class Utils {
};
}

async performOAuthFlowAndGetTokens({userId, origin, clientId, endpoint}) {
const {codeVerifier, state, nonce, url} = this.getAuthParamsAndUrl({userId, origin, clientId, endpoint});
async performOAuthFlowAndGetTokens({userId, origin, clientId, endpoint, changeDetailsCode}) {
const {codeVerifier, state, nonce, url} = this.getAuthParamsAndUrl({userId, origin, clientId, endpoint, changeDetailsCode});

this.popup.open();
this.popup.navigate(url);
Expand Down

0 comments on commit d123d51

Please sign in to comment.