LibAuth for Go - The modern authentication framework that feels as light as a library.
Go to file
AJ ONeal 1dece66bee
feat: update error messages
2023-04-04 17:22:57 -06:00
.well-known feat(example): add example server 2022-05-09 14:00:21 -06:00
chiauth feat: update error messages 2023-04-04 17:22:57 -06:00
examples feat: add more convenience methods 2022-05-09 17:08:38 -06:00
vendor chore: go mod vendor 2022-05-05 18:17:04 -06:00
.gitignore Initial commit 2022-05-05 11:49:31 -06:00
.prettierrc.json chore: add Prettier config (for Markdown, JSON) 2022-05-09 14:00:22 -06:00
LICENSE Initial commit 2022-05-05 11:49:31 -06:00
README.md feat(chiauth): add GetJWS(r) helper 2022-05-09 15:14:12 -06:00
_config.yaml build: include .well-known in GitHub Pages 2022-05-09 14:00:22 -06:00
go.mod chore: go mod vendor 2022-05-05 18:17:04 -06:00
go.sum chore: go mod vendor 2022-05-05 18:17:04 -06:00
libauth.go feat: update error messages 2023-04-04 17:22:57 -06:00

README.md

libauth

LibAuth for Go - A modern authentication framework that feels as light as a library.

godoc_button

Example Usage

How to verify a valid, trusted token as chi middleware:

package main

import (
	"net/http"

	"github.com/go-chi/chi/v5"

	"git.rootprojects.org/root/keypairs/keyfetch"
	"git.rootprojects.org/root/libauth"
	"git.rootprojects.org/root/libauth/chiauth"
)

func main() {
	r := chi.NewRouter()

	whitelist, err := keyfetch.NewWhitelist([]string{"https://therootcompany.github.io/libauth/"})
	if nil != err {
		panic(err)
	}
	tokenVerifier := chiauth.NewTokenVerifier(chiauth.VerificationParams{
		Issuers:  whitelist,
		Optional: false,
	})
	r.Use(tokenVerifier)

	r.Post("/api/users/profile", func(w http.ResponseWriter, r *http.Request) {
		jws := chiauth.GetJWS(r)
		if nil == jws || !jws.Trusted {
			http.Error(w, "Unauthorized", http.StatusUnauthorized)
			return
		}

		userID := jws.Claims["sub"].(string)
		// ...
	})

    // ...
}

How to create a demo token with [keypairs][https://webinstall.dev/keypairs]:

my_key='./examples/privkey.ec.jwk.json'
my_claims='{
    "iss": "https://therootcompany.github.io/libauth/",
    "sub": "1",
    "email_verified": false,
    "email": "jo@example.com"
}'

keypairs sign \
    --exp 1h \
    "${my_key}" \
    "${my_claims}" \
    > jwt.txt
    2> jws.json

How to pass an auth token:

pushd ./examples
go run ./server.go
my_token="$(cat ./examples/jwt.txt)"

curl -X POST http://localhost:3000/api/users/profile \
    -H "Authorization: Bearer ${my_token}" \
    -H 'Content-Type: application/json' \
    --data-binary '{ "foo": "bar" }'

Example OIDC Discovery URLs