Documentation ¶
Overview ¶
Package Token generation and storage management . Simple Usage.
import ( "fmt" "github.com/ysqi/tokenauth" ) func main() { if err := tokenauth.UseDeaultStore(); err != nil { panic(err) } defer tokenauth.Store.Close() // Ready. d := &tokenauth.DefaultProvider{} globalClient := tokenauth.NewAudienceNotStore("globalClient", d.GenerateSecretString) // New token token, err := tokenauth.NewSingleToken("singleID", globalClient, d.GenerateTokenString) if err != nil { fmt.Println("generate token fail,", err.Error()) return } // Check token if checkToken, err := tokenauth.ValidateToken(token.Value); err != nil { fmt.Println("token check did not pass,", err.Error()) } else { fmt.Println("token check pass,token Expiration date:", checkToken.DeadLine) } }
Advanced Usage:
secretFunc := func(clientID string) (secretString string) { return "myself secret for all client" } tokenFunc := func(audience *Audience) string { return "same token string" } globalClient := tokenauth.NewAudienceNotStore("globalClient", secretFunc) // New token t1, err := tokenauth.NewToken(globalClient, tokenFunc) t2, err := tokenauth.NewToken(globalClient, tokenFunc)
Index ¶
- Constants
- Variables
- func ChangeTokenStore(newStore TokenStore) error
- func GenerateRandomString(size int, encodeToBase32 bool) string
- func RegStore(name string, adapter TokenStore)
- func UseDeaultStore() error
- type Audience
- type BoltDBFileStore
- func (store *BoltDBFileStore) Close() error
- func (store *BoltDBFileStore) DBPath() string
- func (store *BoltDBFileStore) DeleteAudience(audienceID string) error
- func (store *BoltDBFileStore) DeleteExpired()
- func (store *BoltDBFileStore) DeleteToken(tokenString string) error
- func (store *BoltDBFileStore) GetAudience(audienceID string) (audience *Audience, err error)
- func (store *BoltDBFileStore) GetToken(tokenString string) (token *Token, err error)
- func (store *BoltDBFileStore) Open(config string) error
- func (store *BoltDBFileStore) SaveAudience(audience *Audience) error
- func (store *BoltDBFileStore) SaveToken(token *Token) error
- type DefaultProvider
- type GenerateSecretString
- type GenerateTokenString
- type ObjectId
- type Token
- type TokenStore
- type ValidationError
Constants ¶
const (
// default secret length.
SecretLength = 32
)
Variables ¶
var ( ERR_InvalidateToken = ValidationError{Code: "40001", Msg: "Invalid token"} ERR_TokenEmpty = ValidationError{Code: "41001", Msg: "Token is empty"} ERR_TokenExpired = ValidationError{Code: "42001", Msg: "Token is expired"} )
var TokenPeriod uint64 = 7200 //2hour
Token effective time,unti: seconds. Defult is 2 Hour.
Functions ¶
func ChangeTokenStore ¶
func ChangeTokenStore(newStore TokenStore) error
Change token store and close old store. New token and New Audience whill be saved to new store,after use new store.
func GenerateRandomString ¶
Returns s random string
func RegStore ¶
func RegStore(name string, adapter TokenStore)
Resister one store provider. If name is empty,will panic. If same name has registerd ,will panic.
func UseDeaultStore ¶
func UseDeaultStore() error
Use default store. Default use bolt db file, "./data/tokendb.bolt" file open or create
Types ¶
type Audience ¶
type Audience struct { Name string ID string // Unique key for audience Secret string //audience secret string,can update. TokenPeriod uint64 //token period ,unit: seconds. }
Audience Info, token rely on audience. Contains secret string , tokenPeriod for generatating token string.
func NewAudience ¶
func NewAudience(name string, secretFunc GenerateSecretString) (*Audience, error)
New audience and this audience will be saved to store.
func NewAudienceNotStore ¶
func NewAudienceNotStore(name string, secretFunc GenerateSecretString) *Audience
Returns a new audience info,not save to store.
type BoltDBFileStore ¶
type BoltDBFileStore struct { Alias string // contains filtered or unexported fields }
Store implement by boltdb,see:https://github.com/boltdb/bolt
func NewBoltDBFileStore ¶
func NewBoltDBFileStore() *BoltDBFileStore
new Bolt DB file store instance.
func (*BoltDBFileStore) DBPath ¶
func (store *BoltDBFileStore) DBPath() string
func (*BoltDBFileStore) DeleteAudience ¶
func (store *BoltDBFileStore) DeleteAudience(audienceID string) error
Delete audience and all tokens of audience.
func (*BoltDBFileStore) DeleteExpired ¶
func (store *BoltDBFileStore) DeleteExpired()
Delete token if token expired
func (*BoltDBFileStore) DeleteToken ¶
func (store *BoltDBFileStore) DeleteToken(tokenString string) error
Delete token Returns error if delete token fail.
func (*BoltDBFileStore) GetAudience ¶
func (store *BoltDBFileStore) GetAudience(audienceID string) (audience *Audience, err error)
Get audience info or returns error.
func (*BoltDBFileStore) GetToken ¶
func (store *BoltDBFileStore) GetToken(tokenString string) (token *Token, err error)
Get token info if find in store,or return error
func (*BoltDBFileStore) Open ¶
func (store *BoltDBFileStore) Open(config string) error
Init and Open BoltDBF. config is json string. e.g:
{"path":"./data/tokenbolt.db"}
func (*BoltDBFileStore) SaveAudience ¶
func (store *BoltDBFileStore) SaveAudience(audience *Audience) error
Save audience into store. Returns error if error occured during execution.
func (*BoltDBFileStore) SaveToken ¶
func (store *BoltDBFileStore) SaveToken(token *Token) error
Save token to store. return error when save fail. Save token json to store and save the relation of token with client if not single model. The first , token must not empty and effectiveness. Does not consider concurrency.
type DefaultProvider ¶
type DefaultProvider struct {
Name string
}
func (*DefaultProvider) GenerateSecretString ¶
func (d *DefaultProvider) GenerateSecretString(clientID string) (secretString string)
func (*DefaultProvider) GenerateTokenString ¶
func (d *DefaultProvider) GenerateTokenString(audience *Audience) string
type GenerateSecretString ¶
Create Secret provider interface
type GenerateTokenString ¶
Create token string provider interface
type ObjectId ¶
type ObjectId string
ObjectId is a unique ID identifying a BSON value. It must be exactly 12 bytes long. clone from https://github.com/go-mgo/mgo/blob/v2-unstable/bson/bson.go. http://www.mongodb.org/display/DOCS/Object+IDs.
func (ObjectId) Counter ¶
Counter returns the incrementing value part of the id. It's a runtime error to call this method with an invalid id.
func (ObjectId) Machine ¶
Machine returns the 3-byte machine id part of the id. It's a runtime error to call this method with an invalid id.
type Token ¶
type Token struct { ClientID string // Audience.ID SingleID string // Single Token ID Value string // Token string DeadLine int64 // Token Expiration date, time unix. }
Token Info
func NewSingleToken ¶
func NewSingleToken(singleID string, a *Audience, tokenFunc GenerateTokenString) (*Token, error)
New Sign Token and this new token will be saved to store.
func NewToken ¶
func NewToken(a *Audience, tokenFunc GenerateTokenString) (*Token, error)
New Token and this new token will be saved to store.
func ValidateToken ¶
Returns Exist tokenstring or error. If token is exist but expired, then delete token and return TokenExpired error.
type TokenStore ¶
type TokenStore interface { // Init store // Returns error if init fail. Open(config string) error // Close store Close() error // Save audience into store. // Returns error if error occured during execution. SaveAudience(audience *Audience) error // Delete audience and all tokens of audience. DeleteAudience(clientID string) error // Get audience info or returns error. GetAudience(clientID string) (*Audience, error) // Save token to token. // Returns error if save token fail. SaveToken(token *Token) error // Delete token info from store. // Returns error if error occured during execution DeleteToken(tokenString string) error // Get token info from store. // Returns nil if not found token. // Returns error if get token fail. GetToken(tokenString string) (*Token, error) DeleteExpired() }
Token store interface.
var Store TokenStore
Global Token Store . Default use
type ValidationError ¶
Customer error.
func (ValidationError) Error ¶
func (v ValidationError) Error() string