-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Apple API sessions from Fastlane Spaceship
- Loading branch information
Showing
8 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Foundation | ||
import Yams | ||
|
||
public class FastlaneCookieParser { | ||
public func parse(cookieString: String) throws -> [HTTPCookie] { | ||
let fixed = cookieString.replacingOccurrences(of: "\\n", with: "\n") | ||
let cookies = try YAMLDecoder().decode([FastlaneCookie].self, from: fixed) | ||
return cookies.compactMap(\.httpCookie) | ||
} | ||
} | ||
|
||
struct FastlaneCookie: Decodable { | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case name | ||
case value | ||
case domain | ||
case forDomain = "for_domain" | ||
case path | ||
case secure | ||
case expires | ||
case maxAge = "max_age" | ||
case createdAt = "created_at" | ||
case accessedAt = "accessed_at" | ||
} | ||
|
||
let name: String | ||
let value: String | ||
let domain: String | ||
let forDomain: Bool | ||
let path: String | ||
let secure: Bool | ||
let expires: Date? | ||
let maxAge: Int? | ||
let createdAt: Date | ||
let accessedAt: Date | ||
} | ||
|
||
protocol HTTPCookieConvertible { | ||
var httpCookie: HTTPCookie? { get } | ||
} | ||
|
||
extension FastlaneCookie: HTTPCookieConvertible { | ||
var httpCookie: HTTPCookie? { | ||
|
||
var properties: [HTTPCookiePropertyKey: Any] = [ | ||
.name: self.name, | ||
.value: self.value, | ||
.domain: self.domain, | ||
.path: self.path, | ||
.secure: self.secure, | ||
] | ||
|
||
if forDomain { | ||
properties[.domain] = ".\(self.domain)" | ||
} else { | ||
properties[.domain] = "\(self.domain)" | ||
} | ||
|
||
if let expires = self.expires { | ||
properties[.expires] = expires | ||
} | ||
|
||
if let maxAge = self.maxAge { | ||
properties[.maximumAge] = maxAge | ||
} | ||
|
||
return HTTPCookie(properties: properties) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Foundation | ||
import AppleAPI | ||
import Path | ||
|
||
public class FastlaneSessionManager { | ||
|
||
public enum Constants { | ||
public static let fastlaneSessionEnvVarName = "FASTLANE_SESSION" | ||
public static let fastlaneSpaceshipDir = Path.environmentHome.url | ||
.appendingPathComponent(".fastlane") | ||
.appendingPathComponent("spaceship") | ||
} | ||
|
||
public init() {} | ||
|
||
public func setupFastlaneAuth(fastlaneUser: String) { | ||
// Use ephemeral session so that cookies don't conflict with normal usage | ||
AppleAPI.Current.network.session = URLSession(configuration: .ephemeral) | ||
switch fastlaneUser { | ||
case Constants.fastlaneSessionEnvVarName: | ||
importFastlaneCookiesFromEnv() | ||
default: | ||
importFastlaneCookiesFromFile(fastlaneUser: fastlaneUser) | ||
} | ||
} | ||
|
||
private func importFastlaneCookiesFromEnv() { | ||
guard let cookieString = Current.shell.env(Constants.fastlaneSessionEnvVarName) else { | ||
Current.logging.log("\(Constants.fastlaneSessionEnvVarName) not set") | ||
return | ||
} | ||
do { | ||
let cookies = try Current.fastlaneCookieParser.parse(cookieString: cookieString) | ||
cookies.forEach(AppleAPI.Current.network.session.configuration.httpCookieStorage!.setCookie) | ||
} catch { | ||
Current.logging.log("Failed to parse cookies from \(Constants.fastlaneSessionEnvVarName)") | ||
return | ||
} | ||
} | ||
|
||
private func importFastlaneCookiesFromFile(fastlaneUser: String) { | ||
let cookieFilePath = Constants | ||
.fastlaneSpaceshipDir | ||
.appendingPathComponent(fastlaneUser) | ||
.appendingPathComponent("cookie") | ||
guard | ||
let cookieString = try? String(contentsOf: cookieFilePath) | ||
else { | ||
Current.logging.log("Could not read cookies from \(cookieFilePath)") | ||
return | ||
} | ||
do { | ||
let cookies = try Current.fastlaneCookieParser.parse(cookieString: cookieString) | ||
cookies.forEach(AppleAPI.Current.network.session.configuration.httpCookieStorage!.setCookie) | ||
} catch { | ||
Current.logging.log("Failed to parse cookies from \(cookieFilePath)") | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
---\n- !ruby/object:HTTP::Cookie\n name: myacinfo\n value: myacinfo_dummy\n domain: apple.com\n for_domain: true\n path: "/"\n secure: true\n httponly: true\n expires: \n max_age: \n created_at: 2022-12-12 16:29:24.988867000 +00:00\n accessed_at: 2022-12-12 16:29:24.990398000 +00:00\n- !ruby/object:HTTP::Cookie\n name: DES5a8153bb0fcd039d87286b59d3accea2\n value: DES5a8153bb0fcd039d87286b59d3accea2_dummy\n domain: idmsa.apple.com\n for_domain: true\n path: "/"\n secure: true\n httponly: true\n expires: \n max_age: 2592000\n created_at: 2022-12-09 14:20:51.920971000 +00:00\n accessed_at: 2022-12-12 16:29:23.114185000 +00:00\n- !ruby/object:HTTP::Cookie\n name: dqsid\n value: dqsid_dummy\n domain: appstoreconnect.apple.com\n for_domain: false\n path: "/"\n secure: true\n httponly: true\n expires: \n max_age: 1800\n created_at: &1 2022-12-12 16:29:26.523387000 +00:00\n accessed_at: *1\n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters