Skip to content

Commit 2702dfd

Browse files
committed
Repari register new user command
Signed-off-by: keliramu <[email protected]>
1 parent 9449530 commit 2702dfd

30 files changed

+386
-876
lines changed

ci/generate_protobuf.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf
1313
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/logout.proto -I protobuf/daemon
1414
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/login_with_token.proto -I protobuf/daemon
1515
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/ping.proto -I protobuf/daemon
16-
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/plans.proto -I protobuf/daemon
1716
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/rate.proto -I protobuf/daemon
18-
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/register.proto -I protobuf/daemon
1917
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/set.proto -I protobuf/daemon
2018
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/settings.proto -I protobuf/daemon
2119
protoc --go_opt=module=github.com/NordSecurity/nordvpn-linux --go_out=. protobuf/daemon/status.proto -I protobuf/daemon

cli/cli_click.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/url"
99
"os"
10+
"strings"
1011
"time"
1112

1213
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
@@ -54,8 +55,15 @@ func (c *cmd) Click(ctx *cli.Context) (err error) {
5455
return formatError(err)
5556
}
5657

57-
if url.Scheme == "nordvpn" {
58-
if url.Host == "claim-online-purchase" {
58+
//TODO/FIXME: remove after testing
59+
fmt.Println("~~~URL")
60+
fmt.Printf("%#v\n", url)
61+
fmt.Println("~~~URL <<<<")
62+
63+
if strings.ToLower(url.Scheme) == "nordvpn" {
64+
65+
switch strings.ToLower(url.Host) {
66+
case "claim-online-purchase":
5967
resp, err := c.client.ClaimOnlinePurchase(context.Background(), &pb.Empty{})
6068
if err != nil {
6169
return formatError(err)
@@ -67,17 +75,26 @@ func (c *cmd) Click(ctx *cli.Context) (err error) {
6775

6876
color.Green(ClaimOnlinePurchaseSuccess)
6977
return nil
70-
}
7178

72-
// if arg is given
73-
// run the same as: login --callback %arg
74-
if err := c.oauth2(ctx); err != nil {
75-
return formatError(err)
79+
case "login":
80+
// login can be regular, or after new account setup & vpn service purchase (signup)
81+
regularLogin := true
82+
if strings.ToLower(url.Query().Get("action")) == "signup" {
83+
regularLogin = false
84+
}
85+
86+
// if arg is given
87+
// run the same as: login --callback %arg
88+
if err := c.oauth2(ctx, regularLogin); err != nil {
89+
return formatError(err)
90+
}
91+
return nil
7692
}
7793
}
78-
} else {
79-
cli.ShowAppHelp(ctx)
8094
}
8195

96+
// for all unhandled cases
97+
cli.ShowAppHelp(ctx)
98+
8299
return nil
83100
}

cli/cli_login.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (c *cmd) Login(ctx *cli.Context) error {
3030
}
3131

3232
if ctx.IsSet(flagLoginCallback) {
33-
return c.oauth2(ctx)
33+
return c.oauth2(ctx, true)
3434
}
3535

3636
if ctx.IsSet(flagToken) {
@@ -44,7 +44,9 @@ func (c *cmd) Login(ctx *cli.Context) error {
4444

4545
cl, err := c.client.LoginOAuth2(
4646
context.Background(),
47-
&pb.Empty{},
47+
&pb.LoginOAuth2Request{
48+
Type: pb.LoginType_LoginType_LOGIN,
49+
},
4850
)
4951
if err != nil {
5052
return formatError(err)
@@ -102,7 +104,7 @@ func LoginRespHandler(ctx *cli.Context, resp *pb.LoginResponse) error {
102104
}
103105

104106
// oauth2 is called by the browser during login via OAuth2.
105-
func (c *cmd) oauth2(ctx *cli.Context) error {
107+
func (c *cmd) oauth2(ctx *cli.Context, regularLogin bool) error {
106108
if ctx.NArg() != 1 {
107109
return formatError(errors.New("expected a url"))
108110
}
@@ -116,8 +118,14 @@ func (c *cmd) oauth2(ctx *cli.Context) error {
116118
return formatError(errors.New("expected a url with nordvpn scheme"))
117119
}
118120

119-
_, err = c.client.LoginOAuth2Callback(context.Background(), &pb.String{
120-
Data: url.Query().Get("exchange_token"),
121+
loginType := pb.LoginType_LoginType_LOGIN
122+
if !regularLogin {
123+
loginType = pb.LoginType_LoginType_SIGNUP
124+
}
125+
126+
_, err = c.client.LoginOAuth2Callback(context.Background(), &pb.LoginOAuth2CallbackRequest{
127+
Token: url.Query().Get("exchange_token"),
128+
Type: loginType,
121129
})
122130
if err != nil {
123131
return formatError(err)

cli/cli_register.go

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package cli
22

33
import (
44
"context"
5-
"errors"
6-
"fmt"
7-
"sort"
5+
"io"
86

9-
"github.com/NordSecurity/nordvpn-linux/client"
107
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
11-
"github.com/NordSecurity/nordvpn-linux/internal"
128

139
"github.com/fatih/color"
1410
"github.com/urfave/cli/v2"
@@ -18,57 +14,28 @@ import (
1814
const RegisterUsageText = "Registers a new user account"
1915

2016
func (c *cmd) Register(ctx *cli.Context) error {
21-
email, password, err := ReadCredentialsFromTerminal()
17+
cl, err := c.client.LoginOAuth2(
18+
context.Background(),
19+
&pb.LoginOAuth2Request{
20+
Type: pb.LoginType_LoginType_SIGNUP,
21+
},
22+
)
2223
if err != nil {
2324
return formatError(err)
2425
}
2526

26-
resp, err := c.client.Register(context.Background(), &pb.RegisterRequest{
27-
Email: email,
28-
Password: password,
29-
})
30-
if err != nil {
31-
return formatError(err)
32-
}
33-
34-
switch resp.Type {
35-
case internal.CodeSuccess:
36-
color.Green(AccountCreationSuccess)
37-
case internal.CodeBadRequest:
38-
err = errors.New(AccountInvalidData)
39-
case internal.CodeConflict:
40-
err = errors.New(AccountEmailTaken)
41-
case internal.CodeInternalError:
42-
err = errors.New(AccountInternalError)
43-
case internal.CodeFailure:
44-
err = internal.ErrUnhandled
45-
}
46-
47-
if err != nil {
48-
return formatError(err)
49-
}
50-
51-
planResp, err := c.client.Plans(context.Background(), &pb.Empty{})
52-
if err != nil {
53-
color.Red("Failed to retrieve subscription plans. Please finish the registration in NordVPN website.")
54-
return browse(client.SubscriptionURL)
55-
}
56-
57-
plans := planResp.GetPlans()
58-
// sort plans by cost in ascending order
59-
sort.Slice(plans, func(i, j int) bool {
60-
costI := plans[i].GetCost()
61-
costJ := plans[j].GetCost()
62-
// this is done to avoid string -> int conversions
63-
if len(costI) == len(costJ) {
64-
return costI < costJ
27+
for {
28+
resp, err := cl.Recv()
29+
if err != nil {
30+
if err == io.EOF {
31+
break
32+
}
33+
return formatError(err)
34+
}
35+
if url := resp.GetData(); url != "" {
36+
color.Green("Continue in the browser: %s", url)
6537
}
66-
return len(costI) < len(costJ)
67-
})
68-
for i, plan := range plans {
69-
description := fmt.Sprintf("%s for %s %s", plan.GetTitle(), plan.GetCost(), plan.GetCurrency())
70-
fmt.Printf("%d) %s\n", i+1, description)
7138
}
7239

73-
return browse(client.SubscriptionURL)
40+
return nil
7441
}

core/authentication.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// Authentication is responsible for verifying user's identity.
1515
type Authentication interface {
16-
Login() (string, error)
16+
Login(bool) (string, error)
1717
Token(string) (*LoginResponse, error)
1818
}
1919

@@ -36,7 +36,7 @@ func NewOAuth2(client *http.Client, baseURL string) *OAuth2 {
3636
}
3737
}
3838

39-
func (o *OAuth2) Login() (string, error) {
39+
func (o *OAuth2) Login(regularLogin bool) (string, error) {
4040
o.Lock()
4141
defer o.Unlock()
4242

@@ -52,8 +52,13 @@ func (o *OAuth2) Login() (string, error) {
5252

5353
query := url.Values{}
5454
query.Add("challenge", o.challenge)
55-
query.Add("preferred_flow", "login")
56-
query.Add("redirect_flow", "default")
55+
if regularLogin {
56+
query.Add("preferred_flow", "login")
57+
query.Add("redirect_flow", "default")
58+
} else {
59+
query.Add("preferred_flow", "registration")
60+
query.Add("redirect_flow", "checkout")
61+
}
5762
path.RawQuery = query.Encode()
5863
log.Println("oauth2 login url", path.String())
5964

core/authentication_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func TestOAuth2_Login(t *testing.T) {
5757
defer server.Close()
5858

5959
api := NewOAuth2(http.DefaultClient, server.URL)
60-
url, err := api.Login()
60+
//TODO/FIXME: add signup test cases
61+
url, err := api.Login(true)
6162
assert.Equal(t, test.hasError, err != nil)
6263
if test.hasError {
6364
assert.True(t, strings.Contains(err.Error(), test.name))

0 commit comments

Comments
 (0)