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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/*
* Copyright 2008 Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*/
#import "AuthenticationController.h"
#import "IPCClient.h"
#import "KerberosFormatters.h"
#import "BadgedImageView.h"
// to get kim_prompt_type enum
#import <Kerberos/kim_ui_plugin.h>
/*
* glueController KVC mapping is as follows:
* name = client app name
* path = client app bundle path
* title = suggested label for prompt field
* message = desired large text message
* description = longer, detailed, small text message
* username = 'user' part of 'user@REALM.ORG'
* realm = 'REALM.ORG' part of 'user@REALM.ORG'
* realm_history = past realms the user has entered
* prompt_response = auth prompt response
* allow_save_password = whether or not to show the 'save password in keychain' checkbox
* should_save_password = whether or not to save the password in the keychain
* old_password = for change password dialog
* new_password = "
* verify_password = "
*/
// localization keys and tables
#define ACLocalizationTable @"AuthenticationController"
#define ACAppPrincReqKey @"AuthControllerApplicationPrincipalRequest"
#define ACPrincReqKey @"AuthControllerPrincipalRequest"
#define ACAppPasswordReqKey @"AuthControllerApplicationPasswordRequest"
#define ACPasswordReqKey @"AuthControllerPasswordRequest"
#define ACPasswordChangeExpired @"ChangePasswordPasswordExpired"
#define ACPasswordChangeApp @"ChangePasswordApplicationRequest"
#define ACPasswordChangePrinc @"ChangePasswordPrincipalRequest"
@implementation AuthenticationController
@synthesize associatedClient;
@synthesize favoriteIdentities;
@synthesize favoriteOptions;
- (id) init
{
return [self initWithWindowNibName: @"Authentication"];
}
- (void) awakeFromNib
{
[[self window] center];
// We need to float over the loginwindow and SecurityAgent so use its hardcoded level.
[[self window] setLevel:NSScreenSaverWindowLevel];
lifetimeFormatter.displaySeconds = NO;
lifetimeFormatter.displayShortFormat = NO;
[glueController addObserver:self
forKeyPath:identity_string_keypath
options:NSKeyValueObservingOptionNew
context:ACKVOContext];
[glueController addObserver:self
forKeyPath:prompt_response_keypath
options:NSKeyValueObservingOptionNew
context:ACKVOContext];
[glueController addObserver:self
forKeyPath:old_password_keypath
options:NSKeyValueObservingOptionNew
context:ACKVOContext];
[glueController addObserver:self
forKeyPath:new_password_keypath
options:NSKeyValueObservingOptionNew
context:ACKVOContext];
[glueController addObserver:self
forKeyPath:verify_password_keypath
options:NSKeyValueObservingOptionNew
context:ACKVOContext];
}
- (void) dealloc
{
[glueController removeObserver:self forKeyPath:identity_string_keypath];
[glueController removeObserver:self forKeyPath:prompt_response_keypath];
[super dealloc];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
BOOL valid = NO;
if ([(NSString *) context isEqualToString:ACKVOContext]) {
if ([keyPath isEqualToString:identity_string_keypath]) {
valid = [KIMUtilities validateIdentity:[glueController valueForKeyPath:identity_string_keypath]];
[glueController setValue:[NSNumber numberWithBool:valid]
forKeyPath:enable_identity_ok_keypath];
}
else if ([keyPath isEqualToString:prompt_response_keypath]) {
valid = ([[glueController valueForKeyPath:prompt_response_keypath] length] > 0);
[glueController setValue:[NSNumber numberWithBool:valid]
forKeyPath:enable_prompt_ok_keypath];
}
else if ([keyPath isEqualToString:old_password_keypath] ||
[keyPath isEqualToString:new_password_keypath] ||
[keyPath isEqualToString:verify_password_keypath]) {
NSString *oldString = [glueController valueForKeyPath:old_password_keypath];
NSString *newString = [glueController valueForKeyPath:new_password_keypath];
NSString *verifyString = [glueController valueForKeyPath:verify_password_keypath];
valid = ([oldString length] > 0 && [newString length] > 0 &&
[verifyString length] > 0 && [newString isEqualToString:verifyString]);
[glueController setValue:[NSNumber numberWithBool:valid]
forKeyPath:change_password_ok_keypath];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (IBAction) showWindow: (id) sender
{
[super showWindow:sender];
}
- (void) setContent: (NSMutableDictionary *) newContent
{
[self window]; // wake up the nib connections
[glueController setContent:newContent];
}
- (void) showEnterIdentity
{
kim_error err = KIM_NO_ERROR;
NSWindow *theWindow = [self window];
NSString *key = (associatedClient.name) ? ACAppPrincReqKey : ACPrincReqKey;
NSString *message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
associatedClient.name];
self.favoriteIdentities = [NSMutableArray array];
self.favoriteOptions = [NSMutableDictionary dictionary];
// get array of favorite identity strings and associated options
if (!err) {
kim_preferences preferences = NULL;
kim_options kimOptions = NULL;
kim_count i;
kim_count count = 0;
err = kim_preferences_create(&preferences);
if (!err) {
err = kim_preferences_get_number_of_favorite_identities(preferences,
&count);
}
for (i = 0; !err && i < count; i++) {
kim_identity kimIdentity = NULL;
kim_string display_string = NULL;
NSString *identityString = nil;
err = kim_preferences_get_favorite_identity_at_index(preferences,
i,
&kimIdentity,
&kimOptions);
if (!err) {
err = kim_identity_get_display_string(kimIdentity, &display_string);
}
if (!err && display_string) {
identityString = [NSString stringWithUTF8String:display_string];
[favoriteIdentities addObject:identityString];
}
if (!err) {
[favoriteOptions setObject:[KIMUtilities dictionaryForKimOptions:kimOptions]
forKey:identityString];
}
kim_options_free(&kimOptions);
kim_string_free (&display_string);
kim_identity_free (&kimIdentity);
}
kim_preferences_free(&preferences);
}
[glueController setValue:favoriteIdentities forKeyPath:favorite_strings_keypath];
// wake up the nib connections and adjust window size
[self window];
// set up controls with info from associatedClient
[enterBadge setBadgePath:associatedClient.path];
[glueController setValue:message
forKeyPath:message_keypath];
[self hideSpinny];
[self swapView:identityView];
[theWindow makeFirstResponder:identityField];
[[self window] makeKeyAndOrderFront:nil];
}
- (void) showAuthPrompt
{
uint32_t type = [[glueController valueForKeyPath:@"content.prompt_type"] unsignedIntegerValue];
[self hideSpinny];
switch (type) {
case kim_prompt_type_password :
[self showEnterPassword]; break;
case kim_prompt_type_preauth :
default :
[self showSAM]; break;
}
}
- (void) showEnterPassword
{
CGFloat shrinkBy;
NSRect frame;
NSString *key = nil;
NSString *message = nil;
NSWindow *theWindow = [self window];
if ([associatedClient.name isEqualToString:[[NSBundle mainBundle] bundlePath]]) {
key = ACPasswordReqKey;
message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
[glueController valueForKeyPath:identity_string_keypath]];
} else {
key = ACAppPasswordReqKey;
message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
associatedClient.name,
[glueController valueForKeyPath:identity_string_keypath]];
}
[glueController setValue:message
forKeyPath:message_keypath];
// set badge
[passwordBadge setBadgePath:associatedClient.path];
// adjust for checkbox visibility
if (![[glueController valueForKeyPath:allow_save_password_keypath] boolValue] &&
![rememberPasswordInKeychainCheckBox isHidden]) {
[rememberPasswordInKeychainCheckBox setHidden:YES];
frame = [passwordView frame];
shrinkBy = ([passwordField frame].origin.y -
[rememberPasswordInKeychainCheckBox frame].origin.y);
frame.size.height -= shrinkBy;
[passwordView setFrame:frame];
}
[self swapView:passwordView];
[theWindow makeFirstResponder:passwordField];
[self showWindow:nil];
}
- (void) swapView: (NSView *) aView
{
NSWindow *theWindow = [self window];
NSRect windowFrame;
NSRect viewFrame;
[[containerView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
windowFrame = [theWindow frame];
viewFrame = [theWindow frameRectForContentRect:[aView frame]];
windowFrame.origin.y -= viewFrame.size.height - windowFrame.size.height;
windowFrame.size.width = viewFrame.size.width;
windowFrame.size.height = viewFrame.size.height;
[theWindow setFrame:windowFrame display:YES animate:YES];
[containerView addSubview:aView];
}
- (void) showSpinny
{
[enterSpinny startAnimation: nil];
[passwordSpinny startAnimation: nil];
[samSpinny startAnimation: nil];
[changePasswordSpinny startAnimation: nil];
[[self window] setIgnoresMouseEvents:YES];
}
- (void) hideSpinny
{
[enterSpinny stopAnimation: nil];
[passwordSpinny stopAnimation: nil];
[samSpinny stopAnimation: nil];
[changePasswordSpinny stopAnimation: nil];
[[self window] setIgnoresMouseEvents:NO];
}
- (void) showSAM
{
// set badge
[samBadge setBadgePath:associatedClient.path];
[glueController setValue:[NSNumber numberWithBool:NO]
forKeyPath:allow_save_password_keypath];
[self swapView:samView];
[self showWindow:nil];
[[self window] makeFirstResponder:samPromptField];
}
- (void) showChangePassword
{
NSString *key = ([glueController valueForKeyPath:password_expired_keypath]) ? ACAppPrincReqKey : ACPrincReqKey;
NSString *message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
associatedClient.name];
NSWindow *theWindow = [self window];
BOOL expired = [[glueController valueForKeyPath:password_expired_keypath] boolValue];
BOOL calledBySelf = [associatedClient.path isEqualToString:[[NSBundle mainBundle] bundlePath]];
if (calledBySelf) {
key = ACPasswordChangePrinc;
message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
[glueController valueForKeyPath:identity_string_keypath]];
// Please change the Kerberos password for \"%@\"
} else if (!expired) {
key = ACPasswordChangeApp;
message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
associatedClient.name,
[glueController valueForKeyPath:identity_string_keypath]];
// %@ requires that you change the Kerberos password for \"%@\"
} else {
key = ACPasswordChangeExpired;
message = [NSString stringWithFormat:
NSLocalizedStringFromTable(key, ACLocalizationTable, NULL),
[glueController valueForKeyPath:identity_string_keypath]];
// Your password has expired, would you like to change it?
}
[glueController setValue:message forKeyPath:message_keypath];
// set badge
[changePasswordBadge setBadgePath:associatedClient.path];
[self hideSpinny];
[self swapView:changePasswordView];
[self showWindow:nil];
[theWindow makeFirstResponder:oldPasswordField];
}
- (void) showError
{
// wake up the nib connections and adjust window size
[self window];
// set badge
[errorBadge setBadgePath:associatedClient.path];
[self swapView:errorView];
[self showWindow:nil];
}
- (IBAction) sliderDidChange: (id) sender
{
NSInteger increment = 0;
NSInteger newValue = 0;
NSString *keyPath = nil;
if ([sender isEqual:validLifetimeSlider]) {
increment = VALID_LIFETIME_INCREMENT;
keyPath = valid_lifetime_keypath;
}
else if ([sender isEqual:renewableLifetimeSlider]) {
increment = RENEWABLE_LIFETIME_INCREMENT;
keyPath = renewal_lifetime_keypath;
}
if (increment > 0) {
newValue = ([sender integerValue] / increment) * increment;
[ticketOptionsController setValue:[NSNumber numberWithInteger:newValue]
forKeyPath:keyPath];
}
}
- (IBAction) showTicketOptions: (id) sender
{
NSDictionary *options = nil;
NSString *expandedString = nil;
// if this is a favorite, try to load its default options
[identityField validateEditing];
expandedString = [KIMUtilities expandedIdentity:[identityField stringValue]];
// edit the favorite options for this favorite identity
if (expandedString) {
options = [favoriteOptions objectForKey:expandedString];
}
// else fallback to options passed from client
// use a copy of the current options
if (!options) {
options = [[[glueController valueForKeyPath:options_keypath] mutableCopy] autorelease];
}
[ticketOptionsController setContent:options];
[ticketOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities minValidLifetime]]
forKeyPath:min_valid_keypath];
[ticketOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities maxValidLifetime]]
forKeyPath:max_valid_keypath];
[ticketOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities minRenewableLifetime]]
forKeyPath:min_renewable_keypath];
[ticketOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities maxRenewableLifetime]]
forKeyPath:max_renewable_keypath];
[validLifetimeSlider setIntegerValue:
[[ticketOptionsController valueForKeyPath:valid_lifetime_keypath] integerValue]];
[renewableLifetimeSlider setIntegerValue:
[[ticketOptionsController valueForKeyPath:renewal_lifetime_keypath] integerValue]];
[self sliderDidChange:validLifetimeSlider];
[self sliderDidChange:renewableLifetimeSlider];
[NSApp beginSheet:ticketOptionsSheet
modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
- (IBAction) cancelTicketOptions: (id) sender
{
[NSApp endSheet:ticketOptionsSheet returnCode:NSUserCancelledError];
}
- (IBAction) saveTicketOptions: (id) sender
{
[NSApp endSheet:ticketOptionsSheet];
}
- (void) sheetDidEnd: (NSWindow *) sheet
returnCode: (int) returnCode
contextInfo: (void *) contextInfo
{
if (returnCode == NSUserCancelledError) {
// discard new options
[ticketOptionsController setContent:nil];
} else {
kim_error err = KIM_NO_ERROR;
kim_preferences prefs = NULL;
kim_identity identity = NULL;
kim_options options = NULL;
NSString *expandedString = [KIMUtilities expandedIdentity:[identityField stringValue]];;
// replace options if favorite exists
// add to favorites if not already in list
if (!expandedString) {
err = KIM_BAD_PRINCIPAL_STRING_ERR;
}
if (!err) {
[favoriteOptions setObject:[ticketOptionsController content]
forKey:expandedString];
}
if (!err) {
err = kim_preferences_create(&prefs);
}
if (!err) {
err = kim_identity_create_from_string(&identity, [[identityField stringValue] UTF8String]);
}
if (!err) {
options = [KIMUtilities kimOptionsForDictionary:[ticketOptionsController content]];
}
if (!identity) { err = KIM_BAD_PRINCIPAL_STRING_ERR; }
if (!options) { err = KIM_BAD_OPTIONS_ERR; }
if (!err && identity) {
err = kim_preferences_remove_favorite_identity(prefs, identity);
}
if (!err && identity && options) {
err = kim_preferences_add_favorite_identity(prefs, identity, options);
}
if (!err) {
err = kim_preferences_synchronize(prefs);
}
kim_preferences_free(&prefs);
kim_options_free(&options);
kim_identity_free(&identity);
}
[ticketOptionsSheet orderOut:nil];
}
- (IBAction) changePasswordGearAction: (id) sender
{
NSString *expandedString = [KIMUtilities expandedIdentity:[identityField stringValue]];
NSDictionary *options = [favoriteOptions objectForKey:expandedString];
if (!options) {
options = [glueController valueForKeyPath:options_keypath];
}
[self showSpinny];
// the principal must already be valid to get this far
[associatedClient didEnterIdentity:expandedString options:options wantsChangePassword:YES];
}
- (IBAction) cancel: (id) sender
{
[associatedClient didCancel];
}
- (IBAction) enterIdentity: (id) sender
{
NSString *expandedString = [KIMUtilities expandedIdentity:[identityField stringValue]];
NSDictionary *options = [favoriteOptions objectForKey:expandedString];
if (!options) {
options = [glueController valueForKeyPath:options_keypath];
}
[self showSpinny];
// the principal must already be valid to get this far
[associatedClient didEnterIdentity:expandedString options:options wantsChangePassword:NO];
}
- (IBAction) answerAuthPrompt: (id) sender
{
NSString *responseString = [glueController valueForKeyPath:prompt_response_keypath];
NSNumber *saveResponse = [glueController valueForKeyPath:should_save_password_keypath];
if (!saveResponse) {
saveResponse = [NSNumber numberWithBool:NO];
}
[self showSpinny];
[associatedClient didPromptForAuth:responseString
saveResponse:saveResponse];
}
- (IBAction) changePassword: (id) sender
{
NSString *oldString = [glueController valueForKeyPath:old_password_keypath];
NSString *newString = [glueController valueForKeyPath:new_password_keypath];
NSString *verifyString = [glueController valueForKeyPath:verify_password_keypath];
[self showSpinny];
[associatedClient didChangePassword:oldString
newPassword:newString
verifyPassword:verifyString];
}
- (IBAction) showedError: (id) sender
{
[associatedClient didHandleError];
}
@end
|