Skip to content

Commit

Permalink
Merge pull request #1188 from Automattic/lantean/1187-updates-magic-l…
Browse files Browse the repository at this point in the history
…ink-endpoints

Magic Links: Wires new Endpoints
  • Loading branch information
jleandroperez authored Jul 17, 2024
2 parents 698da14 + c450a4d commit 53f72a6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Simplenote/LoginRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import Foundation
class LoginRemote: Remote {

func requestLoginEmail(email: String) async throws {
let request = requestForLoginRequest(with: email)
let request = requestForLoginRequest(email: email)
try await performDataTask(with: request)
}

func requestLoginConfirmation(authKey: String, authCode: String) async throws -> LoginConfirmationResponse {
let request = requestForLoginCompletion(authKey: authKey, authCode: authCode)
func requestLoginConfirmation(email: String, authCode: String) async throws -> LoginConfirmationResponse {
let request = requestForLoginCompletion(email: email, authCode: authCode)
return try await performDataTask(with: request, type: LoginConfirmationResponse.self)
}
}
Expand All @@ -28,18 +28,18 @@ struct LoginConfirmationResponse: Codable, Equatable {
//
private extension LoginRemote {

func requestForLoginRequest(with email: String) -> URLRequest {
func requestForLoginRequest(email: String) -> URLRequest {
let url = URL(string: SimplenoteConstants.loginRequestURL)!
return requestForURL(url, method: RemoteConstants.Method.POST, httpBody: [
"request_source": SimplenoteConstants.simplenotePlatformName,
"username": email.lowercased()
])
}

func requestForLoginCompletion(authKey: String, authCode: String) -> URLRequest {
func requestForLoginCompletion(email: String, authCode: String) -> URLRequest {
let url = URL(string: SimplenoteConstants.loginCompletionURL)!
return requestForURL(url, method: RemoteConstants.Method.POST, httpBody: [
"auth_key": authKey,
"username": email,
"auth_code": authCode
])
}
Expand Down
2 changes: 1 addition & 1 deletion Simplenote/LoginRemoteProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
//
protocol LoginRemoteProtocol {
func requestLoginEmail(email: String) async throws
func requestLoginConfirmation(authKey: String, authCode: String) async throws -> LoginConfirmationResponse
func requestLoginConfirmation(email: String, authCode: String) async throws -> LoginConfirmationResponse
}


Expand Down
9 changes: 4 additions & 5 deletions Simplenote/MagicLinkAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ private extension MagicLinkAuthenticator {

@discardableResult
func attemptLoginWithAuthCode(queryItems: [URLQueryItem]) -> Bool {
guard let authKey = queryItems.value(for: Constants.authKeyField),
guard let email = queryItems.base64DecodedValue(for: Constants.emailField),
let authCode = queryItems.value(for: Constants.authCodeField),
!authKey.isEmpty, !authCode.isEmpty
!email.isEmpty, !authCode.isEmpty
else {
return false
}

NSLog("[MagicLinkAuthenticator] Requesting SyncToken for \(authKey) and \(authCode)")
NSLog("[MagicLinkAuthenticator] Requesting SyncToken for \(email) and \(authCode)")
NotificationCenter.default.post(name: .magicLinkAuthWillStart, object: nil)

Task { @MainActor in
do {
let confirmation = try await loginRemote.requestLoginConfirmation(authKey: authKey, authCode: authCode)
let confirmation = try await loginRemote.requestLoginConfirmation(email: email, authCode: authCode)

NSLog("[MagicLinkAuthenticator] Should auth with token \(confirmation.syncToken)")
authenticator.authenticate(withUsername: confirmation.username, token: confirmation.syncToken)
Expand Down Expand Up @@ -114,6 +114,5 @@ private struct AllowedHosts {
private struct Constants {
static let emailField = "email"
static let tokenField = "token"
static let authKeyField = "auth_key"
static let authCodeField = "auth_code"
}
4 changes: 2 additions & 2 deletions SimplenoteTests/LoginRemoteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class LoginRemoteTests: XCTestCase {
mockSessionResponse(statusCode: 200, payload: encodedResponseBody)

/// Run
let decodedResponse = try await loginRemote.requestLoginConfirmation(authKey: "1234", authCode: "5678")
let decodedResponse = try await loginRemote.requestLoginConfirmation(email: "1234@567.com", authCode: "5678")

/// Verify
let body: Dictionary<String, String> = try XCTUnwrap(urlSession.lastRequest?.decodeHtmlBody())

XCTAssertEqual(body["auth_key"], "1234")
XCTAssertEqual(body["username"], "1234@567.com")
XCTAssertEqual(body["auth_code"], "5678")

XCTAssertEqual(expectedResponse, decodedResponse)
Expand Down
9 changes: 5 additions & 4 deletions SimplenoteTests/MagicLinkAuthenticatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ final class MagicLinkAuthenticatorTests: XCTestCase {

func testMagicLinkURLResultsInLoginConfirmationRequest() async throws {
let expectation = expectation(description: "LoginConfirmationRequest")
loginRemote.onLoginConfirmationRequest = { (authKey, authCode) in
loginRemote.onLoginConfirmationRequest = { (email, authCode) in
XCTAssertEqual(authCode, MagicLinkTestConstants.expectedCode)
XCTAssertEqual(authKey, MagicLinkTestConstants.expectedKey)
XCTAssertEqual(email, MagicLinkTestConstants.expectedUsername)

expectation.fulfill()

Expand Down Expand Up @@ -64,6 +64,7 @@ private enum MagicLinkTestConstants {
static let expectedSyncToken = "12345678"
static let expectedKey = "1234"
static let expectedCode = "5678"
static let sampleValidURL = URL(string: "simplenotemac://login?auth_key=\(expectedKey)&auth_code=\(expectedCode)")!
static let sampleInvalidURL = URL(string: "simplenotemac://login?auth_key=&auth_code=")!
static let encodedUsername = expectedUsername.data(using: .utf8)!.base64EncodedString()
static let sampleValidURL = URL(string: "simplenotemac://login?email=\(encodedUsername)&auth_code=\(expectedCode)")!
static let sampleInvalidURL = URL(string: "simplenotemac://login?email=&auth_code=")!
}
6 changes: 3 additions & 3 deletions SimplenoteTests/MockLoginRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import Foundation
class MockLoginRemote: LoginRemoteProtocol {
var lastLoginRequestEmail: String?

var onLoginConfirmationRequest: ((_ authKey: String, _ authCode: String) -> LoginConfirmationResponse)?
var onLoginConfirmationRequest: ((_ email: String, _ authCode: String) -> LoginConfirmationResponse)?

func requestLoginEmail(email: String) async throws {
lastLoginRequestEmail = email
}

func requestLoginConfirmation(authKey: String, authCode: String) async throws -> LoginConfirmationResponse {
guard let response = onLoginConfirmationRequest?(authKey, authCode) else {
func requestLoginConfirmation(email: String, authCode: String) async throws -> LoginConfirmationResponse {
guard let response = onLoginConfirmationRequest?(email, authCode) else {
throw RemoteError.network
}

Expand Down

0 comments on commit 53f72a6

Please sign in to comment.