This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
/
signup_join.go
153 lines (137 loc) · 4.42 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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"
"golang.org/x/net/context"
)
type SignupJoinEngine struct {
uid keybase1.UID
session string
csrf string
username libkb.NormalizedUsername
ppGen libkb.PassphraseGeneration
libkb.Contextified
}
func NewSignupJoinEngine(g *libkb.GlobalContext) *SignupJoinEngine {
return &SignupJoinEngine{Contextified: libkb.NewContextified(g)}
}
// XXX why is this here?
func CheckUsernameAvailable(g *libkb.GlobalContext, s string) (err error) {
_, err = g.API.Get(libkb.APIArg{
Endpoint: "user/lookup",
SessionType: libkb.APISessionTypeNONE,
Args: libkb.HTTPArgs{
"username": libkb.S{Val: s},
"fields": libkb.S{Val: "basics"},
},
})
if err == nil {
err = libkb.AppStatusError{
Code: libkb.SCBadSignupUsernameTaken,
Name: "BAD_SIGNUP_USERNAME_TAKEN",
Desc: fmt.Sprintf("Username '%s' is taken", s),
}
} else if ase, ok := err.(libkb.AppStatusError); ok && ase.Name == "NOT_FOUND" {
err = nil
}
return
}
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.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.uid, arg.Username)
s.ppGen = libkb.PassphraseGeneration(ppGenTmp)
}
return err
}
type SignupJoinEngineRunRes struct {
PassphraseOk bool
PostOk bool
WriteOk bool
UID keybase1.UID
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.UID = s.uid
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.uid, 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.uid, s.username, salt, nilDeviceID)
}
func (s *SignupJoinEngine) PostInviteRequest(ctx context.Context, arg libkb.InviteRequestArg) error {
return libkb.PostInviteRequest(ctx, s.G(), arg)
}