Damus Purple: Add npub authentication for account management API calls
Testing --------- PASS Device: iPhone 15 Pro simulator iOS: 17.2 Damus: This commit damus-api: 626fb9665d8d6c576dd635d5224869cd9b69d190 Server: Ubuntu 22.04 (VM) Setup: 1. On the server, delete the `mdb` database files to start from scratch 2. In iOS, reinstall the app if necessary to make sure there are no in-app purchases 3. Enable subscriptions support via developer settings with localhost test mode and restart app 4. Start server with mock parameters (Run `npm run dev`) Steps: 1. Open top bar and click on "Purple" 2. Purple screen should appear and show both benefits and the purchase options. PASS 3. Click on "monthly". An Apple screen to confirm purchase should appear. PASS 4. Welcome screen with animation should appear. PASS 5. Click continue and restart app (Due to known issue tracked at damus-io#1814) 6. Post something 7. Gold star should appear beside your name 8. Look at the server logs. There should be some requests to create the account (POST), to send the receipt (POST), and to get account status 9. Go to purple view. There should be some information about the subscription, as well as a "manage" button. PASS 10. Click on "manage" button. An iOS sheet should appear allow the user to unsubscribe or manage their subscription to Damus Purple. Closes: https://github.com/damus-io/damus/issues/1809 Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
4703ed80a7
commit
5ca5420ce2
@@ -38,9 +38,8 @@ class DamusPurple: StoreObserverDelegate {
|
||||
func account_exists(pubkey: Pubkey) async -> Bool? {
|
||||
guard let account_data = await self.get_account_data(pubkey: pubkey) else { return nil }
|
||||
|
||||
if let json = try? JSONSerialization.jsonObject(with: account_data, options: []) as? [String: Any],
|
||||
let id = json["id"] as? String {
|
||||
return id == pubkey.hex()
|
||||
if let account_info = try? JSONDecoder().decode(AccountInfo.self, from: account_data) {
|
||||
return account_info.pubkey == pubkey.hex()
|
||||
}
|
||||
|
||||
return false
|
||||
@@ -63,19 +62,27 @@ class DamusPurple: StoreObserverDelegate {
|
||||
|
||||
func create_account(pubkey: Pubkey) async throws {
|
||||
let url = environment.get_base_url().appendingPathComponent("accounts")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
|
||||
let payload: [String: String] = [
|
||||
"pubkey": pubkey.hex()
|
||||
]
|
||||
let encoded_payload = try JSONEncoder().encode(payload)
|
||||
|
||||
request.httpBody = try JSONEncoder().encode(payload)
|
||||
do {
|
||||
let (_, _) = try await URLSession.shared.data(for: request)
|
||||
return
|
||||
} catch {
|
||||
print("Failed to fetch data: \(error)")
|
||||
Log.info("Creating account with Damus Purple server", for: .damus_purple)
|
||||
|
||||
let (data, response) = try await make_nip98_authenticated_request(
|
||||
method: .post,
|
||||
url: url,
|
||||
payload: encoded_payload,
|
||||
auth_keypair: self.keypair
|
||||
)
|
||||
|
||||
if let httpResponse = response as? HTTPURLResponse {
|
||||
switch httpResponse.statusCode {
|
||||
case 200:
|
||||
Log.info("Created an account with Damus Purple server", for: .damus_purple)
|
||||
default:
|
||||
Log.error("Error in creating account with Damus Purple. HTTP status code: %d", for: .damus_purple, httpResponse.statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
@@ -95,26 +102,45 @@ class DamusPurple: StoreObserverDelegate {
|
||||
|
||||
do {
|
||||
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
|
||||
print(receiptData)
|
||||
|
||||
let url = environment.get_base_url().appendingPathComponent("accounts/\(keypair.pubkey.hex())/app-store-receipt")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.httpBody = receiptData
|
||||
|
||||
do {
|
||||
let (_, _) = try await URLSession.shared.data(for: request)
|
||||
print("Sent receipt")
|
||||
} catch {
|
||||
print("Failed to fetch data: \(error)")
|
||||
Log.info("Sending in-app purchase receipt to Damus Purple server", for: .damus_purple)
|
||||
|
||||
let (data, response) = try await make_nip98_authenticated_request(
|
||||
method: .post,
|
||||
url: url,
|
||||
payload: receiptData,
|
||||
auth_keypair: self.keypair
|
||||
)
|
||||
|
||||
if let httpResponse = response as? HTTPURLResponse {
|
||||
switch httpResponse.statusCode {
|
||||
case 200:
|
||||
Log.info("Sent in-app purchase receipt to Damus Purple server successfully", for: .damus_purple)
|
||||
default:
|
||||
Log.error("Error in sending in-app purchase receipt to Damus Purple. HTTP status code: %d", for: .damus_purple, httpResponse.statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch { print("Couldn't read receipt data with error: " + error.localizedDescription) }
|
||||
catch {
|
||||
Log.error("Couldn't read receipt data with error: %s", for: .damus_purple, error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: API types
|
||||
|
||||
extension DamusPurple {
|
||||
fileprivate struct AccountInfo: Codable {
|
||||
let pubkey: String
|
||||
let created_at: UInt64
|
||||
let expiry: UInt64?
|
||||
let active: Bool
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Helper structures
|
||||
|
||||
extension DamusPurple {
|
||||
|
||||
Reference in New Issue
Block a user