Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
137 lines (128 sloc) 5.21 KB
import Foundation
import Cocoa
struct swift_container: Codable {
let name, last_modified:String
let count:Int
let bytes:double_t
}
struct swift_object: Codable {
var hash, last_modified, name, content_type:String
var subdir, cleanName:String?
var bytes:double_t
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
hash = try values.decodeIfPresent(String.self, forKey: .hash) ?? ""
last_modified = try values.decodeIfPresent(String.self, forKey: .last_modified) ?? ""
bytes = try values.decodeIfPresent(double_t.self, forKey: .bytes) ?? 0.0
content_type = try values.decodeIfPresent(String.self, forKey: .content_type) ?? ""
subdir = try values.decodeIfPresent(String.self, forKey: .subdir) ?? nil
name = try values.decodeIfPresent(String.self, forKey: .name) ?? ""
if name == ""{
cleanName = ( subdir! as NSString).lastPathComponent
}else{
cleanName = ( name as NSString).lastPathComponent
}
}
}
let swiftstack = URL(string: "https://<domain here>/v1/<account>/<container>?format=json")!
let obj_request = NSMutableURLRequest(url: swiftstack, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)
obj_request.httpMethod = "GET"
obj_request.allHTTPHeaderFields = [ "x-auth-token": "<authentication token here>", "cache-control": "no-cache" ]
let obj_config = URLSessionConfiguration.default
obj_config.requestCachePolicy = .reloadIgnoringLocalCacheData
obj_config.urlCache = nil
let obj_session = URLSession.init(configuration: obj_config)
let obj_dataTask = obj_session.dataTask(with: obj_request as URLRequest, completionHandler: { (data, response, error) -> Void in
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
do{
let objects = try JSONDecoder().decode([swift_object].self, from: data!)
objects.forEach{ (object) in
if !object.name.hasPrefix("."){
if object.subdir != nil{
// add to subdir object array
}else{
// add to swift_object object array
}
}
}
}catch{
print("Error converting object")
}
}
})
enum HMACAlgorithm {
case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
func toCCHmacAlgorithm() -> CCHmacAlgorithm {
var result: Int = 0
switch self {
case .MD5:
result = kCCHmacAlgMD5
case .SHA1:
result = kCCHmacAlgSHA1
case .SHA224:
result = kCCHmacAlgSHA224
case .SHA256:
result = kCCHmacAlgSHA256
case .SHA384:
result = kCCHmacAlgSHA384
case .SHA512:
result = kCCHmacAlgSHA512
}
return CCHmacAlgorithm(result)
}
func digestLength() -> Int {
var result: CInt = 0
switch self {
case .MD5:
result = CC_MD5_DIGEST_LENGTH
case .SHA1:
result = CC_SHA1_DIGEST_LENGTH
case .SHA224:
result = CC_SHA224_DIGEST_LENGTH
case .SHA256:
result = CC_SHA256_DIGEST_LENGTH
case .SHA384:
result = CC_SHA384_DIGEST_LENGTH
case .SHA512:
result = CC_SHA512_DIGEST_LENGTH
}
return Int(result)
}
}
extension String {
func hmac(algorithm: HMACAlgorithm, key: String) -> String {
let cKey = key.cString(using: String.Encoding.utf8)
let cData = self.cString(using: String.Encoding.utf8)
var result = [CUnsignedChar](repeating: 0, count: Int(algorithm.digestLength()))
let length : Int = Int(strlen(cKey!))
let data : Int = Int(strlen(cData!))
CCHmac(algorithm.toCCHmacAlgorithm(), cKey!,length , cData!, data, &result)
let hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength())))
var bytes = [UInt8](repeating: 0, count: hmacData.length)
hmacData.getBytes(&bytes, length: hmacData.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02hhx", UInt8(byte))
}
return hexString
}
}
func tempUrlSignGenerator()->String{
let expire = "\(Int(Date().timeIntervalSince1970+7200))"
let method = "GET"
let hash:String = "\(method)\n\(expire)\n\<path to object>".hmac(algorithm: HMACAlgorithm.SHA1, key: "<X-Account-Meta-Temp-Url-Key>")
return "\(hash)&temp_url_expires=\(expire)"
}
let request = URLRequest(url: URL(string: "<domain here>/auth/v1.0/")!)
request.setValue("<Username>", forHTTPHeaderField: "X-Auth-User")
request.setValue("<A very secured password>", forHTTPHeaderField: "X-Auth-Key")
request.httpMethod = "GET"
let task: URLSessionDataTask = URLSession(configuration: URLSessionConfiguration.default).dataTask(with: request) { (data, response, error) -> Void in
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200{
self.xAuthToken = httpResponse.allHeaderFields["X-Auth-Token"] as? String
self.xStorageUrl = httpResponse.allHeaderFields["X-Storage-Url"] as? String
}else{
// Handle failed login...
}
}
task.resume()