Files
pop/internal/auth/session.go
Michael Freno 25836e27b9 FRE-680: Initial project scaffold with auth & API client
- Set up Go module with Cobra CLI skeleton
- Implemented login/logout/session commands with 2FA support
- Created ProtonMail API client with rate limiting
- Added config management for ~/.config/pop/
- Configured CI/CD pipeline with GitHub Actions
- Added Makefile for build/test/lint targets

Files:
- main.go, go.mod, go.sum
- cmd/root.go, cmd/auth.go
- internal/auth/session.go
- internal/config/config.go
- internal/api/client.go
- Makefile, README.md, .gitignore
- .github/workflows/ci.yml
2026-04-26 09:45:10 -04:00

91 lines
2.1 KiB
Go

package auth
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/frenocorp/pop/internal/config"
)
type Session struct {
UID string `json:"uid"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt int64 `json:"expires_at"`
TwoFAEnabled bool `json:"two_factor_enabled"`
}
type SessionManager struct {
configDir string
sessionFile string
}
func NewSessionManager() *SessionManager {
cfg := config.NewConfigManager()
return &SessionManager{
configDir: cfg.ConfigDir(),
sessionFile: filepath.Join(cfg.ConfigDir(), "session.json"),
}
}
func (m *SessionManager) Login() error {
// TODO: Implement interactive login with 2FA support
// This will call the ProtonMail API and store the session
session := Session{
UID: "placeholder-uid",
AccessToken: "placeholder-token",
RefreshToken: "placeholder-refresh",
ExpiresAt: 0,
TwoFAEnabled: false,
}
if err := os.MkdirAll(m.configDir, 0755); err != nil {
return fmt.Errorf("failed to create config dir: %w", err)
}
data, err := json.MarshalIndent(session, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal session: %w", err)
}
if err := os.WriteFile(m.sessionFile, data, 0600); err != nil {
return fmt.Errorf("failed to write session file: %w", err)
}
fmt.Println("Logged in successfully")
return nil
}
func (m *SessionManager) Logout() error {
if err := os.Remove(m.sessionFile); err != nil {
return fmt.Errorf("failed to remove session file: %w", err)
}
fmt.Println("Logged out successfully")
return nil
}
func (m *SessionManager) GetSession() (*Session, error) {
data, err := os.ReadFile(m.sessionFile)
if err != nil {
return nil, fmt.Errorf("failed to read session file: %w", err)
}
var session Session
if err := json.Unmarshal(data, &session); err != nil {
return nil, fmt.Errorf("failed to parse session: %w", err)
}
return &session, nil
}
func (m *SessionManager) IsAuthenticated() (bool, error) {
_, err := m.GetSession()
if err != nil {
return false, err
}
return true, nil
}