Skip to content

Commit

Permalink
Expose rest client method for generating login tokens through MSC3882
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Oct 7, 2022
1 parent 29222ee commit 37abb8e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ public extension MXRestClient {
return URL(string: fallbackString)!
}

@nonobjc func generateLoginToken(withCompletion completion: @escaping (_ response: MXResponse<MXLoginToken>) -> Void) -> MXHTTPOperation {
return __generateLoginToken(success: currySuccess(completion), failure: curryFailure(completion))
}

/**
Reset the account password.
Expand Down
11 changes: 11 additions & 0 deletions MatrixSDK/JSONModels/MXJSONModels.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,17 @@ FOUNDATION_EXPORT NSString *const kMXPresenceOffline;

@end

/**
`MXLoginToken` represents the response of a /login/token creation request
*/
@interface MXLoginToken : MXJSONModel

@property (nonatomic) NSString *token;

@property (nonatomic) uint64_t expiresIn;

@end


@class MXPushRuleCondition;

Expand Down
29 changes: 29 additions & 0 deletions MatrixSDK/JSONModels/MXJSONModels.m
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,35 @@ - (NSDictionary *)JSONDictionary

@end

@interface MXLoginToken()

@property (nonatomic) NSDictionary *json;

@end

@implementation MXLoginToken

+ (id)modelFromJSON:(NSDictionary *)JSONDictionary
{
MXLoginToken *loginToken = [[MXLoginToken alloc] init];
if (loginToken)
{
MXJSONModelSetString(loginToken.token, JSONDictionary[@"login_token"]);
MXJSONModelSetUInt64(loginToken.expiresIn, JSONDictionary[@"expires_in"]);

MXJSONModelSetDictionary(loginToken.json, JSONDictionary);
}
return loginToken;
}

- (NSDictionary *)JSONDictionary
{
return _json;
}

@end



NSString *const kMXPushRuleActionStringNotify = @"notify";
NSString *const kMXPushRuleActionStringDontNotify = @"dont_notify";
Expand Down
11 changes: 11 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@ NS_REFINED_FOR_SWIFT;
*/
- (NSString*)loginFallback NS_REFINED_FOR_SWIFT;

/**
Generates a new login token
@param success A block object called when the operation succeeds. It provides the raw JSON response
from the server.
@param failure A block object called when the operation fails.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)generateLoginTokenWithSuccess:(void (^)(MXLoginToken *loginToken))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

/**
Reset the account password.
Expand Down
24 changes: 24 additions & 0 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,30 @@ - (NSString*)loginFallback;
return loginFallback;
}

- (MXHTTPOperation*)generateLoginTokenWithSuccess:(void (^)(MXLoginToken *))success
failure:(void (^)(NSError *error))failure
{
MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
path:[NSString stringWithFormat:@"%@/login/token", apiPathPrefix]
parameters:@{}
success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);

if (success)
{
__block MXLoginToken *loginToken;
[self dispatchProcessing:^{
MXJSONModelSetMXJSONModel(loginToken, MXLoginToken, JSONResponse);
} andCompletion:^{
success(loginToken);
}];
}
} failure:^(NSError *error) {
[self dispatchFailure:error inBlock:failure];
}];
}


#pragma mark - password update operation

Expand Down

0 comments on commit 37abb8e

Please sign in to comment.