Skip to content

Commit

Permalink
added main cook proj
Browse files Browse the repository at this point in the history
  • Loading branch information
nedley committed Dec 11, 2019
1 parent e8fac76 commit 860daf8
Show file tree
Hide file tree
Showing 17 changed files with 1,529 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
xcuserdata/
*.xcuserstate
project.xcworkspace/
2 changes: 1 addition & 1 deletion CookMailPlugin/CookMailPlugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1120;
ORGANIZATIONNAME = " ";
ORGANIZATIONNAME = ned;
TargetAttributes = {
619F2CB51D8BDEF400BC8A84 = {
CreatedOnToolsVersion = 7.3;
Expand Down
410 changes: 410 additions & 0 deletions cook.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions cook.xcodeproj/xcshareddata/xcschemes/cook.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1120"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "81BB91EA2396DB9300EED6B7"
BuildableName = "cook"
BlueprintName = "cook"
ReferencedContainer = "container:cook.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "81BB91EA2396DB9300EED6B7"
BuildableName = "cook"
BlueprintName = "cook"
ReferencedContainer = "container:cook.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "81BB91EA2396DB9300EED6B7"
BuildableName = "cook"
BlueprintName = "cook"
ReferencedContainer = "container:cook.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
157 changes: 157 additions & 0 deletions cook/API/API.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//
// API.swift
// cook
//
// Created by ned on 04/12/2019.
// Copyright © 2019 ned. All rights reserved.
//

import AltSign

enum API {

/* TEAM */

static func fetchTeam(account: ALTAccount, session: ALTAppleAPISession, completion: @escaping((Result<ALTTeam, Error>) -> Void)) {
ALTAppleAPI.shared.fetchTeams(for: account, session: session) { teams, error in
if let teams = teams, !teams.isEmpty {
logger.log(.verbose, "Got \(teams.count) teams: \(teams)")
if let team = teams.first(where: { $0.type == .free }) {
completion(.success(team))
} else {
completion(.failure(TeamError.noFreeTeamsAvailable))
}
} else {
completion(.failure(error ?? TeamError.missingTeams))
}
}
}

/* CERTIFICATES */

static func fetchCertificates(team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<[ALTCertificate], Error>) -> Void)) {
ALTAppleAPI.shared.fetchCertificates(for: team, session: session) { certificates, error in
if let certificates = certificates {
logger.log(.verbose, "Got \(certificates.count) certificates: \(certificates)")
completion(.success(certificates))
} else {
completion(.failure(error ?? CertificateError.missingCertificate))
}
}
}

static func revokeCertificate(certificate: ALTCertificate, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<Bool, Error>) -> Void)) {
ALTAppleAPI.shared.revoke(certificate, for: team, session: session) { (revoked, error) in
if let error = error {
completion(.failure(error))
} else {
completion(.success(revoked))
}
}
}

static func createCertificate(encodedCSR: String, prefix: String, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<ALTCertificate, Error>) -> Void)) {
ALTAppleAPI.shared.addCertificate(encodedCSR: encodedCSR, prefix: prefix, to: team, session: session) { (certificate, error) in
if let certificate = certificate {
logger.log(.verbose, "Added cert: \(certificate)")
completion(.success(certificate))
} else {
completion(.failure(error ?? CertificateError.missingCertificate))
}
}
}

static func createCertificate(machineName: String, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<ALTCertificate, Error>) -> Void)) {
ALTAppleAPI.shared.addCertificate(machineName: machineName, to: team, session: session) { (certificate, error) in
if let certificate = certificate {
logger.log(.verbose, "Added cert: \(certificate)")
completion(.success(certificate))
} else {
completion(.failure(error ?? CertificateError.missingCertificate))
}
}
}

/* APP IDS */

static func fetchAppIds(team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<[ALTAppID], Error>) -> Void)) {
ALTAppleAPI.shared.fetchAppIDs(for: team, session: session) { (appIds, error) in
if let appIds = appIds {
logger.log(.verbose, "Got \(appIds.count) app ids: \(appIds)")
completion(.success(appIds))
} else {
completion(.failure(error ?? AppIdError.missingAppId))
}
}
}

static func registerAppId(appName: String, bundleIdentifier: String, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<ALTAppID, Error>) -> Void)) {
ALTAppleAPI.shared.addAppID(withName: appName, bundleIdentifier: bundleIdentifier, team: team, session: session) { (appId, error) in
if let appId = appId {
logger.log(.verbose, "Successfully registered app id \(appId)")
completion(.success(appId))
} else {
completion(.failure(error ?? AppIdError.missingAppId))
}
}
}

static func deleteAppId(appId: ALTAppID, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<Bool, Error>) -> Void)) {
ALTAppleAPI.shared.delete(appId, for: team, session: session) { (deleted, error) in
if let error = error {
completion(.failure(error))
} else {
completion(.success(deleted))
}
}
}

static func updateAppId(appId: ALTAppID, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<ALTAppID, Error>) -> Void)) {
ALTAppleAPI.shared.update(appId, team: team, session: session) { (appId, error) in
if let appId = appId {
logger.log(.verbose, "Successfully updated app id \(appId)")
completion(.success(appId))
} else {
completion(.failure(error ?? AppIdError.missingAppId))
}
}
}

/* DEVICES */

static func fetchDevices(team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<[ALTDevice], Error>) -> Void)) {
ALTAppleAPI.shared.fetchDevices(for: team, session: session) { (devices, error) in
if let devices = devices {
logger.log(.verbose, "Got \(devices.count) devices: \(devices)")
completion(.success(devices))
} else {
completion(.failure(error ?? DeviceError.missingDevice))
}
}
}

static func registerDevice(name: String, udid: String, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<ALTDevice, Error>) -> Void)) {
ALTAppleAPI.shared.registerDevice(name: name, identifier: udid, team: team, session: session) { (device, error) in
if let device = device {
logger.log(.verbose, "Registered device \(device)")
completion(.success(device))
} else {
completion(.failure(error ?? DeviceError.missingDevice))
}
}
}

/* PROVISIONING PROFILES */

static func fetchProvisioningProfile(for appId: ALTAppID, team: ALTTeam, session: ALTAppleAPISession, completion: @escaping((Result<ALTProvisioningProfile, Error>) -> Void)) {
ALTAppleAPI.shared.fetchProvisioningProfile(for: appId, team: team, session: session) { (profile, error) in
if let profile = profile {
logger.log(.verbose, "Got profile \(profile)")
completion(.success(profile))
} else {
completion(.failure(error ?? ProvisioningError.missingProfile))
}
}
}

}
115 changes: 115 additions & 0 deletions cook/Recipes/CreateCertificate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// CreateCertificate.swift
// cook
//
// Created by ned on 05/12/2019.
// Copyright © 2019 ned. All rights reserved.
//

import AltSign

struct CreateCertificate: ExecutableRecipe {

var csrPath: String
var pemPath: String
var force: Bool

init(csrPath: String, pemPath: String, force: Bool) {
self.csrPath = csrPath
self.pemPath = pemPath
self.force = force
}

func execute() {
auth.start()
auth.completionHandler = { account, session, error in
guard let account = account, let session = session else { return _abort(error ?? AuthError.unknownAuthFailure) }

logger.log(.verbose, "Successfully logged in! \(account)")

logger.log(.info, "Fetching team...")
API.fetchTeam(account: account, session: session) { result in
switch result {
case .failure(let error): return _abort(error)
case .success(let team):
logger.log(.verbose, "Chose team: \(team)")

func fetch() {
logger.log(.info, "Fetching certificates...")
API.fetchCertificates(team: team, session: session) { result in
switch result {
case .failure(let error): return _abort(error)
case .success(let certs):
func revoke(cert: ALTCertificate) {
logger.log(.verbose, "Revoking certificate \(cert)...")
API.revokeCertificate(certificate: cert, team: team, session: session) { result in
switch result {
case .failure(let error): return _abort(error)
case .success(let revoked):
if revoked {
logger.log(.info, "Certificate revoked successfully...")
return fetch()
} else {
logger.log(.verbose, "Certificate could not be revoked...")
return _abort(CertificateError.failedToRevoke)
}
}
}
}

if let cert = certs.first(where: { $0.machineName?.hasPrefix(Utils.machinePrefix) ?? false }) {
if self.force {
logger.log(.info, "Certificate already exists, revoking...")
return revoke(cert: cert)
} else {
logger.log(.error, "Certificate already exists! Aborting... (Use -f to force revoke)")
return _abort(CertificateError.certificateAlreadyExists)
}
} else {
logger.log(.info, "Adding certificate with given CSR...")
do {
let csr = try String(contentsOfFile: self.csrPath, encoding: .utf8)
API.createCertificate(encodedCSR: csr, prefix: Utils.machinePrefix, team: team, session: session) { result in
switch result {
case .failure(let error as NSError):
if error.localizedDescription.contains("7460") {
if self.force {
guard let cert = certs.first(where: { $0.name.contains("iPhone Developer") }) else { return _abort(CertificateError.missingCertificate) }
logger.log(.error, "Unable to add certificate, revoking \(cert)...")
return revoke(cert: cert)
} else {
logger.log(.error, "Error: You already have a current iOS Development certificate or a pending certificate request. Use -f to force revoke.")
return _abort(CertificateError.certificateAlreadyExists)
}
} else {
return _abort(error)
}
case .success(_):
API.fetchCertificates(team: team, session: session) { result in
switch result {
case .failure(let error): return _abort(error)
case .success(let certs):
guard let cert = certs.first(where: { $0.machineName?.hasPrefix(Utils.machinePrefix) ?? false }) else { return _abort(CertificateError.missingCertificate) }
guard let certData = cert.data else { return _abort(CertificateError.missingData) }
Utils.save(data: certData, to: self.pemPath)
logger.log(.success, "Done! Saved PEM cert to \(self.pemPath)")
exit(EXIT_SUCCESS)
}
}
}
}
} catch let error {
return _abort(error)
}
}
}
}
}

fetch()

}
}
}
}
}
Loading

0 comments on commit 860daf8

Please sign in to comment.