aliyun-oss-react-native

RNAliyunOSS+AUTH.m 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // RNAliyunOSS+AUTH.m
  3. // aliyun-oss-rn-sdk
  4. //
  5. // Created by 罗章 on 2018/5/8.
  6. //
  7. #import "RNAliyunOSS+AUTH.h"
  8. @implementation RNAliyunOSS (AUTH)
  9. /**
  10. initWithPlainTextAccessKey
  11. */
  12. RCT_EXPORT_METHOD(initWithPlainTextAccessKey:(NSString *)accessKey secretKey:(NSString *)secretKey endPoint:(NSString *)endPoint configuration:(NSDictionary *)configuration){
  13. id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:accessKey secretKey:secretKey];
  14. [self initConfiguration: configuration];
  15. self.client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential clientConfiguration:self.clientConfiguration];
  16. }
  17. /**
  18. initWithImplementedSigner
  19. */
  20. RCT_EXPORT_METHOD(initWithImplementedSigner:(NSString *)signature accessKey:(NSString *)accessKey endPoint:(NSString *)endPoint configuration:(NSDictionary *)configuration){
  21. id<OSSCredentialProvider> credential = [[OSSCustomSignerCredentialProvider alloc] initWithImplementedSigner:^NSString *(NSString *contentToSign, NSError *__autoreleasing *error) {
  22. if (signature != nil) {
  23. *error = nil;
  24. } else {
  25. // construct error object
  26. *error = [NSError errorWithDomain:endPoint code:OSSClientErrorCodeSignFailed userInfo:nil];
  27. return nil;
  28. }
  29. return [NSString stringWithFormat:@"OSS %@:%@", accessKey, signature];
  30. }];
  31. [self initConfiguration: configuration];
  32. self.client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential clientConfiguration:self.clientConfiguration];
  33. }
  34. /**
  35. initWithSecurityToken
  36. */
  37. RCT_EXPORT_METHOD(initWithSecurityToken:(NSString *)securityToken accessKey:(NSString *)accessKey secretKey:(NSString *)secretKey endPoint:(NSString *)endPoint configuration:(NSDictionary *)configuration){
  38. id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:accessKey secretKeyId:secretKey securityToken:securityToken];
  39. [self initConfiguration: configuration];
  40. self.client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential clientConfiguration:self.clientConfiguration];
  41. }
  42. /**
  43. initWithServerSTS
  44. */
  45. RCT_EXPORT_METHOD(initWithServerSTS:(NSString *)server endPoint:(NSString *)endPoint configuration:(NSDictionary *)configuration){
  46. //直接访问鉴权服务器(推荐,token过期后可以自动更新)
  47. id<OSSCredentialProvider> credential = [[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:server];
  48. [self initConfiguration: configuration];
  49. self.client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential clientConfiguration:self.clientConfiguration];
  50. }
  51. @end