/*
* 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.
*/
#include "base32.h"
#import "FRAError.h"
#import "FRAHotpOathMechanism.h"
#import "FRAIdentity.h"
#import "FRAIdentityDatabase.h"
#import "FRAIdentityModel.h"
#import "FRAMechanismFactory.h"
#import "FRAOathMechanismFactory.h"
#import "FRAQRUtils.h"
#import "FRATotpOathMechanism.h"
- (FRAMechanism *) buildMechanism:(NSURL *)uri database:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel handler:(void (^)(BOOL, NSError *))handler error:(NSError *__autoreleasing *)error {
NSString *issuer = parseIssuer([query objectForKey:@"_issuer"], [query objectForKey:@"issuer"], label);
if(![self hasValidType:_type key:key issuer:issuer counter:counter algorithm:algorithm digits:_digits period:period backgroundColor:backgroundColor]) {
if (error) {
}
return nil;
}
FRAIdentity *identity = [self identityWithIssuer:issuer accountName:label identityModel:identityModel backgroundColor:backgroundColor image:image database:database error:error];
return nil;
}
return mechanism;
}
if (scheme == nil || !([scheme isEqualToString:@"otpauth"] || [scheme isEqualToString:@"pushauth"])) {
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;
}
/*!
* Checks if parameters are valid.
*/
if (!algorithm || key.length == 0 || !digits || (issuer.length == 0) || !isValidBackgroundColor(backgroundColor)) {
return NO;
}
return counter;
}
return period;
}
/*!
* Checks if mechanism type is HOTP.
*/
}
/*!
* Checks if mechanism type is TOTP.
*/
}
/*!
* Make a mechanism from the required data
*/
return [FRAHotpOathMechanism mechanismWithDatabase:database identityModel:identityModel secretKey:key HMACAlgorithm:algorithm codeLength:codeLength counter:counter];
} else {
return [FRATotpOathMechanism mechanismWithDatabase:database identityModel:identityModel secretKey:key HMACAlgorithm:algorithm codeLength:codeLength period:period];
}
}
- (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;
}
if (handler) {
}
}
return false;
}
return true;
}
- (NSString *) getSupportedProtocol {
return @"otpauth";
}
return nil;
}
return nil;
}
return nil;
}
}
}
NSDictionary<NSString *, NSNumber *> *supportedAlgorithms = @{@"md5": [NSNumber numberWithInt:kCCHmacAlgMD5],
}
}
return nil;
}
}
return nil;
}
}
}
return nil;
}
if (intPeriod == 0) {
return nil;
}
}
}
static NSString* parseIssuer(NSString* issuerPrefix, NSString* issuerParameter, NSString* accountName) {
return issuerPrefix;
}
return issuerParameter;
}
return accountName;
}
return YES;
}
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9a-fA-F]{6}$" options:0 error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:color options:0 range:NSMakeRange(0, [color length])];
if (numberOfMatches == 1) {
return YES;
};
return NO;
}
@end