forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
102 lines (85 loc) · 2.59 KB
/
login.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
// This is the main login engine.
package engine
import (
"errors"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
)
var errNoConfig = errors.New("No user config available")
var errNoDevice = errors.New("No device provisioned locally for this user")
// Login is an engine.
type Login struct {
libkb.Contextified
deviceType string
username string
clientType keybase1.ClientType
}
// NewLogin creates a Login engine. username is optional.
// deviceType should be libkb.DeviceTypeDesktop or
// libkb.DeviceTypeMobile.
func NewLogin(g *libkb.GlobalContext, deviceType string, username string, ct keybase1.ClientType) *Login {
return &Login{
Contextified: libkb.NewContextified(g),
deviceType: deviceType,
username: username,
clientType: ct,
}
}
// Name is the unique engine name.
func (e *Login) Name() string {
return "Login"
}
// GetPrereqs returns the engine prereqs.
func (e *Login) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *Login) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *Login) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{
&LoginCurrentDevice{},
&LoginProvision{},
}
}
// Run starts the engine.
func (e *Login) Run(ctx *Context) error {
// first see if this device is already provisioned and it is possible to log in:
eng := NewLoginCurrentDevice(e.G(), e.username)
err := RunEngine(eng, ctx)
if err == nil {
// login successful
e.G().Log.Debug("LoginCurrentDevice.Run() was successful")
return nil
}
// if this device has been provisioned already and there was an error, then
// return that error. Otherwise, ignore it and keep going.
if !e.notProvisionedErr(err) {
return err
}
e.G().Log.Debug("LoginCurrentDevice error: %s (continuing with device provisioning...)", err)
// this device needs to be provisioned:
darg := &LoginProvisionArg{
DeviceType: e.deviceType,
Username: e.username,
ClientType: e.clientType,
}
deng := NewLoginProvision(e.G(), darg)
return RunEngine(deng, ctx)
}
// notProvisionedErr will return true if err signifies that login
// failed because this device has not yet been provisioned.
func (e *Login) notProvisionedErr(err error) bool {
if err == errNoDevice {
return true
}
if err == errNoConfig {
return true
}
e.G().Log.Debug("notProvisioned, not handling error %s (err type: %T)", err, err)
return false
}