Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Adding a sensitiveParameterKeyPahts property to HMRequest class to pr…
Browse files Browse the repository at this point in the history
…event sensitive information from being included in the description.
  • Loading branch information
newboadki committed Oct 24, 2018
1 parent d6a28da commit bdaa7e8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Hermod-tvos.podspec.json
@@ -1,6 +1,6 @@
{
"name": "Hermod-tvos",
"version": "1.1.1",
"version": "1.1.2",
"summary": "User friendly HTTP client on top of AFNetworking",
"description": "Hermod defines a simple, easy to use, intuitive and flexible HTTP client on top of AFNetworking.",
"homepage": "https://github.com/mobilejazz/Hermod",
Expand Down
2 changes: 1 addition & 1 deletion Hermod.podspec.json
@@ -1,6 +1,6 @@
{
"name": "Hermod",
"version": "1.1.1",
"version": "1.1.2",
"summary": "User friendly HTTP client on top of AFNetworking",
"description": "Hermod defines a simple, easy to use, intuitive and flexible HTTP client on top of AFNetworking.",
"homepage": "https://github.com/mobilejazz/Hermod",
Expand Down
6 changes: 6 additions & 0 deletions Sample Project/Hermod.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0082ED3E2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 0082ED3C2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.m */; };
5277E9351D1D3AD6004613F7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5277E9341D1D3AD6004613F7 /* main.m */; };
5277E9381D1D3AD6004613F7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5277E9371D1D3AD6004613F7 /* AppDelegate.m */; };
5277E93B1D1D3AD6004613F7 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5277E93A1D1D3AD6004613F7 /* ViewController.m */; };
Expand Down Expand Up @@ -46,6 +47,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0082ED3C2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+DescriptionHelpers.m"; sourceTree = "<group>"; };
0082ED3D2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+DescriptionHelpers.h"; sourceTree = "<group>"; };
5277E9311D1D3AD5004613F7 /* Hermod- TVOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Hermod- TVOS.app"; sourceTree = BUILT_PRODUCTS_DIR; };
5277E9341D1D3AD6004613F7 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
5277E9361D1D3AD6004613F7 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -269,6 +272,8 @@
D203B7AE1BCE74990088C315 /* HMClientKeychainManager.m */,
D204290C1AC401D1002F18FD /* NSString+HMClientMD5Hashing.h */,
D204290D1AC401D1002F18FD /* NSString+HMClientMD5Hashing.m */,
0082ED3D2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.h */,
0082ED3C2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.m */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -481,6 +486,7 @@
D20429051AC4019D002F18FD /* HMConstants.m in Sources */,
529D82861B74F51C00EEC7FB /* HMHTTPOfflineCacheSessionManager.m in Sources */,
D2FEE5EC1D91668A00443CD6 /* HMConfigurationManager.m in Sources */,
0082ED3E2180A1FC004E4556 /* NSDictionary+DescriptionHelpers.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 5 additions & 0 deletions Source Code/HMRequest.h
Expand Up @@ -61,6 +61,11 @@ extern NSTimeInterval const HMRequestDefaultTimeoutInterval;
**/
@property (nonatomic, assign) NSTimeInterval timeoutInterval;

/**
The keyPaths included in this array will not be logged.
*/
@property (nonatomic, strong, readwrite) NSArray<NSString *> *sensitiveParameterKeyPahts;

/** ************************************************* **
* @name Identifying the request
** ************************************************* **/
Expand Down
6 changes: 5 additions & 1 deletion Source Code/HMRequest.m
Expand Up @@ -16,6 +16,7 @@

#import "HMRequest.h"
#import "NSString+HMClientMD5Hashing.h"
#import "NSDictionary+DescriptionHelpers.h"

NSTimeInterval const HMRequestDefaultTimeoutInterval = 0;

Expand Down Expand Up @@ -54,6 +55,7 @@ - (instancetype)initWithCoder:(NSCoder *)coder
_httpMethod = [coder decodeIntegerForKey:@"httpMethod"];
_path = [coder decodeObjectForKey:@"path"];
_timeoutInterval = [coder decodeIntegerForKey:@"timeoutInterval"];
_sensitiveParameterKeyPahts = [coder decodeObjectForKey:@"sensitiveParameterKeyPahts"];
}
return self;
}
Expand All @@ -64,6 +66,7 @@ - (void)encodeWithCoder:(NSCoder *)coder
[coder encodeInteger:_httpMethod forKey:@"httpMethod"];
[coder encodeObject:_path forKey:@"path"];
[coder encodeInteger:_timeoutInterval forKey:@"timeoutInterval"];
[coder encodeObject:_sensitiveParameterKeyPahts forKey:@"sensitiveParameterKeyPahts"];
}

- (instancetype)copyWithZone:(NSZone *)zone
Expand All @@ -74,6 +77,7 @@ - (instancetype)copyWithZone:(NSZone *)zone
request.parameters = [_parameters copy];
request.path = [_path copy];
request.timeoutInterval = _timeoutInterval;
request.sensitiveParameterKeyPahts = [_sensitiveParameterKeyPahts copy];

return request;
}
Expand Down Expand Up @@ -109,7 +113,7 @@ - (NSString*)description
self.identifier,
_path,
NSStringFromHMHTTPMethod(_httpMethod),
_parameters.description];
[_parameters hm_descriptionRemovingKeyPaths:self.sensitiveParameterKeyPahts]];
}

#pragma mark Public Methods
Expand Down
20 changes: 20 additions & 0 deletions Source Code/Helpers/NSDictionary+DescriptionHelpers.h
@@ -0,0 +1,20 @@
//
// NSDictionary+DescriptionHelpers.h
// Hermod
//
// Created by Borja Arias Drake on 18/10/2018.
//

#import <Foundation/Foundation.h>

@interface NSDictionary (DescriptionHelpers)

/**
Generates a description of the dictionary where there keypaths passed by parameter have been removed.
@param keyPaths Elements in these keypaths will be removed from the description.
@return A description of the dictionary without the specified keypaths.
*/
- (NSString *)hm_descriptionRemovingKeyPaths:(NSArray <NSString *>*)keyPaths;

@end
64 changes: 64 additions & 0 deletions Source Code/Helpers/NSDictionary+DescriptionHelpers.m
@@ -0,0 +1,64 @@
//
// NSDictionary+DescriptionHelpers.m
// Hermod
//
// Created by Borja Arias Drake on 18/10/2018.
//

#import "NSDictionary+DescriptionHelpers.h"

@implementation NSDictionary (DescriptionHelpers)

- (NSString *)hm_descriptionRemovingKeyPaths:(NSArray <NSString *>*)keyPaths
{
if (keyPaths == nil || keyPaths.count == 0) {
return self.description;
}

NSDictionary *copy = [self hm_deepCopyRemovingKeyPaths:keyPaths currentKeyPath:@""];
return copy.description;
}

- (NSDictionary *)hm_deepCopyRemovingKeyPaths:(NSArray <NSString *>*)keypathsToRemove currentKeyPath:(NSString *)currentKeyPath
{
NSMutableDictionary *newDictionary = [NSMutableDictionary new];
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {

// Generate next keyPath
NSString *nextKeyPath = [self hm_appendComponent:key toCurrentKeyPath:currentKeyPath];

// Quit if keypath is in list
if ([keypathsToRemove containsObject:nextKeyPath]) {
// Base case
return;
}

if (!([obj isKindOfClass:NSDictionary.class] || [obj isKindOfClass:NSMutableDictionary.class])) {
// Base case
newDictionary[key] = [obj copy];
return;
}

// Recursion step
newDictionary[key] = [obj hm_deepCopyRemovingKeyPaths:keypathsToRemove currentKeyPath:nextKeyPath];
}];

return [newDictionary copy];
}

- (NSString *)hm_appendComponent:(NSString *)component toCurrentKeyPath:(NSString *)keyPath
{
NSString *nextKeyPath = nil;
if (keyPath.length == 0)
{
nextKeyPath = [keyPath stringByAppendingFormat:@"%@", component];
}
else
{
nextKeyPath = [keyPath stringByAppendingFormat:@".%@", component];
}

return nextKeyPath;
}

@end

0 comments on commit bdaa7e8

Please sign in to comment.