handlers

package
v0.0.0-...-90a46cf Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2019 License: Unlicense Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthorizeHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	username := r.PostFormValue("username")
	password := r.PostFormValue("password")

	var user model.User
	err := user.Find(username)

	if err != nil {
		errorData := make(map[string]string)
		errorData["username"] = "User with given credentials not fond"
		response, _ := json.Marshal(response.UserNotFoundOrPasswordIsWrong{ErrorResponse: response.ErrorResponse{Errors: errorData}})
		w.Write(response)
		return
	}

	err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
	if err != nil {
		errorData := make(map[string]string)
		errorData["username"] = "User with given credentials not fond"
		response, _ := json.Marshal(response.UserNotFoundOrPasswordIsWrong{ErrorResponse: response.ErrorResponse{Errors: errorData}})
		w.Write(response)
		return
	}

	fmt.Println(user)
	token := jwt.New(jwt.SigningMethodHS256)

	mapClaims := make(jwt.MapClaims)
	mapClaims["exp"] = time.Now().Add(time.Minute * 30).Unix()
	mapClaims["user"] = user

	token.Claims = mapClaims

	tokenString, _ := token.SignedString(mySigningKey)

	response, _ := json.Marshal(response.UserLoggedIn{
		SuccessResponse: response.SuccessResponse{
			Status: "OK",
		},
		Data: response.UserData{
			User:  user,
			Token: tokenString,
		},
	})
	w.Write(response)
})
View Source
var GetTokenHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	username := r.PostFormValue("username")
	password := r.PostFormValue("password")

	var user model.User
	err := user.Find(username)

	if err != nil {
		pwd, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
		if err != nil {
			log.Fatal(err)
		}

		user.Username = username
		user.Password = string(pwd)
		user.Roles = []string{"admin", "user"}

		err = user.Create()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("user created")
		return
	}

	err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(user)
	token := jwt.New(jwt.SigningMethodHS256)

	mapClaims := make(jwt.MapClaims)
	mapClaims["exp"] = time.Now().Add(time.Minute * 30).Unix()
	mapClaims["user"] = user

	token.Claims = mapClaims

	tokenString, _ := token.SignedString(mySigningKey)

	w.Write([]byte(tokenString))
})
View Source
var Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	user := User(r)
	log.Println(user.Username)

	w.Write([]byte("Implemented"))
})
View Source
var JwtMiddleware = jwtmiddleware.New(jwtmiddleware.Options{
	ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
		return mySigningKey, nil
	},
	SigningMethod: jwt.SigningMethodHS256,
})
View Source
var NotImplemented = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Not Implemented"))
})

Functions

func User

func User(r *http.Request) (user model.User)

Types

This section is empty.

Jump to

Keyboard shortcuts

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