Skip to content

Commit a856c20

Browse files
authored
Merge pull request #130 from kusaridev/pxp928-set-default-workspace
update workspace name to be a default name if its not set
2 parents fc2da53 + 54a1b1a commit a856c20

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

pkg/auth/httpservice.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ func handleCallbackv2(w http.ResponseWriter, r *http.Request, expectedState stri
3131
return
3232
}
3333

34-
tmpl := template.Must(template.New("success").Parse(getPostLoginHtml(redirectUrl)))
34+
// If redirectUrl is empty, don't redirect - just show success message
35+
// This happens for new users where we'll redirect from CLI after workspace selection
36+
var htmlContent string
37+
if redirectUrl == "" {
38+
htmlContent = getSuccessHtml()
39+
} else {
40+
htmlContent = getPostLoginHtml(redirectUrl)
41+
}
42+
43+
tmpl := template.Must(template.New("success").Parse(htmlContent))
3544
if err := tmpl.Execute(w, nil); err != nil {
3645
http.Error(w, "Internal template error", http.StatusInternalServerError)
3746
return

pkg/auth/oidc.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ import (
1616
"golang.org/x/oauth2/clientcredentials"
1717
)
1818

19-
func Authenticate(ctx context.Context, clientId, clientSecret, redirectUrl, authEndpoint, redirectPort, consoleUrl string) (*oauth2.Token, error) {
19+
func Authenticate(ctx context.Context, clientId, clientSecret, redirectUrl, authEndpoint, redirectPort, consoleUrl, workspaceId string) (*oauth2.Token, error) {
2020
baseURL, err := url.Parse(consoleUrl)
2121
if err != nil {
2222
return nil, err
2323
}
24-
consoleAnalysisUrl := baseURL.JoinPath("analysis").String()
24+
25+
var consoleAnalysisUrl string
26+
// Only redirect from callback if we have a workspace
27+
// For new users, we'll redirect from CLI after workspace selection
28+
if workspaceId != "" {
29+
analysisURL := baseURL.JoinPath("analysis")
30+
query := analysisURL.Query()
31+
query.Set("workspaceId", workspaceId)
32+
analysisURL.RawQuery = query.Encode()
33+
consoleAnalysisUrl = analysisURL.String()
34+
} else {
35+
// Empty string means don't redirect from callback handler
36+
// We'll redirect from CLI after workspace selection
37+
consoleAnalysisUrl = ""
38+
}
2539

2640
oauth2Config := oauthConfig(clientId, redirectUrl, authEndpoint)
2741

pkg/auth/postlogin.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,46 @@ func getPostLoginHtml(redirectUrl string) string {
1717
</html>
1818
`, redirectUrl)
1919
}
20+
21+
func getSuccessHtml() string {
22+
return `
23+
<!DOCTYPE html>
24+
<html>
25+
<head>
26+
<title>Authentication Successful</title>
27+
<style>
28+
body {
29+
font-family: -apple-system, BlinkMacSystemFont,"DM Sans", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
30+
display: flex;
31+
justify-content: center;
32+
align-items: center;
33+
height: 100vh;
34+
margin: 0;
35+
background-color: #0E0816;
36+
}
37+
.container {
38+
text-align: center;
39+
padding: 2rem;
40+
background: #190F27;
41+
border-radius: 8px;
42+
border: 1px solid rgba(255, 255, 255, 0.2);
43+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
44+
}
45+
h1 {
46+
color: rgba(255, 255, 255, 0.7);
47+
margin-bottom: 1rem;
48+
}
49+
p {
50+
color: rgba(255, 255, 255, 0.5);
51+
}
52+
</style>
53+
</head>
54+
<body>
55+
<div class="container">
56+
<h1>Authentication Successful!</h1>
57+
<p>You can close this window and return to the CLI.</p>
58+
</div>
59+
</body>
60+
</html>
61+
`
62+
}

pkg/login/login.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"net/http"
12+
"net/url"
1213
"time"
1314

1415
"github.com/kusaridev/kusari-cli/pkg/auth"
@@ -27,15 +28,23 @@ func Login(ctx context.Context, clientId, clientSecret, redirectUrl, authEndpoin
2728
fmt.Println()
2829
}
2930

30-
token, err := auth.Authenticate(ctx, clientId, clientSecret, redirectUrl, authEndpoint, redirectPort, consoleUrl)
31+
// Check if there's a previously stored workspace for this platform and auth endpoint
32+
// This allows us to include the workspace in the redirect URL
33+
workspaceId := ""
34+
previousWorkspace, _ := auth.LoadWorkspace(platformUrl, currentAuthEndpoint)
35+
if previousWorkspace != nil {
36+
workspaceId = previousWorkspace.ID
37+
fmt.Printf("\nUsing stored workspace: %s\n", previousWorkspace.Description)
38+
}
39+
40+
token, err := auth.Authenticate(ctx, clientId, clientSecret, redirectUrl, authEndpoint, redirectPort, consoleUrl, workspaceId)
3141
if err != nil {
3242
return err
3343
}
3444

3545
fmt.Println("Successfully logged in!")
3646

37-
// Check if there's a previously stored workspace for this platform and auth endpoint
38-
previousWorkspace, _ := auth.LoadWorkspace(platformUrl, currentAuthEndpoint)
47+
// If we already had a workspace, we're done
3948
if previousWorkspace != nil {
4049
fmt.Printf("\nYour current workspace is: %s\n", previousWorkspace.Description)
4150
fmt.Println("To change workspaces, run: kusari auth select-workspace")
@@ -82,6 +91,23 @@ func Login(ctx context.Context, clientId, clientSecret, redirectUrl, authEndpoin
8291
fmt.Printf("\nWorkspace '%s' has been set as your active workspace.\n", selectedWorkspace.Description)
8392
fmt.Println("To change workspaces later, run: kusari auth select-workspace")
8493

94+
// Now that we have a workspace, redirect to the console with the workspace parameter
95+
baseURL, err := urlBuilder.Build(consoleUrl, "/analysis")
96+
if err == nil && baseURL != nil {
97+
// Parse the URL and add workspace as query parameter
98+
parsedURL, parseErr := url.Parse(*baseURL)
99+
if parseErr == nil {
100+
query := parsedURL.Query()
101+
query.Set("workspaceId", selectedWorkspace.ID)
102+
parsedURL.RawQuery = query.Encode()
103+
104+
fmt.Println("\nOpening console in your browser...")
105+
if err := auth.OpenBrowser(parsedURL.String()); err != nil {
106+
fmt.Printf("Failed to open browser automatically. Please visit: %s\n", parsedURL.String())
107+
}
108+
}
109+
}
110+
85111
// ANSI escape codes:
86112
// \033[1m = bold
87113
// \033[34m = blue
@@ -149,5 +175,11 @@ func FetchWorkspaces(platformUrl string, accessToken string) ([]Workspace, error
149175
return nil, fmt.Errorf("no workspaces found for this user")
150176
}
151177

178+
for i := range result.Workspaces {
179+
if result.Workspaces[i].Description == "" {
180+
result.Workspaces[i].Description = "My Workspace"
181+
}
182+
}
183+
152184
return result.Workspaces, nil
153185
}

0 commit comments

Comments
 (0)