FRAOathCode.m revision a3970d0ea62388e4ede01470a6436eb5c6c92353
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell/*
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * The contents of this file are subject to the terms of the Common Development and
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Distribution License (the License). You may not use this file except in compliance with the
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * License.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell *
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * specific language governing permission and limitations under the License.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell *
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * When distributing Covered Software, include this CDDL Header Notice in each file and include
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Header, with the fields enclosed by brackets [] replaced by your own identifying
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * information: "Portions copyright [year] [name of copyright owner]".
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell *
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Copyright 2016 ForgeRock AS.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell *
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Portions Copyright 2014 Nathaniel McCallum, Red Hat
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell */
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell#import "FRAOathCode.h"
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnellstatic uint64_t currentTimeInMilli() {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell struct timeval tv;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell if (gettimeofday(&tv, NULL) != 0) {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell return 0;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell }
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell return tv.tv_sec * 1000 + tv.tv_usec / 1000;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell@implementation FRAOathCode {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell NSString* codeText;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell uint64_t startTime;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell uint64_t endTime;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
094b0368ec64814f07f74f090192a2fe81c8d0b7Craig McDonnell- (instancetype)initWithValue:(NSString*)value startTime:(uint64_t)start endTime:(uint64_t)end {
094b0368ec64814f07f74f090192a2fe81c8d0b7Craig McDonnell _value = value;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell startTime = start * 1000;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell endTime = end * 1000;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell return self;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
094b0368ec64814f07f74f090192a2fe81c8d0b7Craig McDonnell- (float)progress {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell uint64_t now = currentTimeInMilli();
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell if (now < startTime) {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell return 0.0;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell }
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell if (now < endTime) {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell float totalTime = (float) (endTime - startTime);
094b0368ec64814f07f74f090192a2fe81c8d0b7Craig McDonnell return (now - startTime) / totalTime;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell }
094b0368ec64814f07f74f090192a2fe81c8d0b7Craig McDonnell return 1.0;
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
a3970d0ea62388e4ede01470a6436eb5c6c92353Craig McDonnell- (BOOL)hasExpired {
a3970d0ea62388e4ede01470a6436eb5c6c92353Craig McDonnell return [self progress] == 1.0;
a3970d0ea62388e4ede01470a6436eb5c6c92353Craig McDonnell}
a3970d0ea62388e4ede01470a6436eb5c6c92353Craig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell@end