Skip to content

Commit cb196ef

Browse files
authored
Merge pull request #3941 from wmathurin/zero_scopes
Saving scopes with rest of credentials
2 parents c83d097 + c16a7f6 commit cb196ef

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ - (NSString *)devInfoTitleString
492492
@"IDP Enabled", [self idpEnabled] ? @"YES" : @"NO",
493493
@"Identity Provider", [self isIdentityProvider] ? @"YES" : @"NO",
494494
@"Current User", [self userToString:userAccountManager.currentUser],
495+
@"Scopes", [userAccountManager.currentUser.credentials.scopes componentsJoinedByString:@" "],
495496
@"Access Token Expiration", [self accessTokenExpiration],
496497
@"Authenticated Users", [self usersToString:userAccountManager.allUserAccounts],
497498
@"User Key-Value Stores", [self safeJoin:[SFSDKKeyValueEncryptedFileStore allStoreNames] separator:@", "],

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/OAuth/SFOAuthCredentials+Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ extern NSException * _Nullable SFOAuthInvalidIdentifierException(void);
5959
@property (nonatomic, readwrite, nullable) NSString *organizationId;
6060
@property (nonatomic, readwrite, nullable) NSURL *instanceUrl;
6161
@property (nonatomic, readwrite, nullable) NSURL *apiInstanceUrl;
62+
@property (nonatomic, readwrite, nullable) NSArray<NSString *> *scopes;
6263
@property (nonatomic, readwrite, nullable) NSString *communityId;
6364
@property (nonatomic, readwrite, nullable) NSURL *communityUrl;
6465
@property (nonatomic, readwrite, nullable) NSDate *issuedAt;

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/OAuth/SFOAuthCredentials.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ NS_SWIFT_NAME(OAuthCredentials)
146146
147147
This property is available after authentication has successfully completed.
148148
149-
@warning The setter for this property is exposed publicly only for unit tests. Client code should not set this property.
150149
@exception NSInternalInconsistencyException If accessed while the identifier property is `nil`.
151150
*/
152151
@property (nonatomic, readonly, nullable) NSString *organizationId;
@@ -157,8 +156,6 @@ NS_SWIFT_NAME(OAuthCredentials)
157156
158157
This is the URL that client requests should be made to after authentication completes.
159158
This property is set by the `SFOAuthCoordinator` after authentication has successfully completed.
160-
161-
@warning The setter for this property is exposed publicly only for unit tests. Client code should not set this property.
162159
*/
163160
@property (nonatomic, readonly, nullable) NSURL *instanceUrl;
164161

@@ -167,11 +164,16 @@ NS_SWIFT_NAME(OAuthCredentials)
167164
This is the URL that client SFAP requests should be made to after authentication completes.
168165
This property is set by the `SFOAuthCoordinator` after authentication has successfully completed.
169166
This URL is only defined when sfap_api scope is used.
170-
171-
@warning The setter for this property is exposed publicly only for unit tests. Client code should not set this property.
172167
*/
173168
@property (nonatomic, readonly, nullable) NSURL *apiInstanceUrl;
174169

170+
/** The OAuth scopes granted for this session.
171+
172+
This property contains the list of OAuth scopes that were granted during authentication.
173+
This property is set by the `SFOAuthCoordinator` after authentication has successfully completed.
174+
*/
175+
@property (nonatomic, readonly, nullable) NSArray<NSString *> *scopes;
176+
175177
/** The community ID the user choose to log into. This usually happens when the user
176178
logs into the app using a community-based login page
177179
@@ -189,8 +191,6 @@ NS_SWIFT_NAME(OAuthCredentials)
189191
/** The timestamp when the session access token was issued.
190192
191193
This property is set by the `SFOAuthCoordinator` after authentication has successfully completed.
192-
193-
@warning The setter for this property is exposed publicly only for unit tests. Client code should not set this property.
194194
*/
195195
@property (nonatomic, readonly, nullable) NSDate *issuedAt;
196196

@@ -199,8 +199,6 @@ NS_SWIFT_NAME(OAuthCredentials)
199199
that the user belongs to, and userID is the Salesforce user ID.
200200
201201
This property is set by the `SFOAuthCoordinator` after authentication has successfully completed.
202-
203-
@warning The setter for this property is exposed publicly only for unit tests. Client code should not set this property.
204202
*/
205203
@property (nonatomic, readonly, nullable) NSURL *identityUrl;
206204

@@ -212,8 +210,6 @@ NS_SWIFT_NAME(OAuthCredentials)
212210
component of the identityUrl.
213211
214212
This property is available after authentication has successfully completed.
215-
216-
@warning The setter for this property is exposed publicly only for unit tests. Client code should not set this property.
217213
*/
218214
@property (nonatomic, readonly, nullable) NSString *userId;
219215

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/OAuth/SFOAuthCredentials.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ @implementation SFOAuthCredentials
6363
@synthesize userId = _userId; // cached user ID derived from identityURL
6464
@synthesize instanceUrl = _instanceUrl;
6565
@synthesize apiInstanceUrl = _apiInstanceUrl;
66+
@synthesize scopes = _scopes;
6667
@synthesize issuedAt = _issuedAt;
6768
@synthesize protocol = _protocol;
6869
@synthesize encrypted = _encrypted;
@@ -93,6 +94,7 @@ - (id)initWithCoder:(NSCoder *)coder {
9394
self.identityUrl = [coder decodeObjectOfClass:[NSURL class] forKey:@"SFOAuthIdentityUrl"];
9495
self.instanceUrl = [coder decodeObjectOfClass:[NSURL class] forKey:@"SFOAuthInstanceUrl"];
9596
self.apiInstanceUrl = [coder decodeObjectOfClass:[NSURL class] forKey:@"SFOAuthApiInstanceUrl"];
97+
self.scopes = [coder decodeObjectOfClasses:[NSSet setWithObjects:[NSArray class], [NSString class], nil] forKey:@"SFOAuthScopes"];
9698
self.communityId = [coder decodeObjectOfClass:[NSString class] forKey:@"SFOAuthCommunityId"];
9799
self.communityUrl = [coder decodeObjectOfClass:[NSURL class] forKey:@"SFOAuthCommunityUrl"];
98100
self.issuedAt = [coder decodeObjectOfClass:[NSDate class] forKey:@"SFOAuthIssuedAt"];
@@ -145,6 +147,7 @@ - (void)encodeWithCoder:(NSCoder *)coder {
145147
[coder encodeObject:self.identityUrl forKey:@"SFOAuthIdentityUrl"];
146148
[coder encodeObject:self.instanceUrl forKey:@"SFOAuthInstanceUrl"];
147149
[coder encodeObject:self.apiInstanceUrl forKey:@"SFOAuthApiInstanceUrl"];
150+
[coder encodeObject:self.scopes forKey:@"SFOAuthScopes"];
148151
[coder encodeObject:self.communityId forKey:@"SFOAuthCommunityId"];
149152
[coder encodeObject:self.communityUrl forKey:@"SFOAuthCommunityUrl"];
150153
[coder encodeObject:self.issuedAt forKey:@"SFOAuthIssuedAt"];
@@ -204,6 +207,7 @@ - (id)copyWithZone:(nullable NSZone *)zone {
204207
copyCreds.accessToken = self.accessToken;
205208
copyCreds.instanceUrl = self.instanceUrl;
206209
copyCreds.apiInstanceUrl = self.apiInstanceUrl;
210+
copyCreds.scopes = self.scopes;
207211
copyCreds.communityId = self.communityId;
208212
copyCreds.communityUrl = self.communityUrl;
209213
copyCreds.issuedAt = self.issuedAt;
@@ -297,11 +301,11 @@ - (void)setUserId:(NSString *)userId {
297301

298302
- (NSString *)description {
299303
NSString *format = @"<%@: %p, identifier=\"%@\" clientId=\"%@\" domain=\"%@\" identityUrl=\"%@\" instanceUrl=\"%@\" apiInstanceUrl=\"%@\" "
300-
@"communityId=\"%@\" communityUrl=\"%@\" "
304+
@"communityId=\"%@\" communityUrl=\"%@\" scopes=\"%@\" "
301305
@"issuedAt=\"%@\" organizationId=\"%@\" protocol=\"%@\" redirectUri=\"%@\">";
302306
return [NSString stringWithFormat:format, NSStringFromClass(self.class), self,
303307
self.identifier, self.clientId, self.domain, self.identityUrl, self.instanceUrl, self.apiInstanceUrl,
304-
self.communityId, self.communityUrl,
308+
self.communityId, self.communityUrl, self.scopes,
305309
self.issuedAt, self.organizationId, self.protocol, self.redirectUri];
306310
}
307311

@@ -322,6 +326,7 @@ - (void)revokeRefreshToken {
322326
self.refreshToken = nil;
323327
self.instanceUrl = nil;
324328
self.apiInstanceUrl = nil;
329+
self.scopes = nil;
325330
self.communityId = nil;
326331
self.communityUrl = nil;
327332
self.issuedAt = nil;
@@ -378,6 +383,7 @@ - (NSURL *)overrideDomainIfNeeded {
378383
- issuedAt
379384
- instanceUrl
380385
- apiInstanceUrl
386+
- scopes
381387
- identityUrl
382388
- communityId
383389
- communityUrl
@@ -410,6 +416,11 @@ - (void)updateCredentials:(NSDictionary *) params {
410416
if (params[kSFOAuthApiInstanceUrl]) {
411417
[self setPropertyForKey:@"apiInstanceUrl" withValue:[NSURL URLWithString:params[kSFOAuthApiInstanceUrl]]];
412418
}
419+
if (params[kSFOAuthScope]) {
420+
NSString *rawScope = params[kSFOAuthScope];
421+
NSArray<NSString *> *scopesArray = [rawScope componentsSeparatedByString:@" "];
422+
[self setPropertyForKey:@"scopes" withValue:scopesArray];
423+
}
413424
if (params[kSFOAuthId]) {
414425
[self setPropertyForKey:@"identityUrl" withValue:[NSURL URLWithString:params[kSFOAuthId]]];
415426
}

libs/SalesforceSDKCore/SalesforceSDKCore/SFOAuthCredentialsTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ - (void)tryUpdateCredentials:(BOOL)encrypted storageType:(SFOAuthCredentialsStor
6161
[params setObject:@"test-refresh-token" forKey:@"refresh_token"];
6262
[params setObject:@"https://instance.salesforce.com" forKey:@"instance_url"];
6363
[params setObject:@"https://api.salesforce.com" forKey:@"api_instance_url"];
64+
[params setObject:@"api refresh_token" forKey:@"scope"];
6465
[params setObject:@"https://id.salesforce.com" forKey:@"id"];
6566
[params setObject:@"test-community-id" forKey:@"sfdc_community_id"];
6667
[params setObject:@"https://community.salesforce.com" forKey:@"sfdc_community_url"];
@@ -85,6 +86,7 @@ - (void)tryUpdateCredentials:(BOOL)encrypted storageType:(SFOAuthCredentialsStor
8586
XCTAssertEqualObjects(creds.refreshToken, @"test-refresh-token");
8687
XCTAssertEqualObjects(creds.instanceUrl.absoluteString, @"https://instance.salesforce.com");
8788
XCTAssertEqualObjects(creds.apiInstanceUrl.absoluteString, @"https://api.salesforce.com");
89+
XCTAssertEqualObjects(creds.scopes, (@[@"api", @"refresh_token"]));
8890
XCTAssertEqualObjects(creds.identityUrl.absoluteString, @"https://id.salesforce.com");
8991
XCTAssertEqualObjects(creds.communityId, @"test-community-id");
9092
XCTAssertEqualObjects(creds.communityUrl.absoluteString, @"https://community.salesforce.com");

libs/SalesforceSDKCore/SalesforceSDKCore/SFSDKOAuthTokenEndpointResponseTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ - (void)testInitWithDictionary {
4747
[params setObject:@"test-refresh-token" forKey:@"refresh_token"];
4848
[params setObject:@"https://instance.salesforce.com" forKey:@"instance_url"];
4949
[params setObject:@"https://api.salesforce.com" forKey:@"api_instance_url"];
50+
[params setObject:@"api refresh_token" forKey:@"scope"];
5051
[params setObject:@"https://id.salesforce.com" forKey:@"id"];
5152
[params setObject:@"test-community-id" forKey:@"sfdc_community_id"];
5253
[params setObject:@"https://community.salesforce.com" forKey:@"sfdc_community_url"];
@@ -80,6 +81,7 @@ - (void)testInitWithDictionary {
8081
XCTAssertEqualObjects(response.refreshToken, @"test-refresh-token");
8182
XCTAssertEqualObjects(response.instanceUrl.absoluteString, @"https://instance.salesforce.com");
8283
XCTAssertEqualObjects(response.apiInstanceUrl.absoluteString, @"https://api.salesforce.com");
84+
XCTAssertEqualObjects(response.scopes, (@[@"api", @"refresh_token"]));
8385
XCTAssertEqualObjects(response.identityUrl.absoluteString, @"https://id.salesforce.com");
8486
XCTAssertEqualObjects(response.communityId, @"test-community-id");
8587
XCTAssertEqualObjects(response.communityUrl.absoluteString, @"https://community.salesforce.com");

libs/SalesforceSDKCore/SalesforceSDKCoreTests/SalesforceOAuthUnitTests.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ - (void)testCredentialsCoding {
130130
credsIn.identityUrl = [NSURL URLWithString:@"https://login.salesforce.com/ID/orgID/eighteenCharUsrXYZ"];
131131
credsIn.instanceUrl = [NSURL URLWithString:@"http://www.salesforce.com"];
132132
credsIn.apiInstanceUrl = [NSURL URLWithString:@"http://api.salesforce.com"];
133+
credsIn.scopes = @[@"api", @"refresh_token"];
133134
credsIn.issuedAt = [NSDate date];
134135
credsIn.contentDomain = @"mobilesdk.my.salesforce.com";
135136
credsIn.contentSid = @"contentsid";
@@ -173,6 +174,7 @@ - (void)testCredentialsCoding {
173174
XCTAssertEqualObjects(expectedUserId, credsOut.userId, @"userId mismatch");
174175
XCTAssertEqualObjects(credsIn.instanceUrl, credsOut.instanceUrl, @"instanceUrl mismatch");
175176
XCTAssertEqualObjects(credsIn.apiInstanceUrl, credsOut.apiInstanceUrl, @"apiInstanceUrl mismatch");
177+
XCTAssertEqualObjects(credsIn.scopes, credsOut.scopes, @"scopes mismatch");
176178
XCTAssertEqualObjects(credsIn.issuedAt, credsOut.issuedAt, @"issuedAt mismatch");
177179
XCTAssertEqualObjects(credsIn.contentDomain, credsOut.contentDomain, @"contentDomain mismatch");
178180
XCTAssertEqualObjects(credsIn.contentSid, credsOut.contentSid, @"contentSid mismatch");
@@ -202,6 +204,7 @@ - (void)testCredentialsCopying {
202204
NSString *orgIdToCheck = @"orgID";
203205
NSURL *instanceUrlToCheck = [NSURL URLWithString:@"https://na1.salesforce.com"];
204206
NSURL *apiInstanceUrlToCheck = [NSURL URLWithString:@"https://api.salesforce.com"];
207+
NSArray<NSString *> *scopesToCheck = @[@"api", @"refresh_token"];
205208
NSString *communityIdToCheck = @"communityID";
206209
NSURL *communityUrlToCheck = [NSURL URLWithString:@"https://mycomm.my.salesforce.com/customers"];
207210
NSDate *issuedAtToCheck = [NSDate date];
@@ -231,6 +234,7 @@ - (void)testCredentialsCopying {
231234
origCreds.accessToken = accessTokenToCheck;
232235
origCreds.instanceUrl = instanceUrlToCheck;
233236
origCreds.apiInstanceUrl = apiInstanceUrlToCheck;
237+
origCreds.scopes = scopesToCheck;
234238
origCreds.communityId = communityIdToCheck;
235239
origCreds.communityUrl = communityUrlToCheck;
236240
origCreds.issuedAt = issuedAtToCheck;
@@ -267,6 +271,7 @@ - (void)testCredentialsCopying {
267271
origCreds.organizationId = nil;
268272
origCreds.instanceUrl = nil;
269273
origCreds.apiInstanceUrl = nil;
274+
origCreds.scopes = nil;
270275
origCreds.communityId = nil;
271276
origCreds.communityUrl = nil;
272277
origCreds.issuedAt = nil;
@@ -322,6 +327,8 @@ - (void)testCredentialsCopying {
322327
XCTAssertNotEqual(origCreds.instanceUrl, copiedCreds.instanceUrl);
323328
XCTAssertEqual(copiedCreds.apiInstanceUrl, apiInstanceUrlToCheck);
324329
XCTAssertNotEqual(origCreds.apiInstanceUrl, copiedCreds.apiInstanceUrl);
330+
XCTAssertEqual(copiedCreds.scopes, scopesToCheck);
331+
XCTAssertNotEqual(origCreds.scopes, copiedCreds.scopes);
325332
XCTAssertEqual(copiedCreds.communityId, communityIdToCheck);
326333
XCTAssertNotEqual(origCreds.communityId, copiedCreds.communityId);
327334
XCTAssertEqual(copiedCreds.communityUrl, communityUrlToCheck);

0 commit comments

Comments
 (0)