Skip to content

Commit

Permalink
working fasten-sources-js import for use in test tool.
Browse files Browse the repository at this point in the history
make sure dist directory is not used.
  • Loading branch information
AnalogJ committed May 30, 2024
1 parent 803ad22 commit 6a792ad
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 76 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ build-js:
tygo generate

publish-js: build-js
tsc
npm publish
cd js && tsc
cd js && npm run pub

test:
go test ./...
13 changes: 6 additions & 7 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "@fastenhealth/fasten-sources-js",
"version": "0.6.0",
"version": "0.6.0-beta.1",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"prepub": "cp package.json dist",
"pub": "cd dist && npm publish",
"postpub": "cd dist && rm package.json"
},
"author": "Jason Kulatunga <[email protected]>",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {generateSourceAuthorizeUrl} from './connect/authorization-url'
export {LighthouseSourceMetadata} from './models/lighthouse'
export {LighthouseEndpointDefinition} from './models/lighthouse'
export {PatientAccessEndpoint, PatientAccessBrand, PatientAccessPortal} from './models/patient-access-brands'
export {SourceState} from './models/source-state'
export {uuidV4} from './utils/uuid'
87 changes: 21 additions & 66 deletions testutils/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ <h2 class="mb-0">

<script type="module">
import alpinejs from 'https://cdn.skypack.dev/[email protected]';
import * as Oauth from 'https://cdnjs.cloudflare.com/ajax/libs/oauth4webapi/2.0.6/index.js'
import {generateSourceAuthorizeUrl} from 'https://cdn.skypack.dev/@fastenhealth/fasten-sources-js';

alpinejs.data('pageData', pageData);
alpinejs.start();
Expand Down Expand Up @@ -466,7 +466,17 @@ <h2 class="mb-0">
lighthouseSource.brand_id = searchResult.brand_id
lighthouseSource.portal_id = searchResult.portal_id

return this.generateSourceAuthorizeUrl(lighthouseSource)
return generateSourceAuthorizeUrl(lighthouseSource)
.then(authorizeData => {
console.log(authorizeData)
localStorage.setItem(authorizeData.sourceState.state, JSON.stringify(authorizeData.sourceState))

//this might be a custom definition instead of a lighthouse retrieved definition
// we need to store this configuration in local storage so we can use it later
localStorage.setItem(`${authorizeData.sourceState.state}.customDefinition`, JSON.stringify(lighthouseSource))

return this.redirectWithOriginAndDestination(authorizeData.url.toString(), lighthouseSource);
})
})

// showSpinner()
Expand All @@ -477,55 +487,6 @@ <h2 class="mb-0">
// generateSourceAuthorizeUrl(source._id, response.data)
// });
},

async generateSourceAuthorizeUrl(lighthouseSource){
const state = this.uuidV4()
let sourceStateInfo = {}
sourceStateInfo.state = state
sourceStateInfo.endpoint_id = lighthouseSource.id
sourceStateInfo.portal_id = lighthouseSource.portal_id
sourceStateInfo.brand_id = lighthouseSource.brand_id

// generate the authorization url
const authorizationUrl = new URL(lighthouseSource.authorization_endpoint);
authorizationUrl.searchParams.set('redirect_uri', lighthouseSource.redirect_uri);
authorizationUrl.searchParams.set('response_type', lighthouseSource.response_types_supported[0]);
authorizationUrl.searchParams.set('response_mode', lighthouseSource.response_modes_supported[0]);
authorizationUrl.searchParams.set('state', state);
authorizationUrl.searchParams.set('client_id', lighthouseSource.client_id);
if(lighthouseSource.scopes_supported && lighthouseSource.scopes_supported.length){
authorizationUrl.searchParams.set('scope', lighthouseSource.scopes_supported.join(' '));
} else {
authorizationUrl.searchParams.set('scope', '');
}
if (lighthouseSource.aud) {
authorizationUrl.searchParams.set('aud', lighthouseSource.aud);
}

//this is for providers that support CORS and PKCE (public client auth)
if(!lighthouseSource.confidential || (lighthouseSource.code_challenge_methods_supported || []).length > 0){
// https://github.com/panva/oauth4webapi/blob/8eba19eac408bdec5c1fe8abac2710c50bfadcc3/examples/public.ts
const codeVerifier = Oauth.generateRandomCodeVerifier();
const codeChallenge = await Oauth.calculatePKCECodeChallenge(codeVerifier);
const codeChallengeMethod = lighthouseSource.code_challenge_methods_supported[0]; // 'S256'

sourceStateInfo.code_verifier = codeVerifier
sourceStateInfo.code_challenge = codeChallenge
sourceStateInfo.code_challenge_method = codeChallengeMethod

authorizationUrl.searchParams.set('code_challenge', codeChallenge);
authorizationUrl.searchParams.set('code_challenge_method', codeChallengeMethod);
}

localStorage.setItem(state, JSON.stringify(sourceStateInfo))

//this might be a custom definition instead of a lighthouse retrieved definition
// we need to store this configuration in local storage so we can use it later
localStorage.setItem(`${state}.customDefinition`, JSON.stringify(lighthouseSource))

this.redirectWithOriginAndDestination(authorizationUrl.toString(), lighthouseSource);
},

// redirectOpts = {platform_type: string, redirect_uri: string, brand_id: string, portal_id: string, id: string}
redirectWithOriginAndDestination(destUrl, redirectOpts) {
const originUrlParts = new URL(window.location.href)
Expand Down Expand Up @@ -618,7 +579,15 @@ <h2 class="mb-0">
this.saveCustomSource()
}

this.generateSourceAuthorizeUrl(this.customDefinition)
return generateSourceAuthorizeUrl(this.customDefinition).then(authorizeData => {
console.log(authorizeData)
localStorage.setItem(authorizeData.sourceState.state, JSON.stringify(authorizeData.sourceState))

//this is a custom definition instead of a lighthouse retrieved definition
// we need to store this configuration in local storage so we can use it later
localStorage.setItem(`${authorizeData.sourceState.state}.customDefinition`, JSON.stringify(this.customDefinition))
return this.redirectWithOriginAndDestination(authorizeData.url.toString(), this.customDefinition);
})
},
//load custom source from localstorage
loadCustomSource(){
Expand Down Expand Up @@ -652,20 +621,6 @@ <h2 class="mb-0">
return part;
})
return parts.join(separator);
},
uuidV4() {
// http:https://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";

var uuid = s.join("");
return uuid;
}
}
}
Expand Down

0 comments on commit 6a792ad

Please sign in to comment.