I would like to make use of AppAuth’s performActionWithFreshTokens
methodology. This requires utilizing the OIDAuthState returned by the authStateByPresentingAuthorizationRequest
when producing entry and refresh tokens. I wish to save that OIDAuthState within the Keychain for safety causes, then use it at a later time when a token refresh is critical. What I can not seem to do is correctly archive and unarchive the saved worth from the Keychain.
I’ve two strategies: (1) used to transform the OIDAuthState to an NSString to retailer to the Keychain and (2) used to transform the NSString from the Keychain to an OIDAuthState. Right here is the primary:
NSData *checkEncoding;
- (NSString *)authStateToString:(OIDAuthState *)authState {
NSError *errRet;
//NSData *authStateData = [NSKeyedArchiver archivedDataWithRootObject:authState];
NSData *authStateData = [NSKeyedArchiver archivedDataWithRootObject:authState
requiringSecureCoding:NO error:&errRet];
checkEncoding = authStateData;
NSString *authString = [[NSString alloc] initWithData:authStateData
encoding:NSUnicodeStringEncoding];
NSLog(@"ToString size = %ld, authStateData size = %ld",
(unsigned lengthy)authString.size, (unsigned lengthy)authStateData.size);
return authString;
}
And right here is the second:
- (OIDAuthState *)stringToAuthState:(NSString *)authString {
NSError *errRet;
NSData *authStateData = [authString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
NSLog(@"ToAuthState size = %ld, authStateData size = %ld, checkEncoding = %d",
(unsigned lengthy)authString.size, (unsigned lengthy)authStateData.size, [authStateData isEqualToData:checkEncoding]);
OIDAuthState *authState = [NSKeyedUnarchiver unarchivedObjectOfClass:[OIDAuthState class]
fromData:authStateData error:&errRet];
//OIDAuthState *authState = [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:authStateData error:&errRet];
//OIDAuthState *authState = [NSKeyedUnarchiver unarchiveObjectWithData:authStateData];
/* NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:authStateData];
OIDAuthState *authState = [unarchiver decodeObject];
[unarchiver finishDecoding]; */
return authState;
}
(The a number of tries at unarchiving do not come into play since all of them fail and the uncommented model might be the appropriate/undeprecated one.)
Observe the NSLog statements in every, which appear to level out the issue. For the authStateToString
NSLog, the output is “ToString size = 7930, authStateData size = 15860”. For the stringToAuthState
NSLog, the output is “ToAuthState size = 7930, authStateData size = 15862, checkEncoding = 0”. The NSStrings in each strategies are equal in accordance with isEqualToString
.
So, I am guessing that the OIDAuthState cannot be regenerated as a result of the conversion for the NSString variations is occuring however is not regenerated the identical NSData illustration; the encoded model is 15860 bytes whereas the decoded model is 15862 bytes and the isEqualToData
returns false.
I do observe that the OIDAuthState class NSKeyedArchiver methodology encodeWithCoder
is named as anticipated when encoding the state. The corresponding NSKeyedUnarchiver methodology initWithCoder
is not known as however that is not shocking because the NSData being handed might be not legitimate.
I’ve tried a number of dataUsingEncoding
encoding sorts with most failing (the ensuing NSData is zero-length). NSUnicodeStringEncoding
is the one one which appeared to supply applicable size NSData cases. I’ve tried setting ‘requiringSecureCoding’ to each YES and NO. I’ve tried setting ‘allowLossyConversion’ to each YES and NO.
I’m open to different strategies of persisting the OIDAuthState nevertheless it appears what I’ve finished needs to be legitimate. Can somebody recommend why I can not seem to appropriately unarchive?