oauth2more

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2020 License: MIT Imports: 18 Imported by: 0

README

OAuth 2.0 More for Go

Used By Build Status Go Report Card Docs License

More OAuth 2.0 - https://github.com/golang/oauth2 functionality. Currently provides:

  • NewClient() functions to create *http.Client structs for services not supported in oauth2 like aha, metabase, ringcentral, salesforce, visa, etc. Generating *http.Client structs is especially useful for using with Swagger Codegen auto-generated SDKs to support different auth models.
  • Helper libraries to retrieve canonical user information from services. The SCIM user schema is used for a canonical user model.
  • Multi-service libraries to more transparently handle OAuth 2 for multiple services, e.g. a website that supports Google and Facebook auth. This is demoed in grokify/beego-oauth2-demo

Installation

$ go get github.com/grokify/oauth2more

Usage

Canonical User Information

ClientUtil structs satisfy the interface having SetClient() and GetSCIMUser() functions.

Google
import(
	"github.com/grokify/oauth2more/google"
)

// googleOAuth2HTTPClient is *http.Client from Golang OAuth2
googleClientUtil := google.NewClientUtil(googleOAuth2HTTPClient)
scimuser, err := googleClientUtil.GetSCIMUser()
Facebook
import(
	"github.com/grokify/oauth2more/facebook"
)

// fbOAuth2HTTPClient is *http.Client from Golang OAuth2
fbClientUtil := facebook.NewClientUtil(fbOAuth2HTTPClient)
scimuser, err := fbClientUtil.GetSCIMUser()
RingCentral
import(
	"github.com/grokify/oauth2more/ringcentral"
)

// rcOAuth2HTTPClient is *http.Client from Golang OAuth2
rcClientUtil := ringcentral.NewClientUtil(rcOAuth2HTTPClient)
scimuser, err := rcClientUtil.GetSCIMUser()

Test Redirect URL

This repo comes with a generic test OAuth 2 redirect page:

Example App

See the following repo for a Beego-based demo app:

Documentation

Index

Constants

View Source
const (
	VERSION      = "0.2.0"
	PATH         = "github.com/grokify/oauth2more"
	BasicPrefix  = "Basic"
	BearerPrefix = "Bearer"
)

Variables

View Source
var (
	RelCredentialsDir = ".credentials"
)

Functions

func BasicAuthHeader added in v0.3.0

func BasicAuthHeader(userid, password string) (string, error)

func BasicAuthToken

func BasicAuthToken(username, password string) (*oauth2.Token, error)

BasicAuthToken provides Basic Authentication support via an oauth2.Token.

func ClientTLSInsecureSkipVerify added in v0.3.0

func ClientTLSInsecureSkipVerify(client *http.Client) *http.Client

func NewClientAuthCode

func NewClientAuthCode(conf oauth2.Config, authCode string) (*http.Client, error)

func NewClientBasicAuth added in v0.3.1

func NewClientBasicAuth(username, password string, tlsInsecureSkipVerify bool) (*http.Client, error)

NewClientBasicAuth returns a *http.Client given a basic auth username and password.

func NewClientBearerTokenSimple added in v0.3.0

func NewClientBearerTokenSimple(accessToken string) *http.Client

NewClientBearerTokenSimple return a *http.Client given a bearer token string

func NewClientBearerTokenSimpleOrJson added in v0.3.0

func NewClientBearerTokenSimpleOrJson(ctx context.Context, tokenOrJson []byte) (*http.Client, error)

func NewClientHeaders added in v0.3.1

func NewClientHeaders(headersMap map[string]string, tlsInsecureSkipVerify bool) *http.Client

func NewClientPassword added in v0.3.0

func NewClientPassword(conf oauth2.Config, ctx context.Context, username, password string) (*http.Client, error)

func NewClientPasswordConf

func NewClientPasswordConf(conf oauth2.Config, username, password string) (*http.Client, error)

func NewClientTLSToken

func NewClientTLSToken(ctx context.Context, tlsConfig *tls.Config, token *oauth2.Token) *http.Client

func NewClientToken added in v0.3.0

func NewClientToken(tokenType, tokenValue string, tlsInsecureSkipVerify bool) *http.Client

func NewClientTokenBase64Encode added in v0.3.0

func NewClientTokenBase64Encode(tokenType, tokenValue string, tlsInsecureSkipVerify bool) *http.Client

func NewClientTokenJSON added in v0.3.0

func NewClientTokenJSON(ctx context.Context, tokenJSON []byte) (*http.Client, error)

func NewClientTokenOAuth2 added in v0.3.1

func NewClientTokenOAuth2(token *oauth2.Token) *http.Client

func NewClientWebTokenStore

func NewClientWebTokenStore(ctx context.Context, conf *oauth2.Config, tStore *TokenStoreFile, forceNewToken bool) (*http.Client, error)

func NewTokenFromWeb

func NewTokenFromWeb(cfg *oauth2.Config) (*oauth2.Token, error)

func ParseJwtTokenString added in v0.3.1

func ParseJwtTokenString(tokenString string, secretKey string, claims jwt.Claims) (*jwt.Token, error)

func ParseToken added in v0.3.1

func ParseToken(rawToken []byte) (*oauth2.Token, error)

ParseToken parses a JSON token and returns an `*oauth2.Token` with custom properties.

func PathVersion added in v0.3.0

func PathVersion() string

func RFC7617UserPass

func RFC7617UserPass(userid, password string) (string, error)

RFC7617UserPass base64 encodes a user-id and password per: https://tools.ietf.org/html/rfc7617#section-2

func ReadTokenFile

func ReadTokenFile(fpath string) (*oauth2.Token, error)

ReadTokenFile retrieves a Token from a given filepath.

func UserCredentialsDir

func UserCredentialsDir() (string, error)

func UserCredentialsDirMk

func UserCredentialsDirMk(perm os.FileMode) (string, error)

func WriteTokenFile

func WriteTokenFile(fpath string, tok *oauth2.Token) error

WriteTokenFile writes a token file to the the filepaths.

Types

type AppCredentials

type AppCredentials struct {
	Service      string   `json:"service,omitempty"`
	ClientID     string   `json:"client_id"`
	ClientSecret string   `json:"client_secret"`
	RedirectURIs []string `json:"redirect_uris"`
	AuthURI      string   `json:"auth_uri"`
	TokenURI     string   `json:"token_uri"`
	Scopes       []string `json:"scopes"`
}

func (*AppCredentials) Config

func (c *AppCredentials) Config() *oauth2.Config

func (*AppCredentials) Defaultify

func (ac *AppCredentials) Defaultify()

type AppCredentialsWrapper

type AppCredentialsWrapper struct {
	Web       *AppCredentials `json:"web"`
	Installed *AppCredentials `json:"installed"`
}

func NewAppCredentialsWrapperFromBytes

func NewAppCredentialsWrapperFromBytes(data []byte) (AppCredentialsWrapper, error)

func (*AppCredentialsWrapper) Config

func (w *AppCredentialsWrapper) Config() (*oauth2.Config, error)

type ApplicationCredentials

type ApplicationCredentials struct {
	ServerURL    string
	ClientID     string
	ClientSecret string
	Endpoint     oauth2.Endpoint
}

ApplicationCredentials represents information for an app.

type AuthorizationType added in v0.3.0

type AuthorizationType int
const (
	Anonymous AuthorizationType = iota
	Basic
	Bearer
	Digest
	NTLM
	Negotiate
	OAuth
)

func (AuthorizationType) String added in v0.3.0

func (a AuthorizationType) String() string

String returns the English name of the authorizationTypes ("Basic", "Bearer", ...).

type OAuth2Util

type OAuth2Util interface {
	SetClient(*http.Client)
	GetSCIMUser() (scim.User, error)
}

type ServiceType added in v0.3.0

type ServiceType int
const (
	Google ServiceType = iota
	Facebook
	RingCentral
	Aha
)

type TokenStoreFile

type TokenStoreFile struct {
	Token    *oauth2.Token
	Filepath string
}

func NewTokenStoreFile

func NewTokenStoreFile(file string) *TokenStoreFile

func NewTokenStoreFileDefault

func NewTokenStoreFileDefault(tokenPath string, useDefaultDir bool, perm os.FileMode) (*TokenStoreFile, error)

func (*TokenStoreFile) NewTokenFromWeb

func (ts *TokenStoreFile) NewTokenFromWeb(cfg *oauth2.Config) (*oauth2.Token, error)

func (*TokenStoreFile) Read

func (ts *TokenStoreFile) Read() error

func (*TokenStoreFile) Write

func (ts *TokenStoreFile) Write() error

type UserCredentials

type UserCredentials struct {
	Username string
	Password string
}

UserCredentials represents a user's credentials.

Directories

Path Synopsis
aha
auth0 contains a Go implementation of Auth0's PKCE support: https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce
auth0 contains a Go implementation of Auth0's PKCE support: https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce
examples
jwt
examples/send_ics
This package posts an ICS file to Gmail.
This package posts an ICS file to Gmail.
util

Jump to

Keyboard shortcuts

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