Skip to content

Commit

Permalink
WAP helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Jun 23, 2020
1 parent c6637b0 commit d934747
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lerna.json
Expand Up @@ -9,7 +9,8 @@
"redux-demo",
"sdk",
"subscriptions",
"utils"
"utils",
"wap"
],
"version": "0.0.0"
}
2 changes: 2 additions & 0 deletions wap/.npmignore
@@ -0,0 +1,2 @@
tsconfig.es6.json
tsconfig.json
2 changes: 2 additions & 0 deletions wap/.npmrc
@@ -0,0 +1,2 @@
package-lock=false
save-exact=true
17 changes: 17 additions & 0 deletions wap/LICENSE.md
@@ -0,0 +1,17 @@
The MIT License (MIT)

Copyright (c) 2014-2015 RingCentral, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
69 changes: 69 additions & 0 deletions wap/README.md
@@ -0,0 +1,69 @@
# Installation

```bash
$ npm install --save @ringcentral/wap @ringcentral/sdk
```

# Usage

This example is using React Router.

```js
// lib.js
import SDK from "@ringcentral/sdk";
import WAP from "@ringcentral/wap";

const sdk = new SDK({
authProxy: true,
urlPrefix: '/api',
authorizeEndpoint: '/wap/authorize',
clientId: 'XXX', // use HOST clientId here
});

const wap = new WAP({sdk: SDK});

wap.bootstrapApp('http://example.com', 'XXX');
```

Host should be able to handle `/web/success`.

# WAP Authentication Process

- On Host:
1. Initiate regular 3-legged OAuth from Host by opening the URL: `wap.ringcentral.com/wap/authorize?client_id=%HOST_CLIENT_ID%`
2. On success CLW will redirect to a pre-defined 3-legged Redirect URI: `wap.ringcentral.com/oauth-callback?code=%CODE%&state=%STATE%`
3. Using the auth get the code via interop endpoint: `POST` to `wap.ringcentral.com/restapi/v1.0/interop/generate-code` with body `{clientId: %APP_CLIENT_ID%}`
4. Using the code from interop endpoint use launch endpoint to open the app in IFrame: `wap.ringcentral.com/apps/%APP_CLIENT_ID%/api/wap/launch?code=%CODE%&landing_page_uri=%APP_URL%`
- In IFrame
1. Launch endpoint redirect IFrame to `%APP_URL%?endpoint_url=service.ringcentral.com/apps/%APP_CLIENT_ID%/api`
2. Use following URL to access API inside the IFrame: `wap.ringcentral.com/apps/%APP_CLIENT_ID%/api/restapi/v1.0/client-info`

# IFrame part

```js
import SDK from "@ringcentral/sdk";

const apiEntryPointKey = 'apiEntryPoint';

// this will remove the apiEntryPoint from URL
if (window.location.search.includes(apiEntryPointKey)) {
const search = new URLSearchParams(window.location.search);
localStorage.apiEntryPoint = search.get(apiEntryPointKey); // this will preserve entry point between HMR
search.delete(apiEntryPointKey);
const newUrl = new URL(window.location);
newUrl.search = search;
window.history.replaceState(null, null, newUrl.toString());
}

if (!localStorage.apiEntryPoint) throw new Error('No API entry point was provided!');

const apiEntryPoint = decodeURIComponent(localStorage.apiEntryPoint); // we don't need to use ENV here as everything is provided by host

const {pathname, protocol, host} = new URL(apiEntryPoint);

export const sdk = new SDK({
server: `${protocol}//${host}`,
urlPrefix: pathname,
authProxy: true,
});
```
51 changes: 51 additions & 0 deletions wap/package.json
@@ -0,0 +1,51 @@
{
"name": "@ringcentral/wap",
"version": "0.0.0",
"scripts": {
"clean": "rimraf lib/* es6/*",
"build": "npm run clean && npm run build:tsc:es5 && npm run build:tsc:es6",
"build:tsc:es5": "tsc",
"build:tsc:es6": "tsc --project tsconfig.es6.json",
"start": "npm-run-all -p start:tsc:es5 start:tsc:es6",
"start:tsc:es5": "npm run build:tsc:es5 -- --watch --preserveWatchOutput",
"start:tsc:es6": "npm run build:tsc:es6 -- --watch --preserveWatchOutput"
},
"devDependencies": {
"@ringcentral/sdk": "*",
"npm-run-all": "4.1.3",
"rimraf": "2.6.2",
"typescript": "3.2.4"
},
"peerDependencies": {
"@ringcentral/sdk": ">=4",
"react": "*",
"react-dom": "*"
},
"main": "./lib/index.js",
"module": "./es6/index.js",
"types": "./lib/index.d.ts",
"author": {
"name": "RingCentral, Inc.",
"email": "devsupport@ringcentral.com"
},
"contributors": [
{
"name": "Kirill Konshin"
}
],
"repository": {
"type": "git",
"url": "git://github.com/ringcentral/ringcentral-js.git"
},
"bugs": {
"url": "https://github.com/ringcentral/ringcentral-js/issues"
},
"homepage": "https://github.com/ringcentral/ringcentral-js",
"engines": {
"node": ">=4"
},
"license": "MIT",
"publishConfig": {
"access": "public"
}
}
24 changes: 24 additions & 0 deletions wap/src/index.ts
@@ -0,0 +1,24 @@
import SDK from '@ringcentral/sdk';

export default class Wap {
private sdk: SDK;

public constructor({sdk}: {sdk: SDK}) {
this.sdk = sdk;
}

private interopCode = async clientId =>
(await this.sdk.platform().post(`/restapi/v1.0/interop/generate-code`, {clientId})).json();

// ATTENTION WEB APPS! Landing page has to be last parameter because HostSync will append additional path to it
// Technically this and /interop/generate-code should be blended into one endpoint
private launchProxy = ({url, code, clientId}) =>
this.sdk
.platform()
.createUrl(`/apps/${clientId}/api/wap/launch?code=${code}&landing_page_uri=${encodeURIComponent(url)}`);

public bootstrapApp = async ({url, clientId}) => {
const {code} = await this.interopCode(clientId);
return this.launchProxy({url, code, clientId}); // add # to URL enable hash history in IFRAME
};
}
10 changes: 10 additions & 0 deletions wap/tsconfig.es6.json
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.es6.json",
"compilerOptions": {
"declarationDir": "es6",
"outDir": "es6"
},
"include": [
"src"
]
}
10 changes: 10 additions & 0 deletions wap/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declarationDir": "lib",
"outDir": "lib"
},
"include": [
"src"
]
}

0 comments on commit d934747

Please sign in to comment.