-
Notifications
You must be signed in to change notification settings - Fork 6
/
KSPasswordField.h
124 lines (97 loc) · 3.82 KB
/
KSPasswordField.h
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
//
// KSPasswordField.h
// Sandvox
//
// Created by Mike Abdullah on 28/04/2012.
// Copyright (c) 2012-2014 Karelia Software. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Cocoa/Cocoa.h>
typedef NS_ENUM(NSInteger, MATCHING) { HIDE_MATCH = 0, DOESNT_MATCH, PARTIAL_MATCH, FULL_MATCH };
typedef NS_ENUM(NSInteger, PasswordMeter) {
MeterEmpty = 0, // Disallow, very short; don't show strength yet
MeterCommon, // Disallow, too common
MeterShort, // Disallow, too short
MeterInsecure, // Allow, but warn it's insecure (a.k.a. very weak)
MeterWeak, // Allow, but weak
MeterFair,
MeterStrong
};
@interface KSPasswordField : NSSecureTextField <NSTextViewDelegate>
{
@private
BOOL _showsText;
BOOL _becomesFirstResponderWhenToggled;
BOOL _showStrength;
BOOL _showMatchIndicator;
float _strength;
NSUInteger _length;
MATCHING _matching;
PasswordMeter _passwordMeter;
NSString *_descriptionOfStrength;
}
@property(nonatomic) BOOL showStrength;
@property(nonatomic) BOOL showMatchIndicator;
@property(nonatomic) PasswordMeter passwordMeter;
/**
Whether to display the password as plain text or not.
*/
@property(nonatomic) BOOL showsText;
@property (nonatomic, assign) float strength;
@property (nonatomic, assign) NSUInteger length;
@property (nonatomic, assign) MATCHING matching;
@property (nonatomic, copy) NSString *descriptionOfStrength;
/**
Whether the receiver becomes first responder whenever `.showsText` changes.
The default is `YES`, which means that whenever the password is shown or hidden,
the field will try to become the first responder, ready for the user to type
into it. Set to `NO` if you want to perform your own management of the first
responder instead.
*/
@property(nonatomic) BOOL becomesFirstResponderWhenToggled;
/**
Sets `.showsText` to `YES`.
Convenient for connecting up a "Show Password" button.
*/
- (IBAction)showText:(id)sender;
/**
Sets `.showsText` to `NO`.
Convenient for connecting up a "Hide Password" button.
*/
- (IBAction)secureText:(id)sender;
/**
Toggles the value of `.showsText`.
Generally connected up as the action of a "Show Password" checkbox, or some
kind of toggle button.
*/
- (IBAction)toggleTextShown:(id)sender;
/**
Performs cleanup of a potential password by limiting to a single line, and trimming whitespace.
Called by `KSPasswordField` when the user pastes or drags in a string.
@param string The password string to be cleaned up.
*/
- (NSString*)cleanedPasswordForString:(NSString*)string;
- (void) setStrength:(float)strength length:(NSUInteger)length;
- (BOOL) strongEnough;
@end
@interface KSPasswordTextFieldCell : NSTextFieldCell
@end
@interface KSPasswordSecureTextFieldCell : NSSecureTextFieldCell
@end