oauth2

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 10 Imported by: 0

README

golang oauth2 server,OAuth2 非标准实现

快速开始:

package main

import "github.com/0x00xc/oauth2"

func main(){
    ex := example.NewExample()
    ex.Serve(":8000")
}

浏览器访问:

http://127.0.0.1:8000/authorize?client_id=1000&redirect=http%3A%2F%2F127.0.0.1%3A8000%2Fexample%2Fcallback&sid=123456&state=hello+world

授权流程:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthorizeRequest

type AuthorizeRequest struct {
	Request                   //其他公共参数,暂未使用
	RequestType AuthorizeType //授权类型
	ClientId    string        //客户端id
	Redirect    string        //授权回调地址
	State       string        //额外参数
}

func NewAuthorizeRequest

func NewAuthorizeRequest(r *http.Request) (*AuthorizeRequest, error)

type AuthorizeResponse

type AuthorizeResponse struct {
	ClientId  string //
	GrantCode string // 授权码
	State     string // 额外参数
	// contains filtered or unexported fields
}

func (*AuthorizeResponse) Redirect

func (r *AuthorizeResponse) Redirect() string

type AuthorizeType

type AuthorizeType string
const (
	AuthorizationCode   AuthorizeType = "code"
	Implicit            AuthorizeType = "implicit"
	PasswordCredentials AuthorizeType = "password"
	ClientCredentials   AuthorizeType = "client"
)

type Client

type Client interface {
	GetClientId() string
	GetSecret() string
	Callback() []string
	Grant(scope []string, sessionId string) (interface{}, error)
}

type Generator

type Generator interface {
	GenGrantCode(c Client, sessionId string) (string, error)
	GenAccessToken(c Client, sessionId string) (string, error)
	GenRefreshToken(c Client, sessionId string) (string, error)
}

type GrantInfo

type GrantInfo struct {
	ClientId           string      `json:"client_id"`
	SessionId          string      `json:"session_id"`
	AccessToken        string      `json:"access_token"`
	AccessTokenExpire  int64       `json:"access_token_expire"`
	RefreshToken       string      `json:"refresh_token"`
	RefreshTokenExpire int64       `json:"refresh_token_expire"`
	Data               interface{} `json:"data,omitempty"`
}

type GrantRequest

type GrantRequest struct {
	Request
	ClientId  string
	GrantCode string
	Scope     []string
}

func NewGrantRequest

func NewGrantRequest(r *http.Request) (*GrantRequest, error)

type GrantResponse

type GrantResponse struct {
	Info GrantInfo `json:"info"`
}

type KVStorage

type KVStorage interface {
	Get(k string) (interface{}, error)
	Put(k string, v interface{}) error
	Del(k string) error
}

type Options

type Options struct {
	CheckRedirect      bool
	AccessTokenExpire  int64 //second
	RefreshTokenExpire int64 //second

	Generator Generator
	Verify    func(c Client, header http.Header, val url.Values) error
}

func DefaultOptions

func DefaultOptions() *Options

type RefreshRequest

type RefreshRequest struct {
	RefreshToken string
}

type Request

type Request struct {
	Timestamp int64
	Nonce     string
}

type Server

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

func NewServer

func NewServer(opt *Options, storage Storage) *Server

func (*Server) Authorize

func (s *Server) Authorize(request *AuthorizeRequest, sessionId string) (*AuthorizeResponse, error)

request 授权请求参数 sessionId 当前用户识别标识,可以是用户id也可以是其他 每次生成授权码(grant_code)时,会将 grant_code 与 sessionId 关联保存,确认授权时,会读取出 sessionId 返回给业务端, 业务端自行决定 sessionId 用途(一般用作用户标识)

func (*Server) AuthorizeFromRequest

func (s *Server) AuthorizeFromRequest(r *http.Request, sessionId string) (*AuthorizeResponse, error)

func (*Server) GetClient

func (s *Server) GetClient(clientID string) (Client, error)

func (*Server) Grant

func (s *Server) Grant(request *GrantRequest) (*GrantResponse, error)

func (*Server) GrantFromRequest

func (s *Server) GrantFromRequest(r *http.Request) (*GrantResponse, error)

func (*Server) Refresh

func (s *Server) Refresh(refreshToken string) (*GrantResponse, error)

func (*Server) SetStorage

func (s *Server) SetStorage(storage Storage)

func (*Server) Storage

func (s *Server) Storage() Storage

func (*Server) VerifyAccessToken

func (s *Server) VerifyAccessToken(accessToken string) (*GrantResponse, error)

type SimpleClient

type SimpleClient struct {
	Id       string
	Secret   string
	Redirect string
}

func (*SimpleClient) Callback

func (s *SimpleClient) Callback() []string

func (*SimpleClient) GetClientId

func (s *SimpleClient) GetClientId() string

func (*SimpleClient) GetSecret

func (s *SimpleClient) GetSecret() string

func (*SimpleClient) Grant

func (s *SimpleClient) Grant(scope []string, sessionId string) (interface{}, error)

type SimpleGenerator

type SimpleGenerator struct{}

func NewSimpleGenerator

func NewSimpleGenerator() *SimpleGenerator

func (*SimpleGenerator) GenAccessToken

func (g *SimpleGenerator) GenAccessToken(c Client, sessionId string) (string, error)

func (*SimpleGenerator) GenGrantCode

func (g *SimpleGenerator) GenGrantCode(c Client, sessionId string) (string, error)

func (*SimpleGenerator) GenRefreshToken

func (g *SimpleGenerator) GenRefreshToken(c Client, sessionId string) (string, error)

type SimpleStorage

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

func NewSimpleKVStorage

func NewSimpleKVStorage(getClient func(id string) (*SimpleClient, error), newKVStorage func() KVStorage) *SimpleStorage

func NewSimpleStorage

func NewSimpleStorage(getClient func(id string) (*SimpleClient, error)) *SimpleStorage

func (*SimpleStorage) GetClient

func (s *SimpleStorage) GetClient(id string) (Client, error)

func (*SimpleStorage) GetGrantCode

func (s *SimpleStorage) GetGrantCode(code string) (string, error)

func (*SimpleStorage) GetGrantInfoByAccessToken

func (s *SimpleStorage) GetGrantInfoByAccessToken(accessToken string) (GrantInfo, error)

func (*SimpleStorage) GetGrantInfoByRefreshToken

func (s *SimpleStorage) GetGrantInfoByRefreshToken(refreshToken string) (GrantInfo, error)

func (*SimpleStorage) PutClient

func (s *SimpleStorage) PutClient(c *SimpleClient)

func (*SimpleStorage) RemoveAccessToken

func (s *SimpleStorage) RemoveAccessToken(accessToken string) error

func (*SimpleStorage) RemoveClient

func (s *SimpleStorage) RemoveClient(id string)

func (*SimpleStorage) RemoveGrantCode

func (s *SimpleStorage) RemoveGrantCode(code string) error

func (*SimpleStorage) RemoveRefreshToken

func (s *SimpleStorage) RemoveRefreshToken(refreshToken string) error

func (*SimpleStorage) SaveGrantCode

func (s *SimpleStorage) SaveGrantCode(code string, sessionId string) error

func (*SimpleStorage) SaveGrantInfo

func (s *SimpleStorage) SaveGrantInfo(info GrantInfo) error

type Storage

type Storage interface {
	GetClient(id string) (Client, error)
	SaveGrantCode(code string, sessionId string) error
	GetGrantCode(code string) (string, error)
	RemoveGrantCode(code string) error
	SaveGrantInfo(info GrantInfo) error
	GetGrantInfoByAccessToken(accessToken string) (GrantInfo, error)
	GetGrantInfoByRefreshToken(refreshToken string) (GrantInfo, error)
	RemoveAccessToken(accessToken string) error
	RemoveRefreshToken(refreshToken string) error
}

type SyncMap

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

func NewSyncMap

func NewSyncMap() *SyncMap

func (*SyncMap) Del

func (m *SyncMap) Del(k string) error

func (*SyncMap) Get

func (m *SyncMap) Get(k string) (interface{}, error)

func (*SyncMap) Put

func (m *SyncMap) Put(k string, v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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