https://github.com/mthaler/http-basic-auth
Example code showing how to implement HTTP basic auth in Go
https://github.com/mthaler/http-basic-auth
golang http-basic-auth
Last synced: 9 months ago
JSON representation
Example code showing how to implement HTTP basic auth in Go
- Host: GitHub
- URL: https://github.com/mthaler/http-basic-auth
- Owner: mthaler
- Created: 2021-06-19T13:07:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-19T13:51:11.000Z (over 4 years ago)
- Last Synced: 2024-11-05T19:05:07.785Z (about 1 year ago)
- Topics: golang, http-basic-auth
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# http-basic-auth
The simplest form of web authentication is HTTP basic authentication. The request contains a header field in the form of Authorization: Basic where credentials is the Base64 encoding of ID and password joined by a single colon :, e.g. `Authorisation: Basic dGVzdDpzZWNyZXQ=`
## Server
The server will check if the `Authorization` header field is present and if the value is a valid username / password combination.
### Examples
The [httpie](https://httpie.io/) is a user-friendly HTTP client that can be used to test the API:
#### Missing `Authorization` header
```bash
$ http http://localhost:8080
HTTP/1.1 401 Unauthorized
Content-Length: 36
Content-Type: application/json
Date: Sat, 19 Jun 2021 13:16:34 GMT
Www-Authenticate: Basic realm="Give username and password"
{
"message": "No basic auth present"
}
```
#### Valid `Authorization` header
```bash
$ http http://localhost:8080 "Authorization:Basic dGVzdDpzZWNyZXQ="
HTTP/1.1 200 OK
Content-Length: 39
Content-Type: application/json
Date: Sat, 19 Jun 2021 13:38:01 GMT
{
"message": "welcome to golang world!"
}
```
`dGVzdDpzZWNyZXQ=` is the Base64 encoded `test:secret` string.
## Client
The client demonstrates how to set the `Authorization` header required for HTTP basic authentication.
## Credits
The code is from [How to Implement HTTP Basic Auth in Go](https://blog.umesh.wtf/how-to-implement-http-basic-auth-in-gogolang).