11package auth
22
33import (
4- "embed"
4+ _ "embed"
5+ "encoding/base64"
56 "encoding/json"
67 "errors"
78 "fmt"
@@ -11,7 +12,6 @@ import (
1112 "net/http"
1213 "os"
1314 "os/exec"
14- "path"
1515 "runtime"
1616 "strconv"
1717 "strings"
@@ -26,24 +26,23 @@ const (
2626 defaultWellKnownConfig = "https://accounts.stackit.cloud/.well-known/openid-configuration"
2727 defaultCLIClientID = "stackit-cli-0000-0000-000000000001"
2828
29- loginSuccessPath = "/login-successful"
30- stackitLandingPage = "https://www.stackit.de"
31- htmlTemplatesPath = "templates"
32- loginSuccessfulHTMLFile = "login-successful.html"
33- logoPath = "/stackit_nav_logo_light.svg"
34- logoSVGFilePath = "stackit_nav_logo_light.svg"
29+ loginSuccessPath = "/login-successful"
3530
3631 // The IDP doesn't support wildcards for the port,
3732 // so we configure a range of ports from 8000 to 8020
3833 defaultPort = 8000
3934 configuredPortRange = 20
4035)
4136
42- //go:embed templates/*
43- var htmlContent embed. FS
37+ //go:embed templates/login-successful.html
38+ var htmlTemplateContent string
4439
45- type User struct {
40+ //go:embed templates/stackit_nav_logo_light.svg
41+ var logoSvgContent []byte
42+
43+ type InputValues struct {
4644 Email string
45+ Logo string
4746}
4847
4948type apiClient interface {
@@ -210,42 +209,31 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
210209 })
211210
212211 mux .HandleFunc (loginSuccessPath , func (w http.ResponseWriter , _ * http.Request ) {
212+ defer cleanup (server )
213+
213214 email , err := GetAuthField (USER_EMAIL )
214215 if err != nil {
215216 errServer = fmt .Errorf ("read user email: %w" , err )
216217 }
217218
218- user := User {
219+ input := InputValues {
219220 Email : email ,
221+ Logo : base64Encode (logoSvgContent ),
220222 }
221223
222224 // ParseFS expects paths using forward slashes, even on Windows
223225 // See: https://github.com/golang/go/issues/44305#issuecomment-780111748
224- htmlTemplate , err := template .ParseFS ( htmlContent , path . Join ( htmlTemplatesPath , loginSuccessfulHTMLFile ) )
226+ htmlTemplate , err := template .New ( "loginSuccess" ). Parse ( htmlTemplateContent )
225227 if err != nil {
226228 errServer = fmt .Errorf ("parse html file: %w" , err )
227229 }
228230
229- err = htmlTemplate .Execute (w , user )
231+ err = htmlTemplate .Execute (w , input )
230232 if err != nil {
231233 errServer = fmt .Errorf ("render page: %w" , err )
232234 }
233235 })
234236
235- mux .HandleFunc (logoPath , func (w http.ResponseWriter , _ * http.Request ) {
236- defer cleanup (server )
237-
238- img , err := htmlContent .ReadFile (path .Join (htmlTemplatesPath , logoSVGFilePath ))
239- if err != nil {
240- errServer = fmt .Errorf ("read logo file: %w" , err )
241- }
242- w .Header ().Set ("Content-Type" , "image/svg+xml" )
243- _ , err = w .Write (img )
244- if err != nil {
245- return
246- }
247- })
248-
249237 p .Debug (print .DebugLevel , "opening browser for authentication: %s" , authorizationURL )
250238 p .Debug (print .DebugLevel , "using authentication server on %s" , idpWellKnownConfig .Issuer )
251239 p .Debug (print .DebugLevel , "using client ID %s for authentication " , idpClientID )
@@ -272,6 +260,13 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
272260 return nil
273261}
274262
263+ // base64Encode encodes a []byte to a base64 representation as string
264+ func base64Encode (message []byte ) string {
265+ b := make ([]byte , base64 .StdEncoding .EncodedLen (len (message )))
266+ base64 .StdEncoding .Encode (b , message )
267+ return string (b )
268+ }
269+
275270// getUserAccessAndRefreshTokens trades the authorization code retrieved from the first OAuth2 leg for an access token and a refresh token
276271func getUserAccessAndRefreshTokens (idpWellKnownConfig * wellKnownConfig , clientID , codeVerifier , authorizationCode , callbackURL string ) (accessToken , refreshToken string , err error ) {
277272 // Set form-encoded data for the POST to the access token endpoint
0 commit comments