Skip to content

Commit

Permalink
Merge pull request #40 from scotthovestadt/sign-requests
Browse files Browse the repository at this point in the history
refactoring and privateKey support (required for global sites)
  • Loading branch information
iBaryo committed Sep 21, 2019
2 parents 1724970 + adb5992 commit bf54a20
Show file tree
Hide file tree
Showing 16 changed files with 699 additions and 315 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const gigya = new Gigya('YOUR_API_KEY', 'YOUR_DATA_CENTER', 'YOUR_SECRET');
// Initialize SDK with your API Key, User Key, and User Secret.
const gigya = new Gigya('YOUR_API_KEY', 'YOUR_DATA_CENTER', 'YOUR_USER_KEY', 'YOUR_USER_SECRET');

// Initialize SDK with your API Key, User Key, and Private Key.
const gigya = new Gigya('YOUR_API_KEY', 'YOUR_DATA_CENTER', { userKey: 'YOUR_USER_KEY', privateKey: 'YOUR_USER_PRIVATE_KEY' });

// or:

// Initialize without keys and pass to each method.
Expand Down
90 changes: 90 additions & 0 deletions lib/RequestFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import BaseParams, {DataCenter} from "./interfaces/base-params";
import {Headers} from "request";
import _ = require("lodash");

export interface FormatJsonRequest {
format: 'json'
}

export type BaseRequest = { [key: string]: string | null | number | boolean };

export type RequestParams<P = BaseRequest> =
FormatJsonRequest & P;

export type UserParams = BaseParams & BaseRequest;

export interface GigyaRequest<P = BaseRequest> {
host: string;
endpoint: string;
params: RequestParams<P>
headers: Headers;
skipSigning: boolean;
}

export class RequestFactory {
constructor(protected _apiKey: string|undefined,
protected _dataCenter: DataCenter) {
}

public create(endpoint: string, userParams: UserParams) {
// Endpoint "accounts.getAccountInfo" and data center "us1" become "accounts.us1.gigya.com".
const namespace = endpoint.substring(0, endpoint.indexOf('.'));
const isAdminEndpoint = namespace == 'admin';
const isOAuth = !!userParams.oauth_token;

// Data center can be passed as a "param" but shouldn't be sent to the server.
let dataCenter = this._dataCenter;

if (!isAdminEndpoint) {
// treat "dataCenter" param as domain override
// (else keep as an admin endpoint parameter - like for admin.createSite
dataCenter = userParams.dataCenter || dataCenter;
delete userParams.dataCenter;

// complete default apikey only if not oauth request.
if (!userParams.apiKey && !isOAuth) {
userParams.apiKey = this._apiKey;
}
}

const request = {
host: this.getRequestHost(namespace, dataCenter),
endpoint,
params: this.getRequestParams(userParams),
headers: {},
skipSigning: isOAuth || this.isAnonymousEndpoint(endpoint)
} as GigyaRequest;

return request;
}

private isAnonymousEndpoint(endpoint: string) {
return [
'accounts.getScreenSets',
'accounts.getJWTPublicKey'
].includes(endpoint);
}

protected getRequestHost(namespace: string, dataCenter: DataCenter) {
return `${namespace}.${dataCenter}.${dataCenter != 'cn1' ? 'gigya.com' : 'gigya-api.cn'}`;
}

protected getRequestParams(userParams: UserParams): RequestParams {
return _.assignIn(
_.mapValues(userParams, (value: any) => {
if (value && (_.isObject(value) || _.isArray(value))) {
// Gigya wants arrays and objects stringified into JSON, eg Account profile and data objects.
return JSON.stringify(value);
} else if (value === null) {
// Null is meaningful in some contexts. Ensure it is passed.
return 'null';
} else {
return value;
}
}),
{
format: 'json'
} as FormatJsonRequest
);
}
}
2 changes: 1 addition & 1 deletion lib/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Accounts {
* @see http://developers.gigya.com/display/GD/accounts.getJWTPublicKey+REST
*/
public getJWTPublicKey(params?: BaseParams) {
return this.gigya.request<AccountsGetJWTPublicKeyResponse>('accounts.getJWTPublicKey', {});
return this.gigya.request<AccountsGetJWTPublicKeyResponse>('accounts.getJWTPublicKey', params);
}

/**
Expand Down
Loading

0 comments on commit bf54a20

Please sign in to comment.