gooauth

package
v1.2.170 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Go OAuth2.0

基于 golang.org/x/oauth2

封装 通用OAuth2.0常用方法

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DelClient

func DelClient(names ...string)

func Init

func Init(configs ...Config) (err error)

可以一次初始化多个实例或者 多次调用初始化多个实例

Types

type Config

type Config struct {
	Name string `yaml:"Name" json:"name,optional"`
	// Token 信息
	AccessToken  string `yaml:"AccessToken" json:"accessToken,optional"`
	RefreshToken string `yaml:"RefreshToken" json:"refreshToken,optional"`
	ExpiresIn    int64  `yaml:"ExpiresIn" json:"expiresIn,optional"` //秒s

	// 授权参数 OAuthConfig oauth2.Config
	ClientId     string    `yaml:"ClientId" json:"clientId,optional"`
	ClientSecret string    `yaml:"ClientSecret" json:"clientSecret,optional"`
	RedirectURL  string    `yaml:"RedirectURL" json:"redirectURL,optional"`
	Endpoint     *Endpoint `yaml:"Endpoint" json:"endpoint"`
	Scopes       []string  `yaml:"Scopes" json:"scopes,optional"`
	State        string    `yaml:"State" json:"state,optional"`
}

type Endpoint added in v1.2.122

type Endpoint struct {
	TokenURL      string           `yaml:"TokenURL" json:"tokenURL,optional"`
	AuthURL       string           `yaml:"AuthURL" json:"authURL,optional"`
	DeviceAuthURL string           `yaml:"DeviceAuthURL" json:"deviceAuthURL,optional"`
	AuthStyle     oauth2.AuthStyle `yaml:"AuthStyle" json:"authStyle,optional"`

} // `yaml:"Endpoint" json:"endpoint"`

type GoOAuth

type GoOAuth struct {
	Config      Config
	Token       *oauth2.Token
	OAuthConfig oauth2.Config
}

func Default

func Default() *GoOAuth

func GetClient

func GetClient(names ...string) *GoOAuth

func New

func New(config Config) *GoOAuth

func (*GoOAuth) AuthUrl

func (c *GoOAuth) AuthUrl(opts ...oauth2.AuthCodeOption) string

获取授权url

自定义参数

param1 := oauth2.SetAuthURLParam("param1", "value1")
param2 := oauth2.SetAuthURLParam("param2", "value2")

func (*GoOAuth) Exchange

func (c *GoOAuth) Exchange(ctx context.Context, authorizationCode string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)

获取token

自定义参数

param1 := oauth2.SetAuthURLParam("param1", "value1")
param2 := oauth2.SetAuthURLParam("param2", "value2")

func (*GoOAuth) GetAccessToken

func (c *GoOAuth) GetAccessToken() string

func (*GoOAuth) GetRefreshToken

func (c *GoOAuth) GetRefreshToken() string

func (*GoOAuth) GetToken

func (c *GoOAuth) GetToken() *oauth2.Token

详细的信息,注意:只有在刷新过token的时候才会有值

func (*GoOAuth) RefreshToken

func (c *GoOAuth) RefreshToken(ctx context.Context) (*oauth2.Token, error)

刷新token

func (*GoOAuth) TokenSource

func (c *GoOAuth) TokenSource(ctx context.Context) oauth2.TokenSource

type IGoOAuth added in v1.2.122

type IGoOAuth interface {
	// 自定义参数
	//
	//	param1 := oauth2.SetAuthURLParam("param1", "value1")
	//	param2 := oauth2.SetAuthURLParam("param2", "value2")
	AuthUrl(opts ...oauth2.AuthCodeOption) string
	Exchange(ctx context.Context, authorizationCode string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
	RefreshToken(ctx context.Context, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
	GetToken() *oauth2.Token
}

OAuth2.0 授权接口

Jump to

Keyboard shortcuts

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