user

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 12 Imported by: 9

Documentation

Index

Constants

View Source
const (
	// CookieName is the name of the jwt session cookie
	CookieName = "session_jwt"
)

Variables

View Source
var Secret string

Secret holds the hmac secret, is set from main

Functions

func Auth

func Auth(authenticated bool) gin.HandlerFunc

Auth is a gin middleware that checks for session cookie and handles permissions

func CheckDuplicate

func CheckDuplicate(name string) (check bool)

CheckDuplicate will check for duplicate name before registering

func CreateCookie added in v1.3.0

func CreateCookie(token string) *http.Cookie

CreateCookie will make a cookie for the JWT

func DeleteCookie added in v1.3.0

func DeleteCookie() *http.Cookie

DeleteCookie will delete the JWT cookie

func HashPassword

func HashPassword(password string) (hash []byte, err error)

HashPassword will create a bcrypt hash from the given password

func IsValidName

func IsValidName(name string) bool

IsValidName checks if the name is valid

func MakeToken added in v1.3.0

func MakeToken(secret string, uid uint) (newtoken string, err error)

MakeToken will create a JWT token

func Protect

func Protect() gin.HandlerFunc

Protect will check to see if a user has the correct permissions A route protected by this middleware needs an ib parameter

func RandomPassword added in v1.3.0

func RandomPassword() (password string, hash []byte, err error)

RandomPassword will generate a random password for password resets

func UpdatePassword added in v1.3.0

func UpdatePassword(hash []byte, uid uint) (err error)

UpdatePassword will update the user password hash in database

Types

type Authenticator

type Authenticator interface {
	IsValid() bool
	IsAuthorized(ib uint) bool
	SetID(uid uint)
	SetAuthenticated()
	Password() (err error)
	ComparePassword(password string) bool
	FromName(name string) (err error)
	CreateToken() (newtoken string, err error)
}

Authenticator defines the methods for authentication

type TokenClaims added in v1.4.0

type TokenClaims struct {
	User uint `json:"user_id"`
	jwt.RegisteredClaims
}

TokenClaims holds the custom and standard claims for the JWT token

type User

type User struct {
	ID              uint
	Name            string
	IsAuthenticated bool
	// contains filtered or unexported fields
}

User data struct

func DefaultUser

func DefaultUser() User

DefaultUser creates an anonymous user struct

func (*User) ComparePassword

func (u *User) ComparePassword(password string) bool

ComparePassword will compare the supplied password to the hash from the database

func (*User) CreateToken

func (u *User) CreateToken() (newtoken string, err error)

CreateToken will make a JWT token associated with a user

func (*User) FromName

func (u *User) FromName(name string) (err error)

FromName will get the password and user id from the database for a user name

func (*User) IsAuthorized

func (u *User) IsAuthorized(ib uint) bool

IsAuthorized will get the perms and role info from the userid

func (*User) IsValid

func (u *User) IsValid() bool

IsValid will check user struct validity

func (*User) Password

func (u *User) Password() (err error)

Password will get the password and name from the database for an instantiated user

func (*User) SetAuthenticated

func (u *User) SetAuthenticated()

SetAuthenticated sets a user as authenticated

func (*User) SetID added in v1.3.0

func (u *User) SetID(uid uint)

SetID sets the user id

Jump to

Keyboard shortcuts

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