Skip to content

Commit a804aaa

Browse files
authored
feat: Add a way to login without standard credentials
Add functions to export/import session
1 parent ccb8d34 commit a804aaa

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

mega.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,23 @@ func (m *Mega) SetClient(client *http.Client) *Mega {
372372
func discardLogf(format string, v ...interface{}) {
373373
}
374374

375+
// Returns an opaque string representing the session
376+
func (m *Mega) GetSessionID() string {
377+
return m.sid
378+
}
379+
380+
func (m *Mega) GetMasterKey() []byte {
381+
return m.k
382+
}
383+
384+
// "Login" using the session ID (for API auth) and master key (for decryption). Alternative to logging in with username/password
385+
// This can be used to import back a session exported with GetSessionID and GetMasterKey without requiring the password again
386+
func (m *Mega) LoginWithKeys(sessionId string, masterKey []byte) error {
387+
m.sid = sessionId
388+
m.k = masterKey
389+
return m.postAuthInit()
390+
}
391+
375392
// SetLogger sets the logger for important messages. By default this
376393
// is log.Printf. Use nil to discard the messages.
377394
func (m *Mega) SetLogger(logf func(format string, v ...interface{})) *Mega {
@@ -665,9 +682,15 @@ func (m *Mega) MultiFactorLogin(email, passwd, multiFactor string) error {
665682
return err
666683
}
667684

685+
return m.postAuthInit()
686+
}
687+
688+
// Finish initializing the Mega client after Login*()
689+
func (m *Mega) postAuthInit() error {
690+
668691
waitEvent := m.WaitEventsStart()
669692

670-
err = m.getFileSystem()
693+
err := m.getFileSystem()
671694
if err != nil {
672695
return err
673696
}

test_tools/test_real_api.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ func main() {
9696
}
9797
}
9898

99+
// Test 5: Re-create client from session
100+
fmt.Println("\n=== Test 5: Re-create client from session ===")
101+
m2 := mega.New()
102+
err = m2.LoginWithKeys(m.GetSessionID(), m.GetMasterKey())
103+
if err != nil {
104+
log.Fatalf("LoginWithKeys failed: %v", err)
105+
}
106+
107+
user, err = m2.GetUser()
108+
if err != nil {
109+
log.Printf("GetUser failed: %v", err)
110+
} else {
111+
fmt.Printf("User info retrieved in %v: Email=%s\n", time.Since(start), user.Email)
112+
}
113+
99114
fmt.Println("\n=== All tests completed ===")
100115
fmt.Println("If you saw any [DEBUG] messages mentioning 'hashcash', the feature was triggered!")
101116
}

0 commit comments

Comments
 (0)