From b194b4b6422fe8aeb08de4150f19c56873bf7e12 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 4 Nov 2019 13:42:19 -0800 Subject: [PATCH] Fetches Patreon creator access token from AltStore source --- AltStore.xcodeproj/project.pbxproj | 6 ++++++ AltStore/AltStore-Bridging-Header.h | 1 + AltStore/Components/Keychain.swift | 3 +++ AltStore/Model/Source.swift | 7 +++++++ AltStore/Operations/FetchSourceOperation.swift | 5 +++++ AltStore/Patreon/PatreonAPI.swift | 3 ++- AltStore/Resources/apps.json | 5 ++++- AltStore/Types/ALTSourceUserInfoKey.h | 12 ++++++++++++ AltStore/Types/ALTSourceUserInfoKey.m | 11 +++++++++++ 9 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 AltStore/Types/ALTSourceUserInfoKey.h create mode 100644 AltStore/Types/ALTSourceUserInfoKey.m diff --git a/AltStore.xcodeproj/project.pbxproj b/AltStore.xcodeproj/project.pbxproj index 62f81465..aa0df8a8 100644 --- a/AltStore.xcodeproj/project.pbxproj +++ b/AltStore.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ BF1E315F22A0635900370A3C /* libAltKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BF1E315022A0616100370A3C /* libAltKit.a */; }; BF1E316022A0636400370A3C /* libAltKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BF1E315022A0616100370A3C /* libAltKit.a */; }; BF258CE322EBAE2800023032 /* AppProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF258CE222EBAE2800023032 /* AppProtocol.swift */; }; + BF26A0E12370C5D400F53F9F /* ALTSourceUserInfoKey.m in Sources */ = {isa = PBXBuildFile; fileRef = BF26A0E02370C5D400F53F9F /* ALTSourceUserInfoKey.m */; }; BF29012F2318F6B100D88A45 /* AppBannerView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BF29012E2318F6B100D88A45 /* AppBannerView.xib */; }; BF2901312318F7A800D88A45 /* AppBannerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF2901302318F7A800D88A45 /* AppBannerView.swift */; }; BF3D648822E79A3700E9056B /* AppPermission.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF3D648722E79A3700E9056B /* AppPermission.swift */; }; @@ -304,6 +305,8 @@ BF1E315022A0616100370A3C /* libAltKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libAltKit.a; sourceTree = BUILT_PRODUCTS_DIR; }; BF219A7E22CAC431007676A6 /* AltStore.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AltStore.entitlements; sourceTree = ""; }; BF258CE222EBAE2800023032 /* AppProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppProtocol.swift; sourceTree = ""; }; + BF26A0DF2370C5D400F53F9F /* ALTSourceUserInfoKey.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ALTSourceUserInfoKey.h; sourceTree = ""; }; + BF26A0E02370C5D400F53F9F /* ALTSourceUserInfoKey.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ALTSourceUserInfoKey.m; sourceTree = ""; }; BF29012E2318F6B100D88A45 /* AppBannerView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AppBannerView.xib; sourceTree = ""; }; BF2901302318F7A800D88A45 /* AppBannerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppBannerView.swift; sourceTree = ""; }; BF3D648722E79A3700E9056B /* AppPermission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppPermission.swift; sourceTree = ""; }; @@ -613,6 +616,8 @@ BF3D648C22E79AC800E9056B /* ALTAppPermission.m */, BF54E81F2315EF0D000AE0D8 /* ALTPatreonBenefitType.h */, BF54E8202315EF0D000AE0D8 /* ALTPatreonBenefitType.m */, + BF26A0DF2370C5D400F53F9F /* ALTSourceUserInfoKey.h */, + BF26A0E02370C5D400F53F9F /* ALTSourceUserInfoKey.m */, BF41B807233433C100C593A3 /* LoadingState.swift */, ); path = Types; @@ -1459,6 +1464,7 @@ BFD2478F2284C8F900981D42 /* Button.swift in Sources */, BFD5D6F6230DDB12007955AB /* Tier.swift in Sources */, BFB11692229322E400BB457C /* DatabaseManager.swift in Sources */, + BF26A0E12370C5D400F53F9F /* ALTSourceUserInfoKey.m in Sources */, BFC1F38D22AEE3A4003AC21A /* DownloadAppOperation.swift in Sources */, BF54E8212315EF0D000AE0D8 /* ALTPatreonBenefitType.m in Sources */, BFBBE2E122931F81002097FA /* InstalledApp.swift in Sources */, diff --git a/AltStore/AltStore-Bridging-Header.h b/AltStore/AltStore-Bridging-Header.h index 89f935ce..58fcad98 100644 --- a/AltStore/AltStore-Bridging-Header.h +++ b/AltStore/AltStore-Bridging-Header.h @@ -5,3 +5,4 @@ #import "NSError+ALTServerError.h" #import "ALTAppPermission.h" #import "ALTPatreonBenefitType.h" +#import "ALTSourceUserInfoKey.h" diff --git a/AltStore/Components/Keychain.swift b/AltStore/Components/Keychain.swift index da44ae2d..a6705aa0 100644 --- a/AltStore/Components/Keychain.swift +++ b/AltStore/Components/Keychain.swift @@ -71,6 +71,9 @@ class Keychain @KeychainItem(key: "patreonRefreshToken") var patreonRefreshToken: String? + @KeychainItem(key: "patreonCreatorAccessToken") + var patreonCreatorAccessToken: String? + private init() { } diff --git a/AltStore/Model/Source.swift b/AltStore/Model/Source.swift index 5aaf8368..6250ee27 100644 --- a/AltStore/Model/Source.swift +++ b/AltStore/Model/Source.swift @@ -27,6 +27,9 @@ class Source: NSManagedObject, Fetchable, Decodable @NSManaged var identifier: String @NSManaged var sourceURL: URL + /* Non-Core Data Properties */ + var userInfo: [ALTSourceUserInfoKey: String]? + /* Relationships */ @objc(apps) @NSManaged private(set) var _apps: NSOrderedSet @objc(newsItems) @NSManaged private(set) var _newsItems: NSOrderedSet @@ -54,6 +57,7 @@ class Source: NSManagedObject, Fetchable, Decodable case name case identifier case sourceURL + case userInfo case apps case news } @@ -74,6 +78,9 @@ class Source: NSManagedObject, Fetchable, Decodable self.identifier = try container.decode(String.self, forKey: .identifier) self.sourceURL = try container.decode(URL.self, forKey: .sourceURL) + let userInfo = try container.decodeIfPresent([String: String].self, forKey: .userInfo) + self.userInfo = userInfo?.reduce(into: [:]) { $0[ALTSourceUserInfoKey($1.key)] = $1.value } + let apps = try container.decodeIfPresent([StoreApp].self, forKey: .apps) ?? [] for (index, app) in apps.enumerated() { diff --git a/AltStore/Operations/FetchSourceOperation.swift b/AltStore/Operations/FetchSourceOperation.swift index 952250d5..707ade58 100644 --- a/AltStore/Operations/FetchSourceOperation.swift +++ b/AltStore/Operations/FetchSourceOperation.swift @@ -68,6 +68,11 @@ class FetchSourceOperation: ResultOperation let source = try decoder.decode(Source.self, from: data) + if let patreonAccessToken = source.userInfo?[.patreonAccessToken] + { + Keychain.shared.patreonCreatorAccessToken = patreonAccessToken + } + #if STAGING source.sourceURL = self.sourceURL #endif diff --git a/AltStore/Patreon/PatreonAPI.swift b/AltStore/Patreon/PatreonAPI.swift index d8610145..08258206 100644 --- a/AltStore/Patreon/PatreonAPI.swift +++ b/AltStore/Patreon/PatreonAPI.swift @@ -11,7 +11,6 @@ import AuthenticationServices private let clientID = "ZMx0EGUWe4TVWYXNZZwK_fbIK5jHFVWoUf1Qb-sqNXmT-YzAGwDPxxq7ak3_W5Q2" private let clientSecret = "1hktsZB89QyN69cB4R0tu55R4TCPQGXxvebYUUh7Y-5TLSnRswuxs6OUjdJ74IJt" -private let creatorAccessToken = "NSX1ts9Rf9IzKRCu8GjbwsZ6wll8bDtoJxNbPbp2eZo" private let campaignID = "2863968" @@ -348,7 +347,9 @@ private extension PatreonAPI { case .none: break case .creator: + guard let creatorAccessToken = Keychain.shared.patreonCreatorAccessToken else { return completion(.failure(Error.invalidAccessToken)) } request.setValue("Bearer " + creatorAccessToken, forHTTPHeaderField: "Authorization") + case .user: guard let accessToken = Keychain.shared.patreonAccessToken else { return completion(.failure(Error.notAuthenticated)) } request.setValue("Bearer " + accessToken, forHTTPHeaderField: "Authorization") diff --git a/AltStore/Resources/apps.json b/AltStore/Resources/apps.json index 4db81cb6..d1ceacaf 100644 --- a/AltStore/Resources/apps.json +++ b/AltStore/Resources/apps.json @@ -202,5 +202,8 @@ "date": "2019-10-09", "notify": false } - ] + ], + "userInfo": { + "patreonAccessToken": "lb2Sq14mVCejEtITnlGfszbYXDhXmrdpiOMCEZjlfFk" + } } diff --git a/AltStore/Types/ALTSourceUserInfoKey.h b/AltStore/Types/ALTSourceUserInfoKey.h new file mode 100644 index 00000000..f18a0365 --- /dev/null +++ b/AltStore/Types/ALTSourceUserInfoKey.h @@ -0,0 +1,12 @@ +// +// ALTSourceUserInfoKey.h +// AltStore +// +// Created by Riley Testut on 11/4/19. +// Copyright © 2019 Riley Testut. All rights reserved. +// + +#import + +typedef NSString *ALTSourceUserInfoKey NS_TYPED_EXTENSIBLE_ENUM; +extern ALTSourceUserInfoKey const ALTSourceUserInfoKeyPatreonAccessToken; diff --git a/AltStore/Types/ALTSourceUserInfoKey.m b/AltStore/Types/ALTSourceUserInfoKey.m new file mode 100644 index 00000000..4d715cdc --- /dev/null +++ b/AltStore/Types/ALTSourceUserInfoKey.m @@ -0,0 +1,11 @@ +// +// ALTSourceUserInfoKey.m +// AltStore +// +// Created by Riley Testut on 11/4/19. +// Copyright © 2019 Riley Testut. All rights reserved. +// + +#import "ALTSourceUserInfoKey.h" + +ALTSourceUserInfoKey const ALTSourceUserInfoKeyPatreonAccessToken = @"patreonAccessToken";