~mna/webparts-auth0

2af88803f5e8e6f3be6901f8b25d554faa851547 — Martin Angers 4 years ago c6e295f
add example
5 files changed, 82 insertions(+), 2 deletions(-)

M .gitignore
M auth0.go
A example/main.go
M go.mod
M go.sum
M .gitignore => .gitignore +1 -1
@@ 1,5 1,5 @@
# environment files (e.g. managed by direnv) and other secrets
/.env*
.env*

# output files for different tools, e.g. code coverage
/*.out

M auth0.go => auth0.go +2 -1
@@ 77,7 77,8 @@ type Endpoints struct {
}

func New(ctx context.Context, conf *Config) (*Endpoints, error) {
	provider, err := oidc.NewProvider(ctx, "https://"+conf.Domain)
	// final slash is important, that's the issuer returned by auth0
	provider, err := oidc.NewProvider(ctx, "https://"+conf.Domain+"/")
	if err != nil {
		return nil, err
	}

A example/main.go => example/main.go +72 -0
@@ 0,0 1,72 @@
package main

import (
	"context"
	"log"
	"net/http"
	"os"
	"syscall"

	auth0 "git.sr.ht/~mna/webparts-auth0"
	sessions "git.sr.ht/~mna/webparts-sessions"
	"git.sr.ht/~mna/webparts/http/httpssn"
	"git.sr.ht/~mna/webparts/mainer"
)

const localAddr = ":3000"

func main() {
	ctx := context.Background()
	ctx = mainer.CancelOnSignal(ctx, syscall.SIGINT, syscall.SIGTERM)

	store := sessionStore()
	conf := configFromEnvVars(store)
	endpoints, err := auth0.New(ctx, conf)
	if err != nil {
		panic(err)
	}

	mux := http.NewServeMux()
	//mux.Handle("/", renderTpl("home.html"))
	mux.Handle("/login", http.HandlerFunc(endpoints.Login))

	srv := http.Server{
		Addr:    localAddr,
		Handler: mux,
	}
	go func() {
		log.Printf("listening on %s...", localAddr)
		if err := srv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()

	<-ctx.Done()
	log.Printf("stopping server...")
	srv.Close()
}

func sessionStore() httpssn.Store {
	pairs, err := sessions.KeyPairsFromFile(".env_keypairs")
	if err != nil {
		panic(err)
	}

	conf := &sessions.Config{
		KeyPairs: pairs,
		HTTPOnly: true,
	}
	return sessions.New(conf)
}

func configFromEnvVars(store httpssn.Store) *auth0.Config {
	return &auth0.Config{
		ClientID:         os.Getenv("AUTH0_CLIENT_ID"),
		Domain:           os.Getenv("AUTH0_DOMAIN"),
		ClientSecret:     os.Getenv("AUTH0_CLIENT_SECRET"),
		OauthCallbackURL: "http://localhost:3000/callback",
		LogoutReturnURL:  "http://localhost:3000/",
		SessionStore:     store,
		SessionName:      "auth",
	}
}

M go.mod => go.mod +1 -0
@@ 4,6 4,7 @@ go 1.13

require (
	git.sr.ht/~mna/webparts v0.0.0-20191029021002-192cf10606f8
	git.sr.ht/~mna/webparts-sessions v0.0.0-20191031203513-30542a33f756
	github.com/coreos/go-oidc v2.1.0+incompatible
	github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
	golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf // indirect

M go.sum => go.sum +6 -0
@@ 1,11 1,17 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
git.sr.ht/~mna/webparts v0.0.0-20191029021002-192cf10606f8 h1:BOZLZLCJNZR/fpugRNTVmOR/fUK9iiJ8v6Y2QUwbGsA=
git.sr.ht/~mna/webparts v0.0.0-20191029021002-192cf10606f8/go.mod h1:FOnYHb2lCpY5fycBxEF4BqtFDiTxW4AB0CJeoKXzwWo=
git.sr.ht/~mna/webparts-sessions v0.0.0-20191031203513-30542a33f756 h1:JlTuwZYGsYvwfYHbk7Tp7KVctNMvFqL57MrtZJG9qgQ=
git.sr.ht/~mna/webparts-sessions v0.0.0-20191031203513-30542a33f756/go.mod h1:bD0DVjqFoNKlKI/kLspVlc4oFyLr09yU5L0Zc7L0Tk4=
github.com/coreos/go-oidc v2.1.0+incompatible h1:sdJrfw8akMnCuUlaZU3tE/uYXFgfqom8DBE9so9EBsM=
github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAmPf4YLrFg6oQUotqHQeUNWwkvo7jZp1GLU=
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=