docs: web/auth module
continuous-integration/drone the build was successful Details

This commit is contained in:
Geno 2021-06-01 18:44:09 +02:00
parent a2bea2277b
commit 0902defa8a
4 changed files with 23 additions and 7 deletions

View File

@ -1,10 +1,15 @@
package auth package auth
const ( const (
APIErrorUserNotFound string = "user not found" // APIErrorUserNotFound api error string if user not found
APIErrorUserNotFound string = "user not found"
// APIErrorIncorrectPassword api error string if password is incorrect
APIErrorIncorrectPassword string = "incorrect password" APIErrorIncorrectPassword string = "incorrect password"
APIErrorNoSession string = "no session" // APIErrorNoSession api error string if no session exists
APIErrorCreateSession string = "create session" APIErrorNoSession string = "no session"
// APIErrorCreateSession api error string if session could not created
APIErrorCreateSession string = "create session"
// APIErrroCreatePassword api error string if password could not created
APIErrroCreatePassword string = "error during create password" APIErrroCreatePassword string = "error during create password"
) )

View File

@ -12,6 +12,7 @@ import (
"dev.sum7.eu/genofire/golang-lib/web" "dev.sum7.eu/genofire/golang-lib/web"
) )
// GetCurrentUserID get UserID of session in golang-gin
func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) { func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) {
session := sessions.Default(c) session := sessions.Default(c)
@ -27,6 +28,7 @@ func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) {
return id, true return id, true
} }
// GetCurrentUser get User of session from database in golang-gin
func GetCurrentUser(c *gin.Context, ws *web.Service) (*User, bool) { func GetCurrentUser(c *gin.Context, ws *web.Service) (*User, bool) {
id, ok := GetCurrentUserID(c) id, ok := GetCurrentUserID(c)
if !ok { if !ok {

View File

@ -9,6 +9,7 @@ import (
"dev.sum7.eu/genofire/golang-lib/web" "dev.sum7.eu/genofire/golang-lib/web"
) )
// MiddlewareLogin if user id in session for golang-gin
func MiddlewareLogin(ws *web.Service) gin.HandlerFunc { func MiddlewareLogin(ws *web.Service) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
_, ok := GetCurrentUserID(c) _, ok := GetCurrentUserID(c)
@ -18,9 +19,12 @@ func MiddlewareLogin(ws *web.Service) gin.HandlerFunc {
} }
} }
// MiddlewarePermissionParamUUID if user has access to obj, check access by uuid in golang-gin url param uuid
func MiddlewarePermissionParamUUID(ws *web.Service, obj HasPermission) gin.HandlerFunc { func MiddlewarePermissionParamUUID(ws *web.Service, obj HasPermission) gin.HandlerFunc {
return MiddlewarePermissionParam(ws, obj, "uuid") return MiddlewarePermissionParam(ws, obj, "uuid")
} }
// MiddlewarePermissionParam if user has access to obj, check access in golang-gin url by param
func MiddlewarePermissionParam(ws *web.Service, obj HasPermission, param string) gin.HandlerFunc { func MiddlewarePermissionParam(ws *web.Service, obj HasPermission, param string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
userID, ok := GetCurrentUserID(c) userID, ok := GetCurrentUserID(c)

View File

@ -6,12 +6,14 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
// User struct - default User model which could be extended
type User struct { type User struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid()" example:"88078ec0-2135-445f-bf05-632701c77695"` ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid()" example:"88078ec0-2135-445f-bf05-632701c77695"`
Username string `json:"username" gorm:"unique" example:"kukoon"` Username string `json:"username" gorm:"unique" example:"kukoon"`
Password string `json:"-" example:"super secret password"` Password string `json:"-" example:"super secret password"`
} }
// NewUser by username and password
func NewUser(username, password string) (*User, error) { func NewUser(username, password string) (*User, error) {
user := &User{ user := &User{
Username: username, Username: username,
@ -22,20 +24,23 @@ func NewUser(username, password string) (*User, error) {
return user, nil return user, nil
} }
func (this *User) SetPassword(password string) error { // SetPassword - create new hash of password
func (u *User) SetPassword(password string) error {
p, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) p, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil { if err != nil {
return err return err
} }
this.Password = string(p) u.Password = string(p)
return nil return nil
} }
func (this *User) ValidatePassword(password string) bool { // ValidatePassword - check if given password is equal to saved hash
err := bcrypt.CompareHashAndPassword([]byte(this.Password), []byte(password)) func (u *User) ValidatePassword(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
return err == nil return err == nil
} }
// HasPermission interface for middleware check in other models
type HasPermission interface { type HasPermission interface {
HasPermission(tx *gorm.DB, userID, objID uuid.UUID) (interface{}, error) HasPermission(tx *gorm.DB, userID, objID uuid.UUID) (interface{}, error)
} }