user_sso

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2022 License: MIT Imports: 11 Imported by: 0

README

user-sso

基于user服务的sso sdk,降低接入成本,因为暂时没有精力实现oidc,暂时用最简单的方式实现。 但是基本流程差不多,本质是把用户信息作为resource,使用access token获取用户信息。

  • new client,指定oauth2要求的参数和user endpoint
  • new token conf,可以encode和decode,支持gzip压缩以减小体积

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorNilAccessItems = errors.New("no access items")
View Source
var ErrorRequestFailed = errors.New("access request failed")
View Source
var FetchUserInfoErr = errors.New("fetch user info error")

Functions

This section is empty.

Types

type AccessItem

type AccessItem struct {
	Resource string `protobuf:"bytes,1,opt,name=resource,proto3" json:"resource,omitempty"`
	Action   string `protobuf:"bytes,2,opt,name=action,proto3" json:"action,omitempty"`
	Ok       bool   `protobuf:"varint,3,opt,name=ok,proto3" json:"ok,omitempty"`
}

type AccessItems

type AccessItems struct {
	Items []*AccessItem `json:"items"`
}

AccessItems 批量鉴权

type AccessResponse

type AccessResponse struct {
	Code int64         `json:"code"`
	Msg  string        `json:"msg"`
	Data []*AccessItem `json:"data"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conf *Config) *Client

func (*Client) AuthUrl

func (c *Client) AuthUrl(state string, opts ...oauth2.AuthCodeOption) string

AuthUrl use auth code mode

func (*Client) Authentication

func (c *Client) Authentication(_ context.Context, accessToken string, accessItems []*AccessItem) ([]*AccessItem, error)

Authentication batch authentication

func (*Client) Exchange

func (c *Client) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*UserInfo, error)

Exchange fetch user info

func (*Client) ExpirationSoon

func (c *Client) ExpirationSoon(_ context.Context, userInfo *UserInfo, failureInterval float64) bool

ExpirationSoon judge how long there is more to ask for token

func (*Client) LogoutUrl

func (c *Client) LogoutUrl(returnUrl string) string

LogoutUrl linkage user service logout

func (*Client) PasswordCredentials

func (c *Client) PasswordCredentials(ctx context.Context, username, password string) (*UserInfo, error)

PasswordCredentials use username & password login

func (*Client) RefreshToken

func (c *Client) RefreshToken(ctx context.Context, userInfo *UserInfo) error

RefreshToken refresh token

type Config

type Config struct {
	Endpoint     string   `json:"endpoint" form:"endpoint" yaml:"endpoint" toml:"endpoint"`
	ClientId     string   `json:"client_id" form:"client_id" yaml:"client_id" toml:"client_id"`
	ClientSecret string   `json:"client_secret" form:"client_secret" yaml:"client_secret" toml:"client_secret"`
	RedirectUrl  string   `json:"redirect_url" form:"redirect_url" yaml:"redirect_url" toml:"redirect_url"`
	Scopes       []string `json:"scopes" form:"scopes" yaml:"scopes" toml:"scopes"`
}

type UserInfo

type UserInfo struct {
	Name     string            `json:"name,omitempty"`   //姓名
	Email    string            `json:"email,omitempty"`  //邮箱
	Avatar   string            `json:"avatar,omitempty"` //base64 头像
	Token    *oauth2.Token     `json:"encoder"`
	Metadata map[string]string `json:"md,omitempty"`
}

func (*UserInfo) Get

func (u *UserInfo) Get(key string) string

Get get metadata

func (*UserInfo) Set

func (u *UserInfo) Set(key, val string)

Set set metadata

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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