Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

add example with labstack/echo #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 04-Echo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module github.com/passageidentity/example-go

go 1.17

require (
github.com/labstack/echo/v4 v4.11.4
github.com/passageidentity/passage-go v1.11.0
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/jwx v1.2.26 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/oapi-codegen/runtime v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
)
500 changes: 500 additions & 0 deletions 04-Echo/go.sum

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions 04-Echo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"embed"
"html/template"
"io"
"net/http"
"os"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/passageidentity/passage-go"
)

var (
passageAppID = os.Getenv("PASSAGE_APP_ID")
passageApiKey = os.Getenv("PASSAGE_API_KEY")

//go:embed static
assets embed.FS
)

func authRequired(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
psg, err := passage.New(passageAppID, &passage.Config{
APIKey: passageApiKey,
})
if err != nil {
// This should not fail, but abort the request if it does
c.Echo().Logger.Debug(err)
return c.NoContent(http.StatusInternalServerError)
}
passageUserID, err := psg.AuthenticateRequest(c.Request())
if err != nil {
// Authentication failed!
return c.Render(http.StatusOK, "unauthorized", nil)
}

// Get the authenticated user's email and set it in the context
passageUser, err := psg.GetUser(passageUserID)
if err != nil {
// This should not fail, but abort the request if it does
c.Echo().Logger.Debug(err)
return c.String(http.StatusInternalServerError, err.Error())
}
c.Set("userEmail", passageUser.Email)

// Authentication was successful, proceed.
return next(c)
}
}

type TemplateRender struct {
templates *template.Template
}

func (t *TemplateRender) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}

func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Gzip())
e.Debug = true

renderer := &TemplateRender{
templates: template.Must(template.ParseFS(assets, "static/*.tmpl")),
}
e.Renderer = renderer

e.GET("/", func(c echo.Context) error {
return c.Render(http.StatusOK, "index", map[string]any{
"AppID": passageAppID,
})
})

group := e.Group("", authRequired)
group.GET("/dashboard", func(c echo.Context) error {
return c.Render(http.StatusOK, "dashboard", map[string]any{
"email": c.Get("userEmail").(string),
})
})

e.Logger.Fatal(e.Start(":8080"))
}
18 changes: 18 additions & 0 deletions 04-Echo/static/dashboard.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{define "dashboard"}}
<!DOCTYPE html>
<html>
<head>
<title>Gin + Passage</title>
</head>
<body>
<h1>You're signed in, {{ .email }}!</h1>

<style>
body {
text-align: center;
font-family: sans-serif;
}
</style>
</body>
</html>
{{end}}
21 changes: 21 additions & 0 deletions 04-Echo/static/index.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{define "index"}}
<!DOCTYPE html>
<html>
<head>
<title>Gin + Passage</title>
</head>
<body>
<h1>Welcome to my gin app!</h1>

<passage-auth app-id="{{.AppID}}"></passage-auth>
<script src="https://psg.so/web.js"></script>

<style>
body {
text-align: center;
font-family: sans-serif;
}
</style>
</body>
</html>
{{end}}
22 changes: 22 additions & 0 deletions 04-Echo/static/unauthorized.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{define "unauthorized"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>You can't be here!</h1>
<a href="/">Go Home →</a>

<style>
body {
text-align: center;
font-family: sans-serif;
}
</style>
</body>
</html>
{{end}}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Passage is an authentication platform that enables developers to add biometric a
## Examples in this Repo
* [Authenticating with Passage](01-Login/)
* [Authentication with a Profile Page](02-Login-With-Profile/)
* [Example app using Gin](03-Gin/)
* [Example app using Echo](04-Echo/)