credentials

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2021 License: MIT Imports: 19 Imported by: 0

README

GoAuth Credentials

goauth/credentials is a package to manage generic OAuth 2.0 credentials definitions.

The primary use case is to have a single JSON definition of multiple applications for multiple services which can be used to generate token and API requests.

Both OAuth 2.0 and JWT are supported.

It works with goauth/endpoints to add endpoints for known services.

Documentation

Index

Constants

View Source
const (
	TypeOAuth2 = "oauth2"
	TypeJWT    = "jwt"
)
View Source
const (
	SigningMethodES256 = "ES256"
	SigningMethodES384 = "ES384"
	SigningMethodES512 = "ES512"
	SigningMethodHS256 = "HS256"
	SigningMethodHS384 = "HS384"
	SigningMethodHS512 = "HS512"
)

Variables

This section is empty.

Functions

func NewTokenCli

func NewTokenCli(creds Credentials, state string) (token *oauth2.Token, err error)

Types

type Credentials

type Credentials struct {
	Service   string            `json:"service,omitempty"`
	Type      string            `json:"type,omitempty"`
	Subdomain string            `json:"subdomain,omitempty"`
	OAuth2    OAuth2Credentials `json:"oauth2,omitempty"`
	JWT       JWTCredentials    `json:"jwt,omitempty"`
	Token     *oauth2.Token     `json:"token,omitempty"`
}

func NewCredentialsJSON

func NewCredentialsJSON(credsData, accessToken []byte) (Credentials, error)

func ReadCredentialsFromFile

func ReadCredentialsFromFile(credentialsSetFilename, accountKey string, inclAccountsOnError bool) (Credentials, error)

func (*Credentials) Inflate

func (creds *Credentials) Inflate() error

func (*Credentials) NewClient

func (creds *Credentials) NewClient(ctx context.Context) (*http.Client, error)

func (*Credentials) NewClientCli

func (creds *Credentials) NewClientCli(oauth2State string) (*http.Client, error)

func (*Credentials) NewSimpleClient

func (creds *Credentials) NewSimpleClient(httpClient *http.Client) (*httpsimple.SimpleClient, error)

func (*Credentials) NewToken

func (creds *Credentials) NewToken() (*oauth2.Token, error)

func (*Credentials) NewTokenCli

func (creds *Credentials) NewTokenCli(oauth2State string) (*oauth2.Token, error)

NewTokenCli retrieves a token using CLI approach for OAuth 2.0 authorization code or password grant.

type CredentialsSet

type CredentialsSet struct {
	Credentials map[string]Credentials
}

func ReadFileCredentialsSet

func ReadFileCredentialsSet(credentialsSetFilename string, inflateEndpoints bool) (CredentialsSet, error)

func (*CredentialsSet) Accounts

func (set *CredentialsSet) Accounts() []string

func (*CredentialsSet) Get

func (set *CredentialsSet) Get(key string) (Credentials, error)

func (*CredentialsSet) GetClient

func (set *CredentialsSet) GetClient(ctx context.Context, key string) (*http.Client, error)

func (*CredentialsSet) Inflate

func (set *CredentialsSet) Inflate()

func (*CredentialsSet) Keys

func (set *CredentialsSet) Keys() []string

type JWTCredentials

type JWTCredentials struct {
	Issuer        string `json:"issuer,omitempty"`
	PrivateKey    string `json:"privateKey,omitempty"`
	SigningMethod string `json:"signingMethod,omitempty"`
}

func (*JWTCredentials) StandardToken

func (jc *JWTCredentials) StandardToken(tokenDuration time.Duration) (*jwt.Token, string, error)

type OAuth2Credentials

type OAuth2Credentials struct {
	ServerURL       string              `json:"serverURL,omitempty"`
	ApplicationID   string              `json:"applicationID,omitempty"`
	ClientID        string              `json:"clientID,omitempty"`
	ClientSecret    string              `json:"clientSecret,omitempty"`
	OAuth2Endpoint  oauth2.Endpoint     `json:"oauth2Endpoint,omitempty"`
	RedirectURL     string              `json:"redirectURL,omitempty"`
	AppName         string              `json:"applicationName,omitempty"`
	AppVersion      string              `json:"applicationVersion,omitempty"`
	OAuthEndpointID string              `json:"oauthEndpointID,omitempty"`
	AccessTokenTTL  int64               `json:"accessTokenTTL,omitempty"`
	RefreshTokenTTL int64               `json:"refreshTokenTTL,omitempty"`
	GrantType       string              `json:"grantType,omitempty"`
	Username        string              `json:"username,omitempty"`
	Password        string              `json:"password,omitempty"`
	OtherParams     map[string][]string `json:"otherParams,omitempty"`
	Scopes          []string            `json:"scopes,omitempty"`
}

OAuth2Credentials supports OAuth 2.0 authorization_code, password, and client_credentials grant flows.

func NewOAuth2CredentialsEnv added in v0.10.2

func NewOAuth2CredentialsEnv(envPrefix string) OAuth2Credentials

func (*OAuth2Credentials) AppNameAndVersion

func (oc *OAuth2Credentials) AppNameAndVersion() string

func (*OAuth2Credentials) AuthCodeURL

func (oc *OAuth2Credentials) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string

func (*OAuth2Credentials) Config

func (oc *OAuth2Credentials) Config() oauth2.Config

func (*OAuth2Credentials) ConfigClientCredentials

func (oc *OAuth2Credentials) ConfigClientCredentials() clientcredentials.Config

func (*OAuth2Credentials) Exchange

func (oc *OAuth2Credentials) Exchange(code string) (*oauth2.Token, error)

func (*OAuth2Credentials) InflateURL

func (oc *OAuth2Credentials) InflateURL(apiUrlPath string) string

func (*OAuth2Credentials) IsGrantType

func (oc *OAuth2Credentials) IsGrantType(grantType string) bool

func (*OAuth2Credentials) NewClient

func (oc *OAuth2Credentials) NewClient(ctx context.Context) (*http.Client, error)

NewClient returns a `*http.Client` for applications using `client_credentials` grant. The client can be modified using context, e.g. ignoring bad certs or otherwise.

func (*OAuth2Credentials) NewToken

func (oc *OAuth2Credentials) NewToken(ctx context.Context) (*oauth2.Token, error)

NewToken retrieves an `*oauth2.Token` when the requisite information is available. Note this uses `clientcredentials.Config.Token()` which doesn't always work. In This situation, use `goauth.TokenClientCredentials()` as an alternative.

func (*OAuth2Credentials) PasswordRequestBody

func (oc *OAuth2Credentials) PasswordRequestBody() url.Values

type Options

type Options struct {
	CredsPath string `long:"creds" description:"Environment File Path" required:"true"`
	Account   string `long:"account" description:"Environment Variable Name"`
	Token     string `long:"token" description:"Token"`
	CLI       []bool `long:"cli" description:"CLI"`
}

Options is a struct to be used with `github.com/jessevdk/go-flags`. It can be embedded in another struct.

func (*Options) UseCLI

func (opts *Options) UseCLI() bool

Jump to

Keyboard shortcuts

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