Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to enable Cloudflare app proxy usage #39

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Castle/Classes/Client/CASAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#import "CASUtils.h"
#import "CASEvent.h"

NSString *const CASAPIClientHostName = @"api.castle.io";
NSString *const CASAPIClientVersion = @"v1";

@interface CASAPIClient ()

@property (nonatomic, copy, readonly) NSURL *baseURL;
Expand Down Expand Up @@ -117,8 +114,7 @@ - (NSURLSession *)session

- (NSURL *)baseURL
{
NSString *urlString = [NSString stringWithFormat:@"https://%@/%@/", CASAPIClientHostName, CASAPIClientVersion];
return [NSURL URLWithString:urlString];
return self.configuration.baseURL;
}

#pragma mark - NSURLRequest
Expand Down
8 changes: 8 additions & 0 deletions Castle/Classes/Public/Castle.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ extern NSString *const CastleClientIdHeaderName;
*/
+ (BOOL)isWhitelistURL:(NSURL *)url;


/**
Get base url

@return return Base URL
*/
+ (NSURL *)baseURL;

#pragma mark - Metadata

/**
Expand Down
6 changes: 6 additions & 0 deletions Castle/Classes/Public/Castle.m
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ + (BOOL)isWhitelistURL:(NSURL *)url
return [castle.configuration.baseURLWhiteList filteredArrayUsingPredicate:predicate].count > 0;
}

+ (NSURL *)baseURL
{
Castle *castle = [Castle sharedInstance];
return castle.configuration.baseURL;
}

#pragma mark - Private

- (BOOL)eventQueueExceedsFlushLimit
Expand Down
19 changes: 19 additions & 0 deletions Castle/Classes/Public/CastleConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@
*/
@property (nonatomic, strong, readwrite) NSArray<NSURL *> * _Nonnull baseURLWhiteList;

/**
Cloudflare proxying enabled
*/
@property (nonatomic, assign) BOOL useCloudflareApp;

/**
Get base url
*/
@property (nonatomic, copy, readonly, getter=baseURL) NSURL * _Nonnull baseURL;

/**
Get API domain
*/
@property (nonatomic, copy) NSString * _Nonnull apiDomain;

/**
Get API path
*/
@property (nonatomic, copy) NSString * _Nullable apiPath;

/**
Default configuration with provided publishable key
Expand Down
40 changes: 39 additions & 1 deletion Castle/Classes/Public/CastleConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#import "CastleConfiguration.h"

static NSString *CastleConfigurationDefaultAPIDomain = @"api.castle.io";
static NSString *CastleConfigurationCastleAPIPath = @"v1/";
static NSString *CastleConfigurationCloudflareAPIPath = @"v1/c/mobile/";

@interface CastleConfiguration ()
@property (nonatomic, copy, readwrite) NSString *publishableKey;
@end
Expand All @@ -16,13 +20,16 @@ @implementation CastleConfiguration
+ (instancetype _Nonnull)configurationWithPublishableKey:(NSString * _Nonnull)publishableKey
{
NSAssert([publishableKey hasPrefix:@"pk_"], @"You must provide a valid Castle publishable key when initializing the SDK.");

CastleConfiguration *configuration = [[CastleConfiguration alloc] init];
configuration.publishableKey = publishableKey;
configuration.screenTrackingEnabled = YES;
configuration.debugLoggingEnabled = NO;
configuration.flushLimit = 20;
configuration.maxQueueLimit = 1000;
configuration.useCloudflareApp = NO;
configuration.apiDomain = CastleConfigurationDefaultAPIDomain;
configuration.apiPath = nil;
return configuration;
}

Expand All @@ -40,4 +47,35 @@ - (void)setBaseURLWhiteList:(NSArray *)baseURLWhiteList
_baseURLWhiteList = whitelist.copy;
}

- (void)setUseCloudflareApp:(BOOL)useCloudflareApp
{
if (useCloudflareApp && [self.apiDomain isEqualToString:CastleConfigurationDefaultAPIDomain]) {
NSException *exception = [[NSException alloc] initWithName:@"No API domain set" reason:@"You must set a API domain if useCloudflare app is enabled." userInfo:nil];
[exception raise];
}
_useCloudflareApp = useCloudflareApp;
}

#pragma mark - Getters

- (NSURL *)baseURL
{
if (self.useCloudflareApp) {
return [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/%@", self.apiDomain, self.apiPath]];
}
return [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/%@", CastleConfigurationDefaultAPIDomain, self.apiPath]];
}

- (NSString *)apiPath
{
if (self.useCloudflareApp) {
if (_apiPath != nil) {
return _apiPath;
} else {
return CastleConfigurationCloudflareAPIPath;
}
}
return CastleConfigurationCastleAPIPath;
}

@end
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Castle (1.0.1)
- Castle (1.0.4)

DEPENDENCIES:
- Castle (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Castle: fe8d7d29959c4f13922f17a53dd1cd17906b3868
Castle: 0846f02b7850f97d6fa24bcc2d960203c3888c99

PODFILE CHECKSUM: d69237890685db6bab2c6d1cac77481a062aa2cc

COCOAPODS: 1.8.4
COCOAPODS: 1.9.3
36 changes: 36 additions & 0 deletions Example/Tests/Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ - (void)testConfiguration
XCTAssertEqual(configuration.maxQueueLimit, 20);
XCTAssertEqual(configuration.baseURLWhiteList.count, 1);
XCTAssertTrue([configuration.baseURLWhiteList[0].absoluteString isEqualToString:@"https://google.com/"]);
XCTAssertFalse(configuration.useCloudflareApp);
XCTAssertTrue([configuration.baseURL.absoluteString isEqualToString:@"https://api.castle.io/v1/"]);

[configuration setBaseURLWhiteList:@[ [NSURL URLWithString:@"https://google.com/somethingelse"]]];
XCTAssertFalse([configuration.baseURLWhiteList[0].absoluteString isEqualToString:@"https://google.com/somethingelse"]);
Expand All @@ -134,6 +136,40 @@ - (void)testConfiguration
// Check whitelisting on configured instance
XCTAssertTrue([Castle isWhitelistURL:[NSURL URLWithString:@"https://google.com/somethingelse"]]);
XCTAssertFalse([Castle isWhitelistURL:nil]);

[Castle resetConfiguration];

// Test cloudflare logic
configuration = [CastleConfiguration configurationWithPublishableKey:@"pk_SE5aTeotKZpDEn8kurzBYquRZyy21fvZ"];
XCTAssertThrows(configuration.useCloudflareApp = YES, "");

configuration = [CastleConfiguration configurationWithPublishableKey:@"pk_SE5aTeotKZpDEn8kurzBYquRZyy21fvZ"];
configuration.apiDomain = @"example.com";
configuration.useCloudflareApp = YES;

XCTAssertTrue(configuration.useCloudflareApp);
XCTAssertTrue([configuration.apiDomain isEqualToString:@"example.com"]);
XCTAssertTrue([configuration.apiPath isEqualToString:@"v1/c/mobile/"]);
XCTAssertTrue([configuration.baseURL.absoluteString isEqualToString:@"https://example.com/v1/c/mobile/"]);

[Castle configure:configuration];

XCTAssertTrue([Castle.baseURL.absoluteString isEqualToString:@"https://example.com/v1/c/mobile/"]);

[Castle resetConfiguration];

configuration = [CastleConfiguration configurationWithPublishableKey:@"pk_SE5aTeotKZpDEn8kurzBYquRZyy21fvZ"];
configuration.apiDomain = @"example.com";
configuration.apiPath = @"v1/test/";
configuration.useCloudflareApp = YES;

XCTAssertTrue(configuration.useCloudflareApp);
XCTAssertTrue([configuration.apiDomain isEqualToString:@"example.com"]);
XCTAssertTrue([configuration.baseURL.absoluteString isEqualToString:@"https://example.com/v1/test/"]);

[Castle resetConfiguration];

[Castle configureWithPublishableKey:@"pk_SE5aTeotKZpDEn8kurzBYquRZyy21fvZ"];
}

- (void)testDeviceIdentifier
Expand Down