Skip to content

Commit 8694a4d

Browse files
committed
consolidate all user-related requirement checks
* Multiple requests are making the same user-related requirement checks. * Put this into a helper function. * Since each request sets its own path at execution time with an appropriate app ID and alias, the app ID and alias are two separate checks.
1 parent ed888cd commit 8694a4d

File tree

8 files changed

+93
-43
lines changed

8 files changed

+93
-43
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
3CF11E3D2C6D6155002856F5 /* UserExecutorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF11E3C2C6D6155002856F5 /* UserExecutorTests.swift */; };
183183
3CF11E402C6E6DE2002856F5 /* MockNewRecordsState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF11E3F2C6E6DE2002856F5 /* MockNewRecordsState.swift */; };
184184
3CF1A5632C669EA40056B3AA /* OSNewRecordsState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF1A5622C669EA40056B3AA /* OSNewRecordsState.swift */; };
185+
3CF807352C80E3A6003E5FE1 /* OSAliasPair.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF807342C80E3A6003E5FE1 /* OSAliasPair.swift */; };
185186
3CF8629E28A183F900776CA4 /* OSIdentityModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF8629D28A183F900776CA4 /* OSIdentityModel.swift */; };
186187
3CF862A028A1964F00776CA4 /* OSPropertiesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF8629F28A1964F00776CA4 /* OSPropertiesModel.swift */; };
187188
3CF862A228A197D200776CA4 /* OSPropertiesModelStoreListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CF862A128A197D200776CA4 /* OSPropertiesModelStoreListener.swift */; };
@@ -1304,6 +1305,7 @@
13041305
3CF11E3C2C6D6155002856F5 /* UserExecutorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserExecutorTests.swift; sourceTree = "<group>"; };
13051306
3CF11E3F2C6E6DE2002856F5 /* MockNewRecordsState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNewRecordsState.swift; sourceTree = "<group>"; };
13061307
3CF1A5622C669EA40056B3AA /* OSNewRecordsState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSNewRecordsState.swift; sourceTree = "<group>"; };
1308+
3CF807342C80E3A6003E5FE1 /* OSAliasPair.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSAliasPair.swift; sourceTree = "<group>"; };
13071309
3CF8629D28A183F900776CA4 /* OSIdentityModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModel.swift; sourceTree = "<group>"; };
13081310
3CF8629F28A1964F00776CA4 /* OSPropertiesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSPropertiesModel.swift; sourceTree = "<group>"; };
13091311
3CF862A128A197D200776CA4 /* OSPropertiesModelStoreListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSPropertiesModelStoreListener.swift; sourceTree = "<group>"; };
@@ -2082,6 +2084,7 @@
20822084
isa = PBXGroup;
20832085
children = (
20842086
3C2FF9CF2C5FCD760081293B /* OSUserJwtConfig.swift */,
2087+
3CF807342C80E3A6003E5FE1 /* OSAliasPair.swift */,
20852088
);
20862089
path = Jwt;
20872090
sourceTree = "<group>";
@@ -4063,6 +4066,7 @@
40634066
isa = PBXSourcesBuildPhase;
40644067
buildActionMask = 2147483647;
40654068
files = (
4069+
3CF807352C80E3A6003E5FE1 /* OSAliasPair.swift in Sources */,
40664070
DEFB3E652BB7346D00E65DAD /* OSLiveActivities.swift in Sources */,
40674071
3C4F9E4428A4466C009F453A /* OSOperationRepo.swift in Sources */,
40684072
3C11518B289ADEEB00565C41 /* OSEventProducer.swift in Sources */,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2024 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
/**
29+
An alias label and alias ID pair to represent a user.
30+
*/
31+
@objc public class OSAliasPair: NSObject {
32+
public let label: String
33+
public let id: String
34+
35+
public init(_ label: String, _ id: String) {
36+
self.label = label
37+
self.id = id
38+
}
39+
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSRequestAddAliases.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ class OSRequestAddAliases: OneSignalRequest, OSUserRequest {
4040

4141
/// Needs `onesignal_id` without JWT on or `external_id` with valid JWT to send this request
4242
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
43-
let alias = getAlias(identityModel: identityModel)
4443
guard
45-
let onesignalId = identityModel.onesignalId,
46-
newRecordsState.canAccess(onesignalId),
47-
let aliasIdToUse = alias.id,
48-
let appId = OneSignalConfigManager.getAppId(),
49-
addJWTHeaderIsValid(identityModel: identityModel)
44+
let alias = checkUserRequirementsAndReturnAlias(identityModel, newRecordsState),
45+
let appId = OneSignalConfigManager.getAppId()
5046
else {
5147
return false
5248
}
5349

54-
self.path = "apps/\(appId)/users/by/\(alias.label)/\(aliasIdToUse)/identity"
50+
self.path = "apps/\(appId)/users/by/\(alias.label)/\(alias.id)/identity"
5551
return true
5652
}
5753

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSRequestCreateSubscription.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,14 @@ class OSRequestCreateSubscription: OneSignalRequest, OSUserRequest {
4545

4646
/// Needs the `onesignal_id` without JWT on or `external_id` with valid JWT to send this request
4747
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
48-
let alias = getAlias(identityModel: identityModel)
4948
guard
50-
let onesignalId = identityModel.onesignalId,
51-
newRecordsState.canAccess(onesignalId),
52-
let aliasIdToUse = alias.id,
53-
let appId = OneSignalConfigManager.getAppId(),
54-
addJWTHeaderIsValid(identityModel: identityModel)
49+
let alias = checkUserRequirementsAndReturnAlias(identityModel, newRecordsState),
50+
let appId = OneSignalConfigManager.getAppId()
5551
else {
5652
return false
5753
}
5854

59-
self.path = "apps/\(appId)/users/by/\(alias.label)/\(aliasIdToUse)/subscriptions"
55+
self.path = "apps/\(appId)/users/by/\(alias.label)/\(alias.id)/subscriptions"
6056
return true
6157
}
6258

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSRequestFetchUser.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
4848
// TODO: JWT 🔐 Is external ID already handled by this time? Or do we need to check the alias here?
4949
/// Needs `onesignal_id` without JWT on or `external_id` with valid JWT to send this request
5050
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
51-
let alias = getAlias(identityModel: identityModel)
5251
guard
53-
let aliasIdToUse = alias.id,
54-
let appId = OneSignalConfigManager.getAppId(),
55-
newRecordsState.canAccess(onesignalId),
56-
addJWTHeaderIsValid( identityModel: identityModel)
52+
let alias = checkUserRequirementsAndReturnAlias(identityModel, newRecordsState),
53+
let appId = OneSignalConfigManager.getAppId()
5754
else {
58-
OneSignalLog.onesignalLog(.LL_DEBUG, message: "Cannot generate the fetch user request for \(alias) yet.")
55+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "Cannot generate the fetch user request for \(identityModel.aliases) yet.")
5956
return false
6057
}
61-
self.path = "apps/\(appId)/users/by/\(alias.label)/\(aliasIdToUse)"
58+
self.path = "apps/\(appId)/users/by/\(alias.label)/\(alias.id)"
6259
return true
6360
}
6461

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSRequestRemoveAlias.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ class OSRequestRemoveAlias: OneSignalRequest, OSUserRequest {
4040

4141
/// Needs `onesignal_id` without JWT on or `external_id` with valid JWT to send this request
4242
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
43-
let alias = getAlias(identityModel: identityModel)
4443
guard
45-
let onesignalId = identityModel.onesignalId,
46-
newRecordsState.canAccess(onesignalId),
47-
let aliasIdToUse = alias.id,
48-
let appId = OneSignalConfigManager.getAppId(),
49-
addJWTHeaderIsValid(identityModel: identityModel)
44+
let alias = checkUserRequirementsAndReturnAlias(identityModel, newRecordsState),
45+
let appId = OneSignalConfigManager.getAppId()
5046
else {
5147
return false
5248
}
5349

54-
self.path = "apps/\(appId)/users/by/\(alias.label)/\(aliasIdToUse)/identity/\(labelToRemove)"
50+
self.path = "apps/\(appId)/users/by/\(alias.label)/\(alias.id)/identity/\(labelToRemove)"
5551
return true
5652
}
5753

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSRequestUpdateProperties.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,15 @@ class OSRequestUpdateProperties: OneSignalRequest, OSUserRequest {
3939

4040
/// Needs `onesignal_id` without JWT on or `external_id` with valid JWT to send this request
4141
func prepareForExecution(newRecordsState: OSNewRecordsState) -> Bool {
42-
let alias = getAlias(identityModel: identityModel)
4342
guard
44-
let onesignalId = identityModel.onesignalId,
45-
newRecordsState.canAccess(onesignalId),
46-
let aliasIdToUse = alias.id,
47-
let appId = OneSignalConfigManager.getAppId(),
48-
addJWTHeaderIsValid(identityModel: identityModel)
43+
let alias = checkUserRequirementsAndReturnAlias(identityModel, newRecordsState),
44+
let appId = OneSignalConfigManager.getAppId()
4945
else {
5046
return false
5147
}
5248

5349
_ = self.addPushSubscriptionIdToAdditionalHeaders()
54-
self.path = "apps/\(appId)/users/by/\(alias.label)/\(aliasIdToUse)"
50+
self.path = "apps/\(appId)/users/by/\(alias.label)/\(alias.id)"
5551
return true
5652
}
5753

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSUserRequest.swift

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,42 @@ protocol OSUserRequest: OneSignalRequest, NSCoding {
3535

3636
internal extension OneSignalRequest {
3737
/**
38-
Returns the alias pair to use to send this request for. Defaults to Onesignal Id, unless Identity Verification is on.
38+
Handles a full check of user-related requirements.
39+
- The existence of onesignal ID and the ability to access it.
40+
- The existence of an appropriate alias.
41+
- Checks JWT requirements and sets header.
42+
43+
- Returns: The alias pair to use to send this request.
3944
*/
40-
func getAlias(identityModel: OSIdentityModel) -> (label: String, id: String?) {
41-
var label = OS_ONESIGNAL_ID
42-
var id = identityModel.onesignalId
43-
if OneSignalUserManagerImpl.sharedInstance.jwtConfig.isRequired == true {
44-
label = OS_EXTERNAL_ID
45-
id = identityModel.externalId
45+
func checkUserRequirementsAndReturnAlias(_ identityModel: OSIdentityModel, _ newRecordsState: OSNewRecordsState) -> OSAliasPair? {
46+
guard
47+
let onesignalId = identityModel.onesignalId,
48+
newRecordsState.canAccess(onesignalId),
49+
let aliasPair = getAlias(identityModel: identityModel, jwtConfig: OneSignalUserManagerImpl.sharedInstance.jwtConfig),
50+
addJWTHeaderIsValid(identityModel: identityModel)
51+
else {
52+
return nil
53+
}
54+
55+
return aliasPair
56+
}
57+
58+
private func getAlias(identityModel: OSIdentityModel, jwtConfig: OSUserJwtConfig) -> OSAliasPair? {
59+
guard let jwtRequired = jwtConfig.isRequired else {
60+
return nil
4661
}
47-
return (label, id)
62+
63+
if jwtRequired, let externalId = identityModel.externalId
64+
{
65+
// JWT is on and external ID exists
66+
return OSAliasPair(OS_EXTERNAL_ID, externalId)
67+
} else if !jwtRequired, let onesignalId = identityModel.onesignalId {
68+
// JWT is off and onesignal ID exists
69+
return OSAliasPair(OS_ONESIGNAL_ID, onesignalId)
70+
}
71+
72+
// Missing onesignal ID or external ID, when expected
73+
return nil
4874
}
4975

5076
/**

0 commit comments

Comments
 (0)