Skip to content

Commit

Permalink
move srp related code to auth package
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed Jun 24, 2024
1 parent 22e34d6 commit e8f79f3
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 188 deletions.
7 changes: 7 additions & 0 deletions android/app/src/main/kotlin/io/lantern/model/SessionModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ class SessionModel(
} else {
throw IllegalArgumentException("No URL provided for webview")
}
}

"login" -> {
Internalsdk.login(call.argument("email")!!, url = call.argument("password")!!)
}

"signOut" -> {
Internalsdk.logOut()
}

"trackUserAction" -> {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ require (
github.com/getlantern/eventual v1.0.0
github.com/getlantern/eventual/v2 v2.0.2
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c
github.com/getlantern/flashlight/v7 v7.6.87
github.com/getlantern/flashlight/v7 v7.6.90
github.com/getlantern/fronted v0.0.0-20230601004823-7fec719639d8
github.com/getlantern/golog v0.0.0-20230503153817-8e72de7e0a65
github.com/getlantern/i18n v0.0.0-20181205222232-2afc4f49bb1c
github.com/getlantern/idletiming v0.0.0-20231030193830-6767b09f86db
github.com/getlantern/ipproxy v0.0.0-20240305190756-6b5b6347158b
github.com/getlantern/jibber_jabber v0.0.0-20210901195950-68955124cc42
github.com/getlantern/lantern-client v0.0.0-20240529061800-bef437cdab3e
github.com/getlantern/launcher v0.0.0-20230622120034-fe87f9bff286
github.com/getlantern/memhelper v0.0.0-20220104170102-df557102babd
github.com/getlantern/mtime v0.0.0-20200417132445-23682092d1f7
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ github.com/getlantern/fdcount v0.0.0-20210503151800-5decd65b3731/go.mod h1:XZwE+
github.com/getlantern/filepersist v0.0.0-20160317154340-c5f0cd24e799/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c h1:mcz27xtAkb1OuOLBct/uFfL1p3XxAIcFct82GbT+UZM=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/flashlight v0.0.0-20230403092335-5f84ae10c585 h1:lVu4HyDAxXkX4kJEJQWpJJz+Poprb0crUGW5zhoh3GU=
github.com/getlantern/flashlight/v7 v7.6.87 h1:IRwthjTQQS2xcLHHKaIsloKKxg11DODPSiIa9bm88HU=
github.com/getlantern/flashlight/v7 v7.6.87/go.mod h1:A+/KlrUCdU+nO7S4yOtsmfcNb7xec4nYk6uXRdwFnew=
github.com/getlantern/flashlight/v7 v7.6.90 h1:6ji9BMyR5WTYY1IsbFULtCvEcDagnDn8BiNvAHatD+o=
github.com/getlantern/flashlight/v7 v7.6.90/go.mod h1:A+/KlrUCdU+nO7S4yOtsmfcNb7xec4nYk6uXRdwFnew=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede h1:yrU6Px3ZkvCsDLPryPGi6FN+2iqFPq+JeCb7EFoDBhw=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede/go.mod h1:nhnoiS6DE6zfe+BaCMU4YI01UpsuiXnDqM5S8jxHuuI=
github.com/getlantern/fronted v0.0.0-20230601004823-7fec719639d8 h1:r/Z/SPPIfLXDI3QA7/tE6nOfPncrqeUPDjiFjnNGP50=
Expand Down
42 changes: 33 additions & 9 deletions internalsdk/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"sync"
Expand All @@ -25,9 +26,12 @@ import (
"github.com/getlantern/flashlight/v7/geolookup"
"github.com/getlantern/flashlight/v7/logging"
"github.com/getlantern/flashlight/v7/ops"
"github.com/getlantern/flashlight/v7/proxied"
"github.com/getlantern/golog"
"github.com/getlantern/lantern-client/internalsdk/analytics"
"github.com/getlantern/lantern-client/internalsdk/auth"
"github.com/getlantern/lantern-client/internalsdk/common"
"github.com/getlantern/lantern-client/internalsdk/webclient"
"github.com/getlantern/mtime"

// import gomobile just to make sure it stays in go.mod
Expand All @@ -49,6 +53,14 @@ var (
dnsGrabEventual = eventual.NewValue()
dnsGrabAddrEventual = eventual.NewValue()
errNoAdProviderAvailable = errors.New("no ad provider available")
dialTimeout = 30 * time.Second
authClient = auth.NewClient(fmt.Sprintf("https://%s", common.V1BaseUrl), &webclient.Opts{
HttpClient: &http.Client{
Transport: proxied.Fronted(dialTimeout),
Timeout: dialTimeout,
},
})
userConfig *UserConfig
)

type Settings interface {
Expand Down Expand Up @@ -81,7 +93,7 @@ type Session interface {
GetDNSServer() (string, error)
Provider() (string, error)
IsStoreVersion() (bool, error)
Email() (string, error)
GetEmail() (string, error)
Currency() (string, error)
DeviceOS() (string, error)
IsProUser() (bool, error)
Expand Down Expand Up @@ -118,11 +130,11 @@ type PanickingSession interface {
GetTimeZone() string
Code() string
GetCountryCode() string
GetEmail() string
GetForcedCountryCode() string
GetDNSServer() string
Provider() string
IsStoreVersion() bool
Email() string
Currency() string
DeviceOS() string
IsProUser() bool
Expand Down Expand Up @@ -158,6 +170,12 @@ func (s *panickingSessionImpl) GetAppName() string {
return s.wrapped.GetAppName()
}

func (s *panickingSessionImpl) GetEmail() string {
result, err := s.wrapped.GetEmail()
panicIfNecessary(err)
return result
}

func (s *panickingSessionImpl) GetDeviceID() string {
result, err := s.wrapped.GetDeviceID()
panicIfNecessary(err)
Expand Down Expand Up @@ -254,12 +272,6 @@ func (s *panickingSessionImpl) IsStoreVersion() bool {
return result
}

func (s *panickingSessionImpl) Email() string {
result, err := s.wrapped.Email()
panicIfNecessary(err)
return result
}

func (s *panickingSessionImpl) Currency() string {
result, err := s.wrapped.Currency()
panicIfNecessary(err)
Expand Down Expand Up @@ -310,6 +322,7 @@ type UserConfig struct {

func (uc *UserConfig) GetAppName() string { return common.DefaultAppName }
func (uc *UserConfig) GetDeviceID() string { return uc.session.GetDeviceID() }
func (uc *UserConfig) GetEmail() string { return uc.session.GetEmail() }
func (uc *UserConfig) GetUserID() int64 { return uc.session.GetUserID() }
func (uc *UserConfig) GetToken() string { return uc.session.GetToken() }
func (uc *UserConfig) GetEnabledExperiments() []string { return nil }
Expand Down Expand Up @@ -513,6 +526,16 @@ func ReverseDns(grabber dnsgrab.Server) func(string) (string, error) {
}
}

func logIn(email, password string) {
log.Debug("Received sign in request")
authClient.Login(userConfig, email, password)
}

func logOut() {
log.Debug("Received sign out request")
authClient.SignOut(context.Background(), userConfig)
}

func run(configDir, locale string, settings Settings, session PanickingSession) {

appdir.SetHomeDir(configDir)
Expand Down Expand Up @@ -554,7 +577,8 @@ func run(configDir, locale string, settings Settings, session PanickingSession)
config.ForceCountry(forcedCountryCode)
}

userConfig := NewUserConfig(session)
userConfig = NewUserConfig(session)

globalConfigChanged := make(chan interface{})
geoRefreshed := geolookup.OnRefresh()

Expand Down
32 changes: 20 additions & 12 deletions internalsdk/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ type authClient struct {

type AuthClient interface {
//Sign up methods
SignUp(ctx context.Context, signupData *protos.SignupRequest) (bool, error)
SignUp(email string, password string) ([]byte, error)
SignupEmailResendCode(ctx context.Context, data *protos.SignupEmailResendRequest) (bool, error)
SignupEmailConfirmation(ctx context.Context, data *protos.ConfirmSignupRequest) (bool, error)

//Login methods
GetSalt(ctx context.Context, email string) (*protos.GetSaltResponse, error)
LoginPrepare(ctx context.Context, loginData *protos.PrepareRequest) (*protos.PrepareResponse, error)
Login(ctx context.Context, loginData *protos.LoginRequest) (*protos.LoginResponse, error)
Login(uc common.UserConfig, email string, password string) (*protos.LoginResponse, []byte, error)
// Recovery methods
StartRecoveryByEmail(ctx context.Context, loginData *protos.StartRecoveryByEmailRequest) (bool, error)
CompleteRecoveryByEmail(ctx context.Context, loginData *protos.CompleteRecoveryByEmailRequest) (bool, error)
Expand All @@ -42,7 +42,7 @@ type AuthClient interface {
DeleteAccount(ctc context.Context, loginData *protos.DeleteUserRequest) (bool, error)

//Logout
SignOut(ctx context.Context, logoutData *protos.LogoutRequest) (bool, error)
SignOut(ctx context.Context, uc common.UserConfig) (bool, error)
}

// NewClient creates a new instance of AuthClient
Expand Down Expand Up @@ -80,8 +80,8 @@ func (c *authClient) GetSalt(ctx context.Context, email string) (*protos.GetSalt
}

// Sign up API
// SignUp is used to sign up a new user with the SignupRequest
func (c *authClient) SignUp(ctx context.Context, signupData *protos.SignupRequest) (bool, error) {
// signUp is used to sign up a new user with the SignupRequest
func (c *authClient) signUp(ctx context.Context, signupData *protos.SignupRequest) (bool, error) {
var resp protos.EmptyResponse
err := c.webclient.PostPROTOC(ctx, "/users/signup", nil, signupData, &resp)
if err != nil {
Expand Down Expand Up @@ -124,7 +124,7 @@ func (c *authClient) LoginPrepare(ctx context.Context, loginData *protos.Prepare
}

// Login is used to login a user with the LoginRequest
func (c *authClient) Login(ctx context.Context, loginData *protos.LoginRequest) (*protos.LoginResponse, error) {
func (c *authClient) login(ctx context.Context, loginData *protos.LoginRequest) (*protos.LoginResponse, error) {
var resp protos.LoginResponse
err := c.webclient.PostPROTOC(ctx, "/users/login", nil, loginData, &resp)
if err != nil {
Expand Down Expand Up @@ -201,11 +201,19 @@ func (c *authClient) DeleteAccount(ctx context.Context, accountData *protos.Dele

// DeleteAccount is used to delete the account of a user
// Once account is delete make sure to create new account
func (c *authClient) SignOut(ctx context.Context, logoutData *protos.LogoutRequest) (bool, error) {
var resp protos.EmptyResponse
err := c.webclient.PostPROTOC(ctx, "/users/logout", nil, logoutData, &resp)
if err != nil {
return false, err
func (c *authClient) SignOut(ctx context.Context, uc common.UserConfig) (bool, error) {
email := uc.GetEmail()
deviceId := uc.GetDeviceID()
token := uc.GetToken()
userId := uc.GetUserID()
signoutData := &protos.LogoutRequest{
Email: email,
DeviceId: deviceId,
LegacyToken: token,
LegacyUserID: userId,
}
return true, nil
log.Debugf("Sign out request %+v", signoutData)
var resp protos.EmptyResponse
err := c.webclient.PostPROTOC(ctx, "/users/logout", nil, signoutData, &resp)
return err == nil, err
}
3 changes: 3 additions & 0 deletions internalsdk/common/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AuthConfig interface {

type UserConfig interface {
AuthConfig
GetEmail() string
GetLanguage() string
GetTimeZone() (string, error)
GetInternalHeaders() map[string]string
Expand All @@ -26,6 +27,7 @@ type UserConfig interface {
type UserConfigData struct {
AppName string
DeviceID string
Email string
UserID int64
Token string
Language string
Expand All @@ -34,6 +36,7 @@ type UserConfigData struct {

func (uc *UserConfigData) GetAppName() string { return uc.AppName }
func (uc *UserConfigData) GetDeviceID() string { return uc.DeviceID }
func (uc *UserConfigData) GetEmail() string { return uc.Email }
func (uc *UserConfigData) GetUserID() int64 { return uc.UserID }
func (uc *UserConfigData) GetToken() string { return uc.Token }
func (uc *UserConfigData) GetLanguage() string { return uc.Language }
Expand Down
Loading

0 comments on commit e8f79f3

Please sign in to comment.