Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/pkg/auth/templates/login-successful.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
<div class="logo m-5">
<img
alt="logo"
src="https://cdn.apps.01.cf.eu01.stackit.cloud/assets/img/logo_inverted.svg"
src="data:image/svg+xml;base64,{{.Logo}}"
/>
</div>

Expand Down
11 changes: 11 additions & 0 deletions internal/pkg/auth/templates/stackit_nav_logo_light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 14 additions & 12 deletions internal/pkg/auth/user_login.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
"embed"
_ "embed"
"encoding/json"
"errors"
"fmt"
Expand All @@ -11,12 +11,12 @@ import (
"net/http"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"time"

"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"golang.org/x/oauth2"

"github.com/stackitcloud/stackit-cli/internal/pkg/print"
Expand All @@ -26,22 +26,23 @@ const (
defaultWellKnownConfig = "https://accounts.stackit.cloud/.well-known/openid-configuration"
defaultCLIClientID = "stackit-cli-0000-0000-000000000001"

loginSuccessPath = "/login-successful"
stackitLandingPage = "https://www.stackit.de"
htmlTemplatesPath = "templates"
loginSuccessfulHTMLFile = "login-successful.html"
loginSuccessPath = "/login-successful"

// The IDP doesn't support wildcards for the port,
// so we configure a range of ports from 8000 to 8020
defaultPort = 8000
configuredPortRange = 20
)

//go:embed templates/*
var htmlContent embed.FS
//go:embed templates/login-successful.html
var htmlTemplateContent string

type User struct {
//go:embed templates/stackit_nav_logo_light.svg
var logoSvgContent []byte

type InputValues struct {
Email string
Logo string
}

type apiClient interface {
Expand Down Expand Up @@ -215,18 +216,19 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
errServer = fmt.Errorf("read user email: %w", err)
}

user := User{
input := InputValues{
Email: email,
Logo: utils.Base64Encode(logoSvgContent),
}

// ParseFS expects paths using forward slashes, even on Windows
// See: https://github.com/golang/go/issues/44305#issuecomment-780111748
htmlTemplate, err := template.ParseFS(htmlContent, path.Join(htmlTemplatesPath, loginSuccessfulHTMLFile))
htmlTemplate, err := template.New("loginSuccess").Parse(htmlTemplateContent)
if err != nil {
errServer = fmt.Errorf("parse html file: %w", err)
}

err = htmlTemplate.Execute(w, user)
err = htmlTemplate.Execute(w, input)
if err != nil {
errServer = fmt.Errorf("render page: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/base64"
"fmt"
"net/url"
"strings"
Expand Down Expand Up @@ -116,3 +117,10 @@ func PtrByteSizeDefault(size *int64, defaultValue string) string {
}
return bytesize.New(float64(*size)).String()
}

// Base64Encode encodes a []byte to a base64 representation as string
func Base64Encode(message []byte) string {
b := make([]byte, base64.StdEncoding.EncodedLen(len(message)))
base64.StdEncoding.Encode(b, message)
return string(b)
}