Skip to content

Commit 5fa2363

Browse files
KM-17445: use token on signup if present
1 parent f03f80e commit 5fa2363

10 files changed

Lines changed: 43 additions & 18 deletions

File tree

LocalPackages/PIAAccount/Sources/PIAAccount/Models/ResponseModels.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,21 @@ public struct DedicatedIPTokenDetails: Codable, Sendable {
296296
}
297297
}
298298

299-
/// New account credentials after VPN sign up
299+
/// New account credentials after VPN sign up. Can be either:
300+
/// - username + password
301+
/// - username + apiToken + expiresAt
300302
public struct VpnSignUpInformation: Codable, Sendable {
301303
public let username: String
302-
public let password: String
304+
public let password: String?
305+
/// The API token string
306+
public let apiToken: String?
307+
/// ISO 8601 expiration date string
308+
public let expiresAt: String?
309+
310+
enum CodingKeys: String, CodingKey {
311+
case username = "username"
312+
case password = "password"
313+
case apiToken = "api_token"
314+
case expiresAt = "expires_at"
315+
}
303316
}

LocalPackages/PIAAccount/Sources/PIAAccount/PIAAccountClient.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,19 @@ public actor PIAAccountClient: PIAAccountAPI {
408408
public func signUp(information: IOSSignupInformation) async throws -> VpnSignUpInformation {
409409
let bodyData = try JSONEncoder.piaCodable.encode(information)
410410

411-
return try await endpointManager.executeWithFailover(
411+
let response: VpnSignUpInformation = try await endpointManager.executeWithFailover(
412412
path: .signup,
413413
method: .post,
414414
bodyType: .json(bodyData)
415415
)
416+
417+
// store a token if we received one
418+
if let token = response.apiToken, let expiresAt = response.expiresAt {
419+
let tokenResponse = APITokenResponse(apiToken: token, expiresAt: expiresAt)
420+
try await tokenManager.storeAPIToken(tokenResponse)
421+
}
422+
423+
return response
416424
}
417425

418426
// MARK: - Social

LocalPackages/PIALibrary/Sources/PIALibrary/Account/DefaultAccountProvider.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public final class DefaultAccountProvider: AccountProvider, ConfigurationAccess,
495495
accessedDatabase.plain.lastSignupEmail = request.email
496496

497497
do {
498-
let credentials = try await webServices.signup(with: signup)
498+
let (credentials, needsToken) = try await webServices.signup(with: signup)
499499

500500
if let transaction = request.transaction {
501501
accessedStore.finishTransaction(transaction, success: true)
@@ -504,9 +504,13 @@ public final class DefaultAccountProvider: AccountProvider, ConfigurationAccess,
504504
accessedDatabase.plain.lastSignupEmail = nil
505505
accessedDatabase.secure.setPublicUsername(credentials.username)
506506
accessedDatabase.secure.setUsername(credentials.username)
507-
accessedDatabase.secure.setPassword(credentials.password, for: credentials.username)
507+
if !credentials.password.isEmpty {
508+
accessedDatabase.secure.setPassword(credentials.password, for: credentials.username)
509+
}
508510

509-
try await webServices.token(credentials: credentials)
511+
if needsToken {
512+
try await webServices.token(credentials: credentials)
513+
}
510514
let accountInfo = try await webServices.info()
511515
accessedDatabase.plain.accountInfo = accountInfo
512516
accessedDatabase.secure.setPublicUsername(accountInfo.username)

LocalPackages/PIALibrary/Sources/PIALibrary/Account/EphemeralAccountProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ final class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAcc
120120
}
121121

122122
do {
123-
guard let credentials = try await webServices?.signup(with: signup) else {
123+
guard let (credentials, _) = try await webServices?.signup(with: signup) else {
124124
DispatchQueue.main.async { callback?(nil, nil) }
125125
return
126126
}

LocalPackages/PIALibrary/Sources/PIALibrary/Account/UserAccount.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import Foundation
2424

2525
/// The compound user account.
26-
public struct UserAccount: CustomStringConvertible, Equatable {
26+
public struct UserAccount: CustomStringConvertible, Equatable, Sendable {
2727

2828
/// The account credentials.
2929
public let credentials: Credentials

LocalPackages/PIALibrary/Sources/PIALibrary/Mock/MockWebServices.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ final class MockWebServices: WebServices {
7070

7171
func deleteAccount() async throws {}
7272

73-
func signup(with request: Signup) async throws -> Credentials {
73+
func signup(with request: Signup) async throws -> (credentials: Credentials, needsToken: Bool) {
7474
guard let result = credentials?() else {
7575
throw ClientError.unsupported
7676
}
77-
return result
77+
return (credentials: result, needsToken: true)
7878
}
7979

8080
func redeem(with request: Redeem, _ callback: ((Credentials?, Error?) -> Void)?) {

LocalPackages/PIALibrary/Sources/PIALibrary/WebServices/AccountInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import Foundation
2424

2525
/// The information associated with a `Credentials`.
26-
public struct AccountInfo: Codable, Equatable {
26+
public struct AccountInfo: Codable, Equatable, Sendable {
2727

2828
/// The linked email address if any.
2929
public internal(set) var email: String?

LocalPackages/PIALibrary/Sources/PIALibrary/WebServices/Credentials.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import Foundation
2424

2525
/// The account credentials.
26-
public struct Credentials: Codable, Equatable {
26+
public struct Credentials: Codable, Equatable, Sendable {
2727

2828
/// The username, typically a number prefixed with "p".
2929
public let username: String

LocalPackages/PIALibrary/Sources/PIALibrary/WebServices/PIAWebServices.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ final class PIAWebServices: WebServices, ConfigurationAccess {
258258
}
259259

260260
#if os(iOS) || os(tvOS)
261-
func signup(with request: Signup) async throws -> Credentials {
261+
func signup(with request: Signup) async throws -> (credentials: Credentials, needsToken: Bool) {
262262
var marketingJSON = ""
263263
if let marketing = request.marketing {
264264
marketingJSON = stringify(json: marketing)
@@ -278,7 +278,9 @@ final class PIAWebServices: WebServices, ConfigurationAccess {
278278

279279
do {
280280
let response = try await nativeAccountAPI.signUp(information: info)
281-
return Credentials(username: response.username, password: response.password)
281+
let needsToken = response.apiToken == nil || response.apiToken!.isEmpty
282+
let credentials = Credentials(username: response.username, password: response.password ?? "")
283+
return (credentials: credentials, needsToken: needsToken)
282284
} catch {
283285
let code = (error as? PIAAccountError)?.code ?? (error as? PIAMultipleErrors)?.code
284286
throw code == 400 ? ClientError.badReceipt : ClientError.invalidParameter

LocalPackages/PIALibrary/Sources/PIALibrary/WebServices/WebServices.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ protocol WebServices: AnyObject {
6262
*/
6363
func deleteAccount() async throws
6464

65-
#if os(iOS) || os(tvOS)
66-
func signup(with request: Signup) async throws -> Credentials
65+
func signup(with request: Signup) async throws -> (credentials: Credentials, needsToken: Bool)
6766

68-
func processPayment(credentials: Credentials, request: Payment) async throws
69-
#endif
67+
func processPayment(credentials: Credentials, request: Payment) async throws
7068

7169
// MARK: Store
7270

0 commit comments

Comments
 (0)