From c7f9ef355e57bcc17a46b96655d8f36ba54dffa8 Mon Sep 17 00:00:00 2001 From: rrr523 Date: Fri, 8 Sep 2023 18:31:29 +0800 Subject: [PATCH] feat: ListObjectPolicies api --- .changeset/rich-dolls-chew.md | 5 ++ .../src/components/object/info/index.tsx | 18 ++++++ packages/chain-sdk/src/api/objectt.ts | 30 +++++++++ packages/chain-sdk/src/api/storage.ts | 12 ++-- .../spclient/spApis/listObjectPolicies.ts | 62 +++++++++++++++++++ packages/chain-sdk/src/types/sp/Common.ts | 31 ++++++++++ .../src/types/sp/ListObjectPolicies.ts | 19 ++++++ packages/chain-sdk/src/types/sp/index.ts | 1 + 8 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 .changeset/rich-dolls-chew.md create mode 100644 packages/chain-sdk/src/clients/spclient/spApis/listObjectPolicies.ts create mode 100644 packages/chain-sdk/src/types/sp/ListObjectPolicies.ts diff --git a/.changeset/rich-dolls-chew.md b/.changeset/rich-dolls-chew.md new file mode 100644 index 00000000..baa3cbdc --- /dev/null +++ b/.changeset/rich-dolls-chew.md @@ -0,0 +1,5 @@ +--- +'@bnb-chain/greenfield-js-sdk': patch +--- + +feat: Add sp `ListObjectPolicies` API diff --git a/examples/nextjs/src/components/object/info/index.tsx b/examples/nextjs/src/components/object/info/index.tsx index ed68c0f9..1205bedc 100644 --- a/examples/nextjs/src/components/object/info/index.tsx +++ b/examples/nextjs/src/components/object/info/index.tsx @@ -115,6 +115,24 @@ export const ObjectInfo = () => {
+
+ listObjectPolicies by bucket name and object name +
+ +
+
get objects list by bucket name
diff --git a/packages/chain-sdk/src/api/objectt.ts b/packages/chain-sdk/src/api/objectt.ts index 8e6079a9..f6194f12 100644 --- a/packages/chain-sdk/src/api/objectt.ts +++ b/packages/chain-sdk/src/api/objectt.ts @@ -5,6 +5,10 @@ import { getObjectMetaInfo, parseGetObjectMetaResponse, } from '@/clients/spclient/spApis/getObjectMeta'; +import { + getListObjectPoliciesMetaInfo, + parseGetListObjectPoliciesResponse, +} from '@/clients/spclient/spApis/listObjectPolicies'; import { parseListObjectsByBucketNameResponse } from '@/clients/spclient/spApis/listObjectsByBucket'; import { getListObjectsByIDsMetaInfo, @@ -65,6 +69,7 @@ import { AuthType, SpClient } from '../clients/spclient/spClient'; import { CreateObjectApprovalRequest, CreateObjectApprovalResponse, + GetListObjectPoliciesRequest, GetPrivewObject, ListObjectsByBucketNameRequest, ListObjectsByIDsRequest, @@ -161,6 +166,8 @@ export interface IObject { getObjectMeta(params: GetObjectMetaRequest): Promise>; listObjectsByIds(params: ListObjectsByIDsRequest): Promise>; + + listObjectPolicies(params: GetListObjectPoliciesRequest): Promise; // TODO: GetObjectUploadProgress // TODO: getObjectStatusFromSP } @@ -744,4 +751,27 @@ export class Objectt implements IObject { }; } } + + public async listObjectPolicies(params: GetListObjectPoliciesRequest) { + let endpoint = params.endpoint; + if (!endpoint) { + endpoint = await this.sp.getSPUrlByBucket(params.bucketName); + } + const { url } = getListObjectPoliciesMetaInfo(endpoint, params); + + const result = await this.spClient.callApi(url, { + headers: {}, + method: METHOD_GET, + }); + + const xml = await result.text(); + const res = parseGetListObjectPoliciesResponse(xml); + + return { + code: 0, + message: 'success', + statusCode: result.status, + body: res, + }; + } } diff --git a/packages/chain-sdk/src/api/storage.ts b/packages/chain-sdk/src/api/storage.ts index 3b2e8b14..9e58e108 100644 --- a/packages/chain-sdk/src/api/storage.ts +++ b/packages/chain-sdk/src/api/storage.ts @@ -1,5 +1,6 @@ +import { TxClient } from '@/clients/txClient'; +import { getMsgPutPolicySDKTypeEIP712 } from '@/messages/greenfield'; import { MsgDeletePolicySDKTypeEIP712 } from '@/messages/greenfield/storage/MsgDeletePolicy'; -import { MsgPutPolicySDKTypeEIP712 } from '@/messages/greenfield/storage/MsgPutPolicy'; import { QueryGroupMembersExistRequest, QueryGroupMembersExistResponse, @@ -22,9 +23,7 @@ import { } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx'; import { container, delay, inject, singleton } from 'tsyringe'; import { fromTimestamp, MsgDeletePolicyTypeUrl, MsgPutPolicyTypeUrl, TxResponse } from '..'; -import { Basic } from './basic'; import { RpcQueryClient } from '../clients/queryclient'; -import { TxClient } from '@/clients/txClient'; export interface IStorage { params(): Promise; @@ -69,13 +68,18 @@ export class Storage implements IStorage { return await this.txClient.tx( MsgPutPolicyTypeUrl, msg.operator, - MsgPutPolicySDKTypeEIP712, + getMsgPutPolicySDKTypeEIP712(msg.statements[0].resources), { ...toSdk, expiration_time: msg.expirationTime ? fromTimestamp(msg.expirationTime) : '', statements: toSdk.statements.map((e) => { // @ts-ignore e.expiration_time = ''; + + if (e.resources.length == 0) { + // @ts-ignore + e.resources = null; + } return e; }), }, diff --git a/packages/chain-sdk/src/clients/spclient/spApis/listObjectPolicies.ts b/packages/chain-sdk/src/clients/spclient/spApis/listObjectPolicies.ts new file mode 100644 index 00000000..b232a9c2 --- /dev/null +++ b/packages/chain-sdk/src/clients/spclient/spApis/listObjectPolicies.ts @@ -0,0 +1,62 @@ +import { convertStrToBool, formatObjectInfo } from '@/types/sp/Common'; +import { + GetListObjectPoliciesRequest, + GetListObjectPoliciesResponse, +} from '@/types/sp/ListObjectPolicies'; +import { generateUrlByBucketName } from '@/utils'; +import { actionTypeFromJSON } from '@bnb-chain/greenfield-cosmos-types/greenfield/permission/common'; +import { XMLParser } from 'fast-xml-parser'; +import { encodePath, getSortQueryParams } from '../auth'; + +export const getListObjectPoliciesMetaInfo = ( + endpoint: string, + params: GetListObjectPoliciesRequest, +) => { + const { actionType, bucketName, objectName, limit = '10', startAfter = '' } = params; + const path = `/${encodePath(objectName)}`; + const queryMap = { + 'object-policies': 'null', + 'start-after': startAfter, + limit: String(limit), + 'action-type': String(actionTypeFromJSON(actionType)), + }; + + let url = new URL(path, generateUrlByBucketName(endpoint, bucketName)); + url = getSortQueryParams(url, queryMap); + + return { + url: url.href, + }; +}; + +export const parseGetListObjectPoliciesResponse = (data: string) => { + const xmlParser = new XMLParser({ + parseTagValue: false, + }); + const res = xmlParser.parse(data) as GetListObjectPoliciesResponse; + + let Policies = res.GfSpListObjectPoliciesResponse.Policies || []; + + if (Policies) { + if (!Array.isArray(Policies)) { + Policies = [Policies]; + } + + Policies = Policies.map((item) => { + return { + ...item, + PrincipalType: Number(item.ResourceType), + ResourceType: Number(item.ResourceType), + CreateTimestamp: Number(item.CreateTimestamp), + UpdateTimestamp: Number(item.UpdateTimestamp), + ExpirationTime: Number(item.ExpirationTime), + }; + }); + } + + res.GfSpListObjectPoliciesResponse = { + Policies, + }; + + return res; +}; diff --git a/packages/chain-sdk/src/types/sp/Common.ts b/packages/chain-sdk/src/types/sp/Common.ts index 77d3f527..a1436bb7 100644 --- a/packages/chain-sdk/src/types/sp/Common.ts +++ b/packages/chain-sdk/src/types/sp/Common.ts @@ -149,3 +149,34 @@ export function formatGroupInfo(o: GroupInfo) { Id: Number(o.Id), }; } + +export interface PolicyMeta { + /** + * principal_type defines the type of principal + */ + PrincipalType: number; + /** + * principal_value defines the value of principal + */ + PrincipalValue: string; + /** + * resource_type defines the type of resource that grants permission for + */ + ResourceType: number; + /** + * resource_id defines the bucket/object/group id of the resource that grants permission for + */ + ResourceId: string; + /** + * create_timestamp defines the create time of permission + */ + CreateTimestamp: number; + /** + * update_timestamp defines the update time of permission + */ + UpdateTimestamp: number; + /** + * expiration_time defines the expiration time of permission + */ + ExpirationTime: number; +} diff --git a/packages/chain-sdk/src/types/sp/ListObjectPolicies.ts b/packages/chain-sdk/src/types/sp/ListObjectPolicies.ts new file mode 100644 index 00000000..e2683514 --- /dev/null +++ b/packages/chain-sdk/src/types/sp/ListObjectPolicies.ts @@ -0,0 +1,19 @@ +import { ActionType } from '@bnb-chain/greenfield-cosmos-types/greenfield/permission/common'; +import { PolicyMeta } from './Common'; + +export type GetListObjectPoliciesRequest = { + endpoint?: string; + bucketName: string; + objectName: string; + actionType: keyof typeof ActionType; + limit?: number; + startAfter?: string; +}; + +export type GetListObjectPoliciesResponse = { + GfSpListObjectPoliciesResponse: GfSpListObjectPoliciesResponse; +}; + +export interface GfSpListObjectPoliciesResponse { + Policies: PolicyMeta[]; +} diff --git a/packages/chain-sdk/src/types/sp/index.ts b/packages/chain-sdk/src/types/sp/index.ts index 3d94f53b..cea87ef0 100644 --- a/packages/chain-sdk/src/types/sp/index.ts +++ b/packages/chain-sdk/src/types/sp/index.ts @@ -10,6 +10,7 @@ export * from './ListBucketsByIDs'; export * from './ListBucketsByPaymentAccount'; export * from './ListGroups'; export * from './ListGroupsMembers'; +export * from './ListObjectPolicies'; export * from './ListObjectsByBucketName'; export * from './ListObjectsByIDs'; export * from './ListUserGroups';