Skip to content

Commit

Permalink
Merge de433f6 into f296874
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjoyce3 authored Oct 28, 2019
2 parents f296874 + de433f6 commit 1aa7e1b
Show file tree
Hide file tree
Showing 7 changed files with 738 additions and 107 deletions.
385 changes: 335 additions & 50 deletions dist/appid.min.js

Large diffs are not rendered by default.

395 changes: 345 additions & 50 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: 3 additions & 2 deletions src/TokenValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TokenValidator {
const publicKey = this.getPublicKey(publicKeys.keys, kid);

const myKey = this.jwt.KEYUTIL.getKey(publicKey);
const isValid = this.jwt.KJUR.jws.JWS.verify(token, myKey, {alg:[constants.TOKEN_ALG]});
const isValid = this.jwt.KJUR.jws.JWS.verify(token, myKey, {alg: [constants.TOKEN_ALG]});
if (!isValid) {
throw new TokenError(constants.INVALID_SIGNATURE);
}
Expand Down Expand Up @@ -56,7 +56,7 @@ class TokenValidator {

getPublicKey(keys, kid) {
let publicKey;
for(let i = 0; i < keys.length; i++) {
for (let i = 0; i < keys.length; i++) {
if (keys[i].kid === kid) {
publicKey = keys[i];
}
Expand All @@ -68,4 +68,5 @@ class TokenValidator {
return publicKey;
}
}

module.exports = TokenValidator;
6 changes: 6 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = {
NOT_CD_USER: 'Must be a Cloud Directory user',
MISSING_CLIENT_ID: 'Missing client ID',
MISSING_ID_TOKEN: 'Missing id token string',
MISSING_ID_TOKEN_PAYLOAD: 'Missing id token payload',
MISSING_TOKENS: 'Missing tokens',
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 +33,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: 'eyJpc3MiOiJtb2JpbGVjbGllbnRhY2Nlc3Muc3RhZ2UxLm5nLmJsdWVtaXgubmV0IiwiZXhwIjoxNDg3MDg0ODc4LCJhdWQiOiIyNmNiMDEyZWIzMjdjNjEyZDkwYTY4MTkxNjNiNmJjYmQ0ODQ5Y2JiIiwiaWF0IjoxNDg3MDgxMjc4LCJhdXRoX2J5IjoiZmFjZWJvb2siLCJ0ZW5hbnQiOiI0ZGJhOTQzMC01NGU2LTRjZjItYTUxNi02ZjczZmViNzAyYmIiLCJzY29wZSI6ImFwcGlkX2RlZmF1bHQgYXBwaWRfcmVhZHByb2ZpbGUgYXBwaWRfcmVhZHVzZXJhdHRyIGFwcGlkX3dyaXRldXNlcmF0dHIifQ.HHterec250JSDY1965cM2DadBznl2wTKmzKNSnfjpdTAqax9VZvV3EwuFbEnGp9-i6AC-OlsVj7xvbALkdjwG2lZvpQx0M_gRc_3E0NiYuOGVolcm0wEXtbtDUFFqZQAf9BYYOPZ8OintdBiwUGETbH1ZRVtUvt3nalIko1OPE1Q12LvuRlhz5MClNHmvxJcXc7kucxCx4s4UFFy_HJA1gow7HWFqc9-PZf4JMWA-siYqPrdw_zYeBTBzE5co92F6JBEtGLLCjhJVz9eYgLLECXbak3z6hOaY9352Weuj7AgMOWxzw56jKKsiixMtvzrCzLVIcRUG96UJszwPHtPlA',
INVALID_SIGNATURE_TOKEN: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MTU2NjMxMDg5NSwiZXhwIjoxNTY2MzE0NDk1LCJpYXQiOjE1NjYzMTA4OTUsImp0aSI6ImlkMTIzNDU2IiwidHlwIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9yZWdpc3RlciIsImF1ZCI6WyJodHRwOi8vZm9vMS5jb20iLCJodHRwOi8vZm9vMi5jb20iXX0.NBDYg8VZxW770qcbIl40OFvaQntO-FVYUUJocE5ZgcyrvFTuomwF997OcEJghO8BE_Y2m67PbfPFsbN0VI6j'
Expand Down
37 changes: 37 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,43 @@ 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({accessToken, idToken}) {
if (!accessToken && typeof accessToken !== 'string') {
throw new AppIDError(constants.MISSING_ACCESS_TOKEN);
}

if (!idToken && typeof 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 ' + accessToken + ' ' + 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
13 changes: 8 additions & 5 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 Expand Up @@ -135,8 +139,7 @@ class Utils {
token: tokens.access_token,
publicKeys,
issuer,
clientId,
nonce
clientId
});

const idTokenPayload = this.tokenValidator.decodeAndValidate({
Expand Down

0 comments on commit 1aa7e1b

Please sign in to comment.