Vasilis

fixes for bearer

...@@ -713,9 +713,30 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -713,9 +713,30 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
713 } 713 }
714 } failureBlock:^(NSError *error) { 714 } failureBlock:^(NSError *error) {
715 if (failure) { 715 if (failure) {
716 - failure(error); 716 + NSDictionary* dict = [NSDictionary alloc];
717 + dict = [error userInfo];
718 + NSString* errorCode = [dict objectForKey:@"NSLocalizedDescription"];
719 + if ([errorCode isEqual:@"Request failed: unauthorized (401)"]) {
720 + [self refreshToken:^(NSDictionary *response) {
721 + [self sendContext8:jsonData successBlock:^(NSDictionary *contextResponse) {
722 + if (success) {
723 + success(contextResponse);
724 + }
725 + } failureBlock:^(NSError *error) {
726 + if (failure) {
727 + failure(error);
728 + }
729 + }];
730 + } failureBlock:^(NSError *error) {
731 + if (failure) {
732 + [_db executeUpdate:@"DROP TABLE requestVariables"];
733 + failure(error);
734 + }
735 + NSLog(@"Error at token %@", error );
736 + }];
737 + }
738 + NSLog(@"Error at change password %@", error );
717 } 739 }
718 - NSLog(@"Error at change password %@", error );
719 }]; 740 }];
720 } 741 }
721 742
...@@ -3047,17 +3068,20 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -3047,17 +3068,20 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
3047 if (failureBlock) 3068 if (failureBlock)
3048 failureBlock(error); 3069 failureBlock(error);
3049 }; 3070 };
3050 - FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"]; 3071 +
3051 - NSString *accessToken = [NSString alloc];
3052 - while ([accessTokenSet next]) {
3053 - accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
3054 - }
3055 _httpClient.responseSerializer = [AFJSONResponseSerializer serializer]; 3072 _httpClient.responseSerializer = [AFJSONResponseSerializer serializer];
3056 _httpClient.requestSerializer = [AFJSONRequestSerializer serializer]; 3073 _httpClient.requestSerializer = [AFJSONRequestSerializer serializer];
3057 3074
3058 //Set HTTP Headers 3075 //Set HTTP Headers
3059 time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970]; 3076 time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970];
3060 - [_httpClient.requestSerializer setValue:[@"Bearer " stringByAppendingString:accessToken] forHTTPHeaderField:@"Authorization"]; 3077 + NSString *accessToken = [NSString alloc];
3078 + if ([_db tableExists:@"requestVariables"] == YES) {
3079 + FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
3080 + while ([accessTokenSet next]) {
3081 + accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
3082 + }
3083 + [_httpClient.requestSerializer setValue:[@"Bearer " stringByAppendingString:accessToken] forHTTPHeaderField:@"Authorization"];
3084 + }
3061 [_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"]; 3085 [_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"];
3062 [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"]; 3086 [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"];
3063 [_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"]; 3087 [_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"];
...@@ -3178,19 +3202,20 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -3178,19 +3202,20 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
3178 if (failureBlock) 3202 if (failureBlock)
3179 failureBlock(error); 3203 failureBlock(error);
3180 }; 3204 };
3205 +
3206 + _httpClient.responseSerializer = [AFJSONResponseSerializer serializer];
3207 + _httpClient.requestSerializer = [AFJSONRequestSerializer serializer];
3208 +
3209 + //Set HTTP Headers
3210 + time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970];
3181 NSString *accessToken = [NSString alloc]; 3211 NSString *accessToken = [NSString alloc];
3182 if ([_db tableExists:@"requestVariables"] == YES) { 3212 if ([_db tableExists:@"requestVariables"] == YES) {
3183 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"]; 3213 FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
3184 while ([accessTokenSet next]) { 3214 while ([accessTokenSet next]) {
3185 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue]; 3215 accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
3186 } 3216 }
3217 + [_httpClient.requestSerializer setValue:[@"Bearer " stringByAppendingString:accessToken] forHTTPHeaderField:@"Authorization"];
3187 } 3218 }
3188 - _httpClient.responseSerializer = [AFJSONResponseSerializer serializer];
3189 - _httpClient.requestSerializer = [AFJSONRequestSerializer serializer];
3190 -
3191 - //Set HTTP Headers
3192 - time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970];
3193 - [_httpClient.requestSerializer setValue:[@"Bearer " stringByAppendingString:accessToken] forHTTPHeaderField:@"Authorization"];
3194 [_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"]; 3219 [_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"];
3195 [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"]; 3220 [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"];
3196 [_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"]; 3221 [_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"];
...@@ -3648,7 +3673,6 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -3648,7 +3673,6 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
3648 3673
3649 //Set HTTP Headers 3674 //Set HTTP Headers
3650 time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970]; 3675 time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970];
3651 -// [_httpClient.requestSerializer setValue:[@"Bearer " stringByAppendingString:accessToken] forHTTPHeaderField:@"Authorization"];
3652 [_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"]; 3676 [_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"];
3653 [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"]; 3677 [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"];
3654 [_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"]; 3678 [_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"];
......