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
14 changes: 14 additions & 0 deletions internal/config/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/charmbracelet/log"
Expand Down Expand Up @@ -61,6 +62,16 @@ type AuthResponse struct {
TTL int64 `json:"ttl"`
}

func confirmationCodeFromID(id string) string {
id = strings.TrimSpace(id)
if len(id) < 8 {
return strings.ToUpper(id)
}

suffix := id[len(id)-8:]
return strings.ToUpper(suffix[:4] + "-" + suffix[4:])
}

// OAuthTokenResponse represents the response containing the encrypted token from OAuth flow
type OAuthTokenResponse struct {
ID string `json:"id"`
Expand Down Expand Up @@ -415,6 +426,9 @@ func OAuthLogin() (TokenSet, error) {
}

log.Debug("Auth response received", "id", authResponse.ID, "baseURL", authResponse.BaseURL)
if confirmationCode := confirmationCodeFromID(authResponse.ID); confirmationCode != "" {
fmt.Printf("SailApps confirmation code: %s\n", confirmationCode)
}

// Update the base URL for this session
if authResponse.BaseURL != "" {
Expand Down
40 changes: 40 additions & 0 deletions internal/config/oauth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import "testing"

func TestConfirmationCodeFromID(t *testing.T) {
tests := []struct {
name string
id string
want string
}{
{
name: "uuid uses last eight characters",
id: "12345678-90ab-cdef-1234-567890abcdef",
want: "90AB-CDEF",
},
{
name: "short id is uppercased",
id: "abc123",
want: "ABC123",
},
{
name: "whitespace is ignored",
id: " 12345678-90ab-cdef-1234-567890abcdef ",
want: "90AB-CDEF",
},
{
name: "empty id stays empty",
id: "",
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := confirmationCodeFromID(tt.id); got != tt.want {
t.Fatalf("confirmationCodeFromID(%q) = %q, want %q", tt.id, got, tt.want)
}
})
}
}
Loading