github.com/hashicorp/cap@v0.6.0/oidc/examples/spa/route_login.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"net/http"
    10  	"os"
    11  	"time"
    12  
    13  	"github.com/hashicorp/cap/oidc"
    14  )
    15  
    16  func LoginHandler(ctx context.Context, p *oidc.Provider, rc *requestCache, timeout time.Duration, redirectURL string, requestOptions []oidc.Option) http.HandlerFunc {
    17  	return func(w http.ResponseWriter, r *http.Request) {
    18  		oidcRequest, err := oidc.NewRequest(timeout, redirectURL, requestOptions...)
    19  		if err != nil {
    20  			fmt.Fprint(os.Stderr, err.Error())
    21  			return
    22  		}
    23  		rc.Add(oidcRequest)
    24  
    25  		authURL, err := p.AuthURL(ctx, oidcRequest)
    26  		if err != nil {
    27  			fmt.Fprintf(os.Stderr, "error getting auth url: %s", err)
    28  			return
    29  		}
    30  		http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
    31  	}
    32  }