feat(sso): add openid connect client with pkce and guarded discovery
Issuer URLs come from customers, so requests to their IdPs refuse non-public addresses unless private networks are explicitly allowed. Refs #4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
d1976ed57a
commit
9ad5f29f1b
7 files changed
+576
-19
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package sso
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrForbiddenAddress is returned when an IdP resolves to an address the
|
||||
// client may not contact.
|
||||
var ErrForbiddenAddress = errors.New("sso: issuer resolves to a non-public address")
|
||||
|
||||
var cgnat = netip.MustParsePrefix("100.64.0.0/10")
|
||||
|
||||
// SafeHTTPClient returns a client for talking to customer-configured IdPs.
|
||||
// Unless allowPrivate is set it refuses to connect to loopback, private,
|
||||
// link-local and similar ranges. The check runs on the resolved address at
|
||||
// dial time, so DNS rebinding and redirects cannot bypass it.
|
||||
func SafeHTTPClient(allowPrivate bool) *http.Client {
|
||||
d := &net.Dialer{Timeout: 5 * time.Second}
|
||||
if !allowPrivate {
|
||||
d.Control = func(_, address string, _ syscall.RawConn) error {
|
||||
ap, err := netip.ParseAddrPort(address)
|
||||
if err != nil || !isPublic(ap.Addr()) {
|
||||
return ErrForbiddenAddress
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
tr := http.DefaultTransport.(*http.Transport).Clone()
|
||||
tr.Proxy = nil
|
||||
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return d.DialContext(ctx, network, addr)
|
||||
}
|
||||
return &http.Client{Transport: tr, Timeout: 10 * time.Second}
|
||||
}
|
||||
|
||||
func isPublic(a netip.Addr) bool {
|
||||
a = a.Unmap()
|
||||
return a.IsGlobalUnicast() && !a.IsPrivate() && !cgnat.Contains(a)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package sso
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/netip"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsPublic(t *testing.T) {
|
||||
tests := map[string]bool{
|
||||
"8.8.8.8": true,
|
||||
"2606:4700::1111": true,
|
||||
"127.0.0.1": false,
|
||||
"10.1.2.3": false,
|
||||
"172.16.0.1": false,
|
||||
"192.168.1.1": false,
|
||||
"169.254.169.254": false,
|
||||
"100.64.0.1": false,
|
||||
"0.0.0.0": false,
|
||||
"::1": false,
|
||||
"fd00::1": false,
|
||||
"fe80::1": false,
|
||||
"::ffff:127.0.0.1": false,
|
||||
}
|
||||
for addr, want := range tests {
|
||||
if got := isPublic(netip.MustParseAddr(addr)); got != want {
|
||||
t.Errorf("isPublic(%s) = %t, want %t", addr, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeHTTPClientBlocksLoopback(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer srv.Close()
|
||||
|
||||
if _, err := SafeHTTPClient(false).Get(srv.URL); !errors.Is(err, ErrForbiddenAddress) {
|
||||
t.Errorf("Get(%s) with private networks denied: error = %v, want %v", srv.URL, err, ErrForbiddenAddress)
|
||||
}
|
||||
resp, err := SafeHTTPClient(true).Get(srv.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get(%s) with private networks allowed failed: %v", srv.URL, err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// Package sso implements OpenID Connect sign-in against identity providers
|
||||
// that each organization configures for itself.
|
||||
package sso
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// Provider is one organization's IdP registration.
|
||||
type Provider struct {
|
||||
Issuer string
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
Scopes []string
|
||||
}
|
||||
|
||||
// Claims are the ID token fields used to identify and provision a user.
|
||||
type Claims struct {
|
||||
Subject string `json:"sub"`
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Flow holds the per-attempt secrets that must survive the redirect to the
|
||||
// IdP. Callers store it server side, keyed by State.
|
||||
type Flow struct {
|
||||
State string
|
||||
Nonce string
|
||||
Verifier string
|
||||
}
|
||||
|
||||
// ErrInvalidIssuer is returned for issuer URLs that are malformed or use a
|
||||
// scheme the client does not allow.
|
||||
var ErrInvalidIssuer = errors.New("sso: invalid issuer url")
|
||||
|
||||
const discoveryTTL = time.Hour
|
||||
|
||||
// Client performs discovery, builds authorization URLs and verifies
|
||||
// callbacks. Discovery documents are cached per issuer.
|
||||
type Client struct {
|
||||
http *http.Client
|
||||
redirectURL string
|
||||
allowHTTP bool
|
||||
now func() time.Time
|
||||
|
||||
mu sync.Mutex
|
||||
cache map[string]cached
|
||||
}
|
||||
|
||||
type cached struct {
|
||||
provider *oidc.Provider
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
// New returns a Client whose callbacks land on redirectURL. httpClient is
|
||||
// used for every request to an IdP; pass one from SafeHTTPClient. allowHTTP
|
||||
// permits plain-http issuers, which only makes sense for IdPs on a private
|
||||
// network.
|
||||
func New(httpClient *http.Client, redirectURL string, allowHTTP bool) *Client {
|
||||
return &Client{
|
||||
http: httpClient,
|
||||
redirectURL: redirectURL,
|
||||
allowHTTP: allowHTTP,
|
||||
now: time.Now,
|
||||
cache: map[string]cached{},
|
||||
}
|
||||
}
|
||||
|
||||
// RedirectURL is the callback URL to register at the IdP.
|
||||
func (c *Client) RedirectURL() string { return c.redirectURL }
|
||||
|
||||
// ValidateIssuer checks the URL syntactically and runs discovery, so that
|
||||
// misconfiguration surfaces when an admin saves the provider rather than
|
||||
// when users try to sign in.
|
||||
func (c *Client) ValidateIssuer(ctx context.Context, issuer string) error {
|
||||
if err := c.checkIssuer(issuer); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := c.discover(ctx, issuer)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) checkIssuer(issuer string) error {
|
||||
u, err := url.Parse(issuer)
|
||||
if err != nil || u.Host == "" || u.RawQuery != "" || u.Fragment != "" || u.User != nil {
|
||||
return ErrInvalidIssuer
|
||||
}
|
||||
if u.Scheme != "https" && !(c.allowHTTP && u.Scheme == "http") {
|
||||
return ErrInvalidIssuer
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) discover(ctx context.Context, issuer string) (*oidc.Provider, error) {
|
||||
c.mu.Lock()
|
||||
if e, ok := c.cache[issuer]; ok && c.now().Before(e.expires) {
|
||||
c.mu.Unlock()
|
||||
return e.provider, nil
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
p, err := oidc.NewProvider(oidc.ClientContext(ctx, c.http), issuer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sso: discovery for %s: %w", issuer, err)
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.cache[issuer] = cached{provider: p, expires: c.now().Add(discoveryTTL)}
|
||||
c.mu.Unlock()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (c *Client) oauth(p Provider, op *oidc.Provider) *oauth2.Config {
|
||||
scopes := p.Scopes
|
||||
if !slices.Contains(scopes, oidc.ScopeOpenID) {
|
||||
scopes = append([]string{oidc.ScopeOpenID}, scopes...)
|
||||
}
|
||||
return &oauth2.Config{
|
||||
ClientID: p.ClientID,
|
||||
ClientSecret: p.ClientSecret,
|
||||
Endpoint: op.Endpoint(),
|
||||
RedirectURL: c.redirectURL,
|
||||
Scopes: scopes,
|
||||
}
|
||||
}
|
||||
|
||||
// NewFlow returns fresh state, nonce and PKCE verifier.
|
||||
func NewFlow(random func() string) Flow {
|
||||
return Flow{State: random(), Nonce: random(), Verifier: oauth2.GenerateVerifier()}
|
||||
}
|
||||
|
||||
// AuthURL returns the IdP URL to redirect the browser to.
|
||||
func (c *Client) AuthURL(ctx context.Context, p Provider, f Flow) (string, error) {
|
||||
if err := c.checkIssuer(p.Issuer); err != nil {
|
||||
return "", err
|
||||
}
|
||||
op, err := c.discover(ctx, p.Issuer)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return c.oauth(p, op).AuthCodeURL(f.State,
|
||||
oidc.Nonce(f.Nonce),
|
||||
oauth2.S256ChallengeOption(f.Verifier),
|
||||
), nil
|
||||
}
|
||||
|
||||
// Exchange redeems the authorization code and verifies the ID token's
|
||||
// signature, issuer, audience, expiry and nonce.
|
||||
func (c *Client) Exchange(ctx context.Context, p Provider, f Flow, code string) (Claims, error) {
|
||||
op, err := c.discover(ctx, p.Issuer)
|
||||
if err != nil {
|
||||
return Claims{}, err
|
||||
}
|
||||
ctx = oidc.ClientContext(ctx, c.http)
|
||||
tok, err := c.oauth(p, op).Exchange(ctx, code, oauth2.VerifierOption(f.Verifier))
|
||||
if err != nil {
|
||||
return Claims{}, fmt.Errorf("sso: code exchange: %w", err)
|
||||
}
|
||||
raw, ok := tok.Extra("id_token").(string)
|
||||
if !ok {
|
||||
return Claims{}, errors.New("sso: token response without id_token")
|
||||
}
|
||||
idt, err := op.VerifierContext(ctx, &oidc.Config{ClientID: p.ClientID, Now: c.now}).Verify(ctx, raw)
|
||||
if err != nil {
|
||||
return Claims{}, fmt.Errorf("sso: id token: %w", err)
|
||||
}
|
||||
if idt.Nonce != f.Nonce {
|
||||
return Claims{}, errors.New("sso: id token nonce mismatch")
|
||||
}
|
||||
var cl Claims
|
||||
if err := idt.Claims(&cl); err != nil {
|
||||
return Claims{}, fmt.Errorf("sso: id token claims: %w", err)
|
||||
}
|
||||
if cl.Subject == "" {
|
||||
return Claims{}, errors.New("sso: id token without subject")
|
||||
}
|
||||
return cl, nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package sso_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/sso"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/sso/ssotest"
|
||||
)
|
||||
|
||||
const callback = "https://uptime.example/sso/callback"
|
||||
|
||||
// authorize follows the IdP redirect and returns the code and state it
|
||||
// would deliver to the callback.
|
||||
func authorize(t *testing.T, authURL string) (code, state string) {
|
||||
t.Helper()
|
||||
noFollow := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
||||
resp, err := noFollow.Get(authURL)
|
||||
if err != nil {
|
||||
t.Fatalf("GET %s failed: %v", authURL, err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
loc, err := url.Parse(resp.Header.Get("Location"))
|
||||
if err != nil || resp.StatusCode != http.StatusFound {
|
||||
t.Fatalf("authorize = %d %q, want 302 to callback", resp.StatusCode, resp.Header.Get("Location"))
|
||||
}
|
||||
return loc.Query().Get("code"), loc.Query().Get("state")
|
||||
}
|
||||
|
||||
func setup(t *testing.T) (*ssotest.IDP, *sso.Client, sso.Provider) {
|
||||
t.Helper()
|
||||
idp := ssotest.New(t)
|
||||
c := sso.New(sso.SafeHTTPClient(true), callback, true)
|
||||
p := sso.Provider{Issuer: idp.Issuer(), ClientID: idp.ClientID, ClientSecret: idp.Secret, Scopes: []string{"email"}}
|
||||
return idp, c, p
|
||||
}
|
||||
|
||||
func TestFlow(t *testing.T) {
|
||||
idp, c, p := setup(t)
|
||||
idp.SetUser(ssotest.User{Subject: "u-1", Email: "a@corp.test", EmailVerified: true, Name: "A"})
|
||||
ctx := context.Background()
|
||||
|
||||
flow := sso.NewFlow(rand.Text)
|
||||
authURL, err := c.AuthURL(ctx, p, flow)
|
||||
if err != nil {
|
||||
t.Fatalf("AuthURL() failed: %v", err)
|
||||
}
|
||||
code, state := authorize(t, authURL)
|
||||
if state != flow.State {
|
||||
t.Fatalf("state = %q, want %q", state, flow.State)
|
||||
}
|
||||
got, err := c.Exchange(ctx, p, flow, code)
|
||||
if err != nil {
|
||||
t.Fatalf("Exchange() failed: %v", err)
|
||||
}
|
||||
want := sso.Claims{Subject: "u-1", Email: "a@corp.test", EmailVerified: true, Name: "A"}
|
||||
if got != want {
|
||||
t.Errorf("Exchange() = %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchangeRejects(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tamper func(*ssotest.IDP, *sso.Flow)
|
||||
}{
|
||||
{"wrong nonce", func(i *ssotest.IDP, _ *sso.Flow) { i.NonceOverride = "other" }},
|
||||
{"wrong audience", func(i *ssotest.IDP, _ *sso.Flow) { i.AudienceOverride = "someone-else" }},
|
||||
{"wrong pkce verifier", func(_ *ssotest.IDP, f *sso.Flow) { f.Verifier = "x" + f.Verifier[1:] }},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
idp, c, p := setup(t)
|
||||
idp.SetUser(ssotest.User{Subject: "u-1"})
|
||||
ctx := context.Background()
|
||||
flow := sso.NewFlow(rand.Text)
|
||||
authURL, err := c.AuthURL(ctx, p, flow)
|
||||
if err != nil {
|
||||
t.Fatalf("AuthURL() failed: %v", err)
|
||||
}
|
||||
code, _ := authorize(t, authURL)
|
||||
tt.tamper(idp, &flow)
|
||||
if _, err := c.Exchange(ctx, p, flow, code); err == nil {
|
||||
t.Error("Exchange() error = nil, want rejection")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateIssuer(t *testing.T) {
|
||||
idp, _, _ := setup(t)
|
||||
strict := sso.New(sso.SafeHTTPClient(false), callback, false)
|
||||
tests := []struct {
|
||||
issuer string
|
||||
client *sso.Client
|
||||
want error
|
||||
}{
|
||||
{"ftp://idp.test", strict, sso.ErrInvalidIssuer},
|
||||
{"https://idp.test/?q=1", strict, sso.ErrInvalidIssuer},
|
||||
{"https://user@idp.test", strict, sso.ErrInvalidIssuer},
|
||||
{idp.Issuer(), strict, sso.ErrInvalidIssuer},
|
||||
{"https://127.0.0.1:1", strict, sso.ErrForbiddenAddress},
|
||||
{idp.Issuer(), sso.New(sso.SafeHTTPClient(true), callback, true), nil},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
err := tt.client.ValidateIssuer(context.Background(), tt.issuer)
|
||||
if !errors.Is(err, tt.want) {
|
||||
t.Errorf("ValidateIssuer(%q) = %v, want %v", tt.issuer, err, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
// Package ssotest provides a minimal OpenID Connect provider for tests.
|
||||
package ssotest
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-jose/go-jose/v4"
|
||||
"github.com/go-jose/go-jose/v4/jwt"
|
||||
)
|
||||
|
||||
// User is the identity the IDP asserts for the next login.
|
||||
type User struct {
|
||||
Subject string
|
||||
Email string
|
||||
EmailVerified bool
|
||||
Name string
|
||||
}
|
||||
|
||||
// IDP is an in-memory authorization server. It signs ID tokens with a fresh
|
||||
// RSA key and enforces PKCE.
|
||||
type IDP struct {
|
||||
Server *httptest.Server
|
||||
ClientID string
|
||||
Secret string
|
||||
|
||||
key *rsa.PrivateKey
|
||||
signer jose.Signer
|
||||
|
||||
mu sync.Mutex
|
||||
user User
|
||||
codes map[string]grant
|
||||
// NonceOverride, when set, replaces the nonce in issued tokens.
|
||||
NonceOverride string
|
||||
// AudienceOverride, when set, replaces the audience in issued tokens.
|
||||
AudienceOverride string
|
||||
}
|
||||
|
||||
type grant struct {
|
||||
nonce, challenge, redirect string
|
||||
user User
|
||||
}
|
||||
|
||||
// New starts an IDP that is shut down when t ends.
|
||||
func New(t *testing.T) *IDP {
|
||||
t.Helper()
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: jose.JSONWebKey{Key: key, KeyID: "k1"}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
idp := &IDP{ClientID: "client-1", Secret: "secret-1", key: key, signer: signer, codes: map[string]grant{}}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /.well-known/openid-configuration", idp.discovery)
|
||||
mux.HandleFunc("GET /keys", idp.keys)
|
||||
mux.HandleFunc("GET /authorize", idp.authorize)
|
||||
mux.HandleFunc("POST /token", idp.token)
|
||||
idp.Server = httptest.NewServer(mux)
|
||||
t.Cleanup(idp.Server.Close)
|
||||
return idp
|
||||
}
|
||||
|
||||
// Issuer is the issuer URL to configure.
|
||||
func (i *IDP) Issuer() string { return i.Server.URL }
|
||||
|
||||
// SetUser selects who the next authorization asserts.
|
||||
func (i *IDP) SetUser(u User) {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
i.user = u
|
||||
}
|
||||
|
||||
func (i *IDP) discovery(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, map[string]any{
|
||||
"issuer": i.Issuer(),
|
||||
"authorization_endpoint": i.Issuer() + "/authorize",
|
||||
"token_endpoint": i.Issuer() + "/token",
|
||||
"jwks_uri": i.Issuer() + "/keys",
|
||||
"response_types_supported": []string{"code"},
|
||||
"subject_types_supported": []string{"public"},
|
||||
"id_token_signing_alg_values_supported": []string{"RS256"},
|
||||
})
|
||||
}
|
||||
|
||||
func (i *IDP) keys(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, jose.JSONWebKeySet{Keys: []jose.JSONWebKey{{Key: &i.key.PublicKey, KeyID: "k1", Algorithm: "RS256", Use: "sig"}}})
|
||||
}
|
||||
|
||||
// authorize skips the login screen and immediately redirects back with a code.
|
||||
func (i *IDP) authorize(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
if q.Get("client_id") != i.ClientID || q.Get("code_challenge_method") != "S256" {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
code := rand.Text()
|
||||
i.mu.Lock()
|
||||
i.codes[code] = grant{nonce: q.Get("nonce"), challenge: q.Get("code_challenge"), redirect: q.Get("redirect_uri"), user: i.user}
|
||||
i.mu.Unlock()
|
||||
back, _ := url.Parse(q.Get("redirect_uri"))
|
||||
v := back.Query()
|
||||
v.Set("code", code)
|
||||
v.Set("state", q.Get("state"))
|
||||
back.RawQuery = v.Encode()
|
||||
http.Redirect(w, r, back.String(), http.StatusFound)
|
||||
}
|
||||
|
||||
func (i *IDP) token(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "bad form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id, secret, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
id, secret = r.PostForm.Get("client_id"), r.PostForm.Get("client_secret")
|
||||
}
|
||||
if id != i.ClientID || secret != i.Secret {
|
||||
http.Error(w, `{"error":"invalid_client"}`, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
i.mu.Lock()
|
||||
g, ok := i.codes[r.PostForm.Get("code")]
|
||||
delete(i.codes, r.PostForm.Get("code"))
|
||||
i.mu.Unlock()
|
||||
sum := sha256.Sum256([]byte(r.PostForm.Get("code_verifier")))
|
||||
if !ok || base64.RawURLEncoding.EncodeToString(sum[:]) != g.challenge || r.PostForm.Get("redirect_uri") != g.redirect {
|
||||
http.Error(w, `{"error":"invalid_grant"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
nonce, aud := g.nonce, i.ClientID
|
||||
if i.NonceOverride != "" {
|
||||
nonce = i.NonceOverride
|
||||
}
|
||||
if i.AudienceOverride != "" {
|
||||
aud = i.AudienceOverride
|
||||
}
|
||||
now := time.Now()
|
||||
claims := map[string]any{
|
||||
"iss": i.Issuer(), "sub": g.user.Subject, "aud": aud, "nonce": nonce,
|
||||
"iat": now.Unix(), "exp": now.Add(5 * time.Minute).Unix(),
|
||||
"email": g.user.Email, "email_verified": g.user.EmailVerified, "name": g.user.Name,
|
||||
}
|
||||
raw, err := jwt.Signed(i.signer).Claims(claims).Serialize()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"access_token": "at", "token_type": "Bearer", "expires_in": 300, "id_token": raw})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
Reference in new issue
Block a user