github.com/argoproj/argo-cd@v1.8.7/util/oidc/templates.go (about)

     1  package oidc
     2  
     3  import (
     4  	"html/template"
     5  	"log"
     6  	"net/http"
     7  )
     8  
     9  type tokenTmplData struct {
    10  	IDToken      string
    11  	RefreshToken string
    12  	RedirectURL  string
    13  	Claims       string
    14  }
    15  
    16  var tokenTmpl = template.Must(template.New("token.html").Parse(`<html>
    17    <head>
    18      <style>
    19  /* make pre wrap */
    20  pre {
    21   white-space: pre-wrap;       /* css-3 */
    22   white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    23   white-space: -pre-wrap;      /* Opera 4-6 */
    24   white-space: -o-pre-wrap;    /* Opera 7 */
    25   word-wrap: break-word;       /* Internet Explorer 5.5+ */
    26  }
    27      </style>
    28    </head>
    29    <body>
    30      <p> Token: <pre><code>{{ .IDToken }}</code></pre></p>
    31      <p> Claims: <pre><code>{{ .Claims }}</code></pre></p>
    32  	{{ if .RefreshToken }}
    33      <p> Refresh Token: <pre><code>{{ .RefreshToken }}</code></pre></p>
    34  	<form action="{{ .RedirectURL }}" method="post">
    35  	  <input type="hidden" name="refresh_token" value="{{ .RefreshToken }}">
    36  	  <input type="submit" value="Redeem refresh token">
    37      </form>
    38  	{{ end }}
    39    </body>
    40  </html>
    41  `))
    42  
    43  func renderToken(w http.ResponseWriter, redirectURL, idToken, refreshToken string, claims []byte) {
    44  	renderTemplate(w, tokenTmpl, tokenTmplData{
    45  		IDToken:      idToken,
    46  		RefreshToken: refreshToken,
    47  		RedirectURL:  redirectURL,
    48  		Claims:       string(claims),
    49  	})
    50  }
    51  
    52  func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) {
    53  	err := tmpl.Execute(w, data)
    54  	if err == nil {
    55  		return
    56  	}
    57  
    58  	switch err := err.(type) {
    59  	case *template.Error:
    60  		// An ExecError guarantees that Execute has not written to the underlying reader.
    61  		log.Printf("Error rendering template %s: %s", tmpl.Name(), err)
    62  
    63  		// TODO(ericchiang): replace with better internal server error.
    64  		http.Error(w, "Internal server error", http.StatusInternalServerError)
    65  	default:
    66  		// An error with the underlying write, such as the connection being
    67  		// dropped. Ignore for now.
    68  	}
    69  }