|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// This code is licensed under the MIT License. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files(the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions : |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +#import "MSIDBrokerFlightProvider.h" |
| 26 | +#import "NSJSONSerialization+MSIDExtensions.h" |
| 27 | + |
| 28 | +@interface MSIDBrokerFlightProvider() |
| 29 | + |
| 30 | +@property (nonatomic, nullable, readonly) NSDictionary *clientFlightsPayload; |
| 31 | + |
| 32 | +@end |
| 33 | + |
| 34 | +@implementation MSIDBrokerFlightProvider |
| 35 | + |
| 36 | +- (instancetype _Nullable)initWithBase64EncodedFlightsPayload:(nullable NSString *)base64EncodedFlightsPayload |
| 37 | +{ |
| 38 | + self = [super init]; |
| 39 | + |
| 40 | + if (self) |
| 41 | + { |
| 42 | + if ([NSString msidIsStringNilOrBlank:base64EncodedFlightsPayload]) |
| 43 | + { |
| 44 | + MSID_LOG_WITH_CTX(MSIDLogLevelInfo,nil, @"Broker client flights is nil or empty"); |
| 45 | + return nil; |
| 46 | + } |
| 47 | + |
| 48 | + NSDictionary *clientFlightsDict = nil; |
| 49 | + |
| 50 | + NSData *decodedJsonData = [[base64EncodedFlightsPayload msidBase64UrlDecode] dataUsingEncoding:NSUTF8StringEncoding]; |
| 51 | + if (decodedJsonData && [decodedJsonData length]) |
| 52 | + { |
| 53 | + clientFlightsDict = [NSJSONSerialization msidNormalizedDictionaryFromJsonData:decodedJsonData error:nil]; |
| 54 | + |
| 55 | + if (![clientFlightsDict isKindOfClass:[NSDictionary class]]) |
| 56 | + { |
| 57 | + MSID_LOG_WITH_CTX(MSIDLogLevelWarning,nil, @"Invalid broker client flight format"); |
| 58 | + return nil; |
| 59 | + } |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + MSID_LOG_WITH_CTX(MSIDLogLevelWarning,nil, @"Failed to decode base64encoded client flights from broker"); |
| 64 | + return nil; |
| 65 | + } |
| 66 | + |
| 67 | + if (clientFlightsDict) |
| 68 | + { |
| 69 | + _clientFlightsPayload = clientFlightsDict; |
| 70 | + MSID_LOG_WITH_CTX(MSIDLogLevelInfo, nil, @"Client flights from broker is decoded successfully"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return self; |
| 75 | +} |
| 76 | + |
| 77 | +#pragma mark - MSIDFlightManagerInterface |
| 78 | + |
| 79 | +- (BOOL)boolForKey:(nonnull NSString *)flightKey |
| 80 | +{ |
| 81 | + if (self.clientFlightsPayload) |
| 82 | + { |
| 83 | + id value = self.clientFlightsPayload[flightKey]; |
| 84 | + |
| 85 | + if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) |
| 86 | + { |
| 87 | + return [value boolValue]; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return NO; |
| 92 | +} |
| 93 | + |
| 94 | +- (nullable NSString *)stringForKey:(nonnull NSString *)flightKey |
| 95 | +{ |
| 96 | + if (self.clientFlightsPayload) |
| 97 | + { |
| 98 | + id value = self.clientFlightsPayload[flightKey]; |
| 99 | + |
| 100 | + if ([value isKindOfClass:[NSString class]]) |
| 101 | + { |
| 102 | + return value; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return nil; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +@end |
0 commit comments