-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathDynamicOAuthRequestHandler.swift
More file actions
293 lines (251 loc) · 9.9 KB
/
DynamicOAuthRequestHandler.swift
File metadata and controls
293 lines (251 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import AppKit
import Combine
import Foundation
import JSONRPC
import LanguageServerProtocol
import Logger
public protocol DynamicOAuthRequestHandler {
func handleDynamicOAuthRequest(
_ request: DynamicOAuthRequest,
completion: @escaping (AnyJSONRPCResponse) -> Void
)
}
public final class DynamicOAuthRequestHandlerImpl: NSObject, DynamicOAuthRequestHandler {
public static let shared = DynamicOAuthRequestHandlerImpl()
// MARK: - Constants
private enum LayoutConstants {
static let containerWidth: CGFloat = 450
static let fieldWidth: CGFloat = 330
static let labelWidth: CGFloat = 100
static let labelX: CGFloat = 4
static let fieldX: CGFloat = 100
static let spacing: CGFloat = 8
static let hintSpacing: CGFloat = 4
static let labelHeight: CGFloat = 17
static let fieldHeight: CGFloat = 28
static let labelVerticalOffset: CGFloat = 6
static let hintFontSize: CGFloat = 11
static let regularFontSize: CGFloat = 13
}
private enum Strings {
static let clientIdLabel = "Client ID *"
static let clientSecretLabel = "Client Secret"
static let clientIdPlaceholder = "OAuth client ID (azye39d...)"
static let clientSecretPlaceholder = "OAuth client secret (wer32o50f...) or leave it blank"
static let okButton = "OK"
static let cancelButton = "Cancel"
}
public func handleDynamicOAuthRequest(
_ request: DynamicOAuthRequest,
completion: @escaping (AnyJSONRPCResponse) -> Void
) {
guard let params = request.params else { return }
Logger.gitHubCopilot.debug("Received Dynamic OAuth Request: \(params)")
Task { @MainActor in
let response = self.dynamicOAuthRequestAlert(params)
let jsonResult = try? JSONEncoder().encode(response)
let jsonValue = (try? JSONDecoder().decode(JSONValue.self, from: jsonResult ?? Data())) ?? JSONValue.null
completion(AnyJSONRPCResponse(id: request.id, result: jsonValue))
}
}
@MainActor
func dynamicOAuthRequestAlert(_ params: DynamicOAuthParams) -> DynamicOAuthResponse? {
let alert = configureAlert(with: params)
let (clientIdField, clientSecretField) = createAccessoryView(for: alert, params: params)
let modalResponse = alert.runModal()
return handleAlertResponse(
modalResponse,
clientIdField: clientIdField,
clientSecretField: clientSecretField
)
}
// MARK: - Alert Configuration
@MainActor
private func configureAlert(with params: DynamicOAuthParams) -> NSAlert {
let alert = NSAlert()
alert.messageText = params.header ?? params.title
alert.informativeText = params.detail
alert.alertStyle = .warning
alert.addButton(withTitle: Strings.okButton)
alert.addButton(withTitle: Strings.cancelButton)
return alert
}
// MARK: - Accessory View Creation
@MainActor
private func createAccessoryView(
for alert: NSAlert,
params: DynamicOAuthParams
) -> (clientIdField: NSTextField, clientSecretField: NSSecureTextField) {
let (clientIdHint, clientIdHintHeight) = createHintLabel(
text: params.inputs.first(where: { $0.value == "clientId" })?.description ?? ""
)
let (clientSecretHint, clientSecretHintHeight) = createHintLabel(
text: params.inputs.first(where: { $0.value == "clientSecret" })?.description ?? ""
)
let totalHeight = calculateTotalHeight(
clientIdHintHeight: clientIdHintHeight,
clientSecretHintHeight: clientSecretHintHeight
)
let containerView = NSView(frame: NSRect(
x: 0,
y: 0,
width: LayoutConstants.containerWidth,
height: totalHeight
))
let clientIdField = NSTextField()
let clientSecretField = NSSecureTextField()
layoutComponents(
in: containerView,
clientIdField: clientIdField,
clientSecretField: clientSecretField,
clientIdHint: clientIdHint,
clientSecretHint: clientSecretHint,
clientIdHintHeight: clientIdHintHeight,
clientSecretHintHeight: clientSecretHintHeight,
params: params
)
alert.accessoryView = containerView
return (clientIdField, clientSecretField)
}
// MARK: - Component Creation
@MainActor
private func createHintLabel(text: String) -> (label: NSTextField, height: CGFloat) {
let hint = NSTextField(wrappingLabelWithString: text)
hint.font = NSFont.systemFont(ofSize: LayoutConstants.hintFontSize)
hint.textColor = NSColor.secondaryLabelColor
let height = hint.sizeThatFits(NSSize(
width: LayoutConstants.fieldWidth,
height: CGFloat.greatestFiniteMagnitude
)).height
return (hint, height)
}
@MainActor
private func createInputField(placeholder: String) -> NSTextField {
let field = NSTextField()
field.placeholderString = placeholder
field.font = NSFont.systemFont(ofSize: LayoutConstants.regularFontSize)
field.isEditable = true
return field
}
@MainActor
private func createSecureField(placeholder: String) -> NSSecureTextField {
let field = NSSecureTextField()
field.placeholderString = placeholder
field.font = NSFont.systemFont(ofSize: LayoutConstants.regularFontSize)
field.isEditable = true
return field
}
@MainActor
private func createLabel(text: String) -> NSTextField {
let label = NSTextField(labelWithString: text)
label.font = NSFont.systemFont(ofSize: LayoutConstants.regularFontSize)
label.alignment = .left
return label
}
// MARK: - Layout
private func calculateTotalHeight(
clientIdHintHeight: CGFloat,
clientSecretHintHeight: CGFloat
) -> CGFloat {
return clientSecretHintHeight + LayoutConstants.hintSpacing + LayoutConstants.fieldHeight +
LayoutConstants.spacing + clientIdHintHeight + LayoutConstants.hintSpacing +
LayoutConstants.fieldHeight
}
@MainActor
private func layoutComponents(
in containerView: NSView,
clientIdField: NSTextField,
clientSecretField: NSSecureTextField,
clientIdHint: NSTextField,
clientSecretHint: NSTextField,
clientIdHintHeight: CGFloat,
clientSecretHintHeight: CGFloat,
params: DynamicOAuthParams
) {
var currentY: CGFloat = 0
// Client Secret section (bottom)
layoutFieldSection(
in: containerView,
field: clientSecretField,
label: createLabel(text: Strings.clientSecretLabel),
hint: clientSecretHint,
hintHeight: clientSecretHintHeight,
placeholder: params.inputs.first(where: { $0.value == "clientSecret" })?.placeholder ?? Strings.clientSecretPlaceholder,
currentY: ¤tY,
isLastSection: false
)
// Client ID section (top)
layoutFieldSection(
in: containerView,
field: clientIdField,
label: createLabel(text: Strings.clientIdLabel),
hint: clientIdHint,
hintHeight: clientIdHintHeight,
placeholder: params.inputs.first(where: { $0.value == "clientId" })?.placeholder ?? Strings.clientIdPlaceholder,
currentY: ¤tY,
isLastSection: true
)
}
@MainActor
private func layoutFieldSection(
in containerView: NSView,
field: NSTextField,
label: NSTextField,
hint: NSTextField,
hintHeight: CGFloat,
placeholder: String,
currentY: inout CGFloat,
isLastSection: Bool
) {
// Position hint
hint.frame = NSRect(
x: LayoutConstants.fieldX,
y: currentY,
width: LayoutConstants.fieldWidth,
height: hintHeight
)
currentY += hintHeight + LayoutConstants.hintSpacing
// Position field
field.frame = NSRect(
x: LayoutConstants.fieldX,
y: currentY,
width: LayoutConstants.fieldWidth,
height: LayoutConstants.fieldHeight
)
field.placeholderString = placeholder
// Position label
label.frame = NSRect(
x: LayoutConstants.labelX,
y: currentY + LayoutConstants.labelVerticalOffset,
width: LayoutConstants.labelWidth,
height: LayoutConstants.labelHeight
)
// Add to container
containerView.addSubview(label)
containerView.addSubview(field)
containerView.addSubview(hint)
if !isLastSection {
currentY += LayoutConstants.fieldHeight + LayoutConstants.spacing
}
}
// MARK: - Response Handling
private func handleAlertResponse(
_ response: NSApplication.ModalResponse,
clientIdField: NSTextField,
clientSecretField: NSSecureTextField
) -> DynamicOAuthResponse? {
guard response == .alertFirstButtonReturn else {
return nil
}
let clientId = clientIdField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
guard !clientId.isEmpty else {
Logger.gitHubCopilot.info("Client ID is required but was not provided")
return nil
}
let clientSecret = clientSecretField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
return DynamicOAuthResponse(
clientId: clientId,
clientSecret: clientSecret
)
}
}