-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokens.go
More file actions
109 lines (93 loc) · 2.87 KB
/
tokens.go
File metadata and controls
109 lines (93 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type Token struct {
CreatedBy string `json:"created_by"`
IsExpired bool `json:"is_expired"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
ExpiresAtIsEstimate bool `json:"expires_at_is_estimate,omitempty"`
Subject string `json:"subject"`
Signature string `json:"signature"`
}
type TokensResponse struct {
Tokens []Token `json:"tokens"`
Message string `json:"message"`
Success bool `json:"success"`
}
func formatTokenDate(dateStr string) string {
// Try parsing as RFC3339 with fractional seconds
t, err := time.Parse(time.RFC3339, dateStr)
if err != nil {
return dateStr
}
// Convert to local timezone
localTime := t.Local()
return localTime.Format("Jan 02, 2006 15:04:05 -0700")
}
func listTokens(server string, verbose bool) error {
token, err := ensureValidToken()
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
url := fmt.Sprintf("https://%s/app/token/activelist", server)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.IDToken))
req.Header.Set("Accept", "application/json")
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("request failed (status %d): %s", resp.StatusCode, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
var response TokensResponse
if err := json.Unmarshal(body, &response); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
if !response.Success {
return fmt.Errorf("API request failed: %s", response.Message)
}
// Display tokens
fmt.Printf("Tokens (%d total):\n\n", len(response.Tokens))
if verbose {
// Verbose mode: show all details
for _, tok := range response.Tokens {
fmt.Printf("Subject: %s\n", tok.Subject)
fmt.Printf("Signature: %s\n", tok.Signature)
fmt.Printf("Created By: %s\n", tok.CreatedBy)
fmt.Printf("Created At: %s\n", formatTokenDate(tok.CreatedAt))
fmt.Printf("Expires At: %s", formatTokenDate(tok.ExpiresAt))
if tok.ExpiresAtIsEstimate {
fmt.Printf(" (estimate)")
}
fmt.Printf("\n")
fmt.Printf("Expired: %t\n", tok.IsExpired)
fmt.Println()
}
} else {
// Default mode: show only Subject, Created By, and Expired status
for _, tok := range response.Tokens {
fmt.Printf("Subject: %s\n", tok.Subject)
fmt.Printf("Created By: %s\n", tok.CreatedBy)
fmt.Printf("Expired: %t\n", tok.IsExpired)
fmt.Println()
}
}
return nil
}