mozcookiejar

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

mozcookiejar

import "github.com/frioux/mozcookiejar"

Overview

mozcookiejar gives access to cookies stored in SQLite by Mozilla products.

The general idea is that you can log into some website via Firefox (or whatever) and then have your CLI tools effectively be logged in as well.

It is up to you to create a cookie jar and to connect to the SQLite database. The example below should make it clear how to safely and correctly do that. I have tested the github.com/mattn/go-sqlite3 SQLite driver, but I am pretty sure any of the various SQLite drivers will work.

This library does not support any of the following Cookie fields:

* MaxAge
* HttpOnly
* Raw
* Unparsed

See documentation for the underlying format here: http://kb.mozillazine.org/Cookies.sqlite

Example

Code:

jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
    fmt.Fprintf(os.Stderr, "Failed to build cookies: %s\n", err)
    os.Exit(1)
}
db, err := sql.Open("sqlite3", os.Getenv("MOZ_COOKIEJAR"))
if err != nil {
    fmt.Fprintf(os.Stderr, "Failed to open db: %s\n", err)
    os.Exit(1)
}
defer db.Close()

err = mozcookiejar.LoadIntoJar(db, jar)
if err != nil {
    fmt.Fprintf(os.Stderr, "Failed to load cookies: %s\n", err)
    os.Exit(1)
}
ua := http.Client{Jar: jar}

resp, err := ua.Get("https://some.authenticated.com/website")
if err != nil {
    fmt.Fprintf(os.Stderr, "Failed to fetch page: %s\n", err)
    os.Exit(1)
}
io.Copy(os.Stdout, resp.Body)

Index

Examples
Package files

cookiejar.go

func LoadIntoJar

func LoadIntoJar(db *sql.DB, jar *cookiejar.Jar) error

LoadIntoJar populates the cookie jar from values in database.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

mozcookiejar gives access to cookies stored in SQLite by Mozilla products.

The general idea is that you can log into some website via Firefox (or whatever) and then have your CLI tools effectively be logged in as well.

It is up to you to create a cookie jar and to connect to the SQLite database. The example below should make it clear how to safely and correctly do that. I have tested the github.com/mattn/go-sqlite3 SQLite driver, but I am pretty sure any of the various SQLite drivers will work.

This library does not support any of the following Cookie fields:

  • MaxAge
  • HttpOnly
  • Raw
  • Unparsed

See documentation for the underlying format here: http://kb.mozillazine.org/Cookies.sqlite

Example
package main

import (
	"database/sql"
	"fmt"
	"io"
	"net/http"
	"net/http/cookiejar"
	"os"

	"github.com/frioux/mozcookiejar"
	_ "github.com/mattn/go-sqlite3"
	"golang.org/x/net/publicsuffix"
)

// sqlite3 required

func main() {
	jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to build cookies: %s\n", err)
		os.Exit(1)
	}
	db, err := sql.Open("sqlite3", os.Getenv("MOZ_COOKIEJAR"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open db: %s\n", err)
		os.Exit(1)
	}
	defer db.Close()

	err = mozcookiejar.LoadIntoJar(db, jar)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load cookies: %s\n", err)
		os.Exit(1)
	}
	ua := http.Client{Jar: jar}

	resp, err := ua.Get("https://some.authenticated.com/website")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to fetch page: %s\n", err)
		os.Exit(1)
	}
	io.Copy(os.Stdout, resp.Body)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadIntoJar

func LoadIntoJar(db *sql.DB, jar *cookiejar.Jar) error

LoadIntoJar populates the cookie jar from values in database.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL