Categorygithub.com/scheibo/a1
modulepackage
0.1.0
Repository: https://github.com/scheibo/a1.git
Documentation: pkg.go.dev

# README

a1

version  Build Status

a1 provides simple authentication and authorization helpers for a single user service in Go.

The generated GoDoc can be viewed at godoc.org/github.com/scheibo/a1.

Install

$ go install github.com/scheibo/a1
$ a1 password
$2a$10$LhB2d.LDKkLZG/fdk0Zie.LuThQcM/.B.rZi/GPH08qf0KVd/svFK

Usage

func handle(auth *a1.Client) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    path := r.URL.Path
    switch path {
    case "/login":
      switch r.Method {
      case "GET":
        auth.LoginPage().ServerHTTP(w, r)
      case "POST":
        auth.Login().ServeHTTP(w, r)
      default:
        httpError(w, 405)
      }
    case "/logout":
      auth.Logout("/").ServeHTTP(w, r)
    default:
      // auth.CheckXSRF(auth.EnsureAuth(...))
    }
  })
}

func main() {
  auth := a1.New(hash)

  srv := &http.Server{
    Addr:         fmt.Sprintf(":%v", port),
    Handler:      a1.RateLimit(10, handle(auth)),
  }
  srv.ListenAndServe()
}

# Packages

a1 provides a CLI for obtaining a password hash which can then be included in an environment variable and used to configure an authenticated server for a single user.

# Functions

Hash returns the hash of a password that should be passed to New and used to authenticate the user.
New takes a hash returned from Hash and returns a new Client which can be used for authenticating users.
RateLimit restricts the qps of a wrapped handler.

# Constants

CookieName used by a1 for authorization.
LoginPath is the default path used for hosting both the LoginPage (GET) and for performing Login (POST).
LogoutPath is the default path for logging out.
RedirectPath is the default path the user is redirected to after a successful Login or Logout.

# Structs

Client holds the state required by a1 to verify a user.