Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signing requests #40

Merged
merged 9 commits into from
Sep 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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