authsolo

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 6 Imported by: 0

README

authsolo

authsolo

Build Status codecov Go Report Card

Authsolo is a user-less authentication middleware. It provides basic auth flow for your application, and is compatible with the standard net/http Handler.

There are no users, sessions or JWT involved. With Authsolo you authenticate with just one password.

Install

go get github.com/tunedmystic/authsolo

Usage

Using the middleware is simple.


  1. First create the middleware with the master password
auth := authsolo.New("mypassword")

  1. Then, wrap your handler functions you want protected with .Solo method.
r.HandleFunc("/admin", auth.Solo(handleAdmin))

  1. Lastly, wrap the router with .Handler. method This will inject authsolo's custom login and logout handlers.
http.ListenAndServe(":8000", auth.Handler(r))

Example

A simple example using the Authsolo middleware.

// main.go
package main

import (
	"fmt"
	"net/http"

	"github.com/tunedmystic/authsolo"
)

func handleIndex(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "the index page")
}

func handleAccount(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "the account page")
}

func main() {
	r := http.NewServeMux()
	auth := authsolo.New("mypassword") // 1. create middleware

	r.HandleFunc("/", handleIndex)
	r.HandleFunc("/account", auth.Solo(handleAccount)) // 2. protect handlers

	http.ListenAndServe(":8000", auth.Handler(r)) // 3. inject internal handlers
}

License

MIT Licensed. See the included LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth is a middleware that provides password authentication. It also contains custom handlers (login, logout) for basic auth flow.

func New

func New(password string) *Auth

New creates a new Auth instance with the supplied password.

func (*Auth) HandleLogin

func (a *Auth) HandleLogin(w http.ResponseWriter, r *http.Request)

HandleLogin handles the logic for logging in a user. On GET, login form is shown. On POST, cookie is generated if passwords match.

func (*Auth) HandleLogout

func (a *Auth) HandleLogout(w http.ResponseWriter, r *http.Request)

HandleLogout handles the logic for logging out a user. The cookie is cleared and it redirects to the configured loginURL.

func (*Auth) Handler

func (a *Auth) Handler(h http.Handler) http.Handler

Handler wraps an http.Handler and injects the internal handlers for logging in and logging out.

func (*Auth) IsAuthenticated

func (a *Auth) IsAuthenticated(r *http.Request) bool

IsAuthenticated checks if a user is logged in. It checks an http.Request to see if the cookie's hash matches the one stored in the middleware.

func (*Auth) Login

func (a *Auth) Login(w http.ResponseWriter, hashedPw string)

Login creates a cookie with the hashed password, and sets it on the ResponseWriter.

func (*Auth) LoginFormHTML

func (a *Auth) LoginFormHTML(r *http.Request) string

LoginFormHTML creates and returns a login form with the resolved loginSuccessURL.

func (*Auth) Logout

func (a *Auth) Logout(w http.ResponseWriter)

Logout removes the cookie from the ResponseWriter.

func (*Auth) Solo

func (a *Auth) Solo(next http.HandlerFunc) http.HandlerFunc

Solo provides the auth functionality for an http.HandlerFunc.

func (*Auth) SoloH

func (a *Auth) SoloH(next http.Handler) http.Handler

SoloH provides the auth functionality for an http.Handler. If user is authenticated, then allow the `next` handler to execute. If user is NOT authenticated, then redirect to the login page.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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