diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-28 06:16:07 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-28 06:16:07 +0200 |
| commit | 9eacbb42f9a5931bf33d968f775fc806c46b11b0 (patch) | |
| tree | bf9a34182b2db560023ba0deb060146bdbcc0d34 /internal | |
| parent | 4137fae6df4e77198b8ddd9d9647a1ee0c7c49d3 (diff) | |
| download | colabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.tar.gz colabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.zip | |
Init
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/auth/oauth.go | 163 | ||||
| -rw-r--r-- | internal/config/config.go | 79 | ||||
| -rw-r--r-- | internal/config/sessions.go | 126 | ||||
| -rw-r--r-- | internal/drive/drive.go | 116 | ||||
| -rw-r--r-- | internal/notebook/notebook.go | 106 |
5 files changed, 590 insertions, 0 deletions
diff --git a/internal/auth/oauth.go b/internal/auth/oauth.go new file mode 100644 index 0000000..177bbdd --- /dev/null +++ b/internal/auth/oauth.go @@ -0,0 +1,163 @@ +// Package auth handles Google OAuth2 for the CLI using a local loopback server +// to receive the authorization code. +package auth + +import ( + "context" + "crypto/rand" + "encoding/base64" + "errors" + "fmt" + "net" + "net/http" + "os/exec" + "runtime" + "time" + + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" + + "github.com/tetsuo/colabctl/internal/config" +) + +// Scopes required to use the Drive API and the Colab runtime API. +// The colaboratory scope is mandatory; without it the /tun/m/assign +// endpoint returns 403 SCOPE_NOT_PERMITTED. +var scopes = []string{ + "openid", + "https://www.googleapis.com/auth/userinfo.profile", + "https://www.googleapis.com/auth/userinfo.email", + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/colaboratory", + "https://www.googleapis.com/auth/drive", +} + +// oauthConfig builds an oauth2.Config from the supplied client credentials. +func oauthConfig(clientID, clientSecret, redirectURL string) *oauth2.Config { + return &oauth2.Config{ + ClientID: clientID, + ClientSecret: clientSecret, + RedirectURL: redirectURL, + Scopes: scopes, + Endpoint: google.Endpoint, + } +} + +// Login performs an interactive OAuth2 flow. It starts a temporary local HTTP +// server on a free port, opens the browser for the user, waits for the +// authorization code, exchanges it for tokens, and persists them to disk. +func Login(ctx context.Context, clientID, clientSecret string) error { + // Pick a free port on localhost. + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + return fmt.Errorf("start local server: %w", err) + } + port := listener.Addr().(*net.TCPAddr).Port + redirectURL := fmt.Sprintf("http://127.0.0.1:%d/callback", port) + + cfg := oauthConfig(clientID, clientSecret, redirectURL) + + // Generate a random state value to guard against CSRF. + state, err := randomState() + if err != nil { + return err + } + + authURL := cfg.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.ApprovalForce) + + codeCh := make(chan string, 1) + errCh := make(chan error, 1) + + mux := http.NewServeMux() + srv := &http.Server{Handler: mux} + + mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("state") != state { + http.Error(w, "state mismatch", http.StatusBadRequest) + errCh <- errors.New("oauth2 state mismatch; possible CSRF") + return + } + code := r.URL.Query().Get("code") + if code == "" { + msg := r.URL.Query().Get("error") + http.Error(w, "authorization denied", http.StatusBadRequest) + errCh <- fmt.Errorf("authorization denied: %s", msg) + return + } + fmt.Fprintln(w, "<html><body><h2>Authentication successful; you can close this tab.</h2></body></html>") + codeCh <- code + }) + + go func() { + if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) { + errCh <- err + } + }() + + fmt.Printf("Opening browser for authentication.\nIf the browser does not open, visit:\n\n %s\n\n", authURL) + openBrowser(authURL) + + var code string + select { + case code = <-codeCh: + case err = <-errCh: + _ = srv.Shutdown(context.Background()) + return err + case <-time.After(5 * time.Minute): + _ = srv.Shutdown(context.Background()) + return errors.New("timed out waiting for authorization") + } + _ = srv.Shutdown(context.Background()) + + tok, err := cfg.Exchange(ctx, code) + if err != nil { + return fmt.Errorf("exchange code: %w", err) + } + return config.SaveToken(tok) +} + +// TokenSource loads the stored token and wraps it in an auto-refreshing +// TokenSource. Returns an error if no token has been saved (i.e. the user has +// not yet run auth login). +func TokenSource(ctx context.Context, clientID, clientSecret string) (oauth2.TokenSource, error) { + tok, err := config.LoadToken() + if err != nil { + return nil, fmt.Errorf("not authenticated; run \"colabctl auth login\" first: %w", err) + } + cfg := oauthConfig(clientID, clientSecret, "") + return cfg.TokenSource(ctx, tok), nil +} + +// Client returns an *http.Client backed by the stored token. +func Client(ctx context.Context, clientID, clientSecret string) (*http.Client, error) { + ts, err := TokenSource(ctx, clientID, clientSecret) + if err != nil { + return nil, err + } + return oauth2.NewClient(ctx, ts), nil +} + +func randomState() (string, error) { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + return "", fmt.Errorf("generate state: %w", err) + } + return base64.URLEncoding.EncodeToString(b), nil +} + +func openBrowser(url string) { + var cmd string + var args []string + switch runtime.GOOS { + case "darwin": + cmd = "open" + args = []string{url} + case "windows": + cmd = "rundll32" + args = []string{"url.dll,FileProtocolHandler", url} + default: + cmd = "xdg-open" + args = []string{url} + } + _ = exec.Command(cmd, args...).Start() +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..a850386 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,79 @@ +package config + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" + + "golang.org/x/oauth2" +) + +// Dir returns the directory used to store all persistent state. +func Dir() (string, error) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + return filepath.Join(home, ".config", "colab"), nil +} + +// TokenPath returns the path to the stored OAuth2 token file. +func TokenPath() (string, error) { + dir, err := Dir() + if err != nil { + return "", err + } + return filepath.Join(dir, "token.json"), nil +} + +// LoadToken reads the stored token from disk. Returns an error wrapping +// os.ErrNotExist when no token has been saved yet. +func LoadToken() (*oauth2.Token, error) { + path, err := TokenPath() + if err != nil { + return nil, err + } + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + + var tok oauth2.Token + if err := json.NewDecoder(f).Decode(&tok); err != nil { + return nil, err + } + return &tok, nil +} + +// SaveToken writes the token to disk, creating parent directories as needed. +func SaveToken(tok *oauth2.Token) error { + dir, err := Dir() + if err != nil { + return err + } + if err := os.MkdirAll(dir, 0700); err != nil { + return err + } + path := filepath.Join(dir, "token.json") + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer f.Close() + return json.NewEncoder(f).Encode(tok) +} + +// DeleteToken removes the stored token, effectively logging out. +func DeleteToken() error { + path, err := TokenPath() + if err != nil { + return err + } + err = os.Remove(path) + if errors.Is(err, os.ErrNotExist) { + return nil + } + return err +} diff --git a/internal/config/sessions.go b/internal/config/sessions.go new file mode 100644 index 0000000..cbc9ffa --- /dev/null +++ b/internal/config/sessions.go @@ -0,0 +1,126 @@ +package config + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" + "time" +) + +// SessionState is the on-disk record for one active Colab runtime. +type SessionState struct { + Endpoint string `json:"endpoint"` + URL string `json:"url"` + Token string `json:"token"` + TokenExpiresAt time.Time `json:"tokenExpiresAt"` + KernelID string `json:"kernelId,omitempty"` +} + +// Valid returns true if the session's proxy token has not yet expired. +func (s *SessionState) Valid() bool { + return s.Token != "" && time.Now().Before(s.TokenExpiresAt) +} + +type sessionFile struct { + Sessions map[string]*SessionState `json:"sessions"` +} + +func sessionsPath() (string, error) { + dir, err := Dir() + if err != nil { + return "", err + } + return filepath.Join(dir, "sessions.json"), nil +} + +func loadSessionFile() (*sessionFile, error) { + path, err := sessionsPath() + if err != nil { + return nil, err + } + f, err := os.Open(path) + if errors.Is(err, os.ErrNotExist) { + return &sessionFile{Sessions: map[string]*SessionState{}}, nil + } + if err != nil { + return nil, err + } + defer f.Close() + + var sf sessionFile + if err := json.NewDecoder(f).Decode(&sf); err != nil { + return &sessionFile{Sessions: map[string]*SessionState{}}, nil + } + if sf.Sessions == nil { + sf.Sessions = map[string]*SessionState{} + } + return &sf, nil +} + +func saveSessionFile(sf *sessionFile) error { + dir, err := Dir() + if err != nil { + return err + } + if err := os.MkdirAll(dir, 0700); err != nil { + return err + } + path, err := sessionsPath() + if err != nil { + return err + } + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer f.Close() + return json.NewEncoder(f).Encode(sf) +} + +// SaveSession persists a session under the given endpoint key. +func SaveSession(s *SessionState) error { + sf, err := loadSessionFile() + if err != nil { + return err + } + sf.Sessions[s.Endpoint] = s + return saveSessionFile(sf) +} + +// LoadSession returns the saved session for the given endpoint, or nil if not +// found or the token has expired. +func LoadSession(endpoint string) (*SessionState, error) { + sf, err := loadSessionFile() + if err != nil { + return nil, err + } + s, ok := sf.Sessions[endpoint] + if !ok { + return nil, nil + } + return s, nil +} + +// ListSessions returns all saved sessions regardless of token validity. +func ListSessions() ([]*SessionState, error) { + sf, err := loadSessionFile() + if err != nil { + return nil, err + } + out := make([]*SessionState, 0, len(sf.Sessions)) + for _, s := range sf.Sessions { + out = append(out, s) + } + return out, nil +} + +// DeleteSession removes a saved session by endpoint. +func DeleteSession(endpoint string) error { + sf, err := loadSessionFile() + if err != nil { + return err + } + delete(sf.Sessions, endpoint) + return saveSessionFile(sf) +} diff --git a/internal/drive/drive.go b/internal/drive/drive.go new file mode 100644 index 0000000..677880b --- /dev/null +++ b/internal/drive/drive.go @@ -0,0 +1,116 @@ +// Package drive provides a thin wrapper around the Google Drive API v3 for +// operations relevant to Colab notebooks. +package drive + +import ( + "context" + "fmt" + "net/http" + + gdrive "google.golang.org/api/drive/v3" + "google.golang.org/api/googleapi" + "google.golang.org/api/option" +) + +const colabMIMEType = "application/vnd.google.colaboratory" + +// Notebook is a minimal representation of a Colab notebook file in Drive. +type Notebook struct { + ID string + Name string + Modified string // RFC 3339 string + URL string +} + +// Client wraps the Drive API service. +type Client struct { + svc *gdrive.Service +} + +// New creates a Drive client using the provided authenticated HTTP client. +func New(ctx context.Context, httpClient *http.Client) (*Client, error) { + svc, err := gdrive.NewService(ctx, option.WithHTTPClient(httpClient)) + if err != nil { + return nil, fmt.Errorf("create drive service: %w", err) + } + return &Client{svc: svc}, nil +} + +// ListNotebooks returns all Colab notebooks (.ipynb files with the Colab MIME +// type) that are owned by or shared with the authenticated user. +func (c *Client) ListNotebooks(ctx context.Context) ([]*Notebook, error) { + var notebooks []*Notebook + pageToken := "" + fields := googleapi.Field("nextPageToken, files(id, name, modifiedTime, webViewLink)") + query := fmt.Sprintf("mimeType='%s' and trashed=false", colabMIMEType) + + for { + call := c.svc.Files.List(). + Q(query). + Fields(fields). + OrderBy("modifiedTime desc"). + PageSize(100). + Context(ctx) + if pageToken != "" { + call = call.PageToken(pageToken) + } + + resp, err := call.Do() + if err != nil { + return nil, fmt.Errorf("list drive files: %w", err) + } + for _, f := range resp.Files { + notebooks = append(notebooks, &Notebook{ + ID: f.Id, + Name: f.Name, + Modified: f.ModifiedTime, + URL: f.WebViewLink, + }) + } + if resp.NextPageToken == "" { + break + } + pageToken = resp.NextPageToken + } + return notebooks, nil +} + +// GetNotebook fetches the raw JSON bytes of a notebook from Drive by its file +// ID. The content is the raw .ipynb JSON. +func (c *Client) GetNotebook(ctx context.Context, fileID string) ([]byte, error) { + resp, err := c.svc.Files.Export(fileID, "application/json").Context(ctx).Download() + if err != nil { + // Colab files are not Google Docs, so Export will not work. + // Use the plain media download instead. + resp2, err2 := c.svc.Files.Get(fileID).Context(ctx).Download() + if err2 != nil { + return nil, fmt.Errorf("download notebook (file %s): %w", fileID, err) + } + defer resp2.Body.Close() + buf := make([]byte, 0, 512*1024) + tmp := make([]byte, 32*1024) + for { + n, readErr := resp2.Body.Read(tmp) + if n > 0 { + buf = append(buf, tmp[:n]...) + } + if readErr != nil { + break + } + } + return buf, nil + } + defer resp.Body.Close() + buf := make([]byte, 0, 512*1024) + tmp := make([]byte, 32*1024) + for { + n, readErr := resp.Body.Read(tmp) + if n > 0 { + buf = append(buf, tmp[:n]...) + } + if readErr != nil { + break + } + } + return buf, nil +} diff --git a/internal/notebook/notebook.go b/internal/notebook/notebook.go new file mode 100644 index 0000000..9881154 --- /dev/null +++ b/internal/notebook/notebook.go @@ -0,0 +1,106 @@ +// Package notebook parses and represents Jupyter/Colab .ipynb files. +package notebook + +import ( + "encoding/json" + "fmt" +) + +// Cell types used in notebooks. +const ( + CellTypeCode = "code" + CellTypeMarkdown = "markdown" + CellTypeRaw = "raw" +) + +// Source is a notebook cell's source, which can be stored as a single string +// or a list of strings (lines) in the JSON format. +type Source []string + +func (s *Source) UnmarshalJSON(data []byte) error { + // Try string first. + var str string + if err := json.Unmarshal(data, &str); err == nil { + *s = Source{str} + return nil + } + // Fall back to slice. + var lines []string + if err := json.Unmarshal(data, &lines); err != nil { + return err + } + *s = Source(lines) + return nil +} + +// String joins all source lines into one string. +func (s Source) String() string { + out := "" + for _, l := range s { + out += l + } + return out +} + +// Output represents one output entry produced by a cell execution. +type Output struct { + OutputType string `json:"output_type"` + Text Source `json:"text"` + Data json.RawMessage `json:"data"` + EValue Source `json:"evalue"` + Traceback []string `json:"traceback"` +} + +// Cell is a single cell in the notebook. +type Cell struct { + CellType string `json:"cell_type"` + ID string `json:"id"` + Source Source `json:"source"` + Outputs []Output `json:"outputs"` + Metadata struct { + ExecutionCount *int `json:"execution_count"` + } `json:"metadata"` +} + +// Notebook is the top-level structure of a .ipynb file. +type Notebook struct { + NBFormat int `json:"nbformat"` + NBFormatMinor int `json:"nbformat_minor"` + Metadata struct { + KernelInfo struct { + Name string `json:"name"` + } `json:"kernelspec"` + LanguageInfo struct { + Name string `json:"name"` + } `json:"language_info"` + CoLab struct { + Name string `json:"name"` + } `json:"colab"` + } `json:"metadata"` + Cells []*Cell `json:"cells"` +} + +// Parse decodes a .ipynb JSON payload into a Notebook. +func Parse(data []byte) (*Notebook, error) { + var nb Notebook + if err := json.Unmarshal(data, &nb); err != nil { + return nil, fmt.Errorf("parse notebook: %w", err) + } + return &nb, nil +} + +// CodeCells returns only the code cells in order. +func (nb *Notebook) CodeCells() []*Cell { + var out []*Cell + for _, c := range nb.Cells { + if c.CellType == CellTypeCode { + out = append(out, c) + } + } + return out +} + +// Title returns the notebook name from metadata if available. +func (nb *Notebook) Title() string { + return nb.Metadata.CoLab.Name +} |
