/
signup_join.go
130 lines (115 loc) · 3.82 KB
/
signup_join.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"encoding/hex"
"fmt"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
triplesec "github.com/keybase/go-triplesec"
)
type SignupJoinEngine struct {
uv keybase1.UserVersion
session string
csrf string
username libkb.NormalizedUsername
ppGen libkb.PassphraseGeneration
libkb.Contextified
}
func NewSignupJoinEngine(g *libkb.GlobalContext) *SignupJoinEngine {
return &SignupJoinEngine{Contextified: libkb.NewContextified(g)}
}
func (s *SignupJoinEngine) Init() error {
return nil
}
func (s *SignupJoinEngine) CheckRegistered() (err error) {
s.G().Log.Debug("+ libkb.SignupJoinEngine::CheckRegistered")
if cr := s.G().Env.GetConfig(); cr == nil {
err = fmt.Errorf("No configuration file available")
} else if u := cr.GetUID(); u.Exists() {
err = libkb.AlreadyRegisteredError{UID: u}
}
s.G().Log.Debug("- libkb.SignupJoinEngine::CheckRegistered -> %s", libkb.ErrToOk(err))
return err
}
type SignupJoinEngineRunArg struct {
Username string
Email string
InviteCode string
PWHash []byte
PWSalt []byte
PDPKA5KID keybase1.KID
SkipMail bool
}
func (s *SignupJoinEngine) Post(m libkb.MetaContext, arg SignupJoinEngineRunArg) (err error) {
var res *libkb.APIRes
var ppGenTmp int
res, err = m.G().API.Post(libkb.APIArg{
Endpoint: "signup",
NetContext: m.Ctx(),
Args: libkb.HTTPArgs{
"salt": libkb.S{Val: hex.EncodeToString(arg.PWSalt)},
"pwh": libkb.S{Val: hex.EncodeToString(arg.PWHash)},
"username": libkb.S{Val: arg.Username},
"email": libkb.S{Val: arg.Email},
"invitation_id": libkb.S{Val: arg.InviteCode},
"pwh_version": libkb.I{Val: int(triplesec.Version)},
"skip_mail": libkb.B{Val: arg.SkipMail},
"pdpka5_kid": libkb.S{Val: arg.PDPKA5KID.String()},
"platform": libkb.S{Val: libkb.GetPlatformString()},
}})
if err == nil {
s.username = libkb.NewNormalizedUsername(arg.Username)
libkb.GetUIDVoid(res.Body.AtKey("uid"), &s.uv.Uid, &err)
res.Body.AtKey("session").GetStringVoid(&s.session, &err)
res.Body.AtKey("csrf_token").GetStringVoid(&s.csrf, &err)
res.Body.AtPath("me.basics.passphrase_generation").GetIntVoid(&ppGenTmp, &err)
}
if err == nil {
err = libkb.CheckUIDAgainstUsername(s.uv.Uid, arg.Username)
s.ppGen = libkb.PassphraseGeneration(ppGenTmp)
}
return err
}
type SignupJoinEngineRunRes struct {
PassphraseOk bool
PostOk bool
WriteOk bool
UV keybase1.UserVersion
User *libkb.User
Err error
PpGen libkb.PassphraseGeneration
}
func (r SignupJoinEngineRunRes) Error() string {
return r.Err.Error()
}
func (s *SignupJoinEngine) Run(m libkb.MetaContext, arg SignupJoinEngineRunArg) (res SignupJoinEngineRunRes) {
res.PassphraseOk = true
if res.Err = s.Post(m, arg); res.Err != nil {
return
}
res.PostOk = true
if res.Err = s.WriteOut(m, arg.PWSalt); res.Err != nil {
return
}
res.WriteOk = true
res.UV = s.uv
res.PpGen = s.ppGen
return
}
func (s *SignupJoinEngine) WriteOut(m libkb.MetaContext, salt []byte) error {
lctx := m.LoginContext()
if err := lctx.CreateLoginSessionWithSalt(s.username.String(), salt); err != nil {
return err
}
var nilDeviceID keybase1.DeviceID
if err := lctx.SaveState(s.session, s.csrf, s.username, s.uv, nilDeviceID); err != nil {
return err
}
// Switching to a new user is an operation on the GlobalContext, and will atomically
// update the config file and alter the current ActiveDevice. So farm out to over there.
return m.SwitchUserNewConfig(s.uv.Uid, s.username, salt, nilDeviceID)
}
func (s *SignupJoinEngine) PostInviteRequest(m libkb.MetaContext, arg libkb.InviteRequestArg) error {
return libkb.PostInviteRequest(m, arg)
}