Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add support to handle passkey verification

Neeraj Gupta58b9121e527a
diff --git a/cli/config.yaml.example b/cli/config.yaml.example
index a004036568..e6c605095a 100644
--- a/cli/config.yaml.example
+++ b/cli/config.yaml.example
@@ -5,6 +5,8 @@
endpoint:
api: "http://localhost:8080"
+ # Endpoint for the account service for passkey
+ accounts: "http://localhost:3001"
log:
http: false # log status code & time taken by requests
diff --git a/cli/internal/api/login.go b/cli/internal/api/login.go
index cb6180cc6b..61491f1130 100644
--- a/cli/internal/api/login.go
+++ b/cli/internal/api/login.go
@@ -161,3 +161,22 @@ func (c *Client) VerifyTotp(
}
return &res, nil
}
+
+func (c *Client) CheckPasskeyStatus(ctx context.Context,
+ sessionID string) (*AuthorizationResponse, error) {
+ var res AuthorizationResponse
+ r, err := c.restClient.R().
+ SetContext(ctx).
+ SetResult(&res).
+ Get("/users/two-factor/passkeys/get-token?sessionID=" + sessionID)
+ if err != nil {
+ return nil, err
+ }
+ if r.IsError() {
+ return nil, &ApiError{
+ StatusCode: r.StatusCode(),
+ Message: r.String(),
+ }
+ }
+ return &res, nil
+}
diff --git a/cli/internal/api/login_type.go b/cli/internal/api/login_type.go
index 50149aed7c..951e72fcc1 100644
--- a/cli/internal/api/login_type.go
+++ b/cli/internal/api/login_type.go
@@ -37,6 +37,7 @@ type AuthorizationResponse struct {
EncryptedToken string `json:"encryptedToken,omitempty"`
Token string `json:"token,omitempty"`
TwoFactorSessionID string `json:"twoFactorSessionID"`
+ PassKeySessionID string `json:"passkeySessionID"`
// SrpM2 is sent only if the user is logging via SRP
// SrpM2 is the SRP M2 value aka the proof that the server has the verifier
SrpM2 *string `json:"srpM2,omitempty"`
@@ -45,3 +46,7 @@ type AuthorizationResponse struct {
func (a *AuthorizationResponse) IsMFARequired() bool {
return a.TwoFactorSessionID != ""
}
+
+func (a *AuthorizationResponse) IsPasskeyRequired() bool {
+ return a.PassKeySessionID != ""
+}
diff --git a/cli/internal/promt.go b/cli/internal/promt.go
index fe2f61bde0..14dccc64d0 100644
--- a/cli/internal/promt.go
+++ b/cli/internal/promt.go
@@ -38,6 +38,17 @@ func GetUserInput(label string) (string, error) {
return input, nil
}
+func WaitForEnter(prompt string) error {
+ fmt.Println(prompt)
+ // Create a new reader from standard input.
+ reader := bufio.NewReader(os.Stdin)
+ _, err := reader.ReadString('\n')
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
func GetAppType() api.App {
for {
app, err := GetUserInput("Enter app type (default: photos)")
diff --git a/cli/main.go b/cli/main.go
index ff78081fb8..8f9cc34c5f 100644
--- a/cli/main.go
+++ b/cli/main.go
@@ -89,6 +89,7 @@ func initConfig(cliConfigPath string) {
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.SetDefault("endpoint.api", constants.EnteApiUrl)
+ viper.SetDefault("endpoint.accounts", constants.EnteAccountUrl)
viper.SetDefault("log.http", false)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
diff --git a/cli/pkg/account.go b/cli/pkg/account.go
index e411ffacd5..0e94689e5f 100644
--- a/cli/pkg/account.go
+++ b/cli/pkg/account.go
@@ -58,6 +58,10 @@ func (c *ClICtrl) AddAccount(cxt context.Context) {
if authResponse.IsMFARequired() {
authResponse, flowErr = c.validateTOTP(cxt, authResponse)
}
+
+ if authResponse.IsPasskeyRequired() {
+ authResponse, flowErr = c.verifyPassKey(cxt, authResponse)
+ }
if authResponse.EncryptedToken == "" || authResponse.KeyAttributes == nil {
log.Fatalf("missing key attributes or token.\nNote: Please use the mobile,web or desktop app to create a new account.\nIf you are trying to login to an existing account, report a bug.")
}
diff --git a/cli/pkg/sign_in.go b/cli/pkg/sign_in.go
index 1e8c961188..b931a7fe25 100644
--- a/cli/pkg/sign_in.go
+++ b/cli/pkg/sign_in.go
@@ -7,7 +7,9 @@ import (
"github.com/ente-io/cli/internal/api"
eCrypto "github.com/ente-io/cli/internal/crypto"
"github.com/ente-io/cli/pkg/model"
+ "github.com/ente-io/cli/utils/browser"
"github.com/ente-io/cli/utils/encoding"
+ "github.com/spf13/viper"
"log"
"github.com/kong/go-srp"
@@ -139,6 +141,31 @@ func (c *ClICtrl) validateTOTP(ctx context.Context, authResp *api.AuthorizationR
}
}
+func (c *ClICtrl) verifyPassKey(ctx context.Context, authResp *api.AuthorizationResponse) (*api.AuthorizationResponse, error) {
+ if !authResp.IsPasskeyRequired() {
+ return authResp, nil
+ }
+ baseAccountUrl := viper.GetString("endpoint.accounts")
+ passkeyAuthUrl := fmt.Sprintf("%s/passkeys/verify?passkeySessionID=%s&redirect=ente-cli://passkey&clientPackage=io.ente.photos", baseAccountUrl, authResp.PassKeySessionID)
+ fmt.Printf("Open this url in browser to verify passkey: %s\n", passkeyAuthUrl)
+ err := browser.OpenURL(passkeyAuthUrl)
+ if err != nil {
+ fmt.Printf("Failed to open browser: %v\n", err)
+ }
+ for {
+ err = internal.WaitForEnter("Press enter once you have completed the passkey verification")
+ if err != nil {
+ return nil, err
+ }
+ totpResp, err := c.Client.CheckPasskeyStatus(ctx, authResp.PassKeySessionID)
+ if err != nil {
+ log.Printf("failed to verify %v", err)
+ continue
+ }
+ return totpResp, nil
+ }
+}
+
func (c *ClICtrl) validateEmail(ctx context.Context, email string) (*api.AuthorizationResponse, error) {
err := c.Client.SendEmailOTP(ctx, email)
if err != nil {
diff --git a/cli/utils/constants/constants.go b/cli/utils/constants/constants.go
index e6147ae9a6..ee1110e058 100644
--- a/cli/utils/constants/constants.go
+++ b/cli/utils/constants/constants.go
@@ -1,4 +1,6 @@
package constants
const CliDataPath = "/cli-data/"
+
const EnteApiUrl = "https://api.ente.io"
+const EnteAccountUrl = "https://account.ente.io"