MTXClient: Add support for logging out
FossilOrigin-Name: c29845b7b81229ea7bf062304824a4087c160629216064ae1a647fe8286826bb
This commit is contained in:
parent
023ed62f5a
commit
77a2d20fc7
11 changed files with 209 additions and 25 deletions
|
@ -35,6 +35,13 @@ OF_ASSUME_NONNULL_BEGIN
|
||||||
typedef void (^mtx_client_login_block_t)(MTXClient *_Nullable client,
|
typedef void (^mtx_client_login_block_t)(MTXClient *_Nullable client,
|
||||||
id _Nullable exception);
|
id _Nullable exception);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A block called when the device was logged out.
|
||||||
|
*
|
||||||
|
* @param exception `nil` on success, otherwise an exception
|
||||||
|
*/
|
||||||
|
typedef void (^mtx_client_logout_block_t)(id _Nullable exception);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A class that represents a client.
|
* @brief A class that represents a client.
|
||||||
*/
|
*/
|
||||||
|
@ -92,6 +99,15 @@ typedef void (^mtx_client_login_block_t)(MTXClient *_Nullable client,
|
||||||
deviceID: (OFString *)deviceID
|
deviceID: (OFString *)deviceID
|
||||||
accessToken: (OFString *)accessToken
|
accessToken: (OFString *)accessToken
|
||||||
homeserver: (OFURL *)homeserver OF_DESIGNATED_INITIALIZER;
|
homeserver: (OFURL *)homeserver OF_DESIGNATED_INITIALIZER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Logs out the device and invalidates the access token.
|
||||||
|
*
|
||||||
|
* @warning The client can no longer be used after this succeeded!
|
||||||
|
*
|
||||||
|
* @param block The block to call when logging out succeeded or failed
|
||||||
|
*/
|
||||||
|
- (void)asyncLogOutWithBlock: (mtx_client_logout_block_t)block;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
OF_ASSUME_NONNULL_END
|
OF_ASSUME_NONNULL_END
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#import "MTXRequest.h"
|
#import "MTXRequest.h"
|
||||||
|
|
||||||
#import "MTXLoginFailedException.h"
|
#import "MTXLoginFailedException.h"
|
||||||
|
#import "MTXLogoutFailedException.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
validateHomeserver(OFURL *homeserver)
|
validateHomeserver(OFURL *homeserver)
|
||||||
|
@ -76,9 +77,8 @@ validateHomeserver(OFURL *homeserver)
|
||||||
@"password": password
|
@"password": password
|
||||||
};
|
};
|
||||||
|
|
||||||
[request asyncPerformWithBlock:
|
[request asyncPerformWithBlock: ^ (mtx_response_t response,
|
||||||
^ (OFDictionary<OFString *, id> *response, int statusCode,
|
int statusCode, id exception) {
|
||||||
id exception) {
|
|
||||||
if (exception != nil) {
|
if (exception != nil) {
|
||||||
block(nil, exception);
|
block(nil, exception);
|
||||||
return;
|
return;
|
||||||
|
@ -170,4 +170,38 @@ validateHomeserver(OFURL *homeserver)
|
||||||
@">",
|
@">",
|
||||||
self.class, _userID, _deviceID, _accessToken, _homeserver];
|
self.class, _userID, _deviceID, _accessToken, _homeserver];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (MTXRequest *)requestWithPath: (OFString *)path
|
||||||
|
{
|
||||||
|
return [MTXRequest requestWithPath: path
|
||||||
|
accessToken: _accessToken
|
||||||
|
homeserver: _homeserver];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)asyncLogOutWithBlock: (mtx_client_logout_block_t)block
|
||||||
|
{
|
||||||
|
void *pool = objc_autoreleasePoolPush();
|
||||||
|
MTXRequest *request =
|
||||||
|
[self requestWithPath: @"/_matrix/client/r0/logout"];
|
||||||
|
request.method = OF_HTTP_REQUEST_METHOD_POST;
|
||||||
|
[request asyncPerformWithBlock: ^ (mtx_response_t response,
|
||||||
|
int statusCode, id exception) {
|
||||||
|
if (exception != nil) {
|
||||||
|
block(exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (statusCode != 200) {
|
||||||
|
block([MTXLogoutFailedException
|
||||||
|
exceptionWithClient: self
|
||||||
|
statusCode: statusCode
|
||||||
|
response: response]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
block(nil);
|
||||||
|
}];
|
||||||
|
|
||||||
|
objc_autoreleasePoolPop(pool);
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -24,6 +24,13 @@
|
||||||
|
|
||||||
OF_ASSUME_NONNULL_BEGIN
|
OF_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A response to a request.
|
||||||
|
*
|
||||||
|
* This is a typedef for `OFDictionary<OFString *, id> *`.
|
||||||
|
*/
|
||||||
|
typedef OFDictionary<OFString *, id> *mtx_response_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A block called with the response for an MTXRequest.
|
* @brief A block called with the response for an MTXRequest.
|
||||||
*
|
*
|
||||||
|
@ -32,9 +39,8 @@ OF_ASSUME_NONNULL_BEGIN
|
||||||
* @param exception The first exception that occurred during the request,
|
* @param exception The first exception that occurred during the request,
|
||||||
* or `nil` on success
|
* or `nil` on success
|
||||||
*/
|
*/
|
||||||
typedef void (^mtx_request_block_t)(
|
typedef void (^mtx_request_block_t)(mtx_response_t _Nullable response,
|
||||||
OFDictionary<OFString *, id> *_Nullable response, int statusCode,
|
int statusCode, id _Nullable exception);
|
||||||
id _Nullable exception);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief An internal class for performing a request on the Matrix server.
|
* @brief An internal class for performing a request on the Matrix server.
|
||||||
|
|
|
@ -101,8 +101,8 @@
|
||||||
OFMutableDictionary *headers = [OFMutableDictionary dictionary];
|
OFMutableDictionary *headers = [OFMutableDictionary dictionary];
|
||||||
headers[@"User-Agent"] = @"ObjMatrix";
|
headers[@"User-Agent"] = @"ObjMatrix";
|
||||||
if (_accessToken != nil)
|
if (_accessToken != nil)
|
||||||
headers[@"Authentication"] = [OFString
|
headers[@"Authorization"] =
|
||||||
stringWithFormat: @"Bearer %@", _accessToken];
|
[OFString stringWithFormat: @"Bearer %@", _accessToken];
|
||||||
if (_body != nil)
|
if (_body != nil)
|
||||||
headers[@"Content-Length"] = @(_body.count).stringValue;
|
headers[@"Content-Length"] = @(_body.count).stringValue;
|
||||||
|
|
||||||
|
@ -139,9 +139,9 @@
|
||||||
count: length];
|
count: length];
|
||||||
}
|
}
|
||||||
|
|
||||||
OFDictionary<OFString *, id> *responseJSON =
|
mtx_response_t responseJSON = [OFString
|
||||||
[OFString stringWithUTF8String: responseData.items
|
stringWithUTF8String: responseData.items
|
||||||
length: responseData.count]
|
length: responseData.count]
|
||||||
.objectByParsingJSON;
|
.objectByParsingJSON;
|
||||||
|
|
||||||
block(responseJSON, response.statusCode, nil);
|
block(responseJSON, response.statusCode, nil);
|
||||||
|
|
|
@ -24,3 +24,4 @@
|
||||||
#import "MTXRequest.h"
|
#import "MTXRequest.h"
|
||||||
|
|
||||||
#import "MTXLoginFailedException.h"
|
#import "MTXLoginFailedException.h"
|
||||||
|
#import "MTXLogoutFailedException.h"
|
||||||
|
|
|
@ -22,22 +22,24 @@
|
||||||
|
|
||||||
#import <ObjFW/ObjFW.h>
|
#import <ObjFW/ObjFW.h>
|
||||||
|
|
||||||
|
#import "MTXRequest.h"
|
||||||
|
|
||||||
OF_ASSUME_NONNULL_BEGIN
|
OF_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface MTXLoginFailedException: OFException
|
@interface MTXLoginFailedException: OFException
|
||||||
@property (readonly, nonatomic) OFString *user;
|
@property (readonly, nonatomic) OFString *user;
|
||||||
@property (readonly, nonatomic) OFURL *homeserver;
|
@property (readonly, nonatomic) OFURL *homeserver;
|
||||||
@property (readonly, nonatomic) int statusCode;
|
@property (readonly, nonatomic) int statusCode;
|
||||||
@property (readonly, nonatomic) OFDictionary<OFString *, id> *response;
|
@property (readonly, nonatomic) mtx_response_t response;
|
||||||
|
|
||||||
+ (instancetype)exceptionWithUser: (OFString *)user
|
+ (instancetype)exceptionWithUser: (OFString *)user
|
||||||
homeserver: (OFURL *)homeserver
|
homeserver: (OFURL *)homeserver
|
||||||
statusCode: (int)statusCode
|
statusCode: (int)statusCode
|
||||||
response: (OFDictionary<OFString *, id> *)response;
|
response: (mtx_response_t)response;
|
||||||
- (instancetype)initWithUser: (OFString *)user
|
- (instancetype)initWithUser: (OFString *)user
|
||||||
homeserver: (OFURL *)homeserver
|
homeserver: (OFURL *)homeserver
|
||||||
statusCode: (int)statusCode
|
statusCode: (int)statusCode
|
||||||
response: (OFDictionary<OFString *, id> *)response;
|
response: (mtx_response_t)response;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
OF_ASSUME_NONNULL_END
|
OF_ASSUME_NONNULL_END
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
+ (instancetype)exceptionWithUser: (OFString *)user
|
+ (instancetype)exceptionWithUser: (OFString *)user
|
||||||
homeserver: (OFURL *)homeserver
|
homeserver: (OFURL *)homeserver
|
||||||
statusCode: (int)statusCode
|
statusCode: (int)statusCode
|
||||||
response: (OFDictionary<OFString *, id> *)response
|
response: (mtx_response_t)response
|
||||||
{
|
{
|
||||||
return [[[self alloc] initWithUser: user
|
return [[[self alloc] initWithUser: user
|
||||||
homeserver: homeserver
|
homeserver: homeserver
|
||||||
|
@ -37,7 +37,7 @@
|
||||||
- (instancetype)initWithUser: (OFString *)user
|
- (instancetype)initWithUser: (OFString *)user
|
||||||
homeserver: (OFURL *)homeserver
|
homeserver: (OFURL *)homeserver
|
||||||
statusCode: (int)statusCode
|
statusCode: (int)statusCode
|
||||||
response: (OFDictionary<OFString *, id> *)response
|
response: (mtx_response_t)response
|
||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
|
|
||||||
|
@ -62,4 +62,11 @@
|
||||||
|
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (OFString *)description
|
||||||
|
{
|
||||||
|
return [OFString stringWithFormat:
|
||||||
|
@"Failed to log in user %@ on %@: %@",
|
||||||
|
_user, _homeserver, _response];
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|
44
src/exceptions/MTXLogoutFailedException.h
Normal file
44
src/exceptions/MTXLogoutFailedException.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Jonathan Schleifer <js@nil.im>
|
||||||
|
*
|
||||||
|
* https://fossil.nil.im/objmatrix
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice is present in all copies.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#import <ObjFW/ObjFW.h>
|
||||||
|
|
||||||
|
#import "MTXRequest.h"
|
||||||
|
|
||||||
|
OF_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@class MTXClient;
|
||||||
|
|
||||||
|
@interface MTXLogoutFailedException: OFException
|
||||||
|
@property (readonly, nonatomic) MTXClient *client;
|
||||||
|
@property (readonly, nonatomic) int statusCode;
|
||||||
|
@property (readonly, nonatomic) mtx_response_t response;
|
||||||
|
|
||||||
|
+ (instancetype)exceptionWithClient: (MTXClient *)client
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response;
|
||||||
|
- (instancetype)initWithClient: (OFString *)user
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response;
|
||||||
|
@end
|
||||||
|
|
||||||
|
OF_ASSUME_NONNULL_END
|
68
src/exceptions/MTXLogoutFailedException.m
Normal file
68
src/exceptions/MTXLogoutFailedException.m
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Jonathan Schleifer <js@nil.im>
|
||||||
|
*
|
||||||
|
* https://fossil.nil.im/objmatrix
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice is present in all copies.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#import "MTXLogoutFailedException.h"
|
||||||
|
|
||||||
|
#import "MTXClient.h"
|
||||||
|
|
||||||
|
@implementation MTXLogoutFailedException
|
||||||
|
+ (instancetype)exceptionWithClient: (MTXClient *)client
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
{
|
||||||
|
return [[[self alloc] initWithClient: client
|
||||||
|
statusCode: statusCode
|
||||||
|
response: response] autorelease];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)initWithClient: (MTXClient *)client
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
|
||||||
|
@try {
|
||||||
|
_client = [client retain];
|
||||||
|
_statusCode = statusCode;
|
||||||
|
_response = [response copy];
|
||||||
|
} @catch (id e) {
|
||||||
|
[self release];
|
||||||
|
@throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)dealloc
|
||||||
|
{
|
||||||
|
[_client release];
|
||||||
|
[_response release];
|
||||||
|
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (OFString *)description
|
||||||
|
{
|
||||||
|
return [OFString stringWithFormat:
|
||||||
|
@"Failed to log out user %@: %@", _client.userID, _response];
|
||||||
|
}
|
||||||
|
@end
|
|
@ -3,7 +3,8 @@ include ../../extra.mk
|
||||||
STATIC_PIC_LIB_NOINST = ${EXCEPTIONS_LIB_A}
|
STATIC_PIC_LIB_NOINST = ${EXCEPTIONS_LIB_A}
|
||||||
STATIC_LIB_NOINST = ${EXCEPTIONS_A}
|
STATIC_LIB_NOINST = ${EXCEPTIONS_A}
|
||||||
|
|
||||||
SRCS = MTXLoginFailedException.m
|
SRCS = MTXLoginFailedException.m \
|
||||||
|
MTXLogoutFailedException.m
|
||||||
INCLUDES = ${SRCS:.m=.h}
|
INCLUDES = ${SRCS:.m=.h}
|
||||||
|
|
||||||
include ../../buildsys.mk
|
include ../../buildsys.mk
|
||||||
|
|
|
@ -48,17 +48,22 @@ OF_APPLICATION_DELEGATE(Tests)
|
||||||
homeserver: homeserver
|
homeserver: homeserver
|
||||||
block: ^ (MTXClient *client, id exception) {
|
block: ^ (MTXClient *client, id exception) {
|
||||||
if (exception != nil) {
|
if (exception != nil) {
|
||||||
[of_stdout writeFormat: @"Error logging in: %@\n",
|
of_log(@"Error logging in: %@", exception);
|
||||||
exception];
|
|
||||||
if ([exception isKindOfClass:
|
|
||||||
MTXLoginFailedException.class])
|
|
||||||
[of_stdout writeFormat: @"Response: %@\n",
|
|
||||||
[exception response]];
|
|
||||||
[OFApplication terminateWithStatus: 1];
|
[OFApplication terminateWithStatus: 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
[of_stdout writeFormat: @"Logged in client: %@\n", client];
|
of_log(@"Logged in client: %@", client);
|
||||||
[OFApplication terminate];
|
|
||||||
|
[client asyncLogOutWithBlock: ^ (id exception) {
|
||||||
|
if (exception != nil) {
|
||||||
|
of_log(@"Failed to log out: %@\n", exception);
|
||||||
|
[OFApplication terminateWithStatus: 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
of_log(@"Logged out client");
|
||||||
|
|
||||||
|
[OFApplication terminate];
|
||||||
|
}];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue