Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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.
33 changes: 21 additions & 12 deletions internal/pkg/auth/user_login.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package auth

import (
"embed"
_ "embed"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand All @@ -11,7 +12,6 @@ import (
"net/http"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
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: 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 Expand Up @@ -258,6 +260,13 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
return nil
}

// 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)
}

// getUserAccessAndRefreshTokens trades the authorization code retrieved from the first OAuth2 leg for an access token and a refresh token
func getUserAccessAndRefreshTokens(idpWellKnownConfig *wellKnownConfig, clientID, codeVerifier, authorizationCode, callbackURL string) (accessToken, refreshToken string, err error) {
// Set form-encoded data for the POST to the access token endpoint
Expand Down