Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The TurnkeySDK is built to support macOS, iOS, tvOS, watchOS, and visionOS, maki
To integrate the TurnkeySDK into your Swift project, you need to add it as a dependency in your Package.swift file:

```swift
.package(url: "https://github.com/tkhq/swift-sdk", from: "1.1.0")
.package(url: "https://github.com/tkhq/swift-sdk", from: "1.2.0")
```

## Usage
Expand Down
8 changes: 4 additions & 4 deletions Sources/Middleware/ProxyMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ extension ProxyMiddleware: ClientMiddleware {
// Save the current full path of the request (baseUrl + request.path)
let originalURL = baseURL.appendingPathComponent(request.path ?? "")

// Set the X-Forwarded-For header with the saved original request URL
// Set the X-Turnkey-Request-Url header with the saved original request URL
var request = request
let xForwardedForHeader = HTTPField(
name: HTTPField.Name("X-Forwarded-For")!, value: originalURL.absoluteString)
request.headerFields.append(xForwardedForHeader)
let xTurnkeyRequestUrl = HTTPField(
name: HTTPField.Name("X-Turnkey-Request-Url")!, value: originalURL.absoluteString)
request.headerFields.append(xTurnkeyRequestUrl)

// Remove the request path and just forward to the proxyBaseURL
request.path = ""
Expand Down
14 changes: 6 additions & 8 deletions docs/proxy-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ This setup is especially useful for operations like:
- Wallet import/export
- Sub-organization creation

## Important Notes
#### Request Header

#### X-Forwarded-For Header

The middleware adds an `X-Forwarded-For` header to each request, which contains the original request URL. This is used to forward the request to Turnkey's backend.
The middleware adds an `X-Turnkey-Request-Url` header to each request, which contains the original request URL. This is used to forward the request to Turnkey's backend.

Example implementation of a proxy server:

Expand All @@ -35,10 +33,10 @@ app.use(express.json());

app.post('/api/turnkey-proxy', async (req, res) => {
// The original request URL e.g. https://api.turnkey.com/public/v1/submit/email_auth
const turnkeyApiRequestURL = req.headers['x-forwarded-for'];
const turnkeyApiRequestURL = req.headers['X-Turnkey-Request-Url'];

// Remove the 'x-forwarded-for' header
delete req.headers['x-forwarded-for'];
// Remove the 'X-Turnkey-Request-Url' header
delete req.headers['X-Turnkey-Request-Url'];

try {
// Forward the request to the original URL
Expand Down Expand Up @@ -71,4 +69,4 @@ It is crucial that the response from the developer's backend matches exactly wit

## Conclusion

While `ProxyMiddleware` is not required, it provides a convenient way to send requests on behalf of unauthenticated users looking to perform
While `ProxyMiddleware` is not required, it provides a convenient way to send requests on behalf of unauthenticated users looking to perform operations such as email authentication/recovery, wallet import/export, and sub-organization creation.