Skip to content

About

Swift library for accessing various cloud providers incl. Cryptomator vaults

Resources

Code of conduct

Contributing

Security policy

Stars

25 stars

Watchers

6 watching

Forks

Repository files navigation

Swift Compatibility Platform Compatibility Quality Gate Status Coverage

Cloud Access Swift

This library defines the cloud access API used by Cryptomator for iOS. Every cloud is accessed through one protocol, CloudProvider, and a vault provider on top of any of them gives a cleartext view of a Cryptomator vault stored there.

Supported clouds: Box, Dropbox, Google Drive, OneDrive and SharePoint (Microsoft Graph), pCloud, S3, WebDAV, and the local file system, which includes iCloud Drive.

Requirements

  • iOS 17.0 or higher
  • Swift 6.2 or higher

Installation

Add the package with Swift Package Manager:

.package(url: "https://github.com/cryptomator/cloud-access-swift.git", .upToNextMinor(from: "5.1.0"))

It has two products, and an app that uses both imports both modules:

  • CryptomatorCloudAccessCore holds the providers, credentials and vault providers. It's safe to use in app extensions.
  • CryptomatorCloudAccess adds the interactive sign-in of Box, Dropbox, Google Drive, Microsoft Graph and pCloud. It uses API that app extensions can't use, e.g., UIApplication.shared, so link it in the app only.

Quick Start

Sign in to Box in the app, list its root folder, and open a vault stored there:

import CryptomatorCloudAccess
import CryptomatorCloudAccessCore

// once per process
let oauth = try OAuthPersistence.keychain(accessGroup: nil, tokenLockDirectory: tokenLockDirectory)
let setup = BoxSetup(clientID: clientID, clientSecret: clientSecret, oauth: oauth)

// sign in once, and persist the user ID
let credential = try await BoxAuthenticator.authenticate(from: viewController, setup: setup)
let userID = credential.userID

// once per account and process: re-create the credential and create the provider
let provider = try BoxCloudProvider(credential: BoxCredential(userID: userID, setup: setup))
let firstPage = try await provider.fetchItemList(forFolderAt: CloudPath("/"), withPageToken: nil)

// once per vault: open a vault of format 8 in that cloud
let vaultConfig = try UnverifiedVaultConfig(token: vaultConfigToken) // the content of `vault.cryptomator`
let vault = try VaultProviderFactory.createVaultProvider(from: vaultConfig, masterkey: masterkey, vaultPath: CloudPath("/My Vault"), with: provider)
let vaultFirstPage = try await vault.fetchItemList(forFolderAt: CloudPath("/"), withPageToken: nil)

Cloud Providers

Every provider conforms to CloudProvider, whose methods fetch metadata and listings, download, upload, create, delete and move. It is Sendable, and its methods run off the caller's actor. Its doc comments are the contract: what each method throws, how cancellation works, and what is left behind when a call fails.

A network provider is created in three steps:

  1. Create the setup, if the provider has one, once per process, e.g., in the app delegate and in each extension. Pass equal values in every process. The library keeps no global configuration, so pass the setup wherever a credential or a login is created.
  2. Sign in, or create the credential. Persist what re-creates it: the userID of a Box, Dropbox, Google Drive or pCloud credential, the identifier and type of a Microsoft Graph credential, or the S3 or WebDAV credential itself, which holds a secret.
  3. Create the provider with its init(credential:). To run downloads and uploads on a background URL session, e.g., in a File Provider extension, use withBackgroundSession(credential:sessionIdentifier:sharedContainerIdentifier:) instead. Its other requests still run on a foreground session.

Keep one provider per account for the life of the process, because each provider caches what it has looked up. A listing returns one page at a time. In a File Provider extension, pass a maxPageSize such as 500 to stay within the extension's memory limit, and return each page as it arrives. To sign out, call the credential's deauthenticate().

Cloud Credential Notes
Box BoxAuthenticator.authenticate(from:setup:) with a BoxSetup
Dropbox DropboxAuthenticator.authenticate(from:setup:) with a DropboxSetup The login can continue in the Dropbox app. For that, declare dbapi-2 and dbapi-8-emm in LSApplicationQueriesSchemes, add the URL scheme db-<app key>, and pass the URLs that the app opens to DropboxAuthenticator.handleRedirectURL(_:setup:).
Google Drive GoogleDriveAuthenticator.authenticate(from:setup:) with a GoogleDriveSetup
OneDrive, SharePoint MicrosoftGraphAuthenticator.authenticate(from:for:setup:) with a MicrosoftGraphSetup Set up the Info.plist and app delegate as MSAL describes. MicrosoftGraphDiscovery finds a SharePoint site's drives, whose identifier the provider takes as driveIdentifier.
pCloud PCloudAuthenticator.authenticate(from:setup:) with a PCloudSetup
S3 S3Credential, checked with S3Authenticator.verifyCredential(_:) Persist the credential that the check returns. It can carry a corrected region or addressing style.
WebDAV WebDAVCredential, checked with WebDAVAuthenticator.verifyCredential(_:) To trust a self-signed server, pass the data of the certificate that TLSCertificateValidator fetches as allowedCertificate.
Local file system None: LocalFileSystemProvider(rootURL:) Also reaches iCloud Drive.

OAuth Persistence

The library keeps the logins of Box, Dropbox, Google Drive and pCloud in the keychain and refreshes the tokens that expire. Create one OAuthPersistence and pass it to each of their setups. Every process that uses the same logins, e.g., the app and its extensions, must create it with the same values. A process refreshes a token only while holding a lock in the token lock directory, so that no two processes send the same single-use refresh token. That directory must therefore lie in the App Group container that they share:

let oauth = try OAuthPersistence.keychain(accessGroup: accessGroup, tokenLockDirectory: tokenLockDirectory, services: [.box: boxService])

services names the keychain service of a provider's logins, e.g., to read logins that an earlier SDK stored. The doc comment of OAuthPersistence.keychain(accessGroup:tokenLockDirectory:services:) describes the keychain items. To keep the logins elsewhere, implement OAuthTokenStorage and pass it to OAuthPersistence.custom(storage:tokenLockDirectory:).

Vaults

A vault provider wraps any cloud provider and encrypts and decrypts names and contents transparently, following Cryptomator's security architecture. It needs a Masterkey from cryptolib-swift. The library supports vault formats 6, 7 and 8:

// vault format 8, with the config in `vault.cryptomator`
let vault = try VaultProviderFactory.createVaultProvider(from: unverifiedVaultConfig, masterkey: masterkey, vaultPath: vaultPath, with: provider)

// vault format 6 or 7, with the `version` of the `MasterkeyFile`
let legacyVault = try VaultProviderFactory.createLegacyVaultProvider(from: masterkey, vaultVersion: vaultVersion, vaultPath: vaultPath, with: provider)

Both return a VaultProvider, a CloudProvider that can also hand out a receipt for a folder move. On S3, a folder move can leave a reference to the moved folder at its source, and the receipt lets you clean it up safely. See Move Receipts.

Logging

The library logs with CocoaLumberjack, to a DDLog instance of its own. Set its level with dynamicCloudAccessLogLevel, and add loggers with CloudAccessDDLog.add() instead of DDLog.add():

import CocoaLumberjackSwift

dynamicCloudAccessLogLevel = .verbose

CloudAccessDDLog.add(DDOSLogger.sharedInstance) // os_log

let fileLogger = DDFileLogger()
fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
fileLogger.logFileManager.maximumNumberOfLogFiles = 7
CloudAccessDDLog.add(fileLogger)

Contributing

Please read our contribution guide if you would like to report a bug, ask a question or help us with coding. To set up, check and test a change, see Development.

Code of Conduct

Help us keep Cryptomator open and inclusive. Please read and follow our Code of Conduct.

License

This project is dual-licensed under the AGPLv3 for FOSS projects as well as a commercial license derived from the LGPL for independent software vendors and resellers. If you want to use this library in applications that are not licensed under the AGPL, feel free to contact our sales team.

About

Swift library for accessing various cloud providers incl. Cryptomator vaults

Resources

Code of conduct

Contributing

Security policy

Stars

25 stars

Watchers

6 watching

Forks

Releases

Used by

Contributors

Languages