---
title: Go with go-oidc
summary: Sign users in to a Go web app with Tessera, using coreos/go-oidc and golang.org/x/oauth2 with PKCE.
section: quickstart
order: 2
---
# Go with go-oidc

Sign users in to a Go web app with Tessera, using `coreos/go-oidc` and `golang.org/x/oauth2` with PKCE.

**Requires:** Go 1.24 or later (the code uses `rand.Text`).

## 1. Create the app in Tessera

Sign in to the console at https://platform.kurthlabs.com/tessera with your invitation (see [your first sign-in](/docs/start/first-sign-in.md)), open your realm and add an app. Choose Go and where it runs: this computer for now.

The console then shows what you need, each with a copy button:

- the issuer, `https://login.kurthlabs.com/realms/YOUR_REALM`
- the client ID
- the redirect URI, `http://localhost:8080/auth/callback`
- the client secret, **once**. Copy it now, or download the `.env` file. Tessera cannot show it again.

Put the values in `.env.local` as shown under Environment below, and keep that file out of git. Load it into your shell with `set -a; . ./.env.local; set +a`.

If you have the `tessera` CLI (during early access it is by invitation), `tessera init` does this step: it registers the app, writes `.env.local` and prints the code for your stack. The console path above needs no download.

## 2. Add sign-in

```sh
go get github.com/coreos/go-oidc/v3/oidc golang.org/x/oauth2
```

`main.go`

```go
package main

import (
	"context"
	"crypto/rand"
	"fmt"
	"log"
	"net/http"
	"os"
	"strings"

	"github.com/coreos/go-oidc/v3/oidc"
	"golang.org/x/oauth2"
)

func main() {
	provider, err := oidc.NewProvider(context.Background(), os.Getenv("TESSERA_ISSUER"))
	if err != nil {
		log.Fatal(err)
	}
	cfg := oauth2.Config{ClientID: os.Getenv("TESSERA_CLIENT_ID"), ClientSecret: os.Getenv("TESSERA_CLIENT_SECRET"),
		Endpoint: provider.Endpoint(), RedirectURL: "http://localhost:8080/auth/callback", Scopes: []string{"openid", "profile", "email"}}
	http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		state, pkce := rand.Text(), oauth2.GenerateVerifier()
		http.SetCookie(w, &http.Cookie{Name: "oidc", Value: state + "." + pkce, Path: "/auth/", HttpOnly: true,
			SameSite: http.SameSiteLaxMode, Secure: !strings.HasPrefix(cfg.RedirectURL, "http://localhost")})
		http.Redirect(w, r, cfg.AuthCodeURL(state, oauth2.S256ChallengeOption(pkce)), http.StatusFound)
	})
	http.HandleFunc("/auth/callback", func(w http.ResponseWriter, r *http.Request) {
		c, _ := r.Cookie("oidc")
		if c == nil || !strings.HasPrefix(c.Value, r.URL.Query().Get("state")+".") {
			http.Error(w, "bad state", http.StatusBadRequest)
			return
		}
		_, pkce, _ := strings.Cut(c.Value, ".")
		tok, err := cfg.Exchange(r.Context(), r.URL.Query().Get("code"), oauth2.VerifierOption(pkce))
		if err != nil {
			http.Error(w, "sign-in failed", http.StatusUnauthorized)
			return
		}
		info, err := provider.UserInfo(r.Context(), oauth2.StaticTokenSource(tok))
		if err != nil {
			http.Error(w, "sign-in failed", http.StatusBadGateway)
			return
		}
		fmt.Fprintf(w, "signed in: %s\n", info.Email)
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}
```

Load the environment (above), run `go run .` and open `http://localhost:8080/login`. This prints the email address of the signed-in user. Replace the last handler line with your own session logic.

## Environment

The console gives you these values (the downloaded `.env` file has them, and so does `tessera init`). Put them in `.env.local`:

```env
TESSERA_ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
TESSERA_CLIENT_ID=YOUR_CLIENT_ID
TESSERA_CLIENT_SECRET=YOUR_CLIENT_SECRET
```

The redirect URI registered for this app is `http://localhost:8080/auth/callback`. For production, add your production address to the app's redirect URIs in the console (the same path on `https://your-app.example`); the local one can stay.
