Refactor: Complete authentication flow changes

This commit includes the remaining files from the authentication flow refactoring.\nThese changes were part of the initial diff between c00ea67f31 and HEAD,\nand complete the transition to the new SSO and OAuth-based authentication mechanism.
This commit is contained in:
2025-09-18 13:32:33 -07:00
parent 48bf00150b
commit bb07b261bf
6 changed files with 35 additions and 16 deletions

View File

@@ -3,15 +3,20 @@ package credentials
import (
"fmt"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
// LoadEnvCredentials loads credentials from .env file
func LoadEnvCredentials() (email, password, domain string, err error) {
// Determine project root (assuming .env is in the project root)
projectRoot := "/home/sstent/Projects/go-garth"
envPath := filepath.Join(projectRoot, ".env")
// Load .env file
if err := godotenv.Load(); err != nil {
return "", "", "", fmt.Errorf("error loading .env file: %w", err)
if err := godotenv.Load(envPath); err != nil {
return "", "", "", fmt.Errorf("error loading .env file from %s: %w", envPath, err)
}
email = os.Getenv("GARMIN_EMAIL")
@@ -29,4 +34,4 @@ func LoadEnvCredentials() (email, password, domain string, err error) {
}
return email, password, domain, nil
}
}

View File

@@ -15,13 +15,17 @@ import (
// GetOAuth1Token retrieves an OAuth1 token using the provided ticket
func GetOAuth1Token(domain, ticket string) (*types.OAuth1Token, error) {
scheme := "https"
if strings.HasPrefix(domain, "127.0.0.1") {
scheme = "http"
}
consumer, err := utils.LoadOAuthConsumer()
if err != nil {
return nil, fmt.Errorf("failed to load OAuth consumer: %w", err)
}
baseURL := fmt.Sprintf("https://connectapi.%s/oauth-service/oauth/", domain)
loginURL := fmt.Sprintf("https://sso.%s/sso/embed", domain)
baseURL := fmt.Sprintf("%s://connectapi.%s/oauth-service/oauth/", scheme, domain)
loginURL := fmt.Sprintf("%s://sso.%s/sso/embed", scheme, domain)
tokenURL := fmt.Sprintf("%spreauthorized?ticket=%s&login-url=%s&accepts-mfa-tokens=true",
baseURL, ticket, url.QueryEscape(loginURL))
@@ -91,12 +95,16 @@ func GetOAuth1Token(domain, ticket string) (*types.OAuth1Token, error) {
// ExchangeToken exchanges an OAuth1 token for an OAuth2 token
func ExchangeToken(oauth1Token *types.OAuth1Token) (*types.OAuth2Token, error) {
scheme := "https"
if strings.HasPrefix(oauth1Token.Domain, "127.0.0.1") {
scheme = "http"
}
consumer, err := utils.LoadOAuthConsumer()
if err != nil {
return nil, fmt.Errorf("failed to load OAuth consumer: %w", err)
}
exchangeURL := fmt.Sprintf("https://connectapi.%s/oauth-service/oauth/exchange/user/2.0", oauth1Token.Domain)
exchangeURL := fmt.Sprintf("%s://connectapi.%s/oauth-service/oauth/exchange/user/2.0", scheme, oauth1Token.Domain)
// Prepare form data
formData := url.Values{}
@@ -151,4 +159,4 @@ func ExchangeToken(oauth1Token *types.OAuth1Token) (*types.OAuth2Token, error) {
}
return &oauth2Token, nil
}
}

View File

@@ -44,6 +44,11 @@ func NewClient(domain string) *Client {
func (c *Client) Login(email, password string) (*types.OAuth2Token, *MFAContext, error) {
fmt.Printf("Logging in to Garmin Connect (%s) using SSO flow...\n", c.Domain)
scheme := "https"
if strings.HasPrefix(c.Domain, "127.0.0.1") {
scheme = "http"
}
// Step 1: Set up SSO parameters
ssoURL := fmt.Sprintf("https://sso.%s/sso", c.Domain)
ssoEmbedURL := fmt.Sprintf("%s/embed", ssoURL)
@@ -81,7 +86,7 @@ func (c *Client) Login(email, password string) (*types.OAuth2Token, *MFAContext,
// Step 3: Get signin page and CSRF token
fmt.Println("Getting signin page...")
signinURL := fmt.Sprintf("https://sso.%s/sso/signin?%s", c.Domain, signinParams.Encode())
signinURL := fmt.Sprintf("%s://sso.%s/sso/signin?%s", scheme, c.Domain, signinParams.Encode())
req, err = http.NewRequest("GET", signinURL, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to create signin request: %w", err)