forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_session.go
162 lines (142 loc) · 3.27 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
// 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"
)
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
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
ls.loaded = true
ls.cleared = true
return ls
}
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) 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
return nil
}