Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/ttab/clitools"
)
func main() {
env := "stage"
println("Sample application that demonstrates logging in to elephant from a CLI tool\n")
app, err := clitools.NewConfigurationHandler(
"clitools", clitools.DefaultApplicationID, env,
)
if err != nil {
panic(fmt.Errorf("create configuration handler: %w", err))
}
token, err := app.GetAccessToken(context.Background(), []string{
"doc_read",
})
if err != nil {
panic(fmt.Errorf("authenticate: %w", err))
}
err = app.Save()
if err != nil {
panic(fmt.Errorf("save configuration: %w", err))
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
println("Current token:")
_ = enc.Encode(token)
}
Index ¶
- Constants
- func ConfigureCliCommands(name string, clientID string) *cli.Command
- func ElephantAPIEndpoint(proto, base, name string) string
- func LoadEnv(app string) error
- func UserConfigDir() (string, error)
- type AccessToken
- type ConfigurationHandler
- func (ac *ConfigurationHandler) AddEndpoints(endpoints map[string]string)
- func (ac *ConfigurationHandler) GetAccessToken(ctx context.Context, scopes []string) (_ AccessToken, outErr error)
- func (ac *ConfigurationHandler) GetClientAccessToken(ctx context.Context, clientID string, clientSecret string, scopes []string) (oauth2.TokenSource, error)
- func (ac *ConfigurationHandler) GetEndpoint(name string) (string, bool)
- func (ac *ConfigurationHandler) GetEndpoints() map[string]string
- func (ac *ConfigurationHandler) GetOIDCConfig(ctx context.Context) (*OIDCConfig, error)
- func (ac *ConfigurationHandler) Load() (outErr error)
- func (ac *ConfigurationHandler) Save() error
- func (ac *ConfigurationHandler) SetOIDCConfigURL(ctx context.Context, configURL string) error
- type EnvConfiguration
- type OIDCConfig
- type OIDCEnvironment
Examples ¶
Constants ¶
const DefaultApplicationID = "elephant-cli"
DefaultApplicationID when authorising CLI applications.
Variables ¶
This section is empty.
Functions ¶
func ConfigureCliCommands ¶ added in v0.5.0
func ElephantAPIEndpoint ¶ added in v0.5.0
func LoadEnv ¶ added in v0.3.0
LoadEnv loads any ".env" (override by setting DOT_ENV) in the current path and "[user config dir]/[app]/config.env" if they exist.
This will not override any variables that are set, and the .env file takes precedence over config.env.
func UserConfigDir ¶ added in v0.3.0
UserConfigDir gives preference to XDG_CONFIG_HOME, letting users in any OS specify a linux-style user config location. If XDG_CONFIG_HOME is empty it behaves just like os.UserConfigDir().
Types ¶
type AccessToken ¶
type AccessToken struct {
Token string `json:"token"`
Expires time.Time `json:"expires"`
Scopes []string `json:"scopes"`
GrantedScopes []string `json:"granted_scopes"`
}
AccessToken that can be used to communicate with our APIs.
type ConfigurationHandler ¶
type ConfigurationHandler struct {
// contains filtered or unexported fields
}
func NewConfigurationHandler ¶
func NewConfigurationHandler( name string, clientID string, environment string, ) (*ConfigurationHandler, error)
NewConfigurationHandler crates a configuration handler and loads the current configuration from disk if it's available.
func (*ConfigurationHandler) AddEndpoints ¶ added in v0.5.0
func (ac *ConfigurationHandler) AddEndpoints(endpoints map[string]string)
AddEndpoints for the environment.
func (*ConfigurationHandler) GetAccessToken ¶
func (ac *ConfigurationHandler) GetAccessToken( ctx context.Context, scopes []string, ) (_ AccessToken, outErr error)
GetAccessToken either returns an existing non-expired token for the environment that matches the requested scope, or starts the authorization flow to get a new token.
During the authorisation flow we will attempt to automatically open a URL in the users browser.
func (*ConfigurationHandler) GetClientAccessToken ¶ added in v0.4.0
func (ac *ConfigurationHandler) GetClientAccessToken( ctx context.Context, clientID string, clientSecret string, scopes []string, ) (oauth2.TokenSource, error)
Convenience function for using the OIDC configuration to get a client credentials token source.
func (*ConfigurationHandler) GetEndpoint ¶ added in v0.5.0
func (ac *ConfigurationHandler) GetEndpoint(name string) (string, bool)
GetEndpoints returns the specified endpoint.
func (*ConfigurationHandler) GetEndpoints ¶ added in v0.5.0
func (ac *ConfigurationHandler) GetEndpoints() map[string]string
GetEndpoints returns all available endpoints.
func (*ConfigurationHandler) GetOIDCConfig ¶ added in v0.4.0
func (ac *ConfigurationHandler) GetOIDCConfig( ctx context.Context, ) (*OIDCConfig, error)
func (*ConfigurationHandler) Load ¶ added in v0.2.0
func (ac *ConfigurationHandler) Load() (outErr error)
Load configuration and tokens from disk.
func (*ConfigurationHandler) Save ¶
func (ac *ConfigurationHandler) Save() error
Save configuration and tokens to disk.
func (*ConfigurationHandler) SetOIDCConfigURL ¶ added in v0.5.0
func (ac *ConfigurationHandler) SetOIDCConfigURL( ctx context.Context, configURL string, ) error
type EnvConfiguration ¶ added in v0.5.0
type EnvConfiguration struct {
OIDC *OIDCEnvironment `json:"oidc"`
Endpoints map[string]string
}
func (*EnvConfiguration) SetDefaults ¶ added in v0.5.0
func (ec *EnvConfiguration) SetDefaults(env string)
type OIDCConfig ¶ added in v0.2.0
type OIDCConfig struct {
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
IntrospectionEndpoint string `json:"introspection_endpoint"`
UserinfoEndpoint string `json:"userinfo_endpoint"`
EndSessionEndpoint string `json:"end_session_endpoint"`
}
type OIDCEnvironment ¶ added in v0.2.0
type OIDCEnvironment struct {
Refreshed time.Time `json:"refreshed,omitempty,omitzero"`
ConfigURL string `json:"oidc_config_url,omitempty"`
Config *OIDCConfig `json:"oidc_config"`
}