auth

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 10 Imported by: 11

README

Auth Package

This Auth package stands as a versatile, standalone module, tailored for seamless integration into any Golang project, promising flexibility and convenience. It isn't confined solely to any particular platform; it offers full freedom to incorporate it into any Golang platform. We have seamlessly integrated it with the spurtCMS admin system, ensuring a smooth integration process.

Features

  • Smooth admin login
  • Generates and returns an authentication token, typically used for user authentication and authorization processes.
  • Validates the authenticity and integrity of a given authentication token.
  • Check if a user login attempt is valid by verifying credentials against stored user information.
  • Parses and decrypts JWT tokens (Json Web Tokens) from the HTTP request's 'authorization' header
  • Verifies the validity of tokens using the provided jwtSecret

Installation

go get github.com/spurtcms/auth
  • Setup the Auth config
config := Config{
		UserId:     1,
		ExpiryTime: 2, // It should be in hours not minutes or seconds
		SecretKey:  "test@123",
		DB:         &gorm.DB{},
	}

	auth := AuthSetup(config)

Usage Example

func main (){

		config := Config{
			UserId:     1,
			ExpiryTime: 2, // It should be in hours not minutes or seconds
			SecretKey:  "test@123",
			DB:         &gorm.DB{},
		}
		
		auth := AuthSetup(config)
		
		//create token - generates new  JWtoken
		token, _ := auth.CreateToken()
		
		//checklogin - verifies user credentials
		token, userid,err :=auth.Checklogin("Username", "Password")
		
		//verifytoken - parses and verifies the token generated
		userid, err :=	auth.VerifyToken("token","secretkey","currenttime")
	}

Getting help

If you encounter a problem with the package,please refer [Please refer (https://www.spurtcms.com/documentation/cms-admin) or you can create a new Issue in this repo (https://github.com/spurtcms/auth/issues).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorPassword       = errors.New("invalid password")
	ErrorToken          = errors.New("invalid token")
	ErrorOtpExpiry      = errors.New("otp expired")
	ErrorConvertTime    = errors.New("could not convert interface to time.Time")
	ErrorMemberLogin    = errors.New("select any one of the config for member login")
	ErrorUnauthorized   = errors.New("Unauthorized")
	ErrorInactive       = errors.New("user disabled please contact admin")
	ErrorInvalidOTP     = errors.New("invalid OTP")
	ErrorInactiveMember = errors.New("inactive member")
	ErrorTokenExpiry    = errors.New("token expired")
)
View Source
var Authmodel authmodel

Functions

func CreateMemberToken added in v0.0.6

func CreateMemberToken(userid, roleId int, secretKey string, loginType string) (string, error)

Create meber token

func GenerateEmail

func GenerateEmail() error

func IsDeleted

func IsDeleted(db *gorm.DB) *gorm.DB

soft delete check

Types

type Action

type Action string
const (
	Create Action = "Create"

	Read Action = "View"

	Update Action = "Update"

	Delete Action = "Delete"

	CRUD Action = "CRUD"
)

type Auth

type Auth struct {
	UserId        int
	ExpiryTime    int
	ExpiryFlg     bool
	SecretKey     string
	DB            *gorm.DB
	AuthFlg       bool
	PermissionFlg bool
	RoleId        int
	RoleName      string
	DataAccess    int
}

func AuthSetup

func AuthSetup(conf Config) *Auth

AuthSetup used initialize auth configruation

func (*Auth) CheckMemberLogin

func (auth *Auth) CheckMemberLogin(memberlogin MemberLoginCheck) (TblMember, error)

func (*Auth) Checklogin

func (auth *Auth) Checklogin(Username string, Password string) (string, int, error)

Check UserName Password - userlogin

func (*Auth) CreateToken

func (auth *Auth) CreateToken() (string, error)

CreateToken creates a token

func (*Auth) GenerateMemberToken added in v0.0.6

func (auth *Auth) GenerateMemberToken(memberid int, loginType string, secretKey string) (token string, err error)

member token

func (*Auth) IsGranted

func (permission *Auth) IsGranted(modulename string, permisison Action) (bool, error)

Check User Permission

func (*Auth) MemberVerifyToken added in v0.0.6

func (auth *Auth) MemberVerifyToken(token string, secret string) (memberid int, groupid int, loginType string, err error)

verify token

func (*Auth) UpdateMemberOTP added in v0.0.6

func (auth *Auth) UpdateMemberOTP(otp OTP) (int, time.Time, error)

update otp

func (*Auth) VerifyToken

func (auth *Auth) VerifyToken(token string, secret string) (userid int, loginType string, err error)

verify token

type Authentication

type Authentication struct {
	Token     string
	SecretKey string
}

type Config

type Config struct {
	UserId       int      //(optional) if you use login function this userid no need
	ExpiryTime   int      //It should be an hour not a mintues, UTC time only
	ExpiryFlg    bool     //if you want to check token expiry time enable expiryflg true otherwise expirytime not check
	SecretKey    string   //jwt secretkey
	DB           *gorm.DB //database connection
	DataBaseType Type
	RoleId       int
	RoleName     string
	OTPAttempt   int //how many times otp attempt
}

type MemberLoginCheck

type MemberLoginCheck struct {
	Username             string
	Email                string
	Password             string
	OTP                  int
	UsernameWithOTP      bool
	EmailWithOTP         bool
	UsernameWithPassword bool
	EmailwithPassword    bool
}

type OTP added in v0.0.6

type OTP struct {
	Length   int
	Duration time.Duration //minutes only
	MemberId int
}

type TblMember

type TblMember struct {
	Id               int
	Uuid             string
	FirstName        string
	LastName         string
	Email            string
	MobileNo         string
	IsActive         int
	ProfileImage     string
	ProfileImagePath string
	LastLogin        int
	MemberGroupId    int
	Password         string
	Username         string
	Otp              int
	OtpExpiry        time.Time
	LoginTime        time.Time
	IsDeleted        int
	DeletedOn        time.Time
	DeletedBy        int
	CreatedOn        time.Time
	CreatedBy        int
	ModifiedOn       time.Time
	ModifiedBy       int
}

type TblModule

type TblModule struct {
	Id               int
	ModuleName       string
	IsActive         int
	DefaultModule    int
	ParentId         int
	IconPath         string
	AssignPermission int
	Description      string
	OrderIndex       int
	CreatedBy        int
	CreatedOn        time.Time
	MenuType         string
}

type TblModulePermission

type TblModulePermission struct {
	Id                   int
	RouteName            string
	DisplayName          string
	SlugName             string
	Description          string
	ModuleId             int
	FullAccessPermission int
	ParentId             int
	AssignPermission     int
	BreadcrumbName       string
	OrderIndex           int
	CreatedBy            int
	CreatedOn            time.Time
	ModifiedBy           int
	ModifiedOn           time.Time
}

type TblRolePermission

type TblRolePermission struct {
	Id           int
	RoleId       int
	PermissionId int
	CreatedBy    int
	CreatedOn    time.Time
}

type Type

type Type string
const (
	Postgres Type = "postgres"
	Mysql    Type = "mysql"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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