Skip to content

Commit 6bc5fe0

Browse files
authored
Set AIProxy DeviceCheck bypass token as env variable (#55)
1 parent f5de0e0 commit 6bc5fe0

File tree

5 files changed

+96
-134
lines changed

5 files changed

+96
-134
lines changed

Examples/SwiftOpenAIExample/SwiftOpenAIExample/AIProxyIntroView.swift

+3-23
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ import SwiftOpenAI
1111
struct AIProxyIntroView: View {
1212

1313
@State private var partialKey = ""
14-
@State private var deviceCheckBypass = ""
1514

1615
var body: some View {
1716
NavigationStack {
1817
VStack {
1918
Spacer()
2019
VStack(spacing: 24) {
2120
TextField("Enter partial key", text: $partialKey)
22-
TextField("Enter DeviceCheck bypass", text: $deviceCheckBypass)
2321
}
2422
.padding()
2523
.textFieldStyle(.roundedBorder)
2624

25+
Text("You receive a partial key when you configure an app in the AIProxy dashboard")
26+
.font(.caption)
27+
2728
NavigationLink(destination: OptionsListView(openAIService: aiproxyService, options: OptionsListView.APIOption.allCases.filter({ $0 != .localChat }))) {
2829
Text("Continue")
2930
.padding()
@@ -46,28 +47,7 @@ struct AIProxyIntroView: View {
4647
}
4748

4849
private var aiproxyService: some OpenAIService {
49-
// Attention AIProxy customers!
50-
//
51-
// Please do not let a `deviceCheckBypass` slip into an archived version of your app that you distribute (including through TestFlight).
52-
// Doing so would allow an attacker to use the bypass themselves.
53-
// The bypass is intended to only be used by developers during development in the iOS simulator (where DeviceCheck does not exist).
54-
//
55-
// Please retain these conditional checks if you copy this example into your own code.
56-
// Your integration code should look like this:
57-
//
58-
// #if DEBUG && targetEnvironment(simulator)
59-
// OpenAIServiceFactory.service(
60-
// aiproxyPartialKey: "hardcode-partial-key-here",
61-
// aiproxyDeviceCheckBypass: "hardcode-device-check-bypass-here"
62-
// )
63-
// #else
64-
// OpenAIServiceFactory.service(aiproxyPartialKey: "hardcode-partial-key-here")
65-
// #endif
66-
#if DEBUG && targetEnvironment(simulator)
67-
OpenAIServiceFactory.service(aiproxyPartialKey: partialKey, aiproxyDeviceCheckBypass: deviceCheckBypass)
68-
#else
6950
OpenAIServiceFactory.service(aiproxyPartialKey: partialKey)
70-
#endif
7151
}
7252
}
7353

README.md

+16-14
Original file line numberDiff line numberDiff line change
@@ -3012,33 +3012,35 @@ We offer AIProxy support so that developers can build **and** distribute apps us
30123012

30133013
### How does my SwiftOpenAI code change?
30143014

3015-
SwiftOpenAI supports proxying requests through AIProxy with a small change to your integration code.
3015+
SwiftOpenAI supports proxying requests through AIProxy with two changes to your Xcode project:
30163016

3017-
Instead of initializing `service` with:
3017+
1. Instead of initializing `service` with:
30183018

30193019
let apiKey = "your_openai_api_key_here"
30203020
let service = OpenAIServiceFactory.service(apiKey: apiKey)
30213021

30223022
Use:
30233023

3024-
#if DEBUG && targetEnvironment(simulator)
30253024
let service = OpenAIServiceFactory.service(
30263025
aiproxyPartialKey: "hardcode_partial_key_here",
3027-
aiproxyDeviceCheckBypass: "hardcode_device_check_bypass_here"
30283026
)
3029-
#else
3030-
let service = OpenAIServiceFactory.service(
3031-
aiproxyPartialKey: "hardcode_partial_key_here"
3032-
)
3033-
#endif
30343027

3035-
The `aiproxyPartialKey` and `aiproxyDeviceCheckBypass` values are provided to you on the [AIProxy developer dashboard](https://developer.aiproxy.pro).
3028+
The `aiproxyPartialKey` value is provided to you on the [AIProxy developer dashboard](https://developer.aiproxy.pro)
3029+
3030+
2. Add an `AIPROXY_DEVICE_CHECK_BYPASS' env variable to Xcode. This token is provided to you in the AIProxy
3031+
developer dashboard, and is necessary for the iOS simulator to communicate with the AIProxy backend.
3032+
- Type `cmd shift ,` to open up the "Edit Schemes" menu in Xcode
3033+
- Select `Run` in the sidebar
3034+
- Select `Arguments` from the top nav
3035+
- Add to the "Environment Variables" section (not the "Arguments Passed on Launch" section) an env
3036+
variable with name `AIPROXY_DEVICE_CHECK_BYPASS` and value that we provided you in the AIProxy dashboard
3037+
30363038

3037-
⚠️ It is important that you do not let the `aiproxyDeviceCheckBypass` token leak into a distribution
3038-
build of your app (including TestFlight distributions). Please retain the conditional compilation
3039-
checks that are present in the sample code above.
3039+
⚠️ The `AIPROXY_DEVICE_CHECK_BYPASS` is intended for the simulator only. Do not let it leak into
3040+
a distribution build of your app (including a TestFlight distribution). If you follow the steps above,
3041+
then the constant won't leak because env variables are not packaged into the app bundle.
30403042

3041-
#### What is the `aiproxyDeviceCheckBypass` constant?
3043+
#### What is the `AIPROXY_DEVICE_CHECK_BYPASS` constant?
30423044

30433045
AIProxy uses Apple's [DeviceCheck](https://developer.apple.com/documentation/devicecheck) to ensure
30443046
that requests received by the backend originated from your app on a legitimate Apple device.

0 commit comments

Comments
 (0)