-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor extension-starter-kit and fix fundamental-styles import for …
…all extensions
- Loading branch information
Showing
29 changed files
with
3,533 additions
and
3,629 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
File renamed without changes.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ShellSdkHelper } from './shell-sdk-helper'; | ||
import { AuthResponse, ShellSdk } from 'fsm-shell'; | ||
|
||
|
||
const updateUI = (text: string) => { | ||
(document.querySelectorAll('#info')[0] as any).innerHTML = text; | ||
}; | ||
|
||
const createUIContextList = (context: any) => { | ||
let UIContextList = '<ul>'; | ||
for (const [key, value] of Object.entries(context)) { | ||
UIContextList = UIContextList + (typeof value === 'object' ? | ||
'<li>' + `${key}: ${JSON.stringify(value)}` + '</li>' : | ||
'<li>' + `${key}: ${value}` + '</li>'); | ||
} | ||
UIContextList = UIContextList + '</ul>'; | ||
|
||
return UIContextList; | ||
} | ||
|
||
window.addEventListener('load', async () => { | ||
|
||
let UIContextList: string = ''; | ||
let currentContext: any; | ||
|
||
if (ShellSdk.isInsideShell()) { | ||
// Initialize ShellSDK to connect with parent shell library | ||
let shellSdk = ShellSdk.init(parent, '*'); | ||
|
||
let shellSdkHelper = ShellSdkHelper.getInstance(shellSdk); | ||
const initialContext = await shellSdkHelper.getContext(); | ||
currentContext = initialContext; | ||
UIContextList = createUIContextList(initialContext); | ||
|
||
/** | ||
* Update context in case new token is received. A token is only valid for five minutes. | ||
* With the help of shell-sdk-helper.ts a new token is fetched, before the current token | ||
* becomes invalid. | ||
* | ||
* IMPORTANT: You only receive a new token in case the current token's validation period | ||
* is less than one minute. If a new token is requested and the validation period is one | ||
* minute or more, you receive the current token again with an updated validation period. | ||
* A token's validation period is provided via the property expires_in, which contains | ||
* the time left in seconds. | ||
*/ | ||
shellSdkHelper.getNewTokenEmitter().on('newToken', (event: AuthResponse) => { | ||
let updatedContext = { | ||
...currentContext, | ||
auth: event | ||
}; | ||
|
||
let updatedUIContextList = createUIContextList(updatedContext); | ||
|
||
updateUI(updatedUIContextList); | ||
}); | ||
} | ||
|
||
updateUI(UIContextList); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import { ShellSdk, SHELL_EVENTS, AuthResponse } from 'fsm-shell'; | ||
import { EventEmitter } from 'events'; | ||
|
||
|
||
export class ShellSdkHelper { | ||
private static instance: ShellSdkHelper; | ||
private shellSdk: ShellSdk; | ||
private newTokenEmitter = new EventEmitter(); | ||
|
||
private constructor(shellSdk: ShellSdk) { | ||
this.shellSdk = shellSdk; | ||
} | ||
|
||
public static getInstance(shellSdk: ShellSdk): ShellSdkHelper { | ||
if (!ShellSdkHelper.instance) { | ||
ShellSdkHelper.instance = new ShellSdkHelper(shellSdk); | ||
} | ||
|
||
return ShellSdkHelper.instance; | ||
} | ||
|
||
public getContext = () => new Promise<{[x: string]: any}>((resolve, reject) => { | ||
|
||
// Initialize the extension by requesting the fsm context | ||
this.shellSdk.emit(SHELL_EVENTS.Version1.REQUIRE_CONTEXT, { | ||
auth: { | ||
response_type: 'token' // request a user token within the context | ||
} | ||
}); | ||
|
||
// Callback on fsm context response | ||
this.shellSdk.on(SHELL_EVENTS.Version1.REQUIRE_CONTEXT, (event: string) => { | ||
const context = JSON.parse(event); | ||
|
||
// Access_token has a short life span and needs to be refreshed before expiring | ||
// Each extension needs to implement its own strategy to fresh it. | ||
this.initializeRefreshTokenStrategy(context.auth); | ||
|
||
resolve(context); | ||
}); | ||
|
||
}); | ||
|
||
public getNewTokenEmitter() { | ||
return this.newTokenEmitter; | ||
} | ||
|
||
private fetchToken() { | ||
this.shellSdk.emit(SHELL_EVENTS.Version1.REQUIRE_AUTHENTICATION, { | ||
response_type: 'token' // request a user token within the context | ||
}); | ||
} | ||
|
||
// Loop before a token expire to fetch a new one | ||
private initializeRefreshTokenStrategy = (auth: AuthResponse) => { | ||
this.shellSdk.on(SHELL_EVENTS.Version1.REQUIRE_AUTHENTICATION, (event: AuthResponse) => { | ||
this.newTokenEmitter.emit('newToken', event); | ||
setTimeout(() => this.fetchToken(), (event.expires_in * 1000) - 5000); | ||
}); | ||
|
||
setTimeout(() => this.fetchToken(), (auth.expires_in * 1000) - 5000); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.