-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathauthentication.go
544 lines (459 loc) · 14.9 KB
/
authentication.go
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
package adapter
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"sync"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/msteinert/pam/v2"
"github.com/ubuntu/authd/internal/brokers/auth"
"github.com/ubuntu/authd/internal/brokers/layouts"
"github.com/ubuntu/authd/internal/proto/authd"
"github.com/ubuntu/authd/log"
pam_proto "github.com/ubuntu/authd/pam/internal/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
// cancellationWait is the time that we are waiting for the cancellation to be
// delivered to the brokers, but also it's used to compute the time we should
// wait for the fully cancellation to have completed once delivered.
cancellationWait = time.Millisecond * 10
)
var (
errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000"))
)
// sendIsAuthenticated sends the authentication secrets or wait request to the brokers.
// The event will contain the returned value from the broker.
func sendIsAuthenticated(ctx context.Context, client authd.PAMClient, sessionID string,
authData *authd.IARequest_AuthenticationData, secret *string) tea.Cmd {
return func() (msg tea.Msg) {
log.Debugf(context.TODO(), "Authentication request for session %q: %#v",
sessionID, authData.Item)
defer func() {
log.Debugf(context.TODO(), "Authentication completed for session %q: %#v",
sessionID, msg)
}()
res, err := client.IsAuthenticated(ctx, &authd.IARequest{
SessionId: sessionID,
AuthenticationData: authData,
})
if err != nil {
if st := status.Convert(err); st.Code() == codes.Canceled {
// Note that this error is only the client-side error, so being here doesn't
// mean the cancellation on broker side is fully completed.
// Wait for the cancellation requests to have been delivered and actually handled.
// The multiplier can be increased to avoid that we return the cancelled event too
// early, but it implies slowing down the UI responses.
<-time.After(cancellationWait * 3)
return isAuthenticatedResultReceived{
access: auth.Cancelled,
secret: secret,
}
}
return pamError{
status: pam.ErrSystem,
msg: fmt.Sprintf("authentication status failure: %v", err),
}
}
return isAuthenticatedResultReceived{
access: res.Access,
msg: res.Msg,
secret: secret,
}
}
}
// isAuthenticatedRequested is the internal events signalling that authentication
// with the given password or wait has been requested.
type isAuthenticatedRequested struct {
item authd.IARequestAuthenticationDataItem
}
// isAuthenticatedRequestedSend is the internal event signaling that the authentication
// request should be sent to the broker.
type isAuthenticatedRequestedSend struct {
isAuthenticatedRequested
ctx context.Context
}
// isAuthenticatedResultReceived is the internal event with the authentication access result
// and data that was retrieved.
type isAuthenticatedResultReceived struct {
access string
secret *string
msg string
}
// isAuthenticatedCancelled is the event to cancel the auth request.
type isAuthenticatedCancelled struct {
msg string
}
// reselectAuthMode signals to restart auth mode selection with the same id (to resend sms or
// reenable the broker).
type reselectAuthMode struct{}
// authenticationComponent is the interface that all sub layout models needs to match.
type authenticationComponent interface {
Init() tea.Cmd
Update(msg tea.Msg) (tea.Model, tea.Cmd)
View() string
Focus() tea.Cmd
Focused() bool
Blur()
}
// authenticationModel is the orchestrator model of all the authentication sub model layouts.
type authenticationModel struct {
client authd.PAMClient
clientType PamClientType
currentModel authenticationComponent
currentSessionID string
currentBrokerID string
currentSecret string
currentLayout string
authTracker *authTracker
encryptionKey *rsa.PublicKey
errorMsg string
}
type authTracker struct {
cancelFunc func()
cond *sync.Cond
}
// startAuthentication signals that the authentication model can start
// wait:true authentication and reset fields.
type startAuthentication struct{}
// errMsgToDisplay signals from an authentication form to display an error message.
type errMsgToDisplay struct {
msg string
}
// newPasswordCheck is sent to request a new password quality check.
type newPasswordCheck struct {
ctx context.Context
password string
}
// newPasswordCheckResult returns the password quality check result.
type newPasswordCheckResult struct {
ctx context.Context
password string
msg string
}
// newAuthenticationModel initializes a authenticationModel which needs to be Compose then.
func newAuthenticationModel(client authd.PAMClient, clientType PamClientType) authenticationModel {
return authenticationModel{
client: client,
clientType: clientType,
authTracker: &authTracker{cond: sync.NewCond(&sync.Mutex{})},
}
}
// Init initializes authenticationModel.
func (m *authenticationModel) Init() tea.Cmd {
return nil
}
func (m *authenticationModel) cancelIsAuthenticated() tea.Cmd {
authTracker := m.authTracker
return func() tea.Msg {
authTracker.cancelAndWait()
return nil
}
}
// Update handles events and actions.
func (m *authenticationModel) Update(msg tea.Msg) (authModel authenticationModel, command tea.Cmd) {
switch msg := msg.(type) {
case reselectAuthMode:
log.Debugf(context.TODO(), "%#v", msg)
return *m, tea.Sequence(m.cancelIsAuthenticated(), sendEvent(AuthModeSelected{}))
case newPasswordCheck:
log.Debugf(context.TODO(), "%T", msg)
currentSecret := m.currentSecret
return *m, func() tea.Msg {
res := newPasswordCheckResult{ctx: msg.ctx, password: msg.password}
if err := checkPasswordQuality(currentSecret, msg.password); err != nil {
res.msg = err.Error()
}
return res
}
case newPasswordCheckResult:
log.Debugf(msg.ctx, "%T: %s", msg, msg.msg)
if m.clientType != Gdm {
// This may be handled by the current model, so don't return early.
break
}
if msg.msg == "" {
return *m, sendEvent(isAuthenticatedRequestedSend{
ctx: msg.ctx,
isAuthenticatedRequested: isAuthenticatedRequested{
item: &authd.IARequest_AuthenticationData_Secret{Secret: msg.password},
},
})
}
errMsg, err := json.Marshal(msg.msg)
if err != nil {
return *m, sendEvent(pamError{
status: pam.ErrSystem,
msg: fmt.Sprintf("could not encode %q error: %v", msg.msg, err),
})
}
return *m, sendEvent(isAuthenticatedResultReceived{
access: auth.Retry,
msg: fmt.Sprintf(`{"message": %s}`, errMsg),
})
case isAuthenticatedRequested:
log.Debugf(context.TODO(), "%#v", msg)
authTracker := m.authTracker
ctx, cancel := context.WithCancel(context.Background())
cancelFunc := func() {
// Very very ugly, but we need to ensure that IsAuthenticated call has been delivered
// to the broker before calling broker's cancelIsAuthenticated or that cancel request may happen
// before than the IsAuthenticated() one has been invoked, and thus we may have nothing
// to cancel in the broker side.
// So let's wait a bit in such case (we may be even too much generous), before delivering
// the actual cancellation.
<-time.After(cancellationWait)
cancel()
}
// At the point that we proceed with the actual authentication request in the goroutine,
// there may still an authentication in progress, so send the request only after
// we've completed the previous one(s).
clientType := m.clientType
currentLayout := m.currentLayout
return *m, func() tea.Msg {
authTracker.waitAndStart(cancelFunc)
secret, hasSecret := msg.item.(*authd.IARequest_AuthenticationData_Secret)
if hasSecret && clientType == Gdm && currentLayout == layouts.NewPassword {
return newPasswordCheck{ctx: ctx, password: secret.Secret}
}
return isAuthenticatedRequestedSend{msg, ctx}
}
case isAuthenticatedRequestedSend:
log.Debugf(context.TODO(), "%#v", msg)
// no password value, pass it as is
plainTextSecret, err := msg.encryptSecretIfPresent(m.encryptionKey)
if err != nil {
return *m, sendEvent(pamError{status: pam.ErrSystem, msg: fmt.Sprintf("could not encrypt password payload: %v", err)})
}
return *m, sendIsAuthenticated(msg.ctx, m.client, m.currentSessionID, &authd.IARequest_AuthenticationData{Item: msg.item}, plainTextSecret)
case isAuthenticatedCancelled:
log.Debugf(context.TODO(), "%#v", msg)
return *m, m.cancelIsAuthenticated()
case isAuthenticatedResultReceived:
log.Debugf(context.TODO(), "%#v", msg)
// Resets password if the authentication wasn't successful.
defer func() {
// the returned authModel is a copy of function-level's `m` at this point!
m := &authModel
if msg.secret != nil &&
(msg.access == auth.Granted || msg.access == auth.Next) {
m.currentSecret = *msg.secret
}
if msg.access != auth.Next && msg.access != auth.Retry {
m.currentModel = nil
}
m.authTracker.reset()
}()
switch msg.access {
case auth.Granted:
infoMsg, err := dataToMsg(msg.msg)
if err != nil {
return *m, sendEvent(pamError{status: pam.ErrSystem, msg: err.Error()})
}
return *m, sendEvent(PamSuccess{BrokerID: m.currentBrokerID, msg: infoMsg})
case auth.Retry:
errorMsg, err := dataToMsg(msg.msg)
if err != nil {
return *m, sendEvent(pamError{status: pam.ErrSystem, msg: err.Error()})
}
m.errorMsg = errorMsg
return *m, sendEvent(startAuthentication{})
case auth.Denied:
errMsg, err := dataToMsg(msg.msg)
if err != nil {
return *m, sendEvent(pamError{status: pam.ErrSystem, msg: err.Error()})
}
if errMsg == "" {
errMsg = "Access denied"
}
return *m, sendEvent(pamError{status: pam.ErrAuth, msg: errMsg})
case auth.Next:
return *m, sendEvent(GetAuthenticationModesRequested{})
case auth.Cancelled:
// nothing to do
return *m, nil
}
case errMsgToDisplay:
m.errorMsg = msg.msg
return *m, nil
}
if m.clientType != InteractiveTerminal {
return *m, nil
}
if _, ok := msg.(startAuthentication); ok {
log.Debugf(context.TODO(), "%T: %#v: current model %v, focused %v",
m, msg, m.currentModel, m.Focused())
}
// interaction events
if !m.Focused() {
return *m, nil
}
var cmd tea.Cmd
var model tea.Model
if m.currentModel != nil {
model, cmd = m.currentModel.Update(msg)
m.currentModel = convertTo[authenticationComponent](model)
}
return *m, cmd
}
// Focus focuses this model.
func (m *authenticationModel) Focus() tea.Cmd {
log.Debugf(context.TODO(), "%T: Focus", m)
if m.currentModel == nil {
return nil
}
return m.currentModel.Focus()
}
// Focused returns if this model is focused.
func (m *authenticationModel) Focused() bool {
if m.currentModel == nil {
return false
}
return m.currentModel.Focused()
}
// Blur releases the focus from this model.
func (m *authenticationModel) Blur() {
log.Debugf(context.TODO(), "%T: Blur", m)
if m.currentModel == nil {
return
}
m.currentModel.Blur()
}
// Compose initialize the authentication model to be used.
// It creates and attaches the sub layout models based on UILayout.
func (m *authenticationModel) Compose(brokerID, sessionID string, encryptionKey *rsa.PublicKey, layout *authd.UILayout) tea.Cmd {
m.currentBrokerID = brokerID
m.currentSessionID = sessionID
m.encryptionKey = encryptionKey
m.currentLayout = layout.Type
m.errorMsg = ""
if m.clientType != InteractiveTerminal {
m.currentModel = &focusTrackerModel{}
return tea.Sequence(sendEvent(ChangeStage{pam_proto.Stage_challenge}),
sendEvent(startAuthentication{}))
}
switch layout.Type {
case layouts.Form:
form := newFormModel(layout.GetLabel(), layout.GetEntry(), layout.GetButton(), layout.GetWait() == layouts.True)
m.currentModel = form
case layouts.QrCode:
qrcodeModel, err := newQRCodeModel(layout.GetContent(), layout.GetCode(),
layout.GetLabel(), layout.GetButton(), layout.GetWait() == layouts.True)
if err != nil {
return sendEvent(pamError{status: pam.ErrSystem, msg: err.Error()})
}
m.currentModel = qrcodeModel
case layouts.NewPassword:
newPasswordModel := newNewPasswordModel(layout.GetLabel(), layout.GetEntry(), layout.GetButton())
m.currentModel = newPasswordModel
default:
return sendEvent(pamError{
status: pam.ErrSystem,
msg: fmt.Sprintf("unknown layout type: %q", layout.Type),
})
}
return tea.Sequence(
m.currentModel.Init(),
sendEvent(ChangeStage{pam_proto.Stage_challenge}),
sendEvent(startAuthentication{}))
}
// View renders a text view of the authentication UI.
func (m authenticationModel) View() string {
if m.currentModel == nil {
return ""
}
if !m.Focused() {
return ""
}
contents := []string{m.currentModel.View()}
errMsg := m.errorMsg
if errMsg != "" {
contents = append(contents, errorStyle.Render(errMsg))
}
return lipgloss.JoinVertical(lipgloss.Left,
contents...,
)
}
// Resets zeroes any internal state on the authenticationModel.
func (m *authenticationModel) Reset() tea.Cmd {
log.Debugf(context.TODO(), "%T: Reset", m)
m.currentModel = nil
m.currentSessionID = ""
m.currentBrokerID = ""
m.currentLayout = ""
return m.cancelIsAuthenticated()
}
// dataToMsg returns the data message from a given JSON message.
func dataToMsg(data string) (string, error) {
if data == "" {
return "", nil
}
v := make(map[string]string)
if err := json.Unmarshal([]byte(data), &v); err != nil {
return "", fmt.Errorf("invalid json data from provider: %v", err)
}
if len(v) == 0 {
return "", nil
}
r, ok := v["message"]
if !ok {
return "", fmt.Errorf("no message entry in json data from provider: %v", v)
}
return r, nil
}
func (authData *isAuthenticatedRequestedSend) encryptSecretIfPresent(publicKey *rsa.PublicKey) (*string, error) {
// no password value, pass it as is
secret, ok := authData.item.(*authd.IARequest_AuthenticationData_Secret)
if !ok {
return nil, nil
}
ciphertext, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, publicKey, []byte(secret.Secret), nil)
if err != nil {
return nil, err
}
// encrypt it to base64 and replace the password with it
base64Encoded := base64.StdEncoding.EncodeToString(ciphertext)
authData.item = &authd.IARequest_AuthenticationData_Secret{Secret: base64Encoded}
return &secret.Secret, nil
}
// wait waits for the current authentication to be completed.
func (at *authTracker) wait() {
at.cond.L.Lock()
defer at.cond.L.Unlock()
for at.cancelFunc != nil {
at.cond.Wait()
}
}
// waitAndStart waits for the current authentication to be completed and
// marks the authentication as in progress.
func (at *authTracker) waitAndStart(cancelFunc func()) {
at.cond.L.Lock()
defer at.cond.L.Unlock()
for at.cancelFunc != nil {
at.cond.Wait()
}
at.cancelFunc = cancelFunc
}
func (at *authTracker) cancelAndWait() {
at.cond.L.Lock()
cancelFunc := at.cancelFunc
at.cond.L.Unlock()
if cancelFunc == nil {
return
}
cancelFunc()
at.wait()
}
func (at *authTracker) reset() {
at.cond.L.Lock()
defer at.cond.L.Unlock()
at.cancelFunc = nil
at.cond.Signal()
}