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
|
import React from 'react'
import { FormGroup, ControlLabel, FormControl, HelpBlock, Button,
Checkbox, Glyphicon, Overlay, Tooltip, Alert } from 'react-bootstrap'
import Spinner from './spinner'
import Validate from 'lib/validate'
import App from 'app'
import Account from 'models/account'
import Provider from 'models/provider'
class Login extends React.Component {
static get defaultProps() {return{
rememberAllowed: false, // if set, show remember password checkbox
autoAllowed: false, // if set, allow auto setup of provider
domain: null, // if set, only allow this domain
address: null, // if set, only allow this username@domain
onLogin: null, // callback
mode: "login" // one of "login" or "signup"
}}
constructor(props) {
super(props)
// validation states can be null, 'success', 'warning', or 'error'
this.state = {
loading: false,
authError: false, // authentication error message
username: this.props.address,
usernameState: null, // username validation state
usernameError: false, // username help message
password: null, // the main password field
passwordState: null, // password validation state
passwordError: false, // password help message
password2: null, // password confirmation
password2State: null, // password confirm validation state
password2Error: false, // password confirm help message
requireInvite: false,
invite: null, // invite code value
inviteState: null, // invite code validation state
inviteError: false, // invite code error
disabled: false,
remember: false // remember is checked?
}
// prebind:
this.onUsernameChange = this.onUsernameChange.bind(this)
this.onUsernameBlur = this.onUsernameBlur.bind(this)
this.onPassword = this.onPassword.bind(this)
this.onPassword2 = this.onPassword2.bind(this)
this.onInvite = this.onInvite.bind(this)
this.onSubmit = this.onSubmit.bind(this)
this.onKeyPress = this.onKeyPress.bind(this)
this.onRemember = this.onRemember.bind(this)
}
componentDidMount() {
Validate.loadPasswdLib()
if (this.props.domain && this.props.mode == 'signup') {
Provider.get(this.props.domain).then(provider => {
if (provider.enrollment_policy == 'invite') {
this.setState({requireInvite: true})
}
})
}
}
componentDidUpdate(prevProps, prevState) {
// If the user changes anything in the domain (which follows the @ sign)
// it gets replaced reverted to the domain name. This moves the cursor to
// before the @ sign when that happens
if (this.props.domain && this.state.username){
let textarea = this.usernameref
let start = this.state.username.indexOf('@')
if (textarea.selectionStart > start) {
textarea.setSelectionRange(start, start)
}
}
}
render () {
let rememberCheck = ""
let submitButton = ""
let usernameHelp = null
let passwordHelp = null
let password2Help = null
let password2Elem = null
let inviteElem = null
let message = null
let buttonText = "Log In"
/*
* disabled for now
*
if (this.props.rememberAllowed) {
let props = {
style: {marginTop: "0px"},
onChange: this.onRemember
}
if (this.state.remember) {
rememberCheck = <Checkbox {...props} checked>
Remember username and password
</Checkbox>
} else {
rememberCheck = <Checkbox {...props}>
Remember username and password
</Checkbox>
}
}
*/
if (this.state.authError) {
// style may be: success, warning, danger, info
message = (
<Alert bsStyle="danger">{this.state.authError}</Alert>
)
}
if (this.state.usernameError) {
usernameHelp = <HelpBlock>{this.state.usernameError}</HelpBlock>
// let props = {shouldUpdatePosition: true, show:true, placement:"right",
// target:this.refs.username}
// usernameHelp = (
// <Overlay {...props}>
// <Tooltip id="username-tooltip">{this.state.usernameError}</Tooltip>
// </Overlay>
// )
} else {
//usernameHelp = <HelpBlock> </HelpBlock>
}
if (this.state.passwordError) {
passwordHelp = <HelpBlock>{this.state.passwordError}</HelpBlock>
// let props = {shouldUpdatePosition: true, show:true, placement:"right",
// target:this.refs.password, component: {this}}
// passwordHelp = (
// <Overlay {...props}>
// <Tooltip id="password-tooltip">{this.state.passwordError}</Tooltip>
// </Overlay>
// )
} else {
//passwordHelp = <HelpBlock> </HelpBlock>
}
if (this.props.mode == 'signup') {
buttonText = 'Sign Up'
if (this.state.password2Error) {
password2Help = <HelpBlock>{this.state.password2Error}</HelpBlock>
}
password2Elem = (
<FormGroup controlId="loginPassword2" validationState={this.state.password2State}>
<ControlLabel>Repeat Password</ControlLabel>
<FormControl
type="password"
inputRef={ref => this.password2ref = ref}
value={this.state.password2 || ""}
onChange={this.onPassword2}
/>
{this.state.password2State == 'success' ? null : <FormControl.Feedback/>}
{password2Help}
</FormGroup>
)
if (this.state.requireInvite) {
let inviteHelp = null
inviteElem = (
<FormGroup controlId="invite" validationState={this.state.inviteState}>
<ControlLabel>Invite Code</ControlLabel>
<FormControl
value={this.state.invite || ""}
onChange={this.onInvite}
inputRef={ref => this.inviteref = ref}
/>
{inviteHelp}
</FormGroup>
)
}
}
let buttonProps = {
type: "button",
onClick: this.onSubmit,
disabled: !this.maySubmit()
}
if (this.state.loading) {
submitButton = <Button block {...buttonProps}><Spinner /></Button>
} else {
submitButton = <Button block {...buttonProps}>{buttonText}</Button>
}
let usernameDisabled = false
let usernameValue = this.state.username || ""
if (this.props.address) {
usernameDisabled = true
usernameValue = this.props.address
}
const form = (
<form onSubmit={this.onSubmit} onKeyPress={this.onKeyPress}>
{message}
<FormGroup style={{marginBottom: '10px' }} controlId="loginUsername" validationState={this.state.usernameState}>
<ControlLabel>Username</ControlLabel>
<FormControl
componentClass="textarea"
style={{resize: "none"}}
rows="1"
inputRef={(ref) => this.usernameref = ref}
autoFocus
value={usernameValue}
disabled={usernameDisabled}
onChange={this.onUsernameChange}
onBlur={this.onUsernameBlur}
/>
{this.state.usernameState == 'success' ? null : <FormControl.Feedback/>}
{usernameHelp}
</FormGroup>
<FormGroup controlId="loginPassword" validationState={this.state.passwordState}>
<ControlLabel>Password</ControlLabel>
<FormControl
type="password"
inputRef={ref => this.passwordref = ref}
value={this.state.password || ""}
onChange={this.onPassword}
/>
{this.state.passwordState == 'success' ? null : <FormControl.Feedback/>}
{passwordHelp}
</FormGroup>
{password2Elem}
{inviteElem}
{submitButton}
{rememberCheck}
</form>
)
return form
}
//
// Here we do a partial validation, because the user has not stopped typing.
//
onUsernameChange(e) {
let username = e.target.value.toLowerCase().replace("\n", "")
if (this.props.domain) {
let [userpart, domainpart] = username.split(
new RegExp('@|' + this.props.domain.replace(".", "\\.") + '$')
)
username = [userpart, this.props.domain].join('@')
}
let error = Validate.usernameInteractive(username, this.props.domain)
let state = null
if (error) {
state = 'error'
} else {
if (username && username.length > 0) {
let finalError = Validate.username(username)
state = finalError ? null : 'success'
}
}
this.setState({
username: username,
usernameState: state,
usernameError: error ? error : null
})
}
//
// Here we do a more complete validation, since the user have left the field.
//
onUsernameBlur(e) {
let username = e.target.value.toLowerCase()
this.setState({
username: username
})
if (username.length > 0) {
this.validateUsername(username)
} else {
this.setState({
usernameState: null,
usernameError: null
})
}
}
onPassword(e) {
let password = e.target.value
this.setState({password: password})
if (password.length > 0) {
this.validatePassword(password)
} else {
this.setState({
passwordState: null,
passwordError: null
})
}
}
onPassword2(e) {
let password2 = e.target.value
this.setState({password2: password2})
this.validatePassword2(password2, this.state.password)
}
onInvite(e) {
this.setState({invite: e.target.value})
}
onRemember(e) {
let currentValue = e.target.value == 'on' ? true : false
let value = !currentValue
this.setState({remember: value})
}
validateUsername(username) {
let error = Validate.username(username, this.props.domain)
this.setState({
usernameState: error ? 'error' : 'success',
usernameError: error ? error : null
})
}
validatePassword(password) {
let state = null
let message = null
let result = Validate.passwordStrength(password)
if (result) {
message = "Time to crack: " + result.crack_times_display.offline_slow_hashing_1e4_per_second
if (result.score == 0) {
state = 'error'
} else if (result.score == 1 || result.score == 2) {
state = 'warning'
} else {
state = 'success'
}
}
this.setState({
passwordState: state,
passwordError: message
})
this.validatePassword2(this.state.password2, password)
}
validatePassword2(password2, password) {
if (password2) {
if (password != password2) {
this.setState({
password2State: 'error',
password2Error: "Does not match"
})
} else {
this.setState({
password2State: 'success',
password2Error: null
})
}
} else {
this.setState({
password2State: null,
password2Error: null
})
}
}
maySubmit() {
let ok = (
!this.state.loading &&
!this.state.usernameError &&
this.state.username &&
this.state.password
)
if (this.props.mode == 'login') {
return ok
} else if (this.props.mode == 'signup') {
return ok &&
this.state.password2 == this.state.password &&
(!this.state.requireInvite || this.state.invite)
}
}
onSubmit(e) {
e.preventDefault() // don't reload the page please!
if (!this.maySubmit()) { return }
this.setState({loading: true})
if (this.props.mode == 'login') {
this.doLogin()
} else if (this.props.mode == 'signup') {
this.doSignup()
}
}
/**
* Handle key presses
*/
onKeyPress(e) {
// On "Enter", move the focus to the first mounted but unfilled field,
// or submit the form if there are none.
if (e.key === 'Enter') {
// Ignore the @ and domain when checking the username
if (this.usernameref.value.split('@')[0] === ''){
this.usernameref.focus()
return
}
const firstUnfilledField = [
this.passwordref,
this.password2ref,
this.inviteref,
].find(ref => ref != null && ref.value == "")
if (firstUnfilledField) {
firstUnfilledField.focus()
} else {
this.onSubmit(e);
}
}
}
doLogin() {
let account = Account.findOrAdd(this.state.username)
account.login(this.state.password, this.props.autoAllowed).then(
account => {
this.setState({loading: false})
if (this.props.onLogin) {
this.props.onLogin(account)
}
},
error => {
if (error == "") {
error = "Something failed, but we did not get a message"
}
this.setState({
loading: false,
usernameState: 'error',
passwordState: 'error',
authError: error
})
}
)
}
doSignup() {
Account.create(
this.state.username,
this.state.password,
this.state.invite
).then(
account => {
this.doLogin()
},
error => {
if (error == "") {
error = "Something failed, but we did not get a message"
}
// error messages for invite codes are in english, and
// hardcoded in leap_web in the file invite_code_validator.rb
// all the code error messages have the word 'code' in them.
// the api backend is returning {user: {code: "error message"}}
// but that structure is lost by the time it reaches us.
if (/code/.test(error)) {
this.setState({
loading: false,
inviteState: 'error',
authError: error
})
} else {
this.setState({
loading: false,
usernameState: 'error',
passwordState: 'error',
authError: error
})
}
}
)
}
}
export default Login
|