-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/sts-support' into develop
- Loading branch information
Showing
7 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@cardbrother/nestjs-tencent-cloud-sdk": patch | ||
--- | ||
|
||
Add STS support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
GetFederationTokenRequest, | ||
GetFederationTokenResponse, | ||
QueryApiKeyRequest, | ||
QueryApiKeyResponse, | ||
} from 'tencentcloud-sdk-nodejs/tencentcloud/services/sts/v20180813/sts_models'; | ||
|
||
export interface IStsProvider { | ||
/** | ||
* @name 获取临时凭证签名 | ||
* @description 获取临时访问凭证 具体文档参考 https://cloud.tencent.com/document/product/1312/48195 | ||
* @param params GetFederationTokenRequest - 生成临时访问凭证参数 | ||
* @returns GetFederationTokenResponse - 返回临时访问凭证结果 | ||
*/ | ||
createTemporary: ( | ||
params: GetFederationTokenRequest, | ||
) => Promise<GetFederationTokenResponse>; | ||
|
||
/** | ||
* @name 查询API密钥 | ||
* @description 查询API密钥 具体文档参考 https://cloud.tencent.com/document/product/1312/48194 | ||
* @param params QueryApiKeyRequest - 查询API密钥参数 | ||
* @returns UploadFileResult - 返回查询API密钥结果 | ||
*/ | ||
queryApiKey: (params: QueryApiKeyRequest) => Promise<QueryApiKeyResponse>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Client as StsClient } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sts/v20180813/sts_client'; | ||
import { | ||
GetFederationTokenRequest, | ||
GetFederationTokenResponse, | ||
QueryApiKeyRequest, | ||
QueryApiKeyResponse, | ||
} from 'tencentcloud-sdk-nodejs/tencentcloud/services/sts/v20180813/sts_models'; | ||
|
||
import { | ||
TENCENT_CLOUD_MODULE_OPTIONS_TOKEN, | ||
TencentCloudAbstructClient, | ||
TencentCloudModuleOptions, | ||
} from '../tencent-cloud.interface'; | ||
import { IStsProvider } from './sts.interface'; | ||
|
||
@Injectable() | ||
export class StsProvider | ||
extends TencentCloudAbstructClient | ||
implements IStsProvider | ||
{ | ||
private stsClient: StsClient; | ||
constructor( | ||
@Inject(TENCENT_CLOUD_MODULE_OPTIONS_TOKEN) | ||
private readonly options: TencentCloudModuleOptions, | ||
) { | ||
super('sts.tencentcloudapi.com', '2018-08-13', options); | ||
this.stsClient = new StsClient(this); | ||
} | ||
|
||
public async createTemporary( | ||
params: GetFederationTokenRequest, | ||
): Promise<GetFederationTokenResponse> { | ||
return await this.stsClient.GetFederationToken(params); | ||
} | ||
|
||
public async queryApiKey( | ||
params: QueryApiKeyRequest, | ||
): Promise<QueryApiKeyResponse> { | ||
return await this.stsClient.QueryApiKey(params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/@cardbrother/tencent-cloud-sdk/tests/sts-client.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { StsProvider } from '../sts/sts.provider'; | ||
import { TencentCloudClientType } from '../tencent-cloud.interface'; | ||
import { TencentCloudModule } from '../tencent-cloud.module'; | ||
import { TencentCloudService } from '../tencent-cloud.service'; | ||
|
||
/** | ||
* @name @cardbrother/tencentCloudModule OCR Test | ||
* @description This test is end to end for the tencent ocr service | ||
*/ | ||
describe('@cardbrother/tencentCloudModule STS Test', () => { | ||
let tencentCloudService: TencentCloudService; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
TencentCloudModule.forRootAsync({ | ||
useFactory: (config: ConfigService) => { | ||
const tencentSecretId = | ||
config.get<string>('TENCENT_API_ID') || | ||
'XXXXXXXXXXXXXXXXXXXXXXXXX'; | ||
const tencentSecretKey = | ||
config.get<string>('TENCENT_API_SECRET') || | ||
'XXXXXXXXXXXXXXXXXXXXXXXXX'; | ||
return { | ||
apiId: tencentSecretId, | ||
apiSecret: tencentSecretKey, | ||
region: 'ap-guangzhou', | ||
global: true, | ||
}; | ||
}, | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: ['.env'], | ||
}), | ||
], | ||
inject: [ConfigService], | ||
}), | ||
], | ||
}).compile(); | ||
|
||
tencentCloudService = | ||
moduleFixture.get<TencentCloudService>(TencentCloudService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(tencentCloudService).toBeDefined(); | ||
}); | ||
|
||
it('should have the correct config', () => { | ||
const options = tencentCloudService.getOptions(); | ||
expect(options).toBeDefined(); | ||
}); | ||
|
||
it('should have useClient STS Type', async () => { | ||
const sts_client = await tencentCloudService.useClient( | ||
TencentCloudClientType.STS, | ||
); | ||
expect(sts_client).toBeDefined(); | ||
expect(sts_client).toBeInstanceOf(StsProvider); | ||
|
||
// const tempSignature = await sts_client.createTemporary({ | ||
// Name: 'CardBrother-App', | ||
// Policy: encodeURI( | ||
// JSON.stringify({ | ||
// version: '2.0', | ||
// statement: [ | ||
// { | ||
// effect: 'allow', | ||
// action: ['cos:PutObject'], | ||
// resource: ['*'], | ||
// }, | ||
// ], | ||
// }), | ||
// ), | ||
// DurationSeconds: 1800, | ||
// }); | ||
|
||
// console.debug('🐛🐛🐛 --------------------------------------------🐛🐛🐛'); | ||
// console.debug('🐛🐛🐛 ::: tempSignature:::', tempSignature); | ||
// console.debug('🐛🐛🐛 --------------------------------------------🐛🐛🐛'); | ||
}); | ||
}); |