forked from keybase/client
/
login_session.go
193 lines (168 loc) · 4.13 KB
/
login_session.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"time"
keybase1 "github.com/keybase/client/go/protocol"
)
const LoginSessionMemoryTimeout time.Duration = time.Minute * 5
var ErrLoginSessionNotLoaded = errors.New("LoginSession not loaded")
var ErrLoginSessionCleared = errors.New("LoginSession already cleared")
type LoginSession struct {
sessionFor string // set by constructor
salt []byte // retrieved from server, or set by WithSalt constructor
loginSessionB64 string
loginSession []byte // decoded from above parameter
loaded bool // load state
cleared bool // clear state
createTime time.Time // load time
Contextified
}
func NewLoginSession(emailOrUsername string, g *GlobalContext) *LoginSession {
return &LoginSession{
sessionFor: emailOrUsername,
Contextified: NewContextified(g),
}
}
// Upon signup, a login session is created with a generated salt.
func NewLoginSessionWithSalt(emailOrUsername string, salt []byte, g *GlobalContext) *LoginSession {
ls := NewLoginSession(emailOrUsername, g)
ls.salt = salt
// XXX are these right? is this just so the salt can be retrieved?
ls.loaded = true
ls.cleared = true
return ls
}
func (s *LoginSession) Status() *keybase1.SessionStatus {
return &keybase1.SessionStatus{
SessionFor: s.sessionFor,
Loaded: s.loaded,
Cleared: s.cleared,
Expired: !s.NotExpired(),
SaltOnly: s.loaded && s.loginSession == nil && s.salt != nil,
}
}
func (s *LoginSession) Session() ([]byte, error) {
if s == nil {
return nil, ErrLoginSessionNotLoaded
}
if !s.loaded {
return nil, ErrLoginSessionNotLoaded
}
if s.cleared {
return nil, ErrLoginSessionCleared
}
return s.loginSession, nil
}
func (s *LoginSession) SessionEncoded() (string, error) {
if s == nil {
return "", ErrLoginSessionNotLoaded
}
if !s.loaded {
return "", ErrLoginSessionNotLoaded
}
if s.cleared {
return "", ErrLoginSessionCleared
}
return s.loginSessionB64, nil
}
func (s *LoginSession) ExistsFor(emailOrUsername string) bool {
if s == nil {
return false
}
if s.sessionFor != emailOrUsername {
return false
}
if s.cleared {
return false
}
if s.loginSession == nil {
return false
}
return true
}
func (s *LoginSession) NotExpired() bool {
now := s.G().Clock().Now()
if now.Sub(s.createTime) < LoginSessionMemoryTimeout {
return true
}
s.G().Log.Debug("login_session expired")
return false
}
// Clear removes the loginSession value from s. It does not
// clear the salt. Unclear how this is useful.
func (s *LoginSession) Clear() error {
if s == nil {
return nil
}
if !s.loaded {
return ErrLoginSessionNotLoaded
}
s.loginSession = nil
s.loginSessionB64 = ""
s.cleared = true
return nil
}
func (s *LoginSession) Salt() ([]byte, error) {
if s == nil {
return nil, ErrLoginSessionNotLoaded
}
if !s.loaded {
return nil, ErrLoginSessionNotLoaded
}
return s.salt, nil
}
func (s *LoginSession) Dump() {
if s == nil {
fmt.Printf("LoginSession Dump: nil\n")
return
}
fmt.Printf("sessionFor: %q\n", s.sessionFor)
fmt.Printf("loaded: %v\n", s.loaded)
fmt.Printf("cleared: %v\n", s.cleared)
fmt.Printf("salt: %x\n", s.salt)
fmt.Printf("loginSessionB64: %s\n", s.loginSessionB64)
fmt.Printf("\n")
}
func (s *LoginSession) Load() error {
if s.loaded {
return fmt.Errorf("LoginSession already loaded for %s", s.sessionFor)
}
res, err := s.G().API.Get(APIArg{
Endpoint: "getsalt",
NeedSession: false,
Args: HTTPArgs{
"email_or_username": S{Val: s.sessionFor},
},
})
if err != nil {
return err
}
shex, err := res.Body.AtKey("salt").GetString()
if err != nil {
return err
}
salt, err := hex.DecodeString(shex)
if err != nil {
return err
}
b64, err := res.Body.AtKey("login_session").GetString()
if err != nil {
return err
}
ls, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return err
}
s.salt = salt
s.loginSessionB64 = b64
s.loginSession = ls
s.loaded = true
s.cleared = false
s.createTime = s.G().Clock().Now()
return nil
}