app

package
v0.0.0-...-94d3027 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JwtAuthentication = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		tokenHeader := r.Header.Get("Authorization")
		log.Println("Token: " + tokenHeader + " test.")

		if tokenHeader == "" {
			sendInvalidTokenResponse(w, "Missing auth token")
			return
		}

		tk := &models.Token{}

		token, err := jwt.ParseWithClaims(tokenHeader, tk, func(token *jwt.Token) (interface{}, error) {
			return []byte(os.Getenv("token_password")), nil
		})

		fmt.Println("Parse token error:", err)

		if err != nil {
			sendInvalidTokenResponse(w, "Invalid auth token: "+err.Error())
			return
		}

		if !token.Valid {
			sendInvalidTokenResponse(w, "Token is not valid")
			return
		}

		fmt.Println("User:", tk.UserId)
		ctx := context.WithValue(r.Context(), "user", tk.UserId)
		r = r.WithContext(ctx)
		next.ServeHTTP(w, r)
	})
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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