Skip to content

Commit

Permalink
feat: refactored data assets, acl, auth methods and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Oct 1, 2024
1 parent b2ae195 commit 6d0df4d
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 308 deletions.
7 changes: 5 additions & 2 deletions scripts/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ import { WalletService } from '../services/wallet-service';
`export type Environment = 'dev' | 'prod';export type TokenManagementMode = 'jwt' | 'privateKey';
\n`,
`export interface Config {
privateKey?: string;
jwt?:string
environment: Environment;
logging?: boolean;
walletType: WalletTypeEnum;
wallet?: {
privateKey: string;
walletType: WalletTypeEnum;
};
}\n\n`,
`export interface WalletSignMessageType {
signature: string;
Expand Down
7 changes: 5 additions & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ export type Environment = 'dev' | 'prod';
export type TokenManagementMode = 'jwt' | 'privateKey';

export interface Config {
privateKey?: string;
environment: Environment;
jwt?: string;
logging?: boolean;
walletType: WalletTypeEnum;
wallet?: {
privateKey: string;
walletType: WalletTypeEnum;
};
}

export interface WalletSignMessageType {
Expand Down
17 changes: 12 additions & 5 deletions src/helpers/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export const parameterChecker = (
if (!environment)
throw new Error('No url found!.Use either sandbox or production env');

if (jwt) {
if (privateKey) {
mode = 'privateKey';
value = privateKey;
} else if (jwt) {
mode = 'jwt';
value = jwt;

Check warning on line 42 in src/helpers/helper.ts

View check run for this annotation

Codecov / codecov/patch

src/helpers/helper.ts#L41-L42

Added lines #L41 - L42 were not covered by tests
if (!checkJWTTokenExpiration(jwt))
throw new Error('The provided token is expired or invalid.');

Check warning on line 44 in src/helpers/helper.ts

View check run for this annotation

Codecov / codecov/patch

src/helpers/helper.ts#L44

Added line #L44 was not covered by tests
} else if (privateKey) {
mode = 'privateKey';
value = privateKey;
} else {
throw new Error('Need jwt or private key');
}
Expand All @@ -54,6 +54,13 @@ export const parameterChecker = (

let accessToken: string | undefined = undefined;

/**
* The function `checkJWTTokenExpiration` verifies if a JWT token is expired or not.
* @param {string} existinToken - The `existinToken` parameter is a string representing a JWT token
* that you want to check for expiration.
* @returns The function `checkJWTTokenExpiration` returns a boolean value. It returns `true` if the
* JWT token is valid and has not expired, and `false` if the token is invalid or has expired.
*/
export const checkJWTTokenExpiration = (existinToken: string): boolean => {
try {
const decodedToken = jwt.decode(existinToken) as JWTData | null;
Expand All @@ -79,7 +86,7 @@ export const issueJWT = async (
) => {
const auth = new Auth(client);

const message = await auth.generateSignMessage();
const message = await auth.getMessage();
const signatureDetails = await wallet.signMessage(message);

const jwt = await auth.login({
Expand Down
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Account } from './modules/account/account';

export * from './common/types';
export { toRFC3339, checkJWTTokenExpiration } from './helpers/helper';
export { GTWError } from './helpers/custom-error';

class SDKFactory {
static createSDK({
Expand Down Expand Up @@ -66,16 +65,17 @@ export class Gateway {

constructor(config: Config) {
const validationService = new ValidationService();
if (config.privateKey)
if (config.wallet)
this.wallet = new WalletService({
walletPrivateKey: config.privateKey,
walletType: config.walletType,
walletPrivateKey: config.wallet!.privateKey,
walletType: config.wallet!.walletType,
});
this.client = SDKFactory.createSDK({
environment: config.environment,
privateKey: config.privateKey,
privateKey: config.wallet?.privateKey,
logging: config.logging,
wallet: this.wallet,
jwt: config.jwt,
});
this.config = config;

Expand All @@ -92,16 +92,18 @@ export class Gateway {
public updateConfig(newConfig: Partial<Config>) {
const validationService = new ValidationService();
this.config = { ...this.config, ...newConfig };
if (this.config.privateKey)

if (this.config.wallet)
this.wallet = new WalletService({
walletPrivateKey: this.config.privateKey,
walletType: this.config.walletType,
walletPrivateKey: this.config.wallet!.privateKey,
walletType: this.config.wallet!.walletType,
});
this.client = SDKFactory.createSDK({
environment: this.config.environment,
privateKey: this.config.privateKey,
privateKey: this.config.wallet?.privateKey,
logging: this.config.logging,
wallet: this.wallet,
jwt: this.config.jwt,
});
this.initializeModules(validationService);
}
Expand Down
24 changes: 22 additions & 2 deletions src/modules/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export class Auth {
this.cryptoService = new CryptoService();
}

public async generateSignMessage() {
/**
* This TypeScript function asynchronously fetches a message from a specified endpoint and returns
* the message data if successful, throwing an error if there is an issue.
* @returns The `getMessage` function is returning the `message` property from the `data` object
* fetched from the `/auth/message` endpoint.
*/
public async getMessage() {
const { data, error, response } = await this.client.GET('/auth/message');

if (error) {
Expand All @@ -23,7 +29,12 @@ export class Auth {
return data.message;
}

public async generateRefreshToken() {
/**
* This TypeScript function asynchronously retrieves a refresh token from a specified endpoint and
* returns the token if successful.
* @returns The `getRefreshToken` function is returning the `token` property from the `data` object.
*/
public async getRefreshToken() {
const { data, error, response } = await this.client.GET(
'/auth/refresh-token',
);
Expand All @@ -35,6 +46,15 @@ export class Auth {
return data.token;
}

/**
* The login function verifies a message signature and wallet address, then sends a POST request to
* authenticate and returns a token.
* @param {AuthRequest} - The `login` function takes an object as a parameter with the following
* properties:
* @returns The `login` function is returning the `token` from the `data` object after successfully
* verifying the message signature and making a POST request to the `/auth` endpoint with the
* provided message, signature, and wallet address.
*/
public async login({ message, signature, wallet_address }: AuthRequest) {
await this.cryptoService.verifyMessage(signature, message, wallet_address);

Expand Down
99 changes: 99 additions & 0 deletions src/modules/data-asset/acl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { MediaType } from 'openapi-typescript-helpers';
import { paths } from '../../api';
import { OpenAPIClient, ACLRequest, PublicACL } from '../../common/types';
import { GTWError } from '../../helpers/custom-error';
import { ValidationService } from '../../services/validator-service';

export class ACL {
private client: OpenAPIClient<paths, MediaType>;
private validationService: ValidationService;

constructor(
client: OpenAPIClient<paths, MediaType>,
validationService: ValidationService,
) {
this.client = client;
this.validationService = validationService;
}

/**
* This TypeScript function updates the Access Control List (ACL) for a specific data asset
* identified by its ID.
* @param {number} id - The `id` parameter is a number that represents the identifier of a data asset
* for which the Access Control List (ACL) is being updated.
* @param {ACLRequest[]} aclList - The `aclList` parameter in the `update` function is an array of
* `ACLRequest` objects. These objects likely contain information related to access control lists
* (ACLs) for a specific data asset identified by the `id` parameter. The function sends a PATCH
* request to update the ACLs
* @returns The `update` method is returning a Promise that resolves to an array of `PublicACL`
* objects.
*/
public async update(id: number, aclList: ACLRequest[]): Promise<PublicACL[]> {
const { data, error, response } = await this.client.PATCH(
'/data-assets/{id}/acl',
{
params: { path: { id } },
body: aclList,
},
);

if (error) {
throw new GTWError(error, response);
}

return data;
}

/**
* Note:- this method overwrites ACL list for data asset so be sure before using it!
* This TypeScript function adds ACL entries for a specific data asset identified by its ID.
* @param {number} id - The `id` parameter is a number that represents the identifier of a data asset
* to which the Access Control List (ACL) will be added.
* @param {ACLRequest[]} aclList - The `aclList` parameter in the `add` method is an array of
* `ACLRequest` objects. Each `ACLRequest` object likely contains information related to access
* control for a specific data asset identified by the `id` parameter. The `add` method sends a POST
* request to add the
* @returns The `add` method is returning a Promise that resolves to an array of `PublicACL` objects.
*/
public async add(id: number, aclList: ACLRequest[]): Promise<PublicACL[]> {
const { data, error, response } = await this.client.POST(
'/data-assets/{id}/acl',
{
params: { path: { id } },
body: aclList,
},
);

if (error) {
throw new GTWError(error, response);
}

return data;
}

/**
* This TypeScript function deletes ACL entries for a specific data asset using a PATCH request.
* @param {number} id - The `id` parameter in the `delete` function is a number that represents the
* identifier of the data asset that you want to delete the ACL (Access Control List) for.
* @param {ACLRequest[]} aclList - The `aclList` parameter in the `delete` function is an array of
* `ACLRequest` objects. Each `ACLRequest` object likely contains information about access control
* permissions for a specific resource or data asset. The function uses this array to specify which
* access control entries should be deleted for the data
* @returns the message from the `data` object, which is accessed using `data.message`.
*/
public async delete(id: number, aclList: ACLRequest[]) {
const { data, error, response } = await this.client.PATCH(
'/data-assets/{id}/acl/delete',
{
params: { path: { id } },
body: aclList,
},
);

if (error) {
throw new GTWError(error, response);
}

return data.message!;
}
}
Loading

0 comments on commit 6d0df4d

Please sign in to comment.