oauth2

package
v1.0.1-0...-49adebd Latest Latest
Warning

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

Go to latest
Published: May 30, 2018 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TokenEntropy = 32
	SecretLength = 32
)
View Source
var Logger = zap.NewNop()

Functions

func GetCredentialsFromHttp

func GetCredentialsFromHttp(headerAuth string) (string, string, error)

func HierarchicScope

func HierarchicScope(requestScope string, clientScopes []string) bool

func IsNotFound

func IsNotFound(err error) bool

func RandomBytes

func RandomBytes(n int) ([]byte, error)

RandomBytes returns n random bytes by reading from crypto/rand.Reader

func WriteJson

func WriteJson(w http.ResponseWriter, status int, v interface{})

func WriteJsonError

func WriteJsonError(w http.ResponseWriter, err error) error

Types

type AccessToken

type AccessToken struct {
	AccessToken string                 `json:"at"`
	ClientID    string                 `json:"cid,omitempty"`
	UserID      string                 `json:"uid,omitempty"`
	Expired     int64                  `json:"exp"`
	ExpiresIn   int                    `json:"ein"`
	Scopes      []string               `json:"scp"`
	Extras      map[string]interface{} `json:"ext"`
}

func (*AccessToken) HasScope

func (a *AccessToken) HasScope(scopes ...string) bool

func (*AccessToken) Valid

func (a *AccessToken) Valid() bool

type AppErr

type AppErr struct {
	Message string `json:"error_description"`

	Code string `json:"error"`
	// contains filtered or unexported fields
}

func Error

func Error(message string, status int, code string, cause error) *AppErr

func InvalidClient

func InvalidClient(message string) *AppErr

func NotFound

func NotFound(err error) *AppErr

func ServerError

func ServerError(message string, cause error) *AppErr

func UnknownError

func UnknownError(cause error) *AppErr

func (*AppErr) Cause

func (e *AppErr) Cause() error

func (*AppErr) Error

func (e *AppErr) Error() string

func (*AppErr) MarshalLogObject

func (e *AppErr) MarshalLogObject(enc zapcore.ObjectEncoder) error

func (*AppErr) WithCause

func (e *AppErr) WithCause(err error) *AppErr

type AuthorizeCode

type AuthorizeCode struct {
	Code         string                 `json:"c"`
	ClientID     string                 `json:"cid"`
	UserID       string                 `json:"uid"`
	Expired      int64                  `json:"exp"`
	Scopes       []string               `json:"scp"`
	RedirectURI  string                 `json:"rdr"`
	ResponseType string                 `json:"rpt"`
	Extras       map[string]interface{} `json:"ext"`
}

func (*AuthorizeCode) Valid

func (a *AuthorizeCode) Valid() bool

type Client

type Client struct {
	ID           string   `json:"id"`
	Name         string   `json:"n"`
	Secret       string   `json:"s"`
	RedirectURIs []string `json:"rdr"`
	GrantTypes   []string `json:"gt"`
	Scopes       []string `json:"scp"`
	Public       bool     `json:"pub"`
	CreatedAt    string   `json:"cat"`
}

func (*Client) HasGrantType

func (c *Client) HasGrantType(grant string) bool

type CreateAccessTokenRequest

type CreateAccessTokenRequest struct {
	ClientID  string
	UserID    string
	Scopes    []string
	ExpiresIn int
	Extras    map[string]interface{}
}

type HMACTokenGenerator

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

HMACTokenGenerator is responsible for generating and validating challenges.

func NewHMACTokenGenerator

func NewHMACTokenGenerator(secret []byte) *HMACTokenGenerator

func (*HMACTokenGenerator) CreateAccessToken

func (c *HMACTokenGenerator) CreateAccessToken(req *CreateAccessTokenRequest) (string, error)

Generate generates a token and a matching signature or returns an error. This method implements rfc6819 Section 5.1.4.2.2: Use High Entropy for Secrets.

func (*HMACTokenGenerator) CreateCode

func (c *HMACTokenGenerator) CreateCode() string

func (*HMACTokenGenerator) CreateRefreshToken

func (c *HMACTokenGenerator) CreateRefreshToken() string

func (*HMACTokenGenerator) Signature

func (c *HMACTokenGenerator) Signature(token string) string

func (*HMACTokenGenerator) Validate

func (c *HMACTokenGenerator) Validate(token string) error

Validate validates a token and returns its signature or an error if the token is not valid.

type JWTAccessToken

type JWTAccessToken struct {
	Audience  string
	ExpiresAt int64
	ID        string
	IssuedAt  int64
	Issuer    string
	Subject   string
	Extras    map[string]interface{}
	Scopes    []string
}

func ClaimJWTAccessToken

func ClaimJWTAccessToken(publicKey *rsa.PublicKey, accesstoken string) (*JWTAccessToken, error)

func (*JWTAccessToken) HasScope

func (a *JWTAccessToken) HasScope(scopes ...string) bool

func (*JWTAccessToken) Valid

func (a *JWTAccessToken) Valid() bool

type JWTTokenGenerator

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

func NewJWTTokenGenerator

func NewJWTTokenGenerator(privateKey *rsa.PrivateKey) *JWTTokenGenerator

func (*JWTTokenGenerator) CreateAccessToken

func (c *JWTTokenGenerator) CreateAccessToken(req *CreateAccessTokenRequest) (string, error)

func (*JWTTokenGenerator) CreateCode

func (c *JWTTokenGenerator) CreateCode() string

func (*JWTTokenGenerator) CreateRefreshToken

func (c *JWTTokenGenerator) CreateRefreshToken() string

type RefreshToken

type RefreshToken struct {
	RefreshToken         string                 `json:"rt"`
	ClientID             string                 `json:"cid"`
	UserID               string                 `json:"uid"`
	Expired              int64                  `json:"exp"`
	Scopes               []string               `json:"scp"`
	AccessTokenLifespan  int                    `json:"atl"`
	RefreshTokenLifespan int                    `json:"rtl"`
	Extras               map[string]interface{} `json:"ext"`
}

func (*RefreshToken) Valid

func (r *RefreshToken) Valid() bool

type Storage

type Storage interface {
	GetClient(id string) (*Client, error)
	GetClientWithSecret(id, secret string) (*Client, error)
	GetRefreshToken(refreshToken string) (*RefreshToken, error)
	GetAuthorizeCode(code string) (*AuthorizeCode, error)
	GetAccessToken(accessToken string) (*AccessToken, error)
	SaveAccessToken(accessToken *AccessToken) error
	SaveRefreshToken(refreshToken *RefreshToken) error
	SaveAuthorizeCode(authCode *AuthorizeCode) error
	IsAvailableScope(scopes []string) (bool, error)
	RevokeRefreshToken(refreshToken string) error
	RevokeAccessToken(accessToken string) error
}

type TokenGenerator

type TokenGenerator interface {
	CreateAccessToken(req *CreateAccessTokenRequest) (string, error)
	CreateRefreshToken() string
	CreateCode() string
}

type User

type User struct {
	ID     string                 `json:"id"`
	Scopes []string               `json:"scopes"`
	Extras map[string]interface{} `json:"extras"`
}

type UserService

type UserService interface {
	GetUser(username, password string) (*User, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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