authtoken

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2017 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	Implementation string
	Path           string
	Prefix         string
	Expiry         int64
	Secret         string
}

Configuration encapsulates arguments to initialize a token store

type FileStore

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

FileStore implements TokenStore by saving users' Token under a directory specified by a string. Each access token is stored in a separate file.

func NewFileStore

func NewFileStore(address string, expiry int64) *FileStore

NewFileStore creates a file token store.

It panics when it fails to create the directory.

func (*FileStore) Delete

func (f *FileStore) Delete(accessToken string) error

Delete removes the access token from the file store.

Delete return an error if the token cannot removed. It is NOT not an error if the token does not exist at deletion time.

func (*FileStore) Get

func (f *FileStore) Get(accessToken string, token *Token) error

Get tries to read the specified access token from file and writes to the supplied Token.

Get returns an NotFoundError if no such access token exists or such access token is expired. In the latter case the expired access token is still written onto the supplied Token.

func (*FileStore) NewToken

func (f *FileStore) NewToken(appName string, authInfoID string) (Token, error)

NewToken creates a new token for this token store.

func (*FileStore) Put

func (f *FileStore) Put(token *Token) error

Put writes the specified token into a file and overwrites existing Token if any.

type JWTStore

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

JWTStore implements TokenStore by encoding user information into the access token string. This store does not keep state.

func NewJWTStore

func NewJWTStore(secret string, expiry int64) *JWTStore

NewJWTStore creates a JWT token store.

func (*JWTStore) Delete

func (r *JWTStore) Delete(accessToken string) error

Delete does nothing because the JWT token store does not store token.

func (*JWTStore) Get

func (r *JWTStore) Get(accessToken string, token *Token) error

Get decodes and verifies the access token for user information. It returns the access token containing information about the user.

func (*JWTStore) NewToken

func (r *JWTStore) NewToken(appName string, authInfoID string) (Token, error)

NewToken creates a new token for this token store.

func (*JWTStore) Put

func (r *JWTStore) Put(token *Token) error

Put does nothing because the JWT token store does not store token.

type NotFoundError

type NotFoundError struct {
	AccessToken string
	Err         error
}

NotFoundError is the error returned by Get if a TokenStore cannot find the requested token or the fetched token is expired.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type RedisStore

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

RedisStore implements TokenStore by saving users' token in a redis server

func NewRedisStore

func NewRedisStore(address string, prefix string, expiry int64) *RedisStore

NewRedisStore creates a redis token store.

address is url to the redis server

prefix is a string prepending to access token key in redis

For example if the token is `cf4bdc65-3fe6-4d40-b7fd-58f00b82c506`
and the prefix is `myApp`, the key in redis should be
`myApp:cf4bdc65-3fe6-4d40-b7fd-58f00b82c506`.

func (*RedisStore) Delete

func (r *RedisStore) Delete(accessToken string) error

Delete removes the access token from redis store.

func (*RedisStore) Get

func (r *RedisStore) Get(accessToken string, token *Token) error

Get tries to read the specified access token from redis store and writes to the supplied Token.

func (*RedisStore) NewToken

func (r *RedisStore) NewToken(appName string, authInfoID string) (Token, error)

NewToken creates a new token for this token store.

func (*RedisStore) Put

func (r *RedisStore) Put(token *Token) error

Put writes the specified token into redis store and overwrites existing Token if any.

type RedisToken

type RedisToken struct {
	AccessToken string `redis:"accessToken"`
	ExpiredAt   int64  `redis:"expiredAt"`
	IssuedAt    int64  `redis:"issuedAt"`
	AppName     string `redis:"appName"`
	AuthInfoID  string `redis:"authInfoID"`
}

RedisToken stores a Token with UnixNano timestamp

func (RedisToken) ToToken

func (r RedisToken) ToToken() *Token

ToToken converts a RedisToken to auth token

type Store

type Store interface {
	NewToken(appName string, authInfoID string) (Token, error)
	Get(accessToken string, token *Token) error
	Put(token *Token) error
	Delete(accessToken string) error
}

Store represents a persistent storage for Token.

func InitTokenStore

func InitTokenStore(config Configuration) Store

InitTokenStore accept a implementation and path string. Return a Store.

type Token

type Token struct {
	AccessToken string    `json:"accessToken" redis:"accessToken"`
	ExpiredAt   time.Time `json:"expiredAt" redis:"expiredAt"`
	AppName     string    `json:"appName" redis:"appName"`
	AuthInfoID  string    `json:"authInfoID" redis:"authInfoID"`
	// contains filtered or unexported fields
}

Token is an expiry access token associated to a AuthInfo.

func New

func New(appName string, authInfoID string, expiredAt time.Time) Token

New creates a new Token ready for use given a authInfoID and expiredAt date. If expiredAt is passed an empty Time, the token does not expire.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired determines whether the Token has expired now or not.

func (Token) IssuedAt

func (t Token) IssuedAt() time.Time

func (Token) MarshalJSON

func (t Token) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Token) ToRedisToken

func (t Token) ToRedisToken() *RedisToken

ToRedisToken converts an auth token to RedisToken

func (*Token) UnmarshalJSON

func (t *Token) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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