Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kataras/basicauth
The most advanced and powerful Go HTTP Basic Authentication middleware.
https://github.com/kataras/basicauth
basic-authentication basicauthentication go golang
Last synced: 2 months ago
JSON representation
The most advanced and powerful Go HTTP Basic Authentication middleware.
- Host: GitHub
- URL: https://github.com/kataras/basicauth
- Owner: kataras
- License: mit
- Created: 2020-11-23T22:36:21.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T11:21:10.000Z (8 months ago)
- Last Synced: 2024-10-14T11:24:11.079Z (3 months ago)
- Topics: basic-authentication, basicauthentication, go, golang
- Language: Go
- Homepage:
- Size: 45.9 KB
- Stars: 13
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Basic Authentication
[![build status](https://img.shields.io/github/actions/workflow/status/kataras/basicauth/ci.yml?style=for-the-badge)](https://github.com/kataras/basicauth/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/basicauth) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/basicauth)
The most advanced and powerful Go HTTP middleware to handle basic authentication. It is fully compatible with the [net/http](https://pkg.go.dev/net/http) package and third-party frameworks.
In the context of an HTTP transaction, basic access authentication is a method for an HTTP user agent (e.g. a web browser) to provide a user name and password when making a request [RFC 7617](https://tools.ietf.org/html/rfc7617).
> Looking for JWT? Navigate through [kataras/jwt](https://github.com/kataras/jwt) instead.
## Installation
The only requirement is the [Go Programming Language](https://go.dev/dl/).
```sh
$ go get github.com/kataras/basicauth
```Please star this open source project to attract more developers so that together we can improve it even more!
### Examples
- [Basic](_examples/basic/main.go)
- [Load from a slice of Users](_examples/users_list/main.go)
- [Load from a file & encrypted passwords](_examples/users_file_bcrypt)
- [Fetch & validate a User from a Database (MySQL)](_examples/database)## Getting Started
Import the package:
```go
import "github.com/kataras/basicauth"
```Initialize the middleware with a simple map of username:password (see [Options](https://pkg.go.dev/github.com/kataras/basicauth#Options) type and [New](https://pkg.go.dev/github.com/kataras/basicauth#New) function for real-world scenarios):
```go
auth := basicauth.Default(map[string]string{
"admin": "admin",
"my_username": "my_password",
})
```Wrap any `http.Handler` with the `auth` middleware, e.g. `*http.ServeMux`:
```go
mux := http.NewServeMux()
// [...routes]http.ListenAndServe(":8080", auth(mux))
```Or register the middleware to a single `http.HandlerFunc` route:
```go
mux.HandleFunc("/", basicauth.HandlerFunc(auth, routeHandlerFunc))
```Access the authenticated User entry:
```go
routeHandlerFunc := func(w http.ResponseWriter, r *http.Request) {
user := basicauth.GetUser(r).(*basicauth.SimpleUser)
// user.Username
// user.Password
}
```> The `*http.Request.BasicAuth()` works too, but it has limitations when it comes to a [custom user struct](_examples/users_list/main.go).
For a more detailed technical documentation you can head over to our [godocs](https://pkg.go.dev/github.com/kataras/basicauth).
## License
This software is licensed under the [MIT License](LICENSE).