Skip to content

Commit

Permalink
Check if the url is cloud url
Browse files Browse the repository at this point in the history
  • Loading branch information
moshe-kabala committed Oct 4, 2024
1 parent 7bbbbfa commit 1db6c09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func NewLoginCmd() *cobra.Command {
return err
}
}
apiUrl := auth.NormalizeAPIUrl(baseUrl)
apiUrl, err := auth.NormalizeAPIUrl(baseUrl)
if err != nil {
return err
}

hasUserNameOrPassword := userName != "" || password != ""
hasApiKey := apiKey != ""
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type ApiKeyResponse struct {
}

func LoginAndGetAuthTokenWithBrowser(ctx context.Context, apiUrl string) (string, error) {
baseUrl := strings.TrimSuffix(apiUrl, API_PATH)
baseUrl := ChangeToUIUrl(apiUrl)
codeVerifier, err := generateCodeVerifier()
if err != nil {
return "", fmt.Errorf("failed to generate code verifier: %w", err)
Expand Down Expand Up @@ -169,7 +169,7 @@ func AskForUserNameAndPassword(userName, password string) (string, string, error

func LoginAndGetAuthToken(apiUrl, username, password string) (string, error) {

baseUrl := strings.TrimSuffix(apiUrl, API_PATH)
baseUrl := ChangeToUIUrl(apiUrl)
clientID := "tensorleap-client"
realm := "tensorleap"
keycloakURL := fmt.Sprintf("%s/auth", baseUrl)
Expand Down
26 changes: 23 additions & 3 deletions pkg/auth/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,31 @@ func PrintWhoami(ctx context.Context) error {
}

const API_PATH = "/api/v2"
const API_CLOUD_SUBDOMAIN = "://api."

func NormalizeAPIUrl(url string) string {
func isCloudUrl(url string) bool {
return strings.Contains(url, ".tensorleap.ai")
}

func NormalizeAPIUrl(url string) (string, error) {
url = strings.TrimSuffix(url, "/")
hasProtocol := strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
if !hasProtocol {
return "", fmt.Errorf("URL must start with http:// or https://")
}
isCloud := isCloudUrl(url)
hasApiSubdomain := strings.Contains(url, API_CLOUD_SUBDOMAIN)
if isCloud && !hasApiSubdomain {
url = strings.Replace(url, "://", API_CLOUD_SUBDOMAIN, 1)
}

if strings.HasSuffix(url, API_PATH) {
return url
return url, nil
}
return fmt.Sprintf("%s%s", url, API_PATH)
return fmt.Sprintf("%s%s", url, API_PATH), nil
}

func ChangeToUIUrl(apiUrl string) string {
url := strings.Replace(apiUrl, API_PATH, "", 1)
return strings.Replace(url, API_CLOUD_SUBDOMAIN, "://", 1)
}

0 comments on commit 1db6c09

Please sign in to comment.