biu: github.com/tuotoo/biu/auth Index | Examples | Files

package auth

import "github.com/tuotoo/biu/auth"

Index

Examples

Package Files

auth.go

Variables

var DefaultInstance = &Instance{
    Timeout: time.Minute * 5,
    SecretFunc: func(userID string) (secret []byte, err error) {
        return []byte("secret"), nil
    },
    RefreshTimeout: time.Hour * 24 * 7,
}

func CheckToken Uses

func CheckToken(token string) (userID string, err error)

CheckToken accept a jwt token and returns the uid in token with default instance.

func JWTRefreshTimeout Uses

func JWTRefreshTimeout(timeout time.Duration)

func JWTSecret Uses

func JWTSecret(f func(string) ([]byte, error))

func JWTTimeout Uses

func JWTTimeout(timeout time.Duration)

func ParseToken Uses

func ParseToken(token string) (*jwt.Token, error)

ParseToken parse a token string with default instance.

func RefreshToken Uses

func RefreshToken(token string) (newToken string, err error)

RefreshToken accepts a valid token and returns a new token with new expire time.

func Sign Uses

func Sign(userID string) (token string, err error)

Sign returns a signed jwt string with default instance.

Code:

auth.JWTTimeout(4 * time.Second)
auth.JWTSecret(func(userID string) (secret []byte, err error) {
    return []byte("hello world"), nil
})
auth.JWTRefreshTimeout(5 * time.Second)
token, _ := auth.Sign("user")
ctx := &box.Ctx{
    Request: &restful.Request{
        Request: &http.Request{
            Header: map[string][]string{
                "Authorization": {token},
            },
        },
    },
}
u1, err := ctx.IsLogin()
if err != nil {
    panic(err)
}
fmt.Println(u1)
u2, err := auth.CheckToken(token)
if err != nil {
    panic(err)
}
fmt.Println(u2)
time.Sleep(time.Second * 2)
newToken, err := auth.RefreshToken(token)
if err != nil {
    panic(err)
}
_, err = auth.CheckToken(newToken)
if err != nil {
    panic(err)
}

time.Sleep(time.Second * 3)
// token is expired, newToken is still valid
_, err = ctx.IsLogin()
fmt.Println(err != nil)
_, err = auth.CheckToken(token)
fmt.Println(err != nil)
_, err = auth.CheckToken(newToken)
if err != nil {
    panic(err)
}
time.Sleep(time.Second)
// cant refresh token if refresh timeout is reached
_, err = auth.RefreshToken(newToken)
fmt.Println(err != nil)

ctx2 := &box.Ctx{
    Request: &restful.Request{
        Request: &http.Request{
            Header: map[string][]string{
                "Authorization": {"wtf"},
            },
        },
    },
}
_, err = ctx2.IsLogin()
fmt.Println(err != nil)

Output:

user
user
true
true
true
true

type Instance Uses

type Instance struct {
    Timeout        time.Duration
    RefreshTimeout time.Duration
    SecretFunc     func(string) ([]byte, error)
}

func (*Instance) CheckToken Uses

func (i *Instance) CheckToken(token string) (userID string, err error)

CheckToken accept a jwt token and returns the uid in token.

func (*Instance) ParseToken Uses

func (i *Instance) ParseToken(token string) (*jwt.Token, error)

ParseToken parse a token string.

func (*Instance) RefreshToken Uses

func (i *Instance) RefreshToken(token string) (newToken string, err error)

RefreshToken accepts a valid token and returns a new token with new expire time.

func (*Instance) Sign Uses

func (i *Instance) Sign(userID string) (token string, err error)

Sign returns a signed jwt string.

Package auth imports 4 packages (graph) and is imported by 2 packages. Updated 2020-02-29. Refresh now. Tools for package owners.