Skip to content

Commit 817b9b9

Browse files
authored
Merge pull request #1443 from matrix-org/doug/authentication
Add MXUsernameAvailability and an msisdn MXLoginFlowType.
2 parents e872114 + 47905dc commit 817b9b9

File tree

14 files changed

+179
-10
lines changed

14 files changed

+179
-10
lines changed

MatrixSDK/Contrib/Swift/JSONModels/MXJSONModels.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum MXLoginFlowType: Equatable, Hashable {
2323
case recaptcha
2424
case OAuth2
2525
case emailIdentity
26+
case msisdn
2627
case token
2728
case dummy
2829
case emailCode
@@ -34,6 +35,7 @@ public enum MXLoginFlowType: Equatable, Hashable {
3435
case .recaptcha: return kMXLoginFlowTypeRecaptcha
3536
case .OAuth2: return kMXLoginFlowTypeOAuth2
3637
case .emailIdentity: return kMXLoginFlowTypeEmailIdentity
38+
case .msisdn: return kMXLoginFlowTypeMSISDN
3739
case .token: return kMXLoginFlowTypeToken
3840
case .dummy: return kMXLoginFlowTypeDummy
3941
case .emailCode: return kMXLoginFlowTypeEmailCode
@@ -42,8 +44,8 @@ public enum MXLoginFlowType: Equatable, Hashable {
4244
}
4345

4446
public init(identifier: String) {
45-
let flowTypess: [MXLoginFlowType] = [.password, .recaptcha, .OAuth2, .emailIdentity, .token, .dummy, .emailCode]
46-
self = flowTypess.first(where: { $0.identifier == identifier }) ?? .other(identifier)
47+
let flowTypes: [MXLoginFlowType] = [.password, .recaptcha, .OAuth2, .emailIdentity, .msisdn, .token, .dummy, .emailCode]
48+
self = flowTypes.first(where: { $0.identifier == identifier }) ?? .other(identifier)
4749
}
4850
}
4951

MatrixSDK/Contrib/Swift/MXRestClient.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,25 @@ public extension MXRestClient {
107107

108108
- returns: a `MXHTTPOperation` instance.
109109
*/
110+
@available(*, deprecated, message: "Use isUsernameAvailable instead which calls the dedicated API and provides more information.")
110111
@nonobjc @discardableResult func isUserNameInUse(_ username: String, completion: @escaping (_ inUse: Bool) -> Void) -> MXHTTPOperation {
111112
return __isUserName(inUse: username, callback: completion)
112113
}
113114

115+
/**
116+
Checks whether a username is available.
117+
118+
- parameters:
119+
- username: The user name to test.
120+
- completion: A block object called when the operation is completed.
121+
- response: Provides the server response as an `MXUsernameAvailability` instance.
122+
123+
- returns: a `MXHTTPOperation` instance.
124+
*/
125+
@nonobjc @discardableResult func isUsernameAvailable(_ username: String, completion: @escaping (_ response: MXResponse<MXUsernameAvailability>) -> Void) -> MXHTTPOperation {
126+
return __isUsernameAvailable(username, success: currySuccess(completion), failure: curryFailure(completion))
127+
}
128+
114129
/**
115130
Get the list of register flows supported by the home server.
116131

MatrixSDK/Data/AutoDiscovery/MXAutoDiscovery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ NS_ASSUME_NONNULL_BEGIN
5555
failure:(void (^)(NSError *error))failure;;
5656

5757
/**
58-
Get the wellknwon data of the homeserver.
58+
Get the wellknown data of the homeserver.
5959
6060
@param success A block object called when the operation succeeds. It provides
6161
the wellknown data.

MatrixSDK/JSONModels/Login/MXLoginSSOFlow.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ + (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary
3232

3333
if (loginFlow)
3434
{
35-
NSArray *jsonIdentityProdivers;
35+
NSArray *jsonIdentityProviders;
3636

37-
MXJSONModelSetArray(jsonIdentityProdivers, JSONDictionary[MXLoginSSOFlowIdentityProvidersKey]);
37+
MXJSONModelSetArray(jsonIdentityProviders, JSONDictionary[MXLoginSSOFlowIdentityProvidersKey]);
3838

3939
NSArray<MXLoginSSOIdentityProvider*> *identityProviders;
4040

41-
if (jsonIdentityProdivers)
41+
if (jsonIdentityProviders)
4242
{
43-
identityProviders = [MXLoginSSOIdentityProvider modelsFromJSON:jsonIdentityProdivers];
43+
identityProviders = [MXLoginSSOIdentityProvider modelsFromJSON:jsonIdentityProviders];
4444
}
4545

4646
if (!identityProviders)

MatrixSDK/JSONModels/MXJSONModels.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ FOUNDATION_EXPORT NSString *const kMXLoginIdentifierTypePhone;
173173

174174
@end
175175

176+
/**
177+
`MXUsernameAvailability` represents the response returned when checking for username availability.
178+
*/
179+
@interface MXUsernameAvailability : MXJSONModel
180+
181+
/**
182+
A flag to indicate that the username is available. This should always be true when the server replies with 200 OK.
183+
*/
184+
@property (nonatomic) BOOL available;
185+
186+
@end
187+
176188
/**
177189
`MXAuthenticationSession` represents an authentication session returned by the home server.
178190
*/

MatrixSDK/JSONModels/MXJSONModels.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ + (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary
150150

151151
@end
152152

153+
@implementation MXUsernameAvailability
154+
155+
+ (id)modelFromJSON:(NSDictionary *)JSONDictionary
156+
{
157+
MXUsernameAvailability *availability = [[MXUsernameAvailability alloc] init];
158+
if (availability)
159+
{
160+
MXJSONModelSetBoolean(availability.available, JSONDictionary[@"available"]);
161+
}
162+
163+
return availability;
164+
}
165+
166+
@end
167+
153168
@implementation MXAuthenticationSession
154169

155170
+ (id)modelFromJSON:(NSDictionary *)JSONDictionary

MatrixSDK/MXError.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ FOUNDATION_EXPORT NSString *const kMXErrCodeStringNotJSON;
3232
FOUNDATION_EXPORT NSString *const kMXErrCodeStringNotFound;
3333
FOUNDATION_EXPORT NSString *const kMXErrCodeStringLimitExceeded;
3434
FOUNDATION_EXPORT NSString *const kMXErrCodeStringUserInUse;
35+
FOUNDATION_EXPORT NSString *const kMXErrCodeStringInvalidUsername;
36+
/// The resource being requested is reserved by an application service, or the application service making the request has not created the resource.
37+
FOUNDATION_EXPORT NSString *const kMXErrCodeStringExclusiveResource;
3538
FOUNDATION_EXPORT NSString *const kMXErrCodeStringRoomInUse;
3639
FOUNDATION_EXPORT NSString *const kMXErrCodeStringBadPagination;
3740
FOUNDATION_EXPORT NSString *const kMXErrCodeStringUnauthorized;

MatrixSDK/MXError.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
NSString *const kMXErrCodeStringNotFound = @"M_NOT_FOUND";
3030
NSString *const kMXErrCodeStringLimitExceeded = @"M_LIMIT_EXCEEDED";
3131
NSString *const kMXErrCodeStringUserInUse = @"M_USER_IN_USE";
32+
NSString *const kMXErrCodeStringInvalidUsername = @"M_INVALID_USERNAME";
33+
NSString *const kMXErrCodeStringExclusiveResource = @"M_EXCLUSIVE";
3234
NSString *const kMXErrCodeStringRoomInUse = @"M_ROOM_IN_USE";
3335
NSString *const kMXErrCodeStringBadPagination = @"M_BAD_PAGINATION";
3436
NSString *const kMXErrCodeStringUnauthorized = @"M_UNAUTHORIZED";

MatrixSDK/MXRestClient.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,27 @@ NS_REFINED_FOR_SWIFT;
325325
/**
326326
Check whether a username is already in use.
327327
328-
@username the user name to test (This value must not be nil).
328+
@param username the user name to test (This value must not be nil).
329329
@param callback A block object called when the operation is completed.
330330
331331
@return a MXHTTPOperation instance.
332332
*/
333333
- (MXHTTPOperation*)isUserNameInUse:(NSString*)username
334-
callback:(void (^)(BOOL isUserNameInUse))callback NS_REFINED_FOR_SWIFT;
334+
callback:(void (^)(BOOL isUserNameInUse))callback NS_REFINED_FOR_SWIFT __deprecated_msg("Use isUsernameAvailable instead.");
335+
336+
/**
337+
Checks whether a username is available.
338+
339+
@param username the user name to test (This value must not be nil).
340+
@param success A block object called when the operation succeeds. It provides the server response
341+
as an MXUsernameAvailability instance.
342+
@param failure A block object called when the operation fails.
343+
344+
@return a MXHTTPOperation instance.
345+
*/
346+
- (MXHTTPOperation*)isUsernameAvailable:(NSString*)username
347+
success:(void (^)(MXUsernameAvailability *availability))success
348+
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;
335349
/**
336350
Get the list of register flows supported by the home server.
337351

MatrixSDK/MXRestClient.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,34 @@ - (MXHTTPOperation*)isUserNameInUse:(NSString*)username
597597
}];
598598
}
599599

600+
- (MXHTTPOperation*)isUsernameAvailable:(NSString*)username
601+
success:(void (^)(MXUsernameAvailability *availability))success
602+
failure:(void (^)(NSError *error))failure
603+
{
604+
NSDictionary* parameters = @{@"username": username};
605+
606+
MXWeakify(self);
607+
return [httpClient requestWithMethod:@"GET"
608+
path:[NSString stringWithFormat:@"%@/register/available", apiPathPrefix]
609+
parameters:parameters
610+
success:^(NSDictionary *JSONResponse) {
611+
MXStrongifyAndReturnIfNil(self);
612+
613+
if (success)
614+
{
615+
__block MXUsernameAvailability *availability;
616+
[self dispatchProcessing:^{
617+
MXJSONModelSetMXJSONModel(availability, MXUsernameAvailability, JSONResponse);
618+
} andCompletion:^{
619+
success(availability);
620+
}];
621+
}
622+
}
623+
failure:^(NSError *error) {
624+
[self dispatchFailure:error inBlock:failure];
625+
}];
626+
}
627+
600628
- (MXHTTPOperation*)getRegisterSession:(void (^)(MXAuthenticationSession *authSession))success
601629
failure:(void (^)(NSError *error))failure
602630
{

0 commit comments

Comments
 (0)