Skip to content

Instantly share code, notes, and snippets.

View crspybits's full-sized avatar
🏠
Working from home

Christopher Prince crspybits

🏠
Working from home
View GitHub Profile
@crspybits
crspybits / ServerParameters.swift
Created October 23, 2021 21:35
ServerParameters
// Typical parameters to send to a server
// See https://github.com/crspybits/SolidAuthSwift/issues/6
public struct ServerParameters: Codable {
public let refresh: RefreshParameters
// This should be something like "https://crspybits.trinpod.us", "https://crspybits.inrupt.net", or "https://pod.inrupt.com/crspybits".
// Aka. host URL; see https://github.com/SyncServerII/ServerSolidAccount/issues/4
// If this is nil (i.e., it could not be obtained here), and your app needs it, you'll need to prompt the user for it. If it is not nil, you might want to confirm the specific storage location your app plans to use with the user anyways. E.g., your app might want to use "https://pod.inrupt.com/crspybits/YourAppPath/".
public let storageIRI: URL?
@crspybits
crspybits / ResourceCredentials.swift
Created October 23, 2021 21:23
ResourceCredentials
public protocol ResourceConfigurable {
// The public PEM key converted to a JWK.
var jwk: JWK_RSA { get }
var privateKey: String { get }
var clientId: String { get }
var clientSecret: String { get }
// The "base URL" to use to make requests to the users Solid Pod.
@crspybits
crspybits / SignInController-Demo.swift
Created October 23, 2021 21:20
SignInController-Demo
guard let controller = try? SignInController(config: config) else {
logger.error("Could not initialize Controller")
return
}
controller.start() { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
@crspybits
crspybits / SignInConfiguration-Comment.Swift
Created October 23, 2021 21:16
SignInConfiguration-Comment.Swift
/**
* Starts off sequence:
* 1) Discovery: Fetch configuration
* 2) Client registration
* 3) Authorization request
* 4) Get refresh token and id token
* 5) Get the users webid
* 6) Attempt to get storage IRI
*/
@crspybits
crspybits / SignInConfiguration.swift
Created October 23, 2021 21:09
SignInConfiguration
public struct SignInConfiguration {
// e.g., "https://solidcommunity.net"
public let issuer: String
// E.g., "biz.SpasticMuffin.Neebla.demo:/mypath"
public let redirectURI: String
var redirectURL: URL? {
return URL(string: redirectURI)
}
/// Validate the given refresh token. This does *not* generate a new id token-- the API returns an updated *access token*, which Apple doesn't define a use for (and we don't save).
/// On success, updates the lastRefreshTokenValidation.
func validateRefreshToken(refreshToken: String, completion: @escaping (Swift.Error?) -> ()) {
self.refreshToken = refreshToken
let clientSecret:String
do {
clientSecret = try createClientSecret()
} catch let error {
completion(error)
/// This can only be called once for a specific serverAuthCode.
func generateRefreshToken(serverAuthCode: String, completion: @escaping (Swift.Error?) -> ()) {
let clientSecret:String
do {
clientSecret = try createClientSecret()
} catch let error {
completion(error)
return
}
@crspybits
crspybits / ContentHash.swift
Created October 22, 2018 05:37
Create Dropbox Content Hash in Swift on iOS
// CommonCrypto is only available with Xcode 10 for import into Swift; see also https://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework
import CommonCrypto
class Hashing {
// From https://stackoverflow.com/questions/25388747/sha256-in-swift
private static func sha256(data : Data) -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &hash)
}
@crspybits
crspybits / gist:e43c68cffe74de27f0a157006c1220e6
Created October 22, 2018 05:34
Create Dropbox Content Hash in Swift
// CommonCrypto is only available with Xcode 10 for import into Swift; see also https://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework
import CommonCrypto
class Hashing {
// From https://stackoverflow.com/questions/25388747/sha256-in-swift
private static func sha256(data : Data) -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &hash)
}
@crspybits
crspybits / gist:6973616226e2a6ffec6653257fc69a01
Created October 22, 2018 05:31
Dropbox Content Hash In Swift
// CommonCrypto is only available with Xcode 10 for import into Swift; see also https://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework
import CommonCrypto
class Hashing {
// From https://stackoverflow.com/questions/25388747/sha256-in-swift
private static func sha256(data : Data) -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &hash)
}