QNUpToken.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // QNUpToken.m
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 15/6/7.
  6. // Copyright (c) 2015年 Qiniu. All rights reserved.
  7. //
  8. #import "QNUrlSafeBase64.h"
  9. #import "QNUpToken.h"
  10. #define kQNPolicyKeyScope @"scope"
  11. #define kQNPolicyKeyDeadline @"deadline"
  12. #define kQNPolicyKeyReturnUrl @"returnUrl"
  13. @interface QNUpToken ()
  14. - (instancetype)init:(NSDictionary *)policy token:(NSString *)token;
  15. @end
  16. @implementation QNUpToken
  17. + (instancetype)getInvalidToken {
  18. QNUpToken *token = [[QNUpToken alloc] init];
  19. token->_deadline = -1;
  20. return token;
  21. }
  22. - (instancetype)init:(NSDictionary *)policy token:(NSString *)token {
  23. if (self = [super init]) {
  24. _token = token;
  25. _access = [self getAccess];
  26. _bucket = [self getBucket:policy];
  27. _deadline = [policy[kQNPolicyKeyDeadline] longValue];
  28. _hasReturnUrl = (policy[kQNPolicyKeyReturnUrl] != nil);
  29. }
  30. return self;
  31. }
  32. - (NSString *)getAccess {
  33. NSRange range = [_token rangeOfString:@":" options:NSCaseInsensitiveSearch];
  34. return [_token substringToIndex:range.location];
  35. }
  36. - (NSString *)getBucket:(NSDictionary *)info {
  37. NSString *scope = [info objectForKey:kQNPolicyKeyScope];
  38. if (!scope || [scope isKindOfClass:[NSNull class]]) {
  39. return @"";
  40. }
  41. NSRange range = [scope rangeOfString:@":"];
  42. if (range.location == NSNotFound) {
  43. return scope;
  44. }
  45. return [scope substringToIndex:range.location];
  46. }
  47. + (instancetype)parse:(NSString *)token {
  48. if (token == nil) {
  49. return nil;
  50. }
  51. NSArray *array = [token componentsSeparatedByString:@":"];
  52. if (array == nil || array.count != 3) {
  53. return nil;
  54. }
  55. NSData *data = [QNUrlSafeBase64 decodeString:array[2]];
  56. if (!data) {
  57. return nil;
  58. }
  59. NSError *tmp = nil;
  60. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&tmp];
  61. if (tmp != nil || dict[kQNPolicyKeyScope] == nil || dict[kQNPolicyKeyDeadline] == nil) {
  62. return nil;
  63. }
  64. return [[QNUpToken alloc] init:dict token:token];
  65. }
  66. - (NSString *)index {
  67. return [NSString stringWithFormat:@"%@:%@", _access, _bucket];
  68. }
  69. - (BOOL)isValid {
  70. return _access && _access.length > 0 && _bucket && _bucket.length > 0;
  71. }
  72. - (BOOL)isValidForDuration:(long)duration {
  73. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:duration];
  74. return [self isValidBeforeDate:date];
  75. }
  76. - (BOOL)isValidBeforeDate:(NSDate *)date {
  77. if (date == nil) {
  78. return NO;
  79. }
  80. return [date timeIntervalSince1970] < self.deadline;
  81. }
  82. @end