auth

package module
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 9 Imported by: 2

README

simpauth

For online user in http

Usage


import auth "github.com/liut/simpauth"

authoriz := auth.New(
		auth.WithCookie("myname", "/", "mydomain.net"), // custom cookie: name, path, domain
		auth.WithMaxAge(1800),                       // set maxage
		auth.WithRefresh(),                          // auto refresh cookie value
	)

func login(w http.ResponseWriter, r *http.Request) {
	// do login check
	var user = &User{
		UID:  "eagle",
		Name: "liut",
	}
	user.Refresh()
	authoriz.Signin(user, w)
}

func welcome(w http.ResponseWriter, r *http.Request) {
	user, ok := auth.UserFromContext(r.Context())
	if !ok {
		log.Print("userFromContext fail")
		w.WriteHeader(401)
		return
	}
	log.Printf("user %v", user.Name)
}

run() {
	mw := authoriz.Middleware()
	handler := mw(http.HandlerFunc(welcome)))

	// other routers
}

Documentation

Index

Constants

View Source
const (
	UserKey ctxKey = iota
)

consts

Variables

View Source
var (
	UserLifetime int64 = 3600
	Guest              = &User{}
)

vars

View Source
var (
	ErrNoTokenInRequest = errors.New("no token present in request")
)

vars

Functions

func ContextWithUser added in v0.1.1

func ContextWithUser(ctx context.Context, user *User) context.Context

ContextWithUser ...

func Middleware added in v0.1.0

func Middleware(opts ...OptFunc) func(next http.Handler) http.Handler

Middleware ...

func Signin

func Signin(user Encoder, w http.ResponseWriter) error

Signin write user encoded string into cookie, Deprecated

func Signout

func Signout(w http.ResponseWriter)

Signout setcookie with empty, Deprecated

func TokenFrom added in v0.1.3

func TokenFrom(args ...interface{}) string

TokenFrom return token string valid interfaces: *http.Request, Request.Header, *fiber.Ctx

func WithRedirect

func WithRedirect(uri string) func(next http.Handler) http.Handler

WithRedirect ... Deprecated by Middleware(WithURI(uri))

Types

type Authorizer added in v0.1.4

type Authorizer interface {
	Middleware() func(next http.Handler) http.Handler
	MiddlewareWordy(redir bool) func(next http.Handler) http.Handler
	UserFromRequest(r *http.Request) (user *User, err error)
	TokenFromRequest(r *http.Request) (s string, err error)
	TokenFrom(args ...interface{}) string
	Cooking(value string) *http.Cookie
	Signin(user Encoder, w http.ResponseWriter) error
	Signout(w http.ResponseWriter)
	With(opts ...OptFunc)
}

Authorizer ...

func Default added in v0.1.7

func Default() Authorizer

Default return default instance

func New added in v0.1.4

func New(opts ...OptFunc) Authorizer

New build option with args

func NewOption added in v0.1.1

func NewOption(opts ...OptFunc) Authorizer

NewOption ..., Deprecated by New()

type Cookier added in v0.1.3

type Cookier interface {
	Cookie(k string) (*http.Cookie, error)
}

Cookier ex: http.Request

type Encoder added in v0.1.4

type Encoder interface {
	Encode() (string, error)
}

Encoder ...

type FormValuer added in v0.1.3

type FormValuer interface {
	FormValue(k string) string
}

FormValuer ex: http.Request

type Getter added in v0.1.3

type Getter interface {
	Get(k string) string
}

Getter ex: Request.Header

type Names

type Names []string

Names ...

func (Names) Has

func (z Names) Has(name string) bool

Has ...

func (Names) MarshalMsg

func (z Names) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Names) Msgsize

func (z Names) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Names) UnmarshalMsg

func (z *Names) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type OptFunc added in v0.1.0

type OptFunc func(opt *option)

OptFunc ...

func WithCookie added in v0.1.4

func WithCookie(name string, args ...string) OptFunc

WithCookie set cookie 1-3 options: name, path, domain, see also http.Cookie

func WithMaxAge added in v0.1.4

func WithMaxAge(age int) OptFunc

WithMaxAge set cookie max age: >= 0, default 3600, see also http.Cookie

func WithRefresh added in v0.1.0

func WithRefresh() OptFunc

WithRefresh The option with auto refresh

func WithURI added in v0.1.0

func WithURI(uri string) OptFunc

WithURI The option with redirect uri

type User

type User struct {
	UID       string `json:"uid" msg:"u"`
	Name      string `json:"name" msg:"n"`
	Avatar    string `json:"avatar,omitempty" msg:"a"`
	LastHit   int64  `json:"hit,omitempty" msg:"h"`
	TeamID    int64  `json:"tid,omitempty" msg:"t"`
	Roles     Names  `json:"roles,omitempty" msg:"r"`
	Watchings Names  `json:"watching,omitempty" msg:"w"`
}

User 在线用户

func UserFromContext

func UserFromContext(ctx context.Context) (*User, bool)

UserFromContext ...

func UserFromRequest

func UserFromRequest(r *http.Request) (user *User, err error)

UserFromRequest get user from cookie, Deprecated

func (*User) Decode

func (u *User) Decode(s string) (err error)

Decode ...

func (User) Encode

func (u User) Encode() (s string, err error)

Encode ...

func (User) GetName added in v0.1.10

func (u User) GetName() string

func (User) GetUID added in v0.1.10

func (u User) GetUID() string

func (*User) IsExpired

func (u *User) IsExpired() bool

IsExpired ...

func (*User) MarshalMsg

func (z *User) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*User) Msgsize

func (z *User) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*User) NeedRefresh

func (u *User) NeedRefresh() bool

NeedRefresh ...

func (*User) Refresh

func (u *User) Refresh()

Refresh lastHit to time Unix

func (*User) Signin

func (user *User) Signin(w http.ResponseWriter) error

Signin call Signin for login

func (*User) UnmarshalMsg

func (z *User) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

Jump to

Keyboard shortcuts

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