Skip to content
This repository was archived by the owner on Aug 31, 2024. It is now read-only.

Commit 42fff9d

Browse files
authored
Merge pull request #3 from nomadj/main
Allow Basic Auth
2 parents 336df3f + f7d5c9f commit 42fff9d

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ import IPFSKit
2323
let client: IPFSClient?
2424

2525
public init() {
26-
client = try? IPFSClient.init(host: "ipfs.infura.io", port: 5001, ssl: true)
26+
client = try? IPFSClient.init(
27+
host: "ipfs.infura.io",
28+
port: 5001,
29+
ssl: true,
30+
id: "INFURA-PROJECT-ID",
31+
secret: "INFURA-SECRET"
32+
)
2733
}
2834
```
2935

Sources/IPFSKit/HttpIo.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum HttpIoError : Error {
1616
}
1717

1818
public struct HttpIo : NetworkIo {
19+
let auth: String
1920

2021
public func receiveFrom(_ source: String, completionHandler: @escaping (Data) throws -> Void) throws {
2122
guard let url = URL(string: source) else { throw HttpIoError.urlError("Invalid URL") }
@@ -55,15 +56,15 @@ public struct HttpIo : NetworkIo {
5556

5657
public func sendTo(_ target: String, content: Data, completionHandler: @escaping (Data) -> Void) throws {
5758

58-
var multipart = try Multipart(targetUrl: target, encoding: .utf8)
59+
var multipart = try Multipart(targetUrl: target, encoding: .utf8, auth: self.auth)
5960
multipart = try Multipart.addFilePart(multipart, fileName: nil , fileData: content)
6061
Multipart.finishMultipart(multipart, completionHandler: completionHandler)
6162
}
6263

6364

6465
public func sendTo(_ target: String, filePath: String, completionHandler: @escaping (Data) -> Void) throws {
6566

66-
var multipart = try Multipart(targetUrl: target, encoding: .utf8)
67+
var multipart = try Multipart(targetUrl: target, encoding: .utf8, auth: self.auth)
6768

6869
multipart = try handle(oldMultipart: multipart, files: [filePath])
6970

Sources/IPFSKit/IPFSClient.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ extension Multihash : Hashable {
1717

1818
public protocol IPFSBase {
1919
var baseUrl: String { get }
20+
var projectId: String { get }
21+
var secret: String { get }
2022

2123
/// Second Tier commands
2224
var refs: Refs { get }
@@ -160,6 +162,8 @@ enum IpfsCmdString : String {
160162
public class IPFSClient: IPFSBase {
161163

162164
public var baseUrl: String = ""
165+
public var projectId: String = ""
166+
public var secret: String = ""
163167

164168
public let scheme: String
165169
public let host: String
@@ -190,7 +194,7 @@ public class IPFSClient: IPFSBase {
190194
if protoComponents[0].hasPrefix("ip") == true &&
191195
protoComponents[2].hasPrefix("tcp") == true {
192196

193-
try self.init(host: protoComponents[1],port: Int(protoComponents[3])!)
197+
try self.init(host: protoComponents[1],port: Int(protoComponents[3])!, id: "", secret: "")
194198
} else {
195199
throw IpfsApiError.initError
196200
}
@@ -200,16 +204,17 @@ public class IPFSClient: IPFSBase {
200204
try self.init(addr: newMultiaddr(addr))
201205
}
202206

203-
public init(host: String, port: Int, version: String = "/api/v0/", ssl: Bool = false) throws {
207+
public init(host: String, port: Int, version: String = "/api/v0/", ssl: Bool = false, id: String, secret: String) throws {
204208
self.scheme = ssl ? "https://" : "http://"
205209
self.host = host
206210
self.port = port
207211
self.version = version
208-
212+
self.projectId = id
213+
self.secret = secret
209214

210215
/// No https yet as TLS1.2 in OS X 10.11 is not allowing comms with the node.
211216
baseUrl = "\(scheme)\(host):\(port)\(version)"
212-
net = HttpIo()
217+
net = HttpIo(auth: authData(projectId: self.projectId, secret: self.secret))
213218

214219
/** All of IPFSApi's properties need to be set before we can use self which
215220
is why we can't just init the sub commands with self (unless we make
@@ -511,3 +516,9 @@ func buildArgString(_ args: [String]) -> String {
511516
return outString
512517
}
513518

519+
public func authData(projectId: String, secret: String) -> String {
520+
let authString = projectId + ":" + secret
521+
let authD = authString.data(using: .utf8)!.base64EncodedString()
522+
return authD
523+
}
524+

Sources/IPFSKit/Multipart.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct Multipart {
2323
var body = NSMutableData()
2424
let request: NSMutableURLRequest
2525

26-
init(targetUrl: String, encoding: String.Encoding) throws {
26+
init(targetUrl: String, encoding: String.Encoding, auth: String) throws {
2727

2828
// Eg. UTF8
2929
self.encoding = encoding
@@ -35,6 +35,7 @@ public struct Multipart {
3535

3636
guard let url = URL(string: targetUrl) else { throw MultipartError.failedURLCreation }
3737
request = NSMutableURLRequest(url: url)
38+
request.addValue("Basic \(auth)", forHTTPHeaderField: "Authorization")
3839
request.httpMethod = "POST"
3940
request.setValue("multipart/form-data; boundary="+boundary, forHTTPHeaderField: "content-type")
4041
request.setValue("Swift IPFS Client", forHTTPHeaderField: "user-agent")

0 commit comments

Comments
 (0)