If you have a custom app deployed on Runway and want to gate it behind Tailscale identity,
github.com/coreos/go-oidc/v3
+ golang.org/x/oauth2 is
the standard combination for Go. Below is a minimal but complete example.
Register an OIDC client in the tsidp admin UI using redirect URI https://myapp.pqapp.dev/callback,
then note the client_id and client_secret.
/login starts the OIDC flow, /callback handles the code exchange, /private is the protected
route. State is verified via a short-lived cookie. The session map is in-memory - sessions are
lost on restart.
// DO NOT USE AS-IS IN PRODUCTION: no locking, error handling omitted throughout.
package main
import (
"context"
"crypto/rand"
"fmt"
"log"
"net/http"
"os"
"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"
)
var (
oauth2Cfg oauth2.Config
verifier *oidc.IDTokenVerifier
sessions = map[string]string{} // would need locking for concurrent use
)
func main() {
provider, err := oidc.NewProvider(context.Background(), os.Getenv("OIDC_ISSUER"))
if err != nil {
log.Fatal(err)
}
verifier = provider.Verifier(&oidc.Config{ClientID: os.Getenv("OIDC_CLIENT_ID")})
oauth2Cfg = oauth2.Config{
ClientID: os.Getenv("OIDC_CLIENT_ID"),
ClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, "email"},
}
http.HandleFunc("GET /", publicHandler)
http.HandleFunc("GET /private", requireAuth(privateHandler))
http.HandleFunc("GET /login", loginHandler)
http.HandleFunc("GET /callback", callbackHandler)
log.Println("WARNING: DO NOT USE IN PRODUCTION. This is an example only.")
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}
func publicHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "public page - try /private")
}
func privateHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello, %s\n", r.Context().Value("email"))
}
func requireAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session")
if err != nil {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, ok := sessions[cookie.Value]
if !ok {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
next(w, r.WithContext(context.WithValue(r.Context(), "email", email)))
}
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
state := randomString(16)
cfg := oauth2Cfg
cfg.RedirectURL = "https://" + r.Host + "/callback"
http.SetCookie(w, &http.Cookie{Name: "oidc_state", Value: state, Path: "/", HttpOnly: true, Secure: true, MaxAge: 300})
http.Redirect(w, r, cfg.AuthCodeURL(state), http.StatusFound)
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
stateCookie, err := r.Cookie("oidc_state")
if err != nil || stateCookie.Value != r.URL.Query().Get("state") {
http.Error(w, "invalid state", http.StatusBadRequest)
return
}
cfg := oauth2Cfg
cfg.RedirectURL = "https://" + r.Host + "/callback"
token, err := cfg.Exchange(r.Context(), r.URL.Query().Get("code"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rawID, _ := token.Extra("id_token").(string) // error handling elided
idToken, err := verifier.Verify(r.Context(), rawID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var claims struct{ Email string `json:"email"` }
idToken.Claims(&claims) // error handling elided
sessionID := randomString(32)
sessions[sessionID] = claims.Email
http.SetCookie(w, &http.Cookie{Name: "session", Value: sessionID, Path: "/", HttpOnly: true, Secure: true})
http.Redirect(w, r, "/private", http.StatusFound)
}
func randomString(nBytes int) string {
b := make([]byte, nBytes)
rand.Read(b) // error handling elided
return fmt.Sprintf("%x", b)
}
Initialize the Go module, fetch dependencies, and commit everything before deploying:
runway app create myapp
go mod init myapp
go mod tidy
git add .
git commit -m "initial commit"
Deploy your app pointing at your tsidp instance.
Create a Tailscale auth key for the app.
runway app config set \
TS_AUTHKEY=tskey-auth-... \
ALL_PROXY=socks5://localhost:1055/ \
HTTP_PROXY=http://localhost:1055/ \
HTTPS_PROXY=http://localhost:1055/ \
OIDC_ISSUER=https://my-tsidp.tailnet-name.ts.net \
OIDC_CLIENT_ID=<client-id> \
OIDC_CLIENT_SECRET=<client-secret>
runway app deploy
runway app config set \
OIDC_ISSUER=https://my-tsidp.tailnet-name.ts.net \
OIDC_CLIENT_ID=<client-id> \
OIDC_CLIENT_SECRET=<client-secret>
runway app deploy