auth

package module
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: MIT Imports: 22 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 (
	AWSID     string
	AWSKEY    string
	AWSREGION string
	AWSBUCKET string
)
View Source
var Authmodel authmodel

Functions

func CreateFolderToS3 added in v0.0.21

func CreateFolderToS3(foldername string, folderpath string, auth *Auth) (folderPath string, err error)

func CreateMemberToken added in v0.0.6

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

Create meber token

func CreateS3Session added in v0.0.21

func CreateS3Session(Db *gorm.DB) (ses *s3.S3, err error)

func GenerateEmail

func GenerateEmail() error

func IsDeleted

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

soft delete check

func SetS3value added in v0.0.21

func SetS3value(Db *gorm.DB) error

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, tenantid int) (TblMember, error)

func (*Auth) CheckOtpLogin added in v0.0.29

func (auth *Auth) CheckOtpLogin(email string) (Tbluser, error)

func (*Auth) CheckWebAuth added in v0.0.18

func (auth *Auth) CheckWebAuth(login *SocialLogin) (string, Tbluser, bool, error)

func (*Auth) Checklogin

func (auth *Auth) Checklogin(Username string, Password string, tenantid int) (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, tenantid int) (token string, err error)

member token

func (*Auth) IsGranted

func (permission *Auth) IsGranted(modulename string, permisison Action, tenantid int) (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) OtpLoginVerification added in v0.0.17

func (auth *Auth) OtpLoginVerification(otp int, email string, tenantid int) (Tbluser, string, bool, error)

func (*Auth) UpdateMemberOTP added in v0.0.6

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

update otp

func (*Auth) UpdateUserOTP added in v0.0.17

func (auth *Auth) UpdateUserOTP(user Tbluser) (Tbluser, error)

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 SocialLogin added in v0.0.18

type SocialLogin struct {
	Email     string `json:"email"`
	Name      string `json:"name"`
	GivenName string `json:"given_name"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
}

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
	TenantId         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 TblMstrTenant added in v0.0.18

type TblMstrTenant struct {
	Id        int       `gorm:"primaryKey;auto_increment;type:serial"`
	TenantId  int       `gorm:"type:integer"`
	DeletedOn time.Time `gorm:"type:timestamp without time zone;DEFAULT:NULL"`
	DeletedBy int       `gorm:"type:integer;DEFAULT:NULL"`
	IsDeleted int       `gorm:"type:integer;DEFAULT:0"`
}

type TblRolePermission

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

type TblStorageType added in v0.0.21

type TblStorageType struct {
	Id           int
	Local        string
	Aws          datatypes.JSONMap `gorm:"type:jsonb"`
	Azure        datatypes.JSONMap `gorm:"type:jsonb"`
	Drive        datatypes.JSONMap `gorm:"type:jsonb"`
	SelectedType string
}

func GetSelectedType added in v0.0.21

func GetSelectedType(Db *gorm.DB) (storageType TblStorageType, err error)

type Tblrole added in v0.0.18

type Tblrole struct {
	Id          int       `gorm:"column:id"`
	Name        string    `gorm:"column:name"`
	Description string    `gorm:"column:description"`
	Slug        string    `gorm:"column:slug"`
	IsActive    int       `gorm:"column:is_active"`
	IsDeleted   int       `gorm:"column:is_deleted"`
	CreatedOn   time.Time `gorm:"column:created_on"`
	CreatedBy   int       `gorm:"column:created_by"`
	ModifiedOn  time.Time `gorm:"column:modified_on;DEFAULT:NULL"`
	ModifiedBy  int       `gorm:"column:modified_by;DEFAULT:NULL"`
	CreatedDate string    `gorm:"-:migration;<-:false"`
	User        []Tbluser `gorm:"-"`
	TenantId    int       `gorm:"column:tenant_id;DEFAULT:NULL"`
}

type Tbluser added in v0.0.17

type Tbluser struct {
	Id                   int       `gorm:"column:id"`
	Uuid                 string    `gorm:"column:uuid"`
	FirstName            string    `gorm:"column:first_name"`
	LastName             string    `gorm:"column:last_name"`
	RoleId               int       `gorm:"column:role_id"`
	Email                string    `gorm:"column:email"`
	Username             string    `gorm:"column:username"`
	Password             string    `gorm:"column:password"`
	MobileNo             string    `gorm:"column:mobile_no"`
	IsActive             int       `gorm:"column:is_active"`
	ProfileImage         string    `gorm:"column:profile_image"`
	ProfileImagePath     string    `gorm:"column:profile_image_path"`
	StorageType          string    `gorm:"column:storage_type"`
	DataAccess           int       `gorm:"column:data_access"`
	CreatedOn            time.Time `gorm:"column:created_on"`
	CreatedBy            int       `gorm:"column:created_by"`
	ModifiedOn           time.Time `gorm:"column:modified_on;DEFAULT:NULL"`
	ModifiedBy           int       `gorm:"column:modified_by;DEFAULT:NULL"`
	LastLogin            time.Time `gorm:"column:last_login;DEFAULT:NULL"`
	IsDeleted            int       `gorm:"column:is_deleted"`
	DeletedOn            time.Time `gorm:"column:deleted_on;DEFAULT:NULL"`
	DeletedBy            int       `gorm:"column:deleted_by;DEFAULT:NULL"`
	ModuleName           string    `gorm:"-"`
	RouteName            string    `gorm:"-:migration;<-:false"`
	DisplayName          string    `gorm:"-:migration;<-:false"`
	Description          string    `gorm:"-"`
	ModuleId             int       `gorm:"-:migration;<-:false"`
	PermissionId         int       `gorm:"-"`
	FullAccessPermission int       `gorm:"-:migration;<-:false"`
	RoleName             string    `gorm:"-:migration;<-:false"`
	DefaultLanguageId    int       `gorm:"column:default_language_id"`
	NameString           string    `gorm:"-"`
	TenantId             int
	Otp                  int        `gorm:"column:otp"`
	OtpExpiry            *time.Time `gorm:"column:otp_expiry"`
}

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