Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Commit

Permalink
Pass api token to gateway (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhynes authored Sep 13, 2019
1 parent a38037f commit ee62c26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class HttpGateway implements OasisGateway {
private apiToken: string,
private headers: HttpHeaders
) {
this.session = new HttpSession(url, headers);
this.session = new HttpSession(url, apiToken, headers);
this.polling = PollingService.instance({
url: url,
session: this.session,
Expand Down
10 changes: 8 additions & 2 deletions packages/gateway/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { AxiosClient, HttpClient, HttpHeaders, Http } from './http';
import * as _uuid from 'uuid';

let uuid: any = undefined;
let URL: any = undefined;

// Browser.
/* tslint:disable */
if (typeof window !== 'undefined') {
// @ts-ignore
uuid = _uuid.default;
URL = window.URL;
}
// Node.
else {
uuid = require('uuid');
URL = require('url').URL;
}

export class HttpSession implements Http {
Expand All @@ -34,6 +37,7 @@ export class HttpSession implements Http {

public constructor(
public url: string,
public apiToken: string,
headers: HttpHeaders,
client?: HttpClient
) {
Expand All @@ -51,16 +55,18 @@ export class HttpSession implements Http {
api: string,
body: Object
): Promise<any> {
const uri = `${this.url}/${api}`;
const url = new URL(api, this.url).href;
const headers: HttpHeaders = { headers: new Map() };
headers.headers.set('X-OASIS-INSECURE-AUTH', '1');
headers.headers.set('X-OASIS-LOGIN-TOKEN', this.apiToken);
headers.headers.set('X-OASIS-SESSION-KEY', this.sessionKey);
headers.headers.set('Content-type', 'application/json');

this.headers.headers.forEach((value, key) =>
headers.headers.set(key, value)
);

let response = await this.client.request(method, uri, body, headers);
let response = await this.client.request(method, url, body, headers);
return response.data;
}
}
8 changes: 7 additions & 1 deletion packages/gateway/test/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ describe('SessionService', () => {
const headers = { headers: map };
const client = new HttpMockClient();

const session = new HttpSession('http://myurl', headers, client);
const dummyApiToken = 'LPbGhl6lGxaFDHgHF5N8CNZ32a3MgE+IfmutjxEb3FWt4WwP';
const session = new HttpSession(
'http://myurl',
dummyApiToken,
headers,
client
);
let res = await session.request('method', 'myapi', { data: 'data' });

expect(res).toEqual('response');
Expand Down

0 comments on commit ee62c26

Please sign in to comment.