Skip to content

Commit 0416ee8

Browse files
authored
Prevent signing in with another WP.com account (#23682)
* Use shared URLSession to reduce risk of HTTP3 issues * Fix looking up wrong account * Fix wrong argument being used in auto-signin UI tests * Prevent signing in with another WP.com account * Fix blog prompts unit tests set up * Fix PromptRemindersSchedulerTests set up * Fix another unit test
1 parent 916af77 commit 0416ee8

File tree

6 files changed

+26
-5
lines changed

6 files changed

+26
-5
lines changed

WordPress/Classes/Login/WordPressDotComAuthenticator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct WordPressDotComAuthenticator {
7878
}
7979

8080
do {
81-
let urlSession = URLSession(configuration: .default)
81+
let urlSession = URLSession.shared
8282
let (data, _) = try await urlSession.data(for: tokenRequest)
8383

8484
struct Response: Decodable {

WordPress/Classes/Services/BloggingPrompts/BloggingPromptsService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class BloggingPromptsService {
207207
}
208208

209209
// fetch the default account and fall back to default values as needed.
210-
guard let account = try? WPAccount.lookupDefaultWordPressComAccount(in: mainContext) else {
210+
guard let account = blogInContext?.account else {
211211
return (
212212
blogInContext?.dotComID,
213213
remote,

WordPress/Classes/System/WordPressAppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ extension WordPressAppDelegate {
857857
}
858858

859859
let service = WordPressComSyncService()
860-
service.syncWPCom(authToken: "valid_token", isJetpackLogin: true, onSuccess: { account in
860+
service.syncWPCom(authToken: "valid_token", isJetpackLogin: false, onSuccess: { account in
861861
if let blog = try? BlogQuery().hostname(containing: wpComSiteAddress).blog(in: ContextManager.shared.mainContext) {
862862
self.windowManager.showUI(for: blog)
863863
} else {

WordPress/Classes/ViewRelated/Jetpack/Login/JetpackLoginViewController.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class JetpackLoginViewController: UIViewController {
1111

1212
var blog: Blog
1313

14+
// This variable is used to prevent signing into another WP.com account, if the site is not connected to the already signed-in default account.
15+
private var shouldDisableLogin: Bool {
16+
guard let defaultAccount = try? WPAccount.lookupDefaultWordPressComAccount(in: ContextManager.shared.mainContext) else {
17+
return false
18+
}
19+
return defaultAccount.email != blog.jetpack?.connectedEmail
20+
}
21+
1422
// MARK: - Properties
1523

1624
// Defaulting to stats because since that one is written in ObcC we don't have access to the enum there.
@@ -118,7 +126,11 @@ class JetpackLoginViewController: UIViewController {
118126
if jetpack.isSiteConnection {
119127
message = promptType.connectMessage
120128
} else if jetpack.isConnected {
121-
message = jetpack.isUpdatedToRequiredVersion ? Constants.Jetpack.isUpdated : Constants.Jetpack.updateRequired
129+
if let connectedEmail = jetpack.connectedEmail, shouldDisableLogin {
130+
message = Constants.Jetpack.connectToDefaultAccount(connectedEmail: connectedEmail)
131+
} else {
132+
message = jetpack.isUpdatedToRequiredVersion ? Constants.Jetpack.isUpdated : Constants.Jetpack.updateRequired
133+
}
122134
} else {
123135
message = promptType.installMessage
124136
}
@@ -136,7 +148,7 @@ class JetpackLoginViewController: UIViewController {
136148
connectUserButton.contentEdgeInsets = UIEdgeInsets(top: 12, left: 20, bottom: 12, right: 20)
137149

138150
signinButton.setTitle(Constants.Buttons.loginTitle, for: .normal)
139-
signinButton.isHidden = !(blog.hasJetpack && !jetpack.isSiteConnection)
151+
signinButton.isHidden = shouldDisableLogin || !(blog.hasJetpack && !jetpack.isSiteConnection)
140152

141153
let paragraph = NSMutableParagraphStyle(minLineHeight: WPStyleGuide.fontSizeForTextStyle(.footnote),
142154
lineBreakMode: .byWordWrapping,
@@ -338,5 +350,11 @@ private enum Constants {
338350
comment: "Message stating the minimum required " +
339351
"version for Jetpack and asks the user " +
340352
"if they want to upgrade"), JetpackState.minimumVersionRequired)
353+
static func connectToDefaultAccount(connectedEmail: String) -> String {
354+
String.localizedStringWithFormat(
355+
NSLocalizedString("jetpackSite.connectToDefaultAccount", value: "You need to sign in with %@ to use Stats and Notifications.", comment: "Message stating that the user is unable to use Stats and Notifications because their site is connected to a different WordPress.com account"),
356+
connectedEmail
357+
)
358+
}
341359
}
342360
}

WordPress/WordPressTest/BloggingPromptsServiceTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class BloggingPromptsServiceTests: CoreDataTestCase {
3939
remote = BloggingPromptsServiceRemoteMock()
4040
blog = makeBlog()
4141
accountService = makeAccountService()
42+
blog.account = try? WPAccount.lookupDefaultWordPressComAccount(in: mainContext)
4243
service = BloggingPromptsService(contextManager: contextManager, api: api, remote: remote, blog: blog)
4344
testPrompts = loadTestPrompts(from: fetchPromptsResponseFileName)
4445
}

WordPress/WordPressTest/PromptRemindersSchedulerTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class PromptRemindersSchedulerTests: XCTestCase {
4242
dateProvider = MockCurrentDateProvider(currentDate)
4343
blog = makeBlog()
4444
accountService = makeAccountService()
45+
blog.account = try? WPAccount.lookupDefaultWordPressComAccount(in: mainContext)
4546
localStore = MockLocalFileStore()
4647
scheduler = PromptRemindersScheduler(bloggingPromptsServiceFactory: serviceFactory,
4748
notificationScheduler: notificationScheduler,
@@ -333,6 +334,7 @@ class PromptRemindersSchedulerTests: XCTestCase {
333334
// Arrange
334335
let schedule = scheduleForToday
335336
let controlBlog = makeBlog()
337+
controlBlog.account = try? WPAccount.lookupDefaultWordPressComAccount(in: mainContext)
336338
let controlSiteID = controlBlog.dotComID!.intValue
337339

338340
// first, schedule reminders in the control blog.

0 commit comments

Comments
 (0)