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

Expose rest client method for generating login tokens through MSC3882 #1601

Merged
merged 2 commits into from
Oct 10, 2022
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
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:@"%@/org.matrix.msc3882/login/token", kMXAPIPrefixPathUnstable]
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
1 change: 1 addition & 0 deletions changelog.d/pr-1601.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose rest client method for generating login tokens through MSC3882