Skip to content

Commit 05e7593

Browse files
committed
add logo as base64 to the login-successful.html instead of adding an extra handleFunc
- add go:embed - removed unused variables
1 parent 97ce2f7 commit 05e7593

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

internal/pkg/auth/templates/login-successful.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
<div class="logo m-5">
272272
<img
273273
alt="logo"
274-
src="/stackit_nav_logo_light.svg"
274+
src="data:image/svg+xml;base64,{{.Logo}}"
275275
/>
276276
</div>
277277

internal/pkg/auth/user_login.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package auth
22

33
import (
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

4948
type 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
276271
func 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

Comments
 (0)