Repository: parse-community/Parse-SDK-JS
Pull request #358 (2016-10-12 16:30:13)

Add support to secured endpoints throught Authorization header
This is a change I made to allow communication with a server that require authorization.


codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=h1) Report
> Merging [#358](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/2956decbe54f2cf6d2c17ff1b4d8db98327410d6?src=pr&el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358/graphs/tree.svg?width=650&src=pr&token=8nCENM0QHj&height=150)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #358      +/-   ##
==========================================
- Coverage   84.35%   84.32%   -0.04%     
==========================================
  Files          46       46              
  Lines        3663     3668       +5     
  Branches      835      836       +1     
==========================================
+ Hits         3090     3093       +3     
- Misses        573      575       +2
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/CoreManager.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree#diff-c3JjL0NvcmVNYW5hZ2VyLmpz) | `100% <ø> (ø)` | :arrow_up: |
| [src/Parse.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree#diff-c3JjL1BhcnNlLmpz) | `79.41% <ø> (-2.13%)` | :arrow_down: |
| [src/RESTController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree#diff-c3JjL1JFU1RDb250cm9sbGVyLmpz) | `83.06% <ø> (+0.27%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=footer). Last update [2956dec...fe228b9](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=h1) Report
> Merging [#358](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/2956decbe54f2cf6d2c17ff1b4d8db98327410d6?src=pr&el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358/graphs/tree.svg?src=pr&token=8nCENM0QHj&width=650&height=150)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #358      +/-   ##
==========================================
- Coverage   84.35%   84.32%   -0.04%     
==========================================
  Files          46       46              
  Lines        3663     3668       +5     
  Branches      835      836       +1     
==========================================
+ Hits         3090     3093       +3     
- Misses        573      575       +2
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/CoreManager.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree#diff-c3JjL0NvcmVNYW5hZ2VyLmpz) | `100% <ø> (ø)` | :arrow_up: |
| [src/RESTController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree#diff-c3JjL1JFU1RDb250cm9sbGVyLmpz) | `83.06% <ø> (+0.27%)` | :arrow_up: |
| [src/Parse.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=tree#diff-c3JjL1BhcnNlLmpz) | `79.41% <ø> (-2.13%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=footer). Last update [2956dec...fe228b9](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/358?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/CoreManager.js b/src/CoreManager.js
index 494ebde..af7d97d 100644
--- a/src/CoreManager.js
+++ b/src/CoreManager.js
@@ -165,6 +165,8 @@ const config: Config & { [key: string]: mixed } = {
             !process.versions.electron),
   REQUEST_ATTEMPT_LIMIT: 5,
   SERVER_URL: 'https://api.parse.com/1',
+  SERVER_AUTH_TYPE: null,
+  SERVER_AUTH_TOKEN: null,
   LIVEQUERY_SERVER_URL: null,
   VERSION: 'js' + require('../package.json').version,
   APPLICATION_ID: null,
diff --git a/src/Parse.js b/src/Parse.js
index 706cd58..c852f02 100644
--- a/src/Parse.js
+++ b/src/Parse.js
@@ -127,6 +127,35 @@ Object.defineProperty(Parse, 'serverURL', {
     CoreManager.set('SERVER_URL', value);
   }
 });
+
+/**
+ * @member Parse.serverAuthToken
+ * @type string
+ * @static
+ */
+Object.defineProperty(Parse, 'serverAuthToken', {
+  get() {
+    return CoreManager.get('SERVER_AUTH_TOKEN');
+  },
+  set(value) {
+    CoreManager.set('SERVER_AUTH_TOKEN', value);
+  }
+});
+
+/**
+ * @member Parse.serverAuthType
+ * @type string
+ * @static
+ */
+Object.defineProperty(Parse, 'serverAuthType', {
+  get() {
+    return CoreManager.get('SERVER_AUTH_TYPE');
+  },
+  set(value) {
+    CoreManager.set('SERVER_AUTH_TYPE', value);
+  }
+});
+
 /**
  * @member Parse.liveQueryServerURL
  * @type string
diff --git a/src/RESTController.js b/src/RESTController.js
index 06961a4..df4d3e3 100644
--- a/src/RESTController.js
+++ b/src/RESTController.js
@@ -146,6 +146,9 @@ const RESTController = {
         headers['User-Agent'] = 'Parse/' + CoreManager.get('VERSION') +
           ' (NodeJS ' + process.versions.node + ')';
       }
+      if (CoreManager.get('SERVER_AUTH_TYPE') && CoreManager.get('SERVER_AUTH_TOKEN')) {
+        headers['Authorization'] = CoreManager.get('SERVER_AUTH_TYPE') + ' ' + CoreManager.get('SERVER_AUTH_TOKEN');
+      }
 
       if(options && typeof options.progress === 'function') {
         if (xhr.upload) {


Test patch:
diff --git a/src/__tests__/Parse-test.js b/src/__tests__/Parse-test.js
index a4052f6..be4148c 100644
--- a/src/__tests__/Parse-test.js
+++ b/src/__tests__/Parse-test.js
@@ -46,6 +46,16 @@ describe('Parse module', () => {
     expect(Parse.masterKey).toBe('789');
   });
 
+  it('can set auth type and token', () => {
+    Parse.serverAuthType = 'bearer';
+    expect(CoreManager.get('SERVER_AUTH_TYPE')).toBe('bearer');
+    expect(Parse.serverAuthType).toBe('bearer');
+
+    Parse.serverAuthToken = 'some_token';
+    expect(CoreManager.get('SERVER_AUTH_TOKEN')).toBe('some_token');
+    expect(Parse.serverAuthToken).toBe('some_token');
+  });
+
   it('can set LocalDatastoreController', () => {
     const controller = {
       fromPinWithName: function() {},
diff --git a/src/__tests__/RESTController-test.js b/src/__tests__/RESTController-test.js
index c35bc58..fe9788a 100644
--- a/src/__tests__/RESTController-test.js
+++ b/src/__tests__/RESTController-test.js
@@ -377,6 +377,25 @@ describe('RESTController', () => {
     );
   });
 
+  it('sends auth header when the auth type and token flags are set', async () => {
+    CoreManager.set('SERVER_AUTH_TYPE', 'Bearer');
+    CoreManager.set('SERVER_AUTH_TOKEN', 'some_random_token');
+    const credentialsHeader = (header) => "Authorization" === header[0];
+    const xhr = {
+      setRequestHeader: jest.fn(),
+      open: jest.fn(),
+      send: jest.fn()
+    };
+    RESTController._setXHR(function() { return xhr; });
+    RESTController.request('GET', 'classes/MyObject', {}, {});
+    await flushPromises();
+    expect(xhr.setRequestHeader.mock.calls.filter(credentialsHeader)).toEqual(
+      [["Authorization", "Bearer some_random_token"]]
+    );
+    CoreManager.set('SERVER_AUTH_TYPE', null);
+    CoreManager.set('SERVER_AUTH_TOKEN', null);
+  });
+
   it('reports upload progress of the AJAX request when callback is provided', (done) => {
     const xhr = mockXHR([{ status: 200, response: { success: true }}], {
       addEventListener: (name, callback) => {