Categorygithub.com/go-sharp/shellicator
modulepackage
0.0.0-20240419152248-690ff77d1a84
Repository: https://github.com/go-sharp/shellicator.git
Documentation: pkg.go.dev

# README

shellicator

A simple library to get oauth2 tokens for shell commands.

This library opens a local http server to receive a callback from an oauth2 provider and stores the received token locally. It prints out the URL of the provider and if configured, opens a browser pointing to the oauth2 authcode URL.

For a working example: Example

Releases

Release 1.0.0

  • Initial release

Release 2.0.0

  • Device Grant Flow added

Minimal Example

Usage:

package main
import (
	"context"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/go-sharp/shellicator"
	"github.com/go-sharp/shellicator/providers"
)

func main() {
    // You can use any oauth2.Config you like. Important use scopes that
    // tells the provider to return a refresh token.
    // WithProvider is the only required option.
	auth := shellicator.NewAuthenticator(shellicator.WithProvider("google",
		providers.NewGoogleProvider(os.Getenv("SHELLICATOR_CLIENT_ID"),
            os.Getenv("SHELLICATOR_CLIENT_SECRET"))))

    // Now you can request a token. Per default this method will print out
    // an URL to the oauth2 provider. You can use options to open a browser
    // automatically. This call blocks until a answer is received (Default Timeout 5min).
    // The authentication is successfully if no error is returned.
    if err := auth.Authenticate("google"); err != nil {
			log.Fatal(err)
    }

    // Now you can get a client that uses the received token
    // and automatically refresh it if necessary.
    client, _ = auth.NewClient(context.Background(), "google")
    // Do something with the client

    // Or you get the received token with this call.
    token, _ = auth.GetToken("google")
    // Use the token
}

Per default tokens are stored in the volatile MemoryStorage. If persistence is required, use the FileStorage or implement your own Storager.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

NewAuthenticator returns a new Authenticator.
WithCallbackTemplate configures the authenticator to use the specified html for the callback page.
WithPorts configures the Authenticator to use the specified ports.
WithProvider configures an oauth provider with the specified key.
WithStore configures the authenticator to use the provided storager to save and restore tokens.
WithTimeout configures the timeout for the Authenticator.
WithUseDeviceGrant configures the authenticator to use the device grant flow.
WithUseOpenBrowserFeature configures if a browser should automatically openend and navigate to the oauth AuthCodeURL.
WithUsePrinter configures to use the supplied function to print out the oauth AuthCodeURL.

# Structs

Authenticator handles oauth tokens for a command line application.
FileStorage saves and retrieves token from the file system.
PrinterCtx passed to the printer function.

# Interfaces

Provider is an interface that holds the configuration for the shellicator.
Storager persists tokens for later use.

# Type aliases

AuthOptions sets options for the Authenticator.
MemoryStorage implements an in-memory Storager.