Skip to content

Commit

Permalink
Merge pull request #498 from matrix-org/riot_1871
Browse files Browse the repository at this point in the history
Display the consent tool in case of M_CONSENT_NOT_GIVEN error
  • Loading branch information
SBiOSoftWhare authored May 23, 2018
2 parents 6a466c4 + 282f42e commit 93e7264
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
8 changes: 8 additions & 0 deletions MatrixSDK/MXError.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ FOUNDATION_EXPORT NSString *const kMXErrCodeStringThreePIDInUse;
FOUNDATION_EXPORT NSString *const kMXErrCodeStringThreePIDNotFound;
FOUNDATION_EXPORT NSString *const kMXErrCodeStringServerNotTrusted;
FOUNDATION_EXPORT NSString *const kMXErrCodeStringGuestAccessForbidden;
FOUNDATION_EXPORT NSString *const kMXErrCodeStringConsentNotGiven;

FOUNDATION_EXPORT NSString *const kMXErrorStringInvalidToken;

Expand All @@ -66,8 +67,15 @@ FOUNDATION_EXPORT NSString *const kMXSDKErrCodeStringMissingParameters;
*/
@property (nonatomic, readonly) NSString *error;

/**
Additional error info
*/
@property (nonatomic, readonly, copy) NSDictionary *userInfo;

- (id)initWithErrorCode:(NSString*)errcode error:(NSString*)error;

- (id)initWithErrorCode:(NSString*)errcode error:(NSString*)error userInfo:(NSDictionary*)userInfo;

/**
Create a MXError from a NSError.
Expand Down
13 changes: 13 additions & 0 deletions MatrixSDK/MXError.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
NSString *const kMXErrCodeStringThreePIDNotFound = @"M_THREEPID_NOT_FOUND";
NSString *const kMXErrCodeStringServerNotTrusted = @"M_SERVER_NOT_TRUSTED";
NSString *const kMXErrCodeStringGuestAccessForbidden= @"M_GUEST_ACCESS_FORBIDDEN";
NSString *const kMXErrCodeStringConsentNotGiven = @"M_CONSENT_NOT_GIVEN";

NSString *const kMXErrorStringInvalidToken = @"Invalid token";

Expand All @@ -62,6 +63,18 @@ -(id)initWithErrorCode:(NSString*)errcode error:(NSString*)error
return self;
}

- (id)initWithErrorCode:(NSString*)errcode error:(NSString*)error userInfo:(NSDictionary*)userInfo
{
self = [super init];
if (self)
{
_errcode = errcode;
_error = error;
_userInfo = userInfo;
}
return self;
}

-(id)initWithNSError:(NSError*)nsError
{
if ([MXError isMXError:nsError])
Expand Down
9 changes: 9 additions & 0 deletions MatrixSDK/Utils/MXHTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
*/
extern NSString * const MXHTTPClientErrorResponseDataKey;

/**
Posted when the user did not consent to GDPR.
*/
FOUNDATION_EXPORT NSString* const kMXHTTPClientUserConsentNotGivenErrorNotification;
/**
Consent URI userInfo key for notification kMXHTTPClientUserConsentNotGivenErrorNotification
*/
FOUNDATION_EXPORT NSString* const kMXHTTPClientUserConsentNotGivenErrorNotificationConsentURIKey;

/**
Block called when an authentication challenge from a server failed whereas a certificate is present in certificate chain.
Expand Down
54 changes: 48 additions & 6 deletions MatrixSDK/Utils/MXHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@
*/
#define MXHTTPCLIENT_RETRY_JITTER_MS 3000

NSString * const MXHTTPClientErrorResponseDataKey = @"com.matrixsdk.httpclient.error.response.data";
NSString* const kMXHTTPClientUserConsentNotGivenErrorNotification = @"kMXHTTPClientUserConsentNotGivenErrorNotification";
NSString* const kMXHTTPClientUserConsentNotGivenErrorNotificationConsentURIKey = @"kMXHTTPClientUserConsentNotGivenErrorNotificationConsentURIKey";

/**
`MXHTTPClientErrorResponseDataKey`
The corresponding value is an `NSDictionary` containing the response data of the operation associated with an error.
Matrix error API JSON Keys
*/
NSString * const MXHTTPClientErrorResponseDataKey = @"com.matrixsdk.httpclient.error.response.data";
static NSString* const kMXErrorCodeJSONKey = @"errcode";
static NSString* const kMXErrorMessageJSONKey = @"error";

static NSString* const kMXErrorConsentNotGivenConsentURIJSONKey = @"consent_uri";


@interface MXHTTPClient ()
{
Expand Down Expand Up @@ -285,11 +292,10 @@ - (void)tryRequest:(MXHTTPOperation*)mxHTTPOperation
{
NSLog(@"[MXHTTPClient] Error JSONResponse: %@", JSONResponse);

if (JSONResponse[@"errcode"] || JSONResponse[@"error"])
if (JSONResponse[kMXErrorCodeJSONKey] || JSONResponse[kMXErrorMessageJSONKey])
{
// Extract values from the home server JSON response
MXError *mxError = [[MXError alloc] initWithErrorCode:JSONResponse[@"errcode"]
error:JSONResponse[@"error"]];
MXError *mxError = [self mxErrorFromJSON:JSONResponse];

if ([mxError.errcode isEqualToString:kMXErrCodeStringLimitExceeded])
{
Expand Down Expand Up @@ -325,6 +331,24 @@ - (void)tryRequest:(MXHTTPOperation*)mxHTTPOperation
NSLog(@"[MXHTTPClient] Giving up rate limited request %p: spent too long retrying.", mxHTTPOperation);
}
}
if ([mxError.errcode isEqualToString:kMXErrCodeStringConsentNotGiven])
{
NSString* consentURI = mxError.userInfo[kMXErrorConsentNotGivenConsentURIJSONKey];

if (consentURI.length > 0)
{
NSLog(@"[MXHTTPClient] User did not consent to GDPR");

// Send a notification if user did not consent to GDPR
[[NSNotificationCenter defaultCenter] postNotificationName:kMXHTTPClientUserConsentNotGivenErrorNotification
object:self
userInfo:@{ kMXHTTPClientUserConsentNotGivenErrorNotificationConsentURIKey: consentURI }];
}
else
{
NSLog(@"[MXHTTPClient] User did not consent to GDPR but fail to retrieve consent uri");
}
}
else
{
error = [mxError createNSError];
Expand Down Expand Up @@ -721,4 +745,22 @@ - (void)setDefaultSecurityPolicy
}
}

- (MXError*)mxErrorFromJSON:(NSDictionary*)json
{
// Add key/values other than error code and error message in user info
NSMutableDictionary *userInfo = [json mutableCopy];
[userInfo removeObjectForKey:kMXErrorCodeJSONKey];
[userInfo removeObjectForKey:kMXErrorMessageJSONKey];

NSDictionary *mxErrorUserInfo = nil;

if (userInfo.allKeys.count > 0) {
mxErrorUserInfo = [NSDictionary dictionaryWithDictionary:userInfo];
}

return [[MXError alloc] initWithErrorCode:json[kMXErrorCodeJSONKey]
error:json[kMXErrorMessageJSONKey]
userInfo:mxErrorUserInfo];
}

@end

0 comments on commit 93e7264

Please sign in to comment.