Skip to content

Commit

Permalink
Add permanent token support (#41)
Browse files Browse the repository at this point in the history
* Add permanent token support

* Use hardcoded object for testing configuration

* Update README on permanent token usage

* Bump version to 2.1.0
  • Loading branch information
WouterToering authored Nov 13, 2019
1 parent 5792dd3 commit bc5688a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 128 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ node_js:
- "8"
- "10"
- "11"
before_install:
- openssl aes-256-cbc -K $encrypted_4c6b84b54e85_key -iv $encrypted_4c6b84b54e85_iv -in secret.json.enc -out secret.json -d
notifications:
email:
recipients:
Expand Down
51 changes: 40 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

[![Build Status](https://travis-ci.org/Bynder/bynder-js-sdk.svg?branch=master)](https://travis-ci.org/Bynder/bynder-js-sdk)

This SDK aims to help the development of integrations with [Bynder](https://www.bynder.com/en/) that use JavaScript, providing an easy interface to communicate with [Bynder's REST API](https://developer-docs.bynder.com/API/).
This SDK aims to help the development of integrations with
[Bynder](https://www.bynder.com/en/) that use JavaScript, providing an easy
interface to communicate with
[Bynder's REST API](https://developer-docs.bynder.com/API/).

## Requirements

To use this SDK, you will need:

- [Node.js **v6.3.0 or above**](https://nodejs.org/)

Node installation will include [NPM](https://www.npmjs.com/), which is responsible for dependency management.
Node installation will include [NPM](https://www.npmjs.com/), which is
responsible for dependency management.

## Installation

Expand All @@ -22,18 +26,34 @@ Node installation will include [NPM](https://www.npmjs.com/), which is responsib

## Usage

This SDK relies heavily on [Promises](https://developers.google.com/web/fundamentals/getting-started/primers/promises), making it easier to handle the asynchronous requests made to the API.
The SDK provides a `Bynder` object containing several methods which map to the calls and parameters described in [Bynder's API documentation](http://docs.bynder.apiary.io/).
This SDK relies heavily on [Promises](https://developers.google.com/web/fundamentals/getting-started/primers/promises),
making it easier to handle the asynchronous requests made to the API. The SDK
provides a `Bynder` object containing several methods which map to the
calls and parameters described in
[Bynder's API documentation](http://docs.bynder.apiary.io/).

The following snippet is a generic example of how to use the SDK. If you need details for a specific module, refer to the [samples folder](https://github.com/Bynder/bynder-js-sdk/tree/master/samples).
The following snippet is a generic example of how to use the SDK. If you need
details for a specific module, refer to the
[samples folder](https://github.com/Bynder/bynder-js-sdk/tree/master/samples).

Before executing any request, you need to authorize the calls to the API:


#### Using a permanent token
```js
const bynder = new Bynder({
baseURL: "https//portal.getbynder.com/api/",
permanentToken: "<token>",
});
```

#### Using OAuth2

1. Call the constructor with your configuration

```js
const bynder = new Bynder({
baseURL: "http://api-url.bynder.io/api/",
baseURL: "https://portal.getbynder.com/api/",
clientId: "<your OAuth2 client id>",
clientSecret: "<your OAuth2 client secret>",
redirectUri: "<url where user will be redirected after authenticating>"
Expand All @@ -52,7 +72,8 @@ const authorizationURL = bynder.makeAuthorizationURL();
bynder.getToken(code);
```

If you already have an access token, you can also initialize Bynder with the token directly:
If you already have an access token, you can also initialize Bynder with the
token directly:

```js
const bynder = new Bynder({
Expand All @@ -64,9 +85,15 @@ const bynder = new Bynder({
});
```

You can now use the various methods from the SDK to fetch media, metaproperties and other data. Following the Promises notation, you should use `.then()`/`.catch()` to handle the successful and failed requests, respectively.
#### Making requests

You can now use the various methods from the SDK to fetch media, metaproperties
and other data. Following the Promises notation, you should use
`.then()`/`.catch()` to handle the successful and failed requests,
respectively.

Most of the calls take an object as the only parameter but please refer to the API documentation to tune the query as intended.
Most of the calls take an object as the only parameter but please refer to the
API documentation to tune the query as intended.

```js
bynder
Expand Down Expand Up @@ -152,7 +179,8 @@ bynder

## Contribute to the SDK

If you wish to contribute to this repository and further extend the API coverage in the SDK, here are the steps necessary to prepare your environment:
If you wish to contribute to this repository and further extend the API coverage in the SDK, here
are the steps necessary to prepare your environment:

1. Clone the repository
2. In the root folder, run `yarn install` to install all of the dependencies.
Expand All @@ -173,4 +201,5 @@ If you wish to contribute to this repository and further extend the API coverage
- `gulp build` - Run webpack to bundle the code in order to run in a browser.
- `gulp babel` - Run Babel to create a folder 'dist' with ES2015 compatible code.
- `gulp doc` - Run JSDoc to create a 'doc' folder with automatically generated documentation for the source code.
- `gulp webserver` - Deploy a web server from the root folder at `localhost:8080` to run the html samples (in order to avoid CORS problems).
- `gulp webserver` - Deploy a web server from the root folder at
`localhost:8080` to run the html samples (in order to avoid CORS problems).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bynder/bynder-js-sdk",
"version": "2.0.2",
"version": "2.1.0",
"description": "Bynder Javascript SDK",
"main": "./dist/bynder-js-sdk.js",
"scripts": {
Expand Down
Binary file removed secret.json.enc
Binary file not shown.
38 changes: 24 additions & 14 deletions src/bynder-js-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ class APICall {

const headers = {};

if (!this.token) {
if (!this.token && !this.permanentToken) {
throw new Error("No token found");
}
this.token = await (this.token.expired()
? this.token.refresh()
: Promise.resolve(this.token));

headers["Authorization"] = "Bearer " + this.token.token.access_token;
if (this.permanentToken) {
headers["Authorization"] = "Bearer " + this.permanentToken;
} else {
this.token = await (this.token.expired()
? this.token.refresh()
: Promise.resolve(this.token));

headers["Authorization"] = "Bearer " + this.token.token.access_token;
}

let body = "";

Expand Down Expand Up @@ -159,28 +164,33 @@ class Bynder {
this.baseURL = options.baseURL;
this.redirectUri = options.redirectUri;

this.oauthBaseUrl = url.resolve(options.baseURL, "/v6/authentication/");
this.api = new APICall(
options.baseURL,
options.httpsAgent,
options.httpAgent
);

if (typeof options.permanentToken === "string") {
this.api.permanentToken = options.permanentToken;
return;
}

const oauthBaseUrl = url.resolve(options.baseURL, "/v6/authentication/");

this.oauth2 = simpleOAuth2.create({
client: {
id: options.clientId,
secret: options.clientSecret
},
auth: {
tokenHost: this.oauthBaseUrl,
tokenHost: oauthBaseUrl,
tokenPath: "oauth2/token",
revokePath: "oauth2/revoke",
authorizeHost: this.oauthBaseUrl,
authorizeHost: oauthBaseUrl,
authorizePath: "oauth2/auth"
}
});

this.api = new APICall(
options.baseURL,
options.httpsAgent,
options.httpAgent
);

if (options.token) {
if (typeof options.token.access_token !== "string") {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/login.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Make authorization URL should return correct authorization URL 1`] = `"https://integrations.getbynder.com/v6/authentication/oauth2/auth?response_type=code&client_id=5f5206f4-2d7a-4345-bd4c-12fad14ac111&redirect_uri=http%3A%2F%2Flocalhost%2Ftest%2Fcallback&scope=offline&state=state%20example"`;
exports[`Make authorization URL should return correct authorization URL 1`] = `"https://portal.getbynder.com/v6/authentication/oauth2/auth?response_type=code&client_id=test-client-id&redirect_uri=https%3A%2F%2Ftest-redirect-uri.com&scope=offline&state=state%20example"`;
98 changes: 0 additions & 98 deletions tests/connection.js

This file was deleted.

16 changes: 15 additions & 1 deletion tests/login.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const Bynder = require("../src/bynder-js-sdk.js");
const configs = require("../secret.json");
const url = require("url");

const configs = {
"baseURL": "https://portal.getbynder.com/api/",
"clientId": "test-client-id",
"clientSecret": "test-client-secret",
"redirectUri": "https://test-redirect-uri.com"
};

let bynder;

describe("Make authorization URL", () => {
Expand Down Expand Up @@ -56,3 +62,11 @@ describe("Make API call with invalid token", () => {
expect(() => new Bynder(invalidTokenConfig)).toThrow(Error);
});
});

describe("Initialize Bynder with permanent token", () => {
test("should pass", () => {
const bynder = new Bynder({...configs, permanentToken: "test"});

expect(bynder.api.permanentToken).toEqual("test");
});
});

0 comments on commit bc5688a

Please sign in to comment.