/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2016 ForgeRock AS.
*/
#import "FRAError.h"
#import "FRAIdentity.h"
#import "FRAIdentityDatabase.h"
#import "FRAMechanismFactory.h"
#import "FRAMessageUtils.h"
#import "FRAMockURLProtocol.h"
#import "FRAPushMechanism.h"
#import "FRAPushMechanismFactory.h"
#import "FRAQRUtils.h"
#import "FRASerialization.h"
/*! QR code key for the secret. */
/*! QR code key for the authentication endpoint url. */
/*! QR code key for the regsitration endpoint url. */
/*! QR code key for the message. */
/*! QR code key for the background colour of the mechanism. */
/*! QR code key for the registration challange. */
/*! QR code key for the registration challange. */
/*! QR code key for the mechanism image. */
/*! QR code key for the issuer name. */
@interface FRAPushMechanismFactory ()
@end
}
if (self) {
}
return self;
}
}
- (FRAMechanism *) buildMechanism:(NSURL *)uri database:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel handler:(void (^)(BOOL, NSError *))handler error:(NSError *__autoreleasing *)error {
*error = [FRAError createError:NSLocalizedString(@"Cannot register while notifications are disabled or the device is offline", nil) code:FRAMissingDeviceId];
return nil;
}
NSString *regEndpoint = [self utf8StringFromData:[FRAQRUtils decodeURL:[query objectForKey:REGISTRATION_ENDPOINT_URL_QR_KEY]]];
NSString *authEndpoint = [self utf8StringFromData:[FRAQRUtils decodeURL:[query objectForKey:AUTHENTICATION_ENDPOINT_URL_QR_KEY]]];
NSString *challenge = [FRAQRUtils replaceCharactersForURLDecoding:[query objectForKey:REGISTRATION_CHALLENGE_QR_KEY]];
NSString *loadBalancer = [self utf8StringFromData:[FRAQRUtils decodeURL:[query objectForKey:REGISTRATION_LOAD_BALLANCE_KEY]]];
NSString *image = [self utf8StringFromData:[FRAQRUtils decodeURL:[query objectForKey:IMAGE_QR_KEY]]];
NSString *issuer = [self utf8StringFromData:[FRAQRUtils decodeURL:[query objectForKey:ISSUER_QR_KEY]]];
if (![self isValidSecret:secret] || ![self isValid:regEndpoint] || ![self isValid:authEndpoint] || ![self isValid:messageId] || ![self isValid:challenge] || ![self isValid:issuer]) {
return nil;
}
FRAPushMechanism* mechanism = [FRAPushMechanism pushMechanismWithDatabase:database identityModel:identityModel authEndpoint:authEndpoint secret:secret];
FRAIdentity *identity = [self identityWithIssuer:issuer accountName:_label identityModel:identityModel backgroundColor:backgroundColor image:image database:database error:error];
return nil;
}
[self registerMechanismWithEndpoint:regEndpoint secret:secret challenge:challenge messageId:messageId mechanismUid:mechanism.mechanismUID identity:identity mechanism:mechanism identityModel:identityModel loadBalancerCookieData:loadBalancer handler:handler];
return mechanism;
}
return [self isValid:secret] && [FRAQRUtils isBase64:[FRAQRUtils replaceCharactersForURLDecoding:secret]];
}
}
return nil;
}
return nil;
}
// Get the path and strip it of its leading '/'
return nil;
}
}
return nil;
}
// Get issuer and label
return nil;
}
} else {
_issuer = @"";
}
// Parse query
// Value can contain '=' symbols, so look for first symbol.
continue;
}
}
return query;
}
- (FRAIdentity *)identityWithIssuer:(NSString *)issuer accountName:(NSString *)accountName identityModel:(FRAIdentityModel *)identityModel backgroundColor:(NSString *)backgroundColor image:(NSString *)image database:(FRAIdentityDatabase *)database error:(NSError *__autoreleasing *)error {
if (!identity) {
identity = [FRAIdentity identityWithDatabase:database identityModel:identityModel accountName:accountName issuer:issuer image:[NSURL URLWithString:image] backgroundColor:backgroundColor];
return nil;
}
}
return identity;
}
return NO;
}
return YES;
}
- (NSString *)getSupportedProtocol {
return @"pushauth";
}
- (void)registerMechanismWithEndpoint:(NSString *)regEndpoint secret:(NSString *)secret challenge:(NSString *)challenge messageId:(NSString *)messageId mechanismUid:(NSString *)uid identity:(FRAIdentity *)identity mechanism:(FRAMechanism *)mechanism identityModel:(FRAIdentityModel *)identityModel loadBalancerCookieData:(NSString *)loadBalancerCookieData handler:(void(^)(BOOL, NSError *))handler {
@"mechanismUid":uid,
@"deviceType":@"ios",
@"communicationType":@"apns"
}
if (200 != statusCode) {
error = [FRAError createError:@"Failed to contact server for registration" code:FRANetworkFailure underlyingError:error];
} else {
}
}];
}
- (void)invokeRegistrationHandler:(void(^)(BOOL, NSError *))handler result:(BOOL)result error:(NSError *) error {
if (handler) {
}
}
@end