aitm

package module
v0.0.0-...-7d1bfc8 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: BSD-2-Clause Imports: 11 Imported by: 2

README

Auth in the Middle

This package provides a simple, mostly secure cookie based auth middleware for http.Server.

Usage

aitm.Server is meant to wrap around an existing http.Server and broker requests, only passing them to the 'child' http.Handler when the user has authenticated.

At the moment, the only supported way of storing the user databse is in a json file, the json file is expected to be a marshaled slice of the User struct defined in aitm.go.

User information can be grabbed in the client http.Handler through the use of the context.Context struct accessible from http.Request.Context(). Specifically, the Token struct is stored in the request on successful auth with a key of TokenContextKey{}.

Installation

go get github.com/majiru/aitm

Example

import (
	"fmt"
	"log"
	"net/http"

	"github.com/majiru/aitm"
)

func main() {
	srv := aitm.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t := r.Context().Value(TokenContextKey{}).(*aitm.Token)
		fmt.Fprintf(w, "Welcome user %s\n", t.Username)
	}))
	f, _ := os.Open("path/to/db.json")
	srv.LoadUsers(f)
	f.Close()
	srv.Addr = ":8080"
	log.Fatal(srv.ListenAndServe())
}

Implementation

The passwords are hashed and salted using bcrypt. The session cookies are identified based on the github.com/google/uuid package. Each cookie lives for a maximum of 1 day by default.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	*sync.RWMutex
	*http.Server

	Whitelist []string
	// contains filtered or unexported fields
}

func NewServer

func NewServer(h http.Handler) *Server

func WrapServer

func WrapServer(s *http.Server) *Server

func (*Server) LoadUsers

func (s *Server) LoadUsers(f io.Reader) error

func (*Server) NewMux

func (s *Server) NewMux() *http.ServeMux

type Token

type Token struct {
	time.Time
	IP       string
	Username string
}

type TokenContextKey

type TokenContextKey struct{}

type User

type User struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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