wrrn / token

A simple token authentication middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Token

GoDoc Build Status Coverage

Token is a simple middleware that reads a token from the basic auth header. It assumes that the token is stored in the user name field.

Usage

The key function to the tokens package is ValidateTokens(). ValidateTokens() takes anything that implements TokenValidator.

	var (
		validator = ValidatorFunc(func(token string) bool {
			return token == "SECRET"
		})
		handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			w.WriteHeader(http.StatusOK)
		})
	)

	// Verifies token == "SECRET" before calling to handler
	validatedHandler := ValidateTokens(validator, handler)

	log.Fatal(http.ListenAndServe(":8080", validatedHandler))

ValidatorFunc

Use a function as the TokenValidator by casting a function to the appropriate signature.

 ValidateTokens(func(token string) bool {
     return token == "SECRET"
 }, handler)

About

A simple token authentication middleware

License:MIT License


Languages

Language:Go 100.0%