Skip to content

Support NSSecureCoding for auth credentials #2242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Auth/Sample/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2310,11 +2310,11 @@ - (void)getUserTokenResultWithForce:(BOOL)force {
}

/** @fn getAppTokenWithForce:
@brief Gets the token from @c FIRApp , optionally a refreshed one.
@brief Gets the token from @c FIRAuth , optionally a refreshed one.
@param force Whether the refresh is forced or not.
*/
- (void)getAppTokenWithForce:(BOOL)force {
[[FIRApp defaultApp] getTokenForcingRefresh:force withCallback:[self tokenCallback]];
[[FIRAuth auth] getTokenForcingRefresh:force withCallback:[self tokenCallback]];
}

/** @fn setDisplayName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIREmailPasswordAuthCredential
@brief Internal implementation of FIRAuthCredential for Email/Password credentials.
*/
@interface FIREmailPasswordAuthCredential : FIRAuthCredential
@interface FIREmailPasswordAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @property email
@brief The user's email address.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,30 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
@"Attempt to call prepareVerifyAssertionRequest: on a FIREmailPasswordAuthCredential."];
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *email = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"email"];
NSString *password = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"password"];
NSString *link = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"link"];
if (email.length && password.length) {
self = [self initWithEmail:email password:password];
} else if (email.length && link.length) {
self = [self initWithEmail:email link:link];
} else {
self = nil;
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.email forKey:@"email"];
[aCoder encodeObject:self.password forKey:@"password"];
[aCoder encodeObject:self.link forKey:@"link"];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIRFacebookAuthCredential
@brief Internal implementation of FIRAuthCredential for the Facebook IdP.
*/
@interface FIRFacebookAuthCredential : FIRAuthCredential
@interface FIRFacebookAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @fn initWithAccessToken:
@brief Designated initializer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
request.providerAccessToken = _accessToken;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
self = [self initWithAccessToken:accessToken];
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_accessToken forKey:@"accessToken"];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIRGameCenterAuthCredential
@brief Internal implementation of FIRAuthCredential for Game Center credentials.
*/
@interface FIRGameCenterAuthCredential : FIRAuthCredential
@interface FIRGameCenterAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @property playerID
@brief The ID of the Game Center local player.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,35 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
@"Attempt to call prepareVerifyAssertionRequest: on a FIRGameCenterAuthCredential."];
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *playerID = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"playerID"];
NSURL *publicKeyURL = [aDecoder decodeObjectOfClass:[NSURL class] forKey:@"publicKeyURL"];
NSData *signature = [aDecoder decodeObjectOfClass:[NSData class] forKey:@"signature"];
NSData *salt = [aDecoder decodeObjectOfClass:[NSData class] forKey:@"salt"];
NSNumber *timestamp = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:@"timestamp"];
NSString *displayName = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"displayName"];
self = [self initWithPlayerID:playerID
publicKeyURL:publicKeyURL
signature:signature
salt:salt
timestamp:timestamp.unsignedLongLongValue
displayName:displayName];
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.playerID forKey:@"playerID"];
[aCoder encodeObject:self.publicKeyURL forKey:@"publicKeyURL"];
[aCoder encodeObject:self.signature forKey:@"signature"];
[aCoder encodeObject:self.salt forKey:@"salt"];
[aCoder encodeObject:[NSNumber numberWithUnsignedLongLong:self.timestamp] forKey:@"timestamp"];
[aCoder encodeObject:self.displayName forKey:@"displayName"];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIRGitHubAuthCredential
@brief Internal implementation of FIRAuthCredential for GitHub credentials.
*/
@interface FIRGitHubAuthCredential : FIRAuthCredential
@interface FIRGitHubAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @property token
@brief The GitHub OAuth access token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
request.providerAccessToken = _token;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *token = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"token"];
self = [self initWithToken:token];
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.token forKey:@"token"];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIRGoogleAuthCredential
@brief Internal implementation of FIRAuthCredential for the Google IdP.
*/
@interface FIRGoogleAuthCredential : FIRAuthCredential
@interface FIRGoogleAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @fn initWithIDToken:accessToken:
@brief Designated initializer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
request.providerAccessToken = _accessToken;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *IDToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"IDToken"];
NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
self = [self initWithIDToken:IDToken accessToken:accessToken];
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_IDToken forKey:@"IDToken"];
[aCoder encodeObject:_accessToken forKey:@"accessToken"];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIROAuthCredential
@brief Internal implementation of FIRAuthCredential for generic credentials.
*/
@interface FIROAuthCredential : FIRAuthCredential
@interface FIROAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @property IDToken
@brief The ID Token associated with this credential.
Expand Down
18 changes: 18 additions & 0 deletions Firebase/Auth/Source/AuthProviders/OAuth/FIROAuthCredential.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
request.providerAccessToken = _accessToken;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *IDToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"IDToken"];
NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
self = [self initWithProviderID:self.provider IDToken:IDToken accessToken:accessToken];
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.IDToken forKey:@"IDToken"];
[aCoder encodeObject:self.accessToken forKey:@"accessToken"];
}

NS_ASSUME_NONNULL_END

@end
28 changes: 28 additions & 0 deletions Firebase/Auth/Source/AuthProviders/Phone/FIRPhoneAuthCredential.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ - (instancetype)initWithProviderID:(NSString *)providerID
return self;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *verificationID = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"verificationID"];
NSString *verificationCode = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"verificationCode"];
NSString *temporaryProof = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"temporaryProof"];
NSString *phoneNumber = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"phoneNumber"];
if (temporaryProof.length && phoneNumber.length) {
self = [self initWithTemporaryProof:temporaryProof phoneNumber:phoneNumber providerID:self.provider];
} else if (verificationID.length && verificationCode.length) {
self = [self initWithProviderID:self.provider verificationID:verificationID verificationCode:verificationCode];
} else {
self = nil;
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.verificationID forKey:@"verificationID"];
[aCoder encodeObject:self.verificationCode forKey:@"verificationCode"];
[aCoder encodeObject:self.temporaryProof forKey:@"temporaryProof"];
[aCoder encodeObject:self.phoneNumber forKey:@"phoneNumber"];
}

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
/** @class FIRTwitterAuthCredential
@brief Internal implementation of FIRAuthCredential for Twitter credentials.
*/
@interface FIRTwitterAuthCredential : FIRAuthCredential
@interface FIRTwitterAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @property token
@brief The Twitter OAuth token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
request.providerOAuthTokenSecret = _secret;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *token = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"token"];
NSString *secret = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"secret"];
self = [self initWithToken:token secret:secret];
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.token forKey:@"token"];
[aCoder encodeObject:self.secret forKey:@"secret"];
}

@end
2 changes: 1 addition & 1 deletion Firebase/Auth/Source/FIRAuthCredential.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ @implementation FIRAuthCredential
- (instancetype)init {
@throw [NSException exceptionWithName:@"Attempt to call unavailable initializer."
reason:@"This class is an abstract base class. It's init method "
"should not be called directly."
"should not be called directly."
userInfo:nil];
}

Expand Down
2 changes: 1 addition & 1 deletion Firebase/Auth/Source/Public/FIRPhoneAuthCredential.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
@brief Implementation of FIRAuthCredential for Phone Auth credentials.
*/
NS_SWIFT_NAME(PhoneAuthCredential)
@interface FIRPhoneAuthCredential : FIRAuthCredential
@interface FIRPhoneAuthCredential : FIRAuthCredential <NSSecureCoding>

/** @fn init
@brief This class is not supposed to be instantiated directly.
Expand Down