Skip to content

Commit

Permalink
Inject authorization header from sigv2
Browse files Browse the repository at this point in the history
  • Loading branch information
nonelse authored and uerceg committed Mar 19, 2020
1 parent 7c9b5d9 commit ea35910
Showing 1 changed file with 83 additions and 30 deletions.
113 changes: 83 additions & 30 deletions Adjust/ADJUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,10 @@ + (void)sendGetRequest:(NSURL *)baseUrl
NSMutableDictionary *parametersCopy = [[NSMutableDictionary alloc] initWithCapacity:[activityPackage.parameters count]];
[parametersCopy addEntriesFromDictionary:activityPackage.parameters];

NSString *appSecret = [ADJUtil extractAppSecret:parametersCopy];
NSString *secretId = [ADJUtil extractSecretId:parametersCopy];
[ADJUtil extractEventCallbackId:parametersCopy];

NSString * authorizationHeader = [ADJUtil buildAuthorizationHeader:parametersCopy activityKind:activityPackage.activityKind];

NSMutableURLRequest *request = [ADJUtil requestForGetPackage:activityPackage.path
clientSdk:activityPackage.clientSdk
parameters:parametersCopy
Expand All @@ -515,22 +515,19 @@ + (void)sendGetRequest:(NSURL *)baseUrl
[ADJUtil sendRequest:request
prefixErrorMessage:prefixErrorMessage
activityPackage:activityPackage
secretId:secretId
appSecret:appSecret
authorizationHeader:authorizationHeader
responseDataHandler:responseDataHandler];
}

+ (void)sendRequest:(NSMutableURLRequest *)request
prefixErrorMessage:(NSString *)prefixErrorMessage
activityPackage:(ADJActivityPackage *)activityPackage
secretId:(NSString *)secretId
appSecret:(NSString *)appSecret
authorizationHeader:(NSString *)authorizationHeader
responseDataHandler:(void (^)(ADJResponseData *responseData))responseDataHandler {
[ADJUtil sendRequest:request
prefixErrorMessage:prefixErrorMessage
suffixErrorMessage:nil
secretId:secretId
appSecret:appSecret
authorizationHeader:authorizationHeader
activityPackage:activityPackage
responseDataHandler:responseDataHandler];
}
Expand All @@ -540,39 +537,36 @@ + (void)sendPostRequest:(NSURL *)baseUrl
prefixErrorMessage:(NSString *)prefixErrorMessage
suffixErrorMessage:(NSString *)suffixErrorMessage
activityPackage:(ADJActivityPackage *)activityPackage
responseDataHandler:(void (^)(ADJResponseData *responseData))responseDataHandler {
responseDataHandler:(void (^)(ADJResponseData *responseData))responseDataHandler
{
NSMutableDictionary *parametersCopy = [[NSMutableDictionary alloc] initWithCapacity:[activityPackage.parameters count]];
[parametersCopy addEntriesFromDictionary:activityPackage.parameters];

NSString *appSecret = [ADJUtil extractAppSecret:parametersCopy];
NSString *secretId = [ADJUtil extractSecretId:parametersCopy];
[ADJUtil extractEventCallbackId:parametersCopy];

NSString * authorizationHeader = [ADJUtil buildAuthorizationHeader:parametersCopy activityKind:activityPackage.activityKind];

NSMutableURLRequest *request = [ADJUtil requestForPostPackage:activityPackage.path
clientSdk:activityPackage.clientSdk
parameters:parametersCopy
baseUrl:baseUrl queueSize:queueSize];
[ADJUtil sendRequest:request
prefixErrorMessage:prefixErrorMessage
suffixErrorMessage:suffixErrorMessage
secretId:secretId
appSecret:appSecret
authorizationHeader:authorizationHeader
activityPackage:activityPackage
responseDataHandler:responseDataHandler];
}

+ (void)sendRequest:(NSMutableURLRequest *)request
prefixErrorMessage:(NSString *)prefixErrorMessage
suffixErrorMessage:(NSString *)suffixErrorMessage
secretId:(NSString *)secretId
appSecret:(NSString *)appSecret
authorizationHeader:(NSString *)authorizationHeader
activityPackage:(ADJActivityPackage *)activityPackage
responseDataHandler:(void (^)(ADJResponseData *responseData))responseDataHandler {
NSString *authHeader = [ADJUtil buildAuthorizationHeader:appSecret
secretId:secretId
activityPackage:activityPackage];
if (authHeader != nil) {
[request setValue:authHeader forHTTPHeaderField:@"Authorization"];
responseDataHandler:(void (^)(ADJResponseData *responseData))responseDataHandler
{
if (authorizationHeader != nil) {
[request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
}
if (userAgent != nil) {
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
Expand All @@ -594,6 +588,53 @@ + (void)sendRequest:(NSMutableURLRequest *)request
}
}

+ (NSString *)buildAuthorizationHeader:(NSMutableDictionary *)parameters
activityKind:(ADJActivityKind)activityKind
{
NSString * secretId = [ADJUtil extractSecretId:parameters];
NSString * signature = [ADJUtil extractSignature:parameters];
NSString * headersId = [ADJUtil extractHeadersId:parameters];
NSString * authorizationHeader = [ADJUtil buildAuthorizationHeaderV2:signature
secretId:secretId
headersId:headersId];
if (authorizationHeader != nil) {
return authorizationHeader;
}

NSString * appSecret = [ADJUtil extractAppSecret:parameters];
return [ADJUtil buildAuthorizationHeaderV1:appSecret
secretId:secretId
parameters:parameters
activityKind:activityKind];
}

+ (NSString *)extractSecretId:(NSMutableDictionary *)parameters {
NSString *appSecret = [parameters objectForKey:@"secret_id"];
if (appSecret == nil) {
return nil;
}
[parameters removeObjectForKey:@"secret_id"];
return appSecret;
}

+ (NSString *)extractSignature:(NSMutableDictionary *)parameters {
NSString *appSecret = [parameters objectForKey:@"signature"];
if (appSecret == nil) {
return nil;
}
[parameters removeObjectForKey:@"signature"];
return appSecret;
}

+ (NSString *)extractHeadersId:(NSMutableDictionary *)parameters {
NSString *appSecret = [parameters objectForKey:@"headers_id"];
if (appSecret == nil) {
return nil;
}
[parameters removeObjectForKey:@"headers_id"];
return appSecret;
}

+ (NSString *)extractAppSecret:(NSMutableDictionary *)parameters {
NSString *appSecret = [parameters objectForKey:@"app_secret"];
if (appSecret == nil) {
Expand All @@ -603,13 +644,24 @@ + (NSString *)extractAppSecret:(NSMutableDictionary *)parameters {
return appSecret;
}

+ (NSString *)extractSecretId:(NSMutableDictionary *)parameters {
NSString *appSecret = [parameters objectForKey:@"secret_id"];
if (appSecret == nil) {
+ (NSString *)buildAuthorizationHeaderV2:(NSString *)signature
secretId:(NSString*)secretId
headersId:(NSString*)headersId
{
if (secretId == nil || signature == nil || headersId == nil) {
return nil;
}
[parameters removeObjectForKey:@"secret_id"];
return appSecret;

NSString * signatureHeader = [NSString stringWithFormat:@"signature=\"%@\"", signature];
NSString * secretIdHeader = [NSString stringWithFormat:@"secret_id=\"%@\"", secretId];
NSString * idHeader = [NSString stringWithFormat:@"headers_id=\"%@\"", headersId];
NSString * algorithmHeader = [NSString stringWithFormat:@"algorithm=\"adj1\""];

NSString * authorizationHeader = [NSString stringWithFormat:@"Signature %@,%@,%@,%@",
signatureHeader, secretIdHeader, algorithmHeader, idHeader];
[ADJAdjustFactory.logger debug:@"authorizationHeader %@", authorizationHeader];

return authorizationHeader;
}

+ (void)extractEventCallbackId:(NSMutableDictionary *)parameters {
Expand Down Expand Up @@ -659,15 +711,16 @@ + (NSMutableURLRequest *)requestForPostPackage:(NSString *)path
return request;
}

+ (NSString *)buildAuthorizationHeader:(NSString *)appSecret
+ (NSString *)buildAuthorizationHeaderV1:(NSString *)appSecret
secretId:(NSString *)secretId
activityPackage:(ADJActivityPackage *)activityPackage {
parameters:(NSMutableDictionary *)parameters
activityKind:(ADJActivityKind)activityKind
{
if (appSecret == nil) {
return nil;
}

NSMutableDictionary *parameters = activityPackage.parameters;
NSString *activityKindS = [ADJActivityKindUtil activityKindToString:activityPackage.activityKind];
NSString *activityKindS = [ADJActivityKindUtil activityKindToString:activityKind];
NSDictionary *signatureParameters = [ADJUtil buildSignatureParameters:parameters
appSecret:appSecret
activityKindS:activityKindS];
Expand Down

0 comments on commit ea35910

Please sign in to comment.